|
| 1 | +using Redis.OM.Contracts; |
| 2 | +using Redis.OM.Modeling; |
| 3 | +using Redis.OM.Searching; |
| 4 | +using Redis.OM.Unit.Tests.RediSearchTests; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Data.Common; |
| 8 | +using System.Linq; |
| 9 | +using System.Text; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace Redis.OM.Unit.Tests.SearchJsonTests |
| 14 | +{ |
| 15 | + [Collection("Redis")] |
| 16 | + public class RedisJsonNestedComposeValueTest |
| 17 | + { |
| 18 | + public RedisJsonNestedComposeValueTest(RedisSetup setup) |
| 19 | + { |
| 20 | + _connection = setup.Connection; |
| 21 | + } |
| 22 | + private IRedisConnection _connection = null; |
| 23 | + |
| 24 | + [Document(StorageType = StorageType.Json)] |
| 25 | + public partial class PersonWithNestedArrayOfObject |
| 26 | + { |
| 27 | + [RedisIdField] |
| 28 | + [Indexed] |
| 29 | + public string Id { get; set; } |
| 30 | + [Searchable] |
| 31 | + public string Name { get; set; } |
| 32 | + [Indexed(JsonPath = "$.City")] |
| 33 | + [Indexed(JsonPath = "$.State")] |
| 34 | + public List<Address> Addresses { get; set; } |
| 35 | + } |
| 36 | + |
| 37 | + [Fact] |
| 38 | + public void TestEmbeddedMultipleList() |
| 39 | + { |
| 40 | + var currentAddress = new Address { State = "TN", City = "Chennai" }; |
| 41 | + var permanentAddress = new Address { State = "TN", City = "Attur" }; |
| 42 | + var personWithDualAddress = new PersonWithNestedArrayOfObject { Name = "Jeeva", Addresses = new List<Address> { currentAddress, permanentAddress } }; |
| 43 | + var collection = new RedisCollection<PersonWithNestedArrayOfObject>(_connection); |
| 44 | + collection.Insert(personWithDualAddress); |
| 45 | + var person = collection.FindById(personWithDualAddress.Id); |
| 46 | + person.Addresses[0].State = person.Addresses[0].State + "(Tamil Nadu)"; |
| 47 | + collection.Save(); |
| 48 | + var result = collection.Where(x => x.Addresses.Any(x => x.State == "TN(Tamil Nadu)")).ToList(); |
| 49 | + Assert.NotEmpty(result); |
| 50 | + collection.Delete(person); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public async Task TestAnySearchEmbeddedObjects() |
| 55 | + { |
| 56 | + var obj = new ObjectWithEmbeddedArrayOfObjects() |
| 57 | + { |
| 58 | + Name = "John", |
| 59 | + Numeric = 100, |
| 60 | + Addresses = new[] { new Address { City = "Newark", State = "New Jersey" } }, |
| 61 | + AddressList = new List<Address> { new() { City = "Satellite Beach", State = "Florida" } } |
| 62 | + }; |
| 63 | + |
| 64 | + await _connection.SetAsync(obj); |
| 65 | + |
| 66 | + var collection = new RedisCollection<ObjectWithEmbeddedArrayOfObjects>(_connection); |
| 67 | + |
| 68 | + var results = await collection.Where(x => x.Addresses.Any(x => x.State == "New Jersey")).ToListAsync(); |
| 69 | + Assert.NotEmpty(results); |
| 70 | + results[0].Addresses[0].City = "Satellite Beach"; |
| 71 | + results[0].Addresses[0].State = "Florida"; |
| 72 | + await collection.SaveAsync(); |
| 73 | + results = await collection.Where(x => x.Addresses.Any(x => x.City == "Satellite Beach")).ToListAsync(); |
| 74 | + Assert.NotEmpty(results); |
| 75 | + collection.Delete(obj); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public async Task TestEmbeddedMultipleListWithUpdate() |
| 80 | + { |
| 81 | + var currentAddress = new Address { State = "KL", City = "Kozhikode" }; |
| 82 | + var permanentAddress = new Address { State = "TN", City = "Salem" }; |
| 83 | + var personWithDualAddress = new PersonWithNestedArrayOfObject { Name = "Raja", Addresses = new List<Address> { currentAddress, permanentAddress } }; |
| 84 | + var collection = new RedisCollection<PersonWithNestedArrayOfObject>(_connection); |
| 85 | + collection.Insert(personWithDualAddress); |
| 86 | + var person = collection.FindById(personWithDualAddress.Id); |
| 87 | + person.Addresses[0].State = person.Addresses[0].State + "(Kerala)"; |
| 88 | + await collection.UpdateAsync(person); |
| 89 | + var result = collection.Where(x => x.Addresses.Any(x => x.State == "KL(Kerala)")).ToList(); |
| 90 | + Assert.NotEmpty(result); |
| 91 | + collection.Delete(person); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public async Task TestEmbeddedMultipleListWithUpdateInsert() |
| 96 | + { |
| 97 | + var currentAddress = new Address { State = "KL", City = "Kozhikode" }; |
| 98 | + var permanentAddress = new Address { State = "TN", City = "Salem" }; |
| 99 | + var addressToBeInserted = new Address { State = "FL", City = "Hollywood" }; |
| 100 | + var personWithDualAddress = new PersonWithNestedArrayOfObject { Name = "Raja", Addresses = new List<Address> { currentAddress, permanentAddress } }; |
| 101 | + var collection = new RedisCollection<PersonWithNestedArrayOfObject>(_connection); |
| 102 | + collection.Insert(personWithDualAddress); |
| 103 | + var person = collection.FindById(personWithDualAddress.Id); |
| 104 | + person.Addresses.Add(addressToBeInserted); |
| 105 | + collection.Update(person); |
| 106 | + var result = collection.Where(x => x.Addresses.Any(x => x.City == "Hollywood")).ToList(); |
| 107 | + Assert.NotEmpty(result); |
| 108 | + collection.Delete(person); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments