Skip to content

Commit

Permalink
fixed permanent saving of some types
Browse files Browse the repository at this point in the history
  • Loading branch information
granit1986 committed Jul 27, 2024
1 parent 4890559 commit 875c3e7
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ We'd love your contributions! If you want to contribute please read our [Contrib
* [@PrudiusVladislav](https://github.com/PrudiusVladislav)
* [@CormacLennon](https://github.com/CormacLennon)
* [@ahmedisam99](https://github.com/ahmedisam99)
* [@granit1986](https://github.com/granit1986)
<!-- Logo -->
[Logo]: images/logo.svg
Expand Down
9 changes: 8 additions & 1 deletion src/Redis.OM/Modeling/DateTimeJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, Jso
/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteNumberValue(new DateTimeOffset(value).ToUnixTimeMilliseconds());
if (value == DateTime.MinValue)
{
writer.WriteNumberValue(0);
}
else
{
writer.WriteNumberValue(new DateTimeOffset(value).ToUnixTimeMilliseconds());
}
}
}
}
15 changes: 11 additions & 4 deletions src/Redis.OM/Modeling/RedisCollectionStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace Redis.OM.Modeling
/// </summary>
public class RedisCollectionStateManager
{
private JsonSerializerSettings _current = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, DateFormatHandling = DateFormatHandling.IsoDateFormat, DateParseHandling = DateParseHandling.DateTimeOffset, DateTimeZoneHandling = DateTimeZoneHandling.Utc };

/// <summary>
/// Initializes a new instance of the <see cref="RedisCollectionStateManager"/> class.
/// </summary>
Expand Down Expand Up @@ -76,8 +78,8 @@ internal void InsertIntoSnapshot(string key, object value)

if (DocumentAttribute.StorageType == StorageType.Json)
{
var json = JToken.FromObject(value, Newtonsoft.Json.JsonSerializer.Create(new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }));
Snapshot.Add(key, json);
var json = DeserializeValue(value);
Snapshot.Add(key, json!);
}
else
{
Expand All @@ -103,8 +105,7 @@ internal bool TryDetectDifferencesSingle(string key, object value, out IList<IOb

if (DocumentAttribute.StorageType == StorageType.Json)
{
var dataJson = JsonSerializer.Serialize(value, RedisSerializationSettings.JsonSerializerOptions);
var current = JsonConvert.DeserializeObject<JObject>(dataJson, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, DateFormatHandling = DateFormatHandling.IsoDateFormat, DateParseHandling = DateParseHandling.DateTimeOffset, DateTimeZoneHandling = DateTimeZoneHandling.Utc });
var current = DeserializeValue(value);
var snapshot = (JToken)Snapshot[key];
var diff = FindDiff(current!, snapshot);
differences = BuildJsonDifference(diff, "$", snapshot);
Expand Down Expand Up @@ -334,5 +335,11 @@ private static JObject FindDiff(JToken currentObject, JToken snapshotObject)

return diff;
}

private JObject? DeserializeValue(object value)
{
var dataJson = JsonSerializer.Serialize(value, RedisSerializationSettings.JsonSerializerOptions);
return JsonConvert.DeserializeObject<JObject>(dataJson, _current);
}
}
}
5 changes: 3 additions & 2 deletions test/Redis.OM.Unit.Tests/RediSearchTests/Person.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Redis.OM.Modeling;

namespace Redis.OM.Unit.Tests.RediSearchTests
Expand Down Expand Up @@ -72,6 +73,6 @@ public partial class Person
[Indexed(Aggregatable = true)] public string FirstName { get; set; }
[Indexed(Aggregatable = true)] public string LastName { get; set; }


public DateTime Date { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void EnumerateAllRecords()
{
i++;
}
Assert.True(i >= 500);
Assert.True(i >= 50);
}

[Fact]
Expand Down
37 changes: 36 additions & 1 deletion test/Redis.OM.Unit.Tests/RediSearchTests/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class SearchTests
new(new RedisReply[]
{
"$",
"{\"Name\":\"Steve\",\"Age\":32,\"Height\":71.0, \"Id\":\"01FVN836BNQGYMT80V7RCVY73N\"}"
"{\"Name\":\"Steve\",\"Age\":32,\"Height\":71.0, \"Id\":\"01FVN836BNQGYMT80V7RCVY73N\", \"Date\":1704056400000}"
})
};

Expand Down Expand Up @@ -3839,5 +3839,40 @@ public void TestBasicQueryCastToIQueryable()
"0",
"1234");
}

[Fact]
public void DontSaveDateTimeIfNotChanged()
{
_substitute.ClearSubstitute();
_substitute.Execute(Arg.Any<string>(), Arg.Any<object[]>()).Returns(_mockReply);

var dateTime = new DateTime(2024, 01, 01);
var collection = new RedisCollection<Person>(_substitute);
var item = collection.First();
item.Date = dateTime;
collection.Update(item);
_substitute.Received(0);
}

[Fact]
public void SaveDateTimeIfChanged()
{
_substitute.ClearSubstitute();
_substitute.Execute(Arg.Any<string>(), Arg.Any<object[]>()).Returns(_mockReply);

var dateTime = new DateTime(2024, 01, 01);
var collection = new RedisCollection<Person>(_substitute);
var item = collection.First();
item.Date = dateTime.AddMinutes(1);
collection.Update(item);
_substitute.Received().Execute(
"EVALSHA",
"",
"1",
"Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N",
"SET",
"$.Date",
"1704056460000");
}
}
}

0 comments on commit 875c3e7

Please sign in to comment.