Skip to content

Commit

Permalink
adding proper translation for DateTimeOffsets
Browse files Browse the repository at this point in the history
  • Loading branch information
slorello89 committed Aug 11, 2023
1 parent 6399ed5 commit ec86ff5
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Redis.OM/Common/ExpressionParserUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,11 @@ private static string ValueToString(object value)
return ((double)value).ToString(CultureInfo.InvariantCulture);
}

if (value is DateTimeOffset dto)
{
return dto.ToUnixTimeMilliseconds().ToString(CultureInfo.InvariantCulture);
}

if (value is DateTime dt)
{
return new DateTimeOffset(dt).ToUnixTimeMilliseconds().ToString(CultureInfo.InvariantCulture);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class ObjectWithDateTime
public string Id { get; set; }
[Indexed(Sortable = true)]
public DateTime Timestamp { get; set; }
[Indexed(Sortable = true)]
public DateTimeOffset TimestampOffset { get; set; }
[Indexed]
public DateTime? NullableTimestamp { get; set; }
}
Expand Down
78 changes: 78 additions & 0 deletions test/Redis.OM.Unit.Tests/RediSearchTests/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2409,6 +2409,84 @@ public void Issue201()
));
}

[Fact]
public void RangeOnDateTimeWithMultiplePredicates()
{
_mock.Setup(x => x.Execute(It.IsAny<string>(), It.IsAny<string[]>()))
.Returns(_mockReply);

var fromDto = DateTimeOffset.UtcNow.Subtract(TimeSpan.FromHours(4));
var toDto = DateTimeOffset.UtcNow;

var fromDt = fromDto.DateTime;
var toDt = toDto.DateTime;

var msFrom = fromDto.ToUnixTimeMilliseconds();
var msTo = toDto.ToUnixTimeMilliseconds();

var collection = new RedisCollection<ObjectWithDateTime>(_mock.Object, 1000);
_ = collection.Where(x => x.TimestampOffset <= fromDto && x.TimestampOffset >= toDto).ToList();
_ = collection.Where(x => x.TimestampOffset <= fromDt && x.TimestampOffset >= toDt).ToList();
_ = collection.Where(x => x.Timestamp <= fromDto && x.Timestamp >= toDto).ToList();
_ = collection.Where(x => x.Timestamp <= fromDt && x.Timestamp >= toDt).ToList();
_ = collection.Where(x => x.NullableTimestamp <= fromDto && x.NullableTimestamp >= toDto).ToList();
_ = collection.Where(x => x.NullableTimestamp <= fromDt && x.NullableTimestamp >= toDt).ToList();

_mock.Verify(x => x.Execute(
"FT.SEARCH",
"objectwithdatetime-idx",
$"((@TimestampOffset:[-inf {msFrom}]) (@TimestampOffset:[{msTo} inf]))",
"LIMIT",
"0",
"1000"
));

_mock.Verify(x => x.Execute(
"FT.SEARCH",
"objectwithdatetime-idx",
$"((@TimestampOffset:[-inf {new DateTimeOffset(fromDt).ToUnixTimeMilliseconds()}]) (@TimestampOffset:[{new DateTimeOffset(toDt).ToUnixTimeMilliseconds()} inf]))",
"LIMIT",
"0",
"1000"
));

_mock.Verify(x => x.Execute(
"FT.SEARCH",
"objectwithdatetime-idx",
$"((@Timestamp:[-inf {msFrom}]) (@Timestamp:[{msTo} inf]))",
"LIMIT",
"0",
"1000"
));

_mock.Verify(x => x.Execute(
"FT.SEARCH",
"objectwithdatetime-idx",
$"((@Timestamp:[-inf {new DateTimeOffset(fromDt).ToUnixTimeMilliseconds()}]) (@Timestamp:[{new DateTimeOffset(toDt).ToUnixTimeMilliseconds()} inf]))",
"LIMIT",
"0",
"1000"
));

_mock.Verify(x => x.Execute(
"FT.SEARCH",
"objectwithdatetime-idx",
$"((@NullableTimestamp:[-inf {msFrom}]) (@NullableTimestamp:[{msTo} inf]))",
"LIMIT",
"0",
"1000"
));

_mock.Verify(x => x.Execute(
"FT.SEARCH",
"objectwithdatetime-idx",
$"((@NullableTimestamp:[-inf {new DateTimeOffset(fromDt).ToUnixTimeMilliseconds()}]) (@NullableTimestamp:[{new DateTimeOffset(toDt).ToUnixTimeMilliseconds()} inf]))",
"LIMIT",
"0",
"1000"
));
}

[Fact]
public void RangeOnDatetime()
{
Expand Down

0 comments on commit ec86ff5

Please sign in to comment.