Path: redb/redb.Tests.Integration/Tests/Base/FreeAnyExistsTestsBase.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/FreeAnyExistsTestsBase.cs ---- using redb.Core; 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; /// /// Free AnyAsync runs the inner statement of the v2-pvt module under EXISTS, which stops at the first match; it used to /// count every match. Every form gives the answer it gave, the count above zero. /// public abstract class FreeAnyExistsTestsBase { protected readonly IRedbService Redb; protected FreeAnyExistsTestsBase(IRedbService redb) => Redb = redb; private async Task SeedEmployeesAsync() { await Redb.SyncSchemeAsync(); var tag = "any-" + Guid.NewGuid().ToString("N")[..8]; await Redb.SaveAsync(Enumerable.Range(0, 5) .Select(i => TestDataFactory.CreateEmployee(i, salary: 100m * (i + 1), department: $"{tag}-D{i % 2}")).ToList()); return tag; } public static IEnumerable FlatCases() => new[] { new object[] { "filter", true }, new object[] { "filter-none", false }, new object[] { "order", true }, new object[] { "distinct", true }, new object[] { "distinct-by", true }, new object[] { "distinct-none", false }, new object[] { "where-in-empty", false }, new object[] { "take-0", false }, new object[] { "skip-past", false }, new object[] { "skip-inside", true }, new object[] { "predicate", true }, new object[] { "predicate-none", false } }; [Theory] [MemberData(nameof(FlatCases))] public async Task AnyAsync_FlatQuery_GivesTheAnswerOfTheCount(string kind, bool expected) { var tag = await SeedEmployeesAsync(); IRedbQueryable Tagged() => Redb.Query().Where(e => e.Department.StartsWith(tag)); var query = kind switch { "filter" => Tagged().Where(e => e.Salary > 300m), "filter-none" => Tagged().Where(e => e.Salary > 10_000m), "order" => Tagged().OrderByDescending(e => e.Salary), "distinct" => Tagged().Distinct(), "distinct-by" => Tagged().DistinctBy(e => e.Department), "distinct-none" => Tagged().Where(e => e.Salary < 0m).DistinctBy(e => e.Department), "where-in-empty" => Tagged().WhereIn(e => e.Department, Array.Empty()), "take-0" => Tagged().Take(0), "skip-past" => Tagged().OrderBy(e => e.Salary).Skip(5), "skip-inside" => Tagged().OrderBy(e => e.Salary).Skip(4), "predicate" => Tagged(), "predicate-none" => Tagged(), _ => throw new ArgumentOutOfRangeException(nameof(kind)) }; var any = kind switch { "predicate" => await query.AnyAsync(e => e.Salary == 500m), "predicate-none" => await query.AnyAsync(e => e.Salary == 501m), _ => await query.AnyAsync() }; var count = kind switch { "predicate" => await query.Where(e => e.Salary == 500m).CountAsync(), "predicate-none" => await query.Where(e => e.Salary == 501m).CountAsync(), _ => await query.CountAsync() }; any.Should().Be(expected); any.Should().Be(count > 0, "AnyAsync answers as the count above zero did"); } public static IEnumerable TreeCases() => new[] { new object[] { "subtree", true }, new object[] { "subtree-filter-none", false }, new object[] { "level-1", true }, new object[] { "level-5", false }, new object[] { "children", true }, new object[] { "leaf-children", false }, new object[] { "unrooted", true }, new object[] { "has-ancestor", true }, new object[] { "has-ancestor-none", false } }; [Theory] [MemberData(nameof(TreeCases))] public async Task AnyAsync_TreeQuery_GivesTheAnswerOfTheCount(string kind, bool expected) { await Redb.SyncSchemeAsync(); var tag = "anyt-" + Guid.NewGuid().ToString("N")[..8]; var root = new TreeRedbObject { name = tag + "-r", Props = new SimpleProps { Title = tag } }; root.id = await Redb.SaveAsync(root); var child = new TreeRedbObject { name = tag + "-c", Props = new SimpleProps { Title = tag, Count = 1 } }; child.id = await Redb.CreateChildAsync(child, root); var leaf = new TreeRedbObject { name = tag + "-l", Props = new SimpleProps { Title = tag, Count = 2 } }; leaf.id = await Redb.CreateChildAsync(leaf, child); var query = kind switch { "subtree" => Redb.TreeQuery(root.id), "subtree-filter-none" => Redb.TreeQuery(root.id).Where(p => p.Count > 10), "level-1" => Redb.TreeQuery(root.id).WhereLevel(1), "level-5" => Redb.TreeQuery(root.id).WhereLevel(5), "children" => Redb.TreeQuery().WhereChildrenOf(root.id), "leaf-children" => Redb.TreeQuery().WhereChildrenOf(leaf.id), "unrooted" => Redb.TreeQuery().Where(p => p.Title == tag), "has-ancestor" => Redb.TreeQuery().Where(p => p.Title == tag).WhereHasAncestor(a => a.Count == 1), "has-ancestor-none" => Redb.TreeQuery().Where(p => p.Title == tag).WhereHasAncestor(a => a.Count == 99), _ => throw new ArgumentOutOfRangeException(nameof(kind)) }; var any = await query.AnyAsync(); any.Should().Be(expected); any.Should().Be(await query.CountAsync() > 0, "AnyAsync answers as the count above zero did"); } }