Skip to content

Commit

Permalink
fixing issue with byte array updates
Browse files Browse the repository at this point in the history
  • Loading branch information
slorello89 committed Oct 23, 2024
1 parent f506618 commit e4ea385
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Redis.OM/Modeling/RedisCollectionStateManager.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Redis.OM.Modeling.Vectors;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace Redis.OM.Modeling
Expand Down Expand Up @@ -329,7 +329,16 @@ private static JObject FindDiff(JToken currentObject, JToken snapshotObject)

break;
default:
if (currentObject.ToString() != snapshotObject.ToString())
if (snapshotObject.Type == JTokenType.Bytes)
{
var snapShotObjectStr = Convert.ToBase64String(snapshotObject.Value<byte[]>());

if (snapShotObjectStr != currentObject.ToString())
{
diff["+"] = currentObject.ToString();
}
}
else if (currentObject.ToString() != snapshotObject.ToString())
{
diff["+"] = currentObject;
diff["-"] = snapshotObject;
Expand Down
15 changes: 15 additions & 0 deletions test/Redis.OM.Unit.Tests/RediSearchTests/ObjectWithByteArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Redis.OM.Modeling;

namespace Redis.OM.Unit.Tests.RediSearchTests;

[Document(StorageType = StorageType.Json, Prefixes = new []{"obj"})]
public class ObjectWithByteArray
{
[RedisIdField]
[Indexed]
public string Id { get; set; }

public byte[] Bytes1 { get; set; }

public byte[] Bytes2 { get; set; }
}
15 changes: 15 additions & 0 deletions test/Redis.OM.Unit.Tests/RediSearchTests/SearchFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1270,5 +1270,20 @@ public async Task TestSearchByMatchPattern(string pattern, int existingRecordsCo
var res = await collection.Where(x => x.Name.MatchPattern(pattern)).ToListAsync();
Assert.Equal(existingRecordsCount, res.Count);
}

[Fact]
public async Task TestUpdateByteArray()
{
var collection = new RedisCollection<ObjectWithByteArray>(_connection);
var obj = new ObjectWithByteArray() { Bytes1 = new byte[] { 1, 2, 3 }, Bytes2 = new byte[] { 4, 5, 6 } };
var id = await collection.InsertAsync(obj);
var res = (await collection.Where(x => x.Id == obj.Id).ToListAsync()).First();
res.Bytes1 = new byte[] { 7, 8, 9 };
res.Bytes2 = new byte[] { 10, 11, 12 };
await collection.UpdateAsync(res);
var updated = (await collection.Where(x => x.Id == obj.Id).ToListAsync()).First();
Assert.Equal(new byte[] { 7, 8, 9 }, updated.Bytes1);
Assert.Equal(new byte[] { 10, 11, 12 }, updated.Bytes2);
}
}
}
27 changes: 27 additions & 0 deletions test/Redis.OM.Unit.Tests/RediSearchTests/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ public class SearchTests
})
};

private readonly RedisReply _mockedReplyObjectWIthMultipleByteArrays = new[]
{
new RedisReply(1),
new RedisReply(
"obj:01FVN836BNQGYMT80V7RCVY73N"),
new RedisReply(new RedisReply[]
{
"$",
"{\"Id\":\"01FVN836BNQGYMT80V7RCVY73N\",\"Bytes1\":\"AQID\",\"Bytes2\":\"BAUG\"}"
})
};

private readonly RedisReply _mockedReplyObjectWIthMultipleDateTimesHash = new[]
{
new RedisReply(1),
Expand Down Expand Up @@ -1092,6 +1104,21 @@ public async Task TestUpdateJsonWithMultipleDateTimesHash()
Scripts.ShaCollection.Clear();
}

[Fact]
public async Task TestUpdateJsonWithByteArrays()
{
_substitute.ExecuteAsync("FT.SEARCH", Arg.Any<object[]>()).Returns(_mockedReplyObjectWIthMultipleByteArrays);
_substitute.ExecuteAsync("EVALSHA", Arg.Any<object[]>()).Returns(Task.FromResult(new RedisReply("42")));
_substitute.ExecuteAsync("SCRIPT", Arg.Any<object[]>())
.Returns(Task.FromResult(new RedisReply("cbbf1c4fab5064f419e469cc51c563f8bf51e6fb")));

var collection = new RedisCollection<ObjectWithByteArray>(_substitute);
var obj = (await collection.Where(x => x.Id == "01FVN836BNQGYMT80V7RCVY73N").ToListAsync()).First();
obj.Bytes1 = new byte[] { 4, 5, 6 };
await collection.UpdateAsync(obj);
await _substitute.Received().ExecuteAsync("EVALSHA", Arg.Any<string>(), "1", new RedisKey("obj:01FVN836BNQGYMT80V7RCVY73N"), "SET", "$.Bytes1","\"BAUG\"");
}

[Fact]
public async Task TestUpdateJsonUnloadedScriptAsync()
{
Expand Down
2 changes: 2 additions & 0 deletions test/Redis.OM.Unit.Tests/RedisSetupCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public RedisSetup()
Connection.CreateIndex(typeof(SelectTestObject));
Connection.CreateIndex(typeof(ObjectWithDateTimeOffsetJson));
Connection.CreateIndex(typeof(ObjectWithMultipleSearchableAttributes));
Connection.CreateIndex(typeof(ObjectWithByteArray));
}

private IRedisConnectionProvider _provider;
Expand Down Expand Up @@ -64,6 +65,7 @@ public void Dispose()
Connection.DropIndexAndAssociatedRecords(typeof(SelectTestObject));
Connection.DropIndexAndAssociatedRecords(typeof(ObjectWithDateTimeOffsetJson));
Connection.DropIndexAndAssociatedRecords(typeof(ObjectWithMultipleSearchableAttributes));
Connection.DropIndexAndAssociatedRecords(typeof(ObjectWithByteArray));
}
}
}

0 comments on commit e4ea385

Please sign in to comment.