From 2ee37433f275bd73add3fd2eb953ec0bd1482171 Mon Sep 17 00:00:00 2001 From: slorello89 Date: Fri, 11 Aug 2023 14:59:27 -0400 Subject: [PATCH] fixing aggregation queries as well, quality-of-life fix for skipping GH only unit tests --- .../AggregationPredicates/QueryPredicate.cs | 24 ++++++++++++---- test/Redis.OM.Unit.Tests/ConnectionTests.cs | 4 +-- test/Redis.OM.Unit.Tests/CoreTests.cs | 2 +- .../RediSearchTests/AggregationSetTests.cs | 28 +++++++++++++++++++ .../SkipIfMissingEnvVarAttribute.cs | 25 +++++++++++++++++ 5 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 test/Redis.OM.Unit.Tests/SkipIfMissingEnvVarAttribute.cs diff --git a/src/Redis.OM/Aggregation/AggregationPredicates/QueryPredicate.cs b/src/Redis.OM/Aggregation/AggregationPredicates/QueryPredicate.cs index c3bfa91d..575063e3 100644 --- a/src/Redis.OM/Aggregation/AggregationPredicates/QueryPredicate.cs +++ b/src/Redis.OM/Aggregation/AggregationPredicates/QueryPredicate.cs @@ -40,9 +40,21 @@ public IEnumerable Serialize() /// protected override void ValidateAndPushOperand(Expression expression, Stack stack) { - if (expression is BinaryExpression binaryExpression - && binaryExpression.Left is MemberExpression memberExpression) + if (expression is BinaryExpression binaryExpression) { + var memberExpression = binaryExpression.Left as MemberExpression; + if (memberExpression is null) + { + if (binaryExpression.Left is UnaryExpression { NodeType: ExpressionType.Convert, Operand: MemberExpression } leftUnary) + { + memberExpression = (MemberExpression)leftUnary.Operand; + } + else + { + throw new InvalidOperationException("Invalid Expression Type"); + } + } + if (binaryExpression.Right is ConstantExpression constantExpression) { stack.Push(BuildQueryPredicate(binaryExpression.NodeType, memberExpression.Member, constantExpression)); @@ -54,17 +66,17 @@ protected override void ValidateAndPushOperand(Expression expression, Stack x.Execute("FT.AGGREGATE", "person-idx", "@FirstName:{Walter\\-Junior}", "WITHCURSOR", "COUNT", "10000")); } + + [Fact] + public void DateTimeQuery() + { + var dt = DateTime.Now; + var dtMs = new DateTimeOffset(dt).ToUnixTimeMilliseconds(); + + var dto = DateTimeOffset.Now.Subtract(TimeSpan.FromHours(3)); + var dtoMs = dto.ToUnixTimeMilliseconds(); + var collection = new RedisAggregationSet(_mock.Object, true, chunkSize: 10000); + _mock.Setup(x => x.Execute("FT.AGGREGATE", It.IsAny())).Returns(MockedResult); + _mock.Setup(x => x.Execute("FT.CURSOR", It.IsAny())).Returns(MockedResultCursorEnd); + + Expression, bool>> query = a => a.RecordShell.Timestamp > dt; + _ = collection.Where(query).ToList(); + _mock.Verify(x => x.Execute("FT.AGGREGATE", "objectwithdatetime-idx", $"@Timestamp:[({dtMs} inf]", "WITHCURSOR", "COUNT", "10000")); + + query = a => a.RecordShell.Timestamp > dto; + _ = collection.Where(query).ToList(); + _mock.Verify(x => x.Execute("FT.AGGREGATE", "objectwithdatetime-idx", $"@Timestamp:[({dtoMs} inf]", "WITHCURSOR", "COUNT", "10000")); + query = a => a.RecordShell.TimestampOffset > dto; + _ = collection.Where(query).ToList(); + _mock.Verify(x => x.Execute("FT.AGGREGATE", "objectwithdatetime-idx", $"@TimestampOffset:[({dtoMs} inf]", "WITHCURSOR", "COUNT", "10000")); + + query = a => a.RecordShell.TimestampOffset > dt; + _ = collection.Where(query).ToList(); + _mock.Verify(x => x.Execute("FT.AGGREGATE", "objectwithdatetime-idx", $"@TimestampOffset:[({dtMs} inf]", "WITHCURSOR", "COUNT", "10000")); + } } } diff --git a/test/Redis.OM.Unit.Tests/SkipIfMissingEnvVarAttribute.cs b/test/Redis.OM.Unit.Tests/SkipIfMissingEnvVarAttribute.cs new file mode 100644 index 00000000..397b5faa --- /dev/null +++ b/test/Redis.OM.Unit.Tests/SkipIfMissingEnvVarAttribute.cs @@ -0,0 +1,25 @@ +using System; +using System.Linq; +using Xunit; + +namespace Redis.OM.Unit.Tests; + +[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] +public class SkipIfMissingEnvVarAttribute : FactAttribute +{ + private readonly string[] _envVars; + + public SkipIfMissingEnvVarAttribute(params string[] envVars) + { + _envVars = envVars; + } + + public override string Skip + { + get + { + var missingEnvVars = _envVars.Where(x => Environment.GetEnvironmentVariable(x) == null); + return $"Skipping because the following environment variables were missing: {string.Join(",", _envVars)}"; + } + } +} \ No newline at end of file