Path: redb/redb.Tests.Integration/Tests/Base/RedundantArrayGuardTestsBase.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/Base/RedundantArrayGuardTestsBase.cs ---- using System.Linq.Expressions; using redb.Core; using redb.Core.Models.Contracts; using redb.Core.Models.Entities; using redb.Core.Query; using redb.Tests.Integration.Helpers; using redb.Tests.Integration.Models; namespace redb.Tests.Integration.Tests.Base; /// /// x.Skills != null && x.Skills.Contains(v): the Pro parser drops the null guard, which an element test of the /// same AND chain already implies, so the statement is the one of the test alone. The objects found and their count stay /// those of the guarded condition, over arrays with elements, an empty array and no array, on the flat and the tree /// query. The shapes are those of the examples E168 (Contains), E169 (Contains OR Contains), E170 (Contains AND Contains) /// and E171 (a scalar AND Contains), and Any with a predicate over an array of classes. /// public abstract class RedundantArrayGuardTestsBase { protected readonly IRedbService Redb; protected RedundantArrayGuardTestsBase(IRedbService redb) => Redb = redb; // Seven objects: elements in five of them, an empty array and no array. private static readonly string[]?[] SkillSets = [["C#", "SQL"], ["C#"], ["Python"], ["C#", "Python"], ["SQL"], [], null]; private sealed record Seeded(long RootId, List Ids, List Props); private async Task SeedAsync() { await Redb.SyncSchemeAsync(); var tag = "rag-" + Guid.NewGuid().ToString("N")[..8]; var root = new TreeRedbObject { name = $"{tag}-root", Props = TestDataFactory.CreateEmployee(0, department: $"{tag}-root").Props }; root.id = await Redb.SaveAsync(root); var ids = new List(); var props = new List(); for (var i = 0; i < SkillSets.Length; i++) { var p = TestDataFactory.CreateEmployee(i + 1, firstName: $"{tag}-{i}", department: tag).Props; p.Age = 27 + i; p.Skills = SkillSets[i]; ids.Add(await Redb.CreateChildAsync(new TreeRedbObject { name = $"{tag}-{i}", Props = p }, root)); props.Add(p); } return new Seeded(root.id, ids, props); } private IRedbQueryable Employees(Seeded s, bool tree) { if (tree) return Redb.TreeQuery(s.RootId).WhereRedb(o => o.Id != s.RootId); var idSet = s.Ids.ToHashSet(); return Redb.Query().WhereRedb(o => idSet.Contains(o.Id)); } private static long[] Expected(Seeded s, Func rule) => s.Ids.Where((_, i) => rule(s.Props[i])).OrderBy(id => id).ToArray(); private async Task ShouldFind(Seeded s, bool tree, Expression> filter, long[] expected) { var found = (await Employees(s, tree).Where(filter).ToListAsync()).Select(o => o.id).OrderBy(id => id).ToArray(); found.Should().Equal(expected); (await Employees(s, tree).Where(filter).CountAsync()).Should().Be(expected.Length); } public static TheoryData FlatAndTree => new() { false, true }; [Theory] [MemberData(nameof(FlatAndTree))] public async Task Contains_E168(bool tree) { var s = await SeedAsync(); await ShouldFind(s, tree, e => e.Skills != null && e.Skills.Contains("C#"), Expected(s, p => p.Skills != null && p.Skills.Contains("C#"))); } [Theory] [MemberData(nameof(FlatAndTree))] public async Task ContainsOrContains_E169(bool tree) { var s = await SeedAsync(); await ShouldFind(s, tree, e => e.Skills != null && (e.Skills.Contains("C#") || e.Skills.Contains("Python")), Expected(s, p => p.Skills != null && (p.Skills.Contains("C#") || p.Skills.Contains("Python")))); } [Theory] [MemberData(nameof(FlatAndTree))] public async Task ContainsAndContains_E170(bool tree) { var s = await SeedAsync(); await ShouldFind(s, tree, e => e.Skills != null && e.Skills.Contains("C#") && e.Skills.Contains("SQL"), Expected(s, p => p.Skills != null && p.Skills.Contains("C#") && p.Skills.Contains("SQL"))); } [Theory] [MemberData(nameof(FlatAndTree))] public async Task ScalarAndContains_E171(bool tree) { var s = await SeedAsync(); await ShouldFind(s, tree, e => e.Age > 28 && e.Skills != null && e.Skills.Contains("C#"), Expected(s, p => p.Age > 28 && p.Skills != null && p.Skills.Contains("C#"))); } [Fact] public async Task GuardedStatement_IsTheStatementOfTheTestAlone() { await Redb.SyncSchemeAsync(); async Task Same(Expression> guarded, Expression> alone) { var a = await Redb.Query().Where(guarded).Take(100).ToSqlStringAsync(); var b = await Redb.Query().Where(alone).Take(100).ToSqlStringAsync(); a.Should().Be(b); } await Same(e => e.Skills != null && e.Skills.Contains("C#"), e => e.Skills!.Contains("C#")); await Same(e => e.Skills != null && (e.Skills.Contains("C#") || e.Skills.Contains("Python")), e => e.Skills!.Contains("C#") || e.Skills!.Contains("Python")); await Same(e => e.Age > 30 && e.Skills != null && e.Skills.Contains("C#"), e => e.Age > 30 && e.Skills!.Contains("C#")); } [Fact] public async Task AnyOverAnArrayOfClasses_WithTheGuard() { await Redb.SyncSchemeAsync(); var tag = "rag-" + Guid.NewGuid().ToString("N")[..8]; List rows = [ new() { Tag = tag, Number = 1, Lines = [new() { Kind = "a", Qty = 1 }, new() { Kind = "b", Qty = 2 }] }, new() { Tag = tag, Number = 2, Lines = [new() { Kind = "b", Qty = 3 }] }, new() { Tag = tag, Number = 3, Lines = [] }, new() { Tag = tag, Number = 4 }, ]; await Redb.SaveAsync(rows.Select(p => (IRedbObject)new RedbObject { name = $"{tag}-{p.Number}", Props = p }).ToList()); var query = Redb.Query().Where(p => p.Tag == tag).Where(p => p.Lines != null && p.Lines.Any(l => l.Kind == "a")); (await query.ToListAsync()).Select(o => o.Props.Number).Should().Equal(1); (await query.CountAsync()).Should().Be(1); } }