Skip to content

Commit

Permalink
fixing aggregation queries as well, quality-of-life fix for skipping …
Browse files Browse the repository at this point in the history
…GH only unit tests
  • Loading branch information
slorello89 committed Aug 11, 2023
1 parent ec86ff5 commit 2ee3743
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 9 deletions.
24 changes: 18 additions & 6 deletions src/Redis.OM/Aggregation/AggregationPredicates/QueryPredicate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,21 @@ public IEnumerable<string> Serialize()
/// <inheritdoc/>
protected override void ValidateAndPushOperand(Expression expression, Stack<string> 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));
Expand All @@ -54,17 +66,17 @@ protected override void ValidateAndPushOperand(Expression expression, Stack<stri
case ConstantExpression c:
stack.Push(BuildQueryPredicate(binaryExpression.NodeType, memberExpression.Member, c));
break;
case MemberExpression mem when mem.Expression is ConstantExpression frame:
case MemberExpression mem:
{
var val = ExpressionParserUtilities.GetValue(mem.Member, frame.Value);
var val = ExpressionParserUtilities.GetOperandString(mem);
stack.Push(BuildQueryPredicate(binaryExpression.NodeType, memberExpression.Member, System.Linq.Expressions.Expression.Constant(val)));
break;
}
}
}
else if (binaryExpression.Right is MemberExpression mem && mem.Expression is ConstantExpression frame)
else if (binaryExpression.Right is MemberExpression mem)
{
var val = ExpressionParserUtilities.GetValue(mem.Member, frame.Value);
var val = ExpressionParserUtilities.GetOperandString(mem);
stack.Push(BuildQueryPredicate(binaryExpression.NodeType, memberExpression.Member, System.Linq.Expressions.Expression.Constant(val)));
}
else
Expand Down
4 changes: 2 additions & 2 deletions test/Redis.OM.Unit.Tests/ConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void TestConnectStandalone()
Assert.Equal("Bar",res);
}

[Fact]
[SkipIfMissingEnvVar("SENTINLE_HOST_PORT")]
public void TestSentinel()
{
var hostInfo = System.Environment.GetEnvironmentVariable("SENTINLE_HOST_PORT") ?? "localhost:26379";
Expand Down Expand Up @@ -63,7 +63,7 @@ public void GivenMultiplexerConnection_WhenTestingSetCommand_ThenShouldExecuteSe
Assert.Equal("Bar", res);
}

[Fact]
[SkipIfMissingEnvVar("PRIVATE_HOST", "PRIVATE_PORT", "PRIVATE_PASSWORD")]
public void TestPrivateConnection()
{
var host = Environment.GetEnvironmentVariable("PRIVATE_HOST") ?? "redis-private";
Expand Down
2 changes: 1 addition & 1 deletion test/Redis.OM.Unit.Tests/CoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ public void CommandTest()
stream.Flush();
}

[Fact]
[SkipIfMissingEnvVar("AGGRESSIVELY_SHORT_TIMEOUT_REDIS")]
public async Task SearchTimeoutTest()
{
var hostInfo = Environment.GetEnvironmentVariable("AGGRESSIVELY_SHORT_TIMEOUT_REDIS") ?? string.Empty;
Expand Down
28 changes: 28 additions & 0 deletions test/Redis.OM.Unit.Tests/RediSearchTests/AggregationSetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,34 @@ public void PunctuationMarkInTagQuery()
_ = collection.Where(query).ToList();
_mock.Verify(x => 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<ObjectWithDateTime>(_mock.Object, true, chunkSize: 10000);
_mock.Setup(x => x.Execute("FT.AGGREGATE", It.IsAny<string[]>())).Returns(MockedResult);
_mock.Setup(x => x.Execute("FT.CURSOR", It.IsAny<string[]>())).Returns(MockedResultCursorEnd);

Expression<Func<AggregationResult<ObjectWithDateTime>, 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"));
}
}
}
25 changes: 25 additions & 0 deletions test/Redis.OM.Unit.Tests/SkipIfMissingEnvVarAttribute.cs
Original file line number Diff line number Diff line change
@@ -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)}";
}
}
}

0 comments on commit 2ee3743

Please sign in to comment.