Path: redb/redb.Tests.Integration/Tests/Unit/RedundantArrayGuardTests.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Tests.Integration/Tests/Unit/RedundantArrayGuardTests.cs
----
using System.Linq.Expressions;
using redb.Core.Pro.Query;
using redb.Core.Query.QueryExpressions;
namespace redb.Tests.Integration.Tests.Unit;
///
/// The Pro parser drops the null guard of an array that an element test of the same AND chain already requires, and
/// keeps every other guard. Pure parsing, no database.
///
public class RedundantArrayGuardTests
{
public class GuardRow
{
public string? Kind { get; set; }
}
public class GuardProps
{
public string[]? Skills { get; set; }
public List? Tags { get; set; }
public GuardRow[]? Rows { get; set; }
public int Age { get; set; }
}
private static FilterExpression Parse(Expression> predicate)
=> new ProFilterExpressionParser().ParseFilter(predicate);
private static List Guards(FilterExpression node) => node switch
{
NullCheckExpression n => [n],
LogicalExpression l => l.Operands.SelectMany(Guards).ToList(),
_ => []
};
// Dropped
[Fact]
public void GuardBeforeContains_IsDropped()
{
var filter = Parse(p => p.Skills != null && p.Skills.Contains("C#"));
var contains = Assert.IsType(filter);
Assert.Equal(ComparisonOperator.ArrayContains, contains.Operator);
Assert.Equal("Skills", contains.Property.Name);
}
[Fact]
public void GuardAfterContains_IsDropped()
{
var filter = Parse(p => p.Skills!.Contains("C#") && p.Skills != null);
Assert.IsType(filter);
}
[Fact]
public void GuardBetweenScalarAndContains_IsDropped_TheScalarStays()
{
var filter = Parse(p => p.Age > 30 && p.Skills != null && p.Skills.Contains("C#"));
Assert.Empty(Guards(filter));
var and = Assert.IsType(filter);
Assert.Equal(LogicalOperator.And, and.Operator);
Assert.Contains(and.Operands, o => o is ComparisonExpression { Property.Name: "Age" });
Assert.Contains(and.Operands, o => o is ComparisonExpression { Operator: ComparisonOperator.ArrayContains });
}
[Fact]
public void GuardWithTwoContainsUnderAnd_IsDropped()
{
var filter = Parse(p => p.Skills != null && p.Skills.Contains("C#") && p.Skills.Contains("SQL"));
Assert.Empty(Guards(filter));
var and = Assert.IsType(filter);
Assert.Equal(2, and.Operands.Count);
}
[Fact]
public void GuardBeforeOrOfContainsOnTheSameArray_IsDropped()
{
var filter = Parse(p => p.Skills != null && (p.Skills.Contains("C#") || p.Skills.Contains("Python")));
var or = Assert.IsType(filter);
Assert.Equal(LogicalOperator.Or, or.Operator);
Assert.Empty(Guards(filter));
}
[Fact]
public void GuardBeforeAnyWithPredicateOverClasses_IsDropped()
{
var filter = Parse(p => p.Rows != null && p.Rows.Any(r => r.Kind == "a"));
Assert.IsType(filter);
}
[Fact]
public void GuardInANestedAndOfTheRootChain_IsDropped()
{
var filter = Parse(p => (p.Skills != null && p.Age > 30) && p.Skills.Contains("C#"));
Assert.Empty(Guards(filter));
}
// Kept
[Fact]
public void GuardAlone_Stays()
{
var filter = Parse(p => p.Skills != null);
Assert.IsType(filter);
}
[Fact]
public void GuardWithAScalarOnly_Stays()
{
Assert.Single(Guards(Parse(p => p.Skills != null && p.Age > 30)));
}
[Fact]
public void GuardOnAnotherArray_Stays()
{
var guard = Assert.Single(Guards(Parse(p => p.Tags != null && p.Skills!.Contains("C#"))));
Assert.Equal("Tags", guard.Property.Name);
}
[Fact]
public void IsNull_Stays()
{
var guard = Assert.Single(Guards(Parse(p => p.Skills == null || p.Skills.Contains("C#"))));
Assert.True(guard.IsNull);
}
[Fact]
public void GuardUnderOr_Stays()
{
Assert.Single(Guards(Parse(p => (p.Skills != null || p.Age > 30) && p.Skills!.Contains("C#"))));
}
[Fact]
public void GuardUnderNot_Stays()
{
Assert.Single(Guards(Parse(p => !(p.Skills != null && p.Skills.Contains("C#")))));
}
[Fact]
public void GuardBeforeNegatedContains_Stays()
{
Assert.Single(Guards(Parse(p => p.Skills != null && !p.Skills.Contains("C#"))));
}
[Fact]
public void GuardBeforeContainsOfNull_Stays()
{
Assert.Single(Guards(Parse(p => p.Skills != null && p.Skills.Contains(null!))));
}
[Fact]
public void GuardBeforeOrWithABranchWithoutAnElementTest_Stays()
{
Assert.Single(Guards(Parse(p => p.Skills != null && (p.Skills.Contains("C#") || p.Age > 30))));
}
[Fact]
public void GuardBeforeOrOfContainsOnDifferentArrays_Stays()
{
Assert.Single(Guards(Parse(p => p.Skills != null && (p.Skills.Contains("C#") || p.Tags!.Contains("x")))));
}
[Fact]
public void GuardBeforeACount_Stays()
{
Assert.Single(Guards(Parse(p => p.Tags != null && p.Tags.Count > 0)));
Assert.Single(Guards(Parse(p => p.Tags != null && p.Tags.Count == 0)));
}
}