Path: redb/redb.Tests.Integration/Tests/MsSqlPro/MsSqlProHasDescendantFormTests.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/MsSqlPro/MsSqlProHasDescendantFormTests.cs ---- using redb.Core; using redb.Core.Query; using redb.Tests.Integration.Fixtures; using redb.Tests.Integration.Helpers; using redb.Tests.Integration.Models; namespace redb.Tests.Integration.Tests.MsSqlPro; /// /// WhereHasDescendant on SQL Server Pro: with no root, no parents and no other tree filter, the climb from the matching /// descendants runs in the batch of the statement that reads the nodes, into a temporary table that statement reads; /// otherwise its ids come back first and are sent as a condition on the object id. Each case returns the objects that /// have a matching descendant (within maxDepth), in both planner modes, through the list, the count, Any and an /// aggregate; the batch takes one statement less than the ids sent back. /// [Collection("MsSqlPro")] public class MsSqlProHasDescendantFormTests { private readonly IRedbService _redb; public MsSqlProHasDescendantFormTests(MsSqlProFixture fixture) => _redb = fixture.Redb; // r -> a -> b (marked), r -> c; x -> y (marked); z (marked, no parent, no child). private async Task<(string Tag, long R, long A, long B, long C, long X, long Y, long Z)> SeedAsync() { var tag = "hd-" + Guid.NewGuid().ToString("N")[..8]; async Task Node(string name, long? parent, bool marked, decimal budget) { var n = TestDataFactory.CreateTreeNode($"{tag}-{name}", tag, budget); n.Props.Description = marked ? "marked" : "plain"; if (parent == null) return await _redb.SaveAsync(n); var parentNode = await _redb.LoadAsync(parent.Value); return await _redb.CreateChildAsync(n, parentNode!); } var r = await Node("r", null, false, 1); var a = await Node("a", r, false, 10); var b = await Node("b", a, true, 100); var c = await Node("c", r, false, 1000); var x = await Node("x", null, false, 10000); var y = await Node("y", x, true, 100000); var z = await Node("z", null, true, 1000000); return (tag, r, a, b, c, x, y, z); } private async Task EachFormAsync(Func check) { var restore = _redb.Configuration.EnablePvtPrefilter; try { foreach (var on in new[] { false, true }) { _redb.UpdateConfiguration(c => c.EnablePvtPrefilter = on); await check(on ? "planner on" : "planner off"); } } finally { _redb.UpdateConfiguration(c => c.EnablePvtPrefilter = restore); } } [Fact] public async Task EachCase_ReturnsTheObjectsAboveAMatch() { var (tag, r, a, _, _, x, _, _) = await SeedAsync(); IRedbQueryable Above(string description, int? maxDepth = null) => _redb.TreeQuery() .Where(n => n.Code == tag).WhereHasDescendant(n => n.Code == tag && n.Description == description, maxDepth); await EachFormAsync(async form => { (await Above("marked").ToListAsync()).Select(o => o.id).Should().BeEquivalentTo([r, a, x], form); (await Above("marked", 1).ToListAsync()).Select(o => o.id).Should().BeEquivalentTo([a, x], $"maxDepth 1, {form}"); (await Above("marked").CountAsync()).Should().Be(3, $"count, {form}"); (await Above("marked").AnyAsync()).Should().BeTrue($"any, {form}"); (await Above("marked").SumAsync(n => n.Budget)).Should().Be(10011m, $"sum, {form}"); (await Above("none").ToListAsync()).Should().BeEmpty($"no match, {form}"); (await Above("none").CountAsync()).Should().Be(0, $"no match, count, {form}"); (await Above("none").AnyAsync()).Should().BeFalse($"no match, any, {form}"); // Under a root: the climb looks among the root's descendants and its ids come back first. (await _redb.TreeQuery(r).Where(n => n.Code == tag) .WhereHasDescendant(n => n.Code == tag && n.Description == "marked").ToListAsync()) .Select(o => o.id).Should().BeEquivalentTo([a], $"under a root, {form}"); }); } [Fact] public async Task InBatch_TakesOneStatementLess_AndShowsTheClimbInItsPreview() { var (tag, r, a, _, _, x, _, _) = await SeedAsync(); var plain = _redb.TreeQuery().Where(n => n.Code == tag) .WhereHasDescendant(n => n.Code == tag && n.Description == "marked"); // A second tree filter keeps the ids sent back. var listed = _redb.TreeQuery().Where(n => n.Code == tag) .WhereHasDescendant(n => n.Code == tag && n.Description == "marked") .WhereHasDescendant(n => n.Code == tag && n.Description == "marked"); List plainIds = [], listedIds = []; var inBatch = await CommandCounter.CountAsync(async () => plainIds = (await plain.ToListAsync()).Select(o => o.id).ToList()); var sentBack = await CommandCounter.CountAsync(async () => listedIds = (await listed.ToListAsync()).Select(o => o.id).ToList()); plainIds.Should().BeEquivalentTo([r, a, x]); listedIds.Should().BeEquivalentTo([r, a, x]); inBatch.Should().BeLessThan(sentBack, "the climb runs in the batch of the statement; sent back it is a statement of its own and a second for the next filter"); var preview = await plain.ToSqlStringAsync(); preview.Should().Contain("#redb_up", "the climb is part of the batch shown"); } }