Skip to content

Commit 4ff9633

Browse files
support for JSON.ArrIndex with none scalar value (#285)
1 parent 97be804 commit 4ff9633

File tree

4 files changed

+121
-5
lines changed

4 files changed

+121
-5
lines changed

test/Redis.OM.Unit.Tests/RediSearchTests/ObjectWithEmbeddedArrayOfObjects.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ namespace Redis.OM.Unit.Tests.RediSearchTests
66
[Document(StorageType = StorageType.Json)]
77
public class ObjectWithEmbeddedArrayOfObjects
88
{
9+
[RedisIdField]
10+
public string Id { get; set; }
11+
912
[Indexed(JsonPath = "$.City")]
1013
[Indexed(JsonPath = "$.State")]
1114
[Indexed(JsonPath = "$.AddressType")]

test/Redis.OM.Unit.Tests/RediSearchTests/SearchTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ public async Task TestUpdateJson()
778778
var steve = collection.First(x => x.Name == "Steve");
779779
steve.Age = 33;
780780
await collection.UpdateAsync(steve);
781-
_mock.Verify(x => x.ExecuteAsync("EVALSHA", "cbbf1c4fab5064f419e469cc51c563f8bf51e6fb", "1", "Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N", "SET", "$.Age", "33"));
781+
_mock.Verify(x => x.ExecuteAsync("EVALSHA", It.IsAny<string>(), "1", "Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N", "SET", "$.Age", "33"));
782782
Scripts.ShaCollection.Clear();
783783
}
784784

@@ -811,7 +811,7 @@ public void TestUpdateJsonUnloadedScript()
811811
var steve = collection.First(x => x.Name == "Steve");
812812
steve.Age = 33;
813813
collection.Update(steve);
814-
_mock.Verify(x => x.Execute("EVALSHA", "cbbf1c4fab5064f419e469cc51c563f8bf51e6fb", "1", "Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N", "SET", "$.Age", "33"));
814+
_mock.Verify(x => x.Execute("EVALSHA", It.IsAny<string>(), "1", "Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N", "SET", "$.Age", "33"));
815815
_mock.Verify(x => x.Execute("EVAL",Scripts.JsonDiffResolution, "1", "Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N", "SET", "$.Age", "33"));
816816
Scripts.ShaCollection.Clear();
817817
}
@@ -843,12 +843,12 @@ public async Task TestUpdateJsonNestedObject()
843843
steve.Address = new Address { State = "Florida" };
844844
await collection.UpdateAsync(steve);
845845
var expected = $"{{{Environment.NewLine} \"State\": \"Florida\"{Environment.NewLine}}}";
846-
_mock.Verify(x => x.ExecuteAsync("EVALSHA", "cbbf1c4fab5064f419e469cc51c563f8bf51e6fb", "1", "Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N", "SET", "$.Address", expected));
846+
_mock.Verify(x => x.ExecuteAsync("EVALSHA", It.IsAny<string>(), "1", "Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N", "SET", "$.Address", expected));
847847

848848
steve.Address.City = "Satellite Beach";
849849
await collection.UpdateAsync(steve);
850850
expected = "\"Satellite Beach\"";
851-
_mock.Verify(x => x.ExecuteAsync("EVALSHA", "cbbf1c4fab5064f419e469cc51c563f8bf51e6fb", "1", "Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N", "SET", "$.Address.City", expected));
851+
_mock.Verify(x => x.ExecuteAsync("EVALSHA", It.IsAny<string>(), "1", "Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N", "SET", "$.Address.City", expected));
852852

853853
Scripts.ShaCollection.Clear();
854854
}

test/Redis.OM.Unit.Tests/RedisSetupCollection.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using Redis.OM.Unit.Tests.RediSearchTests;
44
using Xunit;
5+
using static Redis.OM.Unit.Tests.SearchJsonTests.RedisJsonNestedComposeValueTest;
56

67
namespace Redis.OM.Unit.Tests
78
{
@@ -24,6 +25,7 @@ public RedisSetup()
2425
Connection.CreateIndex(typeof(ObjectWithTwoStopwords));
2526
Connection.CreateIndex(typeof(ObjectWithDateTime));
2627
Connection.CreateIndex(typeof(ObjectWithDateTimeHash));
28+
Connection.CreateIndex(typeof(PersonWithNestedArrayOfObject));
2729
}
2830

2931
private IRedisConnection _connection = null;
@@ -57,7 +59,7 @@ public void Dispose()
5759
Connection.DropIndexAndAssociatedRecords(typeof(ClassForEmptyRedisCollection));
5860
Connection.DropIndexAndAssociatedRecords(typeof(ObjectWithDateTime));
5961
Connection.DropIndexAndAssociatedRecords(typeof(ObjectWithDateTimeHash));
60-
62+
Connection.DropIndexAndAssociatedRecords(typeof(PersonWithNestedArrayOfObject));
6163
}
6264
}
6365
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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

Comments
 (0)