Path: redb/redb.Tests.Integration/Tests/MsSqlPro/MsSqlProHasAncestorFormTests.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/MsSqlProHasAncestorFormTests.cs
----
using redb.Core;
using redb.Core.Models.Contracts;
using redb.Core.Models.Entities;
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;
///
/// WhereHasAncestor on SQL Server Pro: without a root, parents, query depth or other tree filter, the query walks down from
/// the matching ancestors as its parents, in the statement that reads the page: without maxDepth at any depth, with a
/// maxDepth at most that many levels (the query's own depth limit). Every other case lists the descendants first and sends
/// their ids back. Each case returns the right objects; the walk takes one data statement less than the listing. With a
/// maxDepth the walk must count from the nearest matching ancestor and return a node under two nested ones once.
///
[Collection("MsSqlPro")]
public class MsSqlProHasAncestorFormTests
{
private readonly IRedbService _redb;
public MsSqlProHasAncestorFormTests(MsSqlProFixture fixture) => _redb = fixture.Redb;
// root (marked) -> a -> b -> c, and root -> d. Only the root matches the ancestor condition.
private async Task<(string Tag, long Root, long A, long B, long C, long D)> SeedAsync()
{
var tag = "ha-" + Guid.NewGuid().ToString("N")[..8];
var root = TestDataFactory.CreateTreeNode($"{tag}-root", tag);
root.Props.Description = "anchor";
root.id = await _redb.SaveAsync(root);
var a = TestDataFactory.CreateTreeNode($"{tag}-a", tag); a.Props.Description = "mid"; a.id = await _redb.CreateChildAsync(a, root);
var b = TestDataFactory.CreateTreeNode($"{tag}-b", tag); b.id = await _redb.CreateChildAsync(b, a);
var c = TestDataFactory.CreateTreeNode($"{tag}-c", tag); c.id = await _redb.CreateChildAsync(c, b);
var d = TestDataFactory.CreateTreeNode($"{tag}-d", tag); d.id = await _redb.CreateChildAsync(d, root);
return (tag, root.id, a.id, b.id, c.id, d.id);
}
private static async Task<(List Ids, int Statements)> RunAsync(IRedbQueryable query)
{
List ids = [];
var statements = await CommandCounter.CountAsync(async () =>
ids = (await query.ToListAsync()).Select(o => o.id).ToList());
return (ids, statements);
}
[Fact]
public async Task EachCase_ReturnsItsAnswer_InTheFormItsShapeCalls()
{
var (tag, root, a, b, c, d) = await SeedAsync();
IRedbQueryable Tree() => _redb.TreeQuery().Where(n => n.Code == tag);
// No maxDepth: every descendant of the root, walked from it.
var any = await RunAsync(Tree().WhereHasAncestor(n => n.Code == tag && n.Description == "anchor"));
any.Ids.Should().BeEquivalentTo([a, b, c, d]);
// maxDepth 1: the children, walked from the root.
var children = await RunAsync(Tree().WhereHasAncestor(n => n.Code == tag && n.Description == "anchor", 1));
children.Ids.Should().BeEquivalentTo([a, d]);
// maxDepth 2: two levels, walked from the root with the query's depth limit.
var two = await RunAsync(Tree().WhereHasAncestor(n => n.Code == tag && n.Description == "anchor", 2));
two.Ids.Should().BeEquivalentTo([a, b, d]);
// A root of the query: listed first. The ancestor is looked for inside the query's tree, so it is a, not the root.
var rooted = await RunAsync(_redb.TreeQuery(root).Where(n => n.Code == tag)
.WhereHasAncestor(n => n.Code == tag && n.Description == "mid"));
rooted.Ids.Should().BeEquivalentTo([b, c]);
// Another tree filter beside it (a second WhereHasAncestor): the first one is listed, the second resolved after it.
var both = await RunAsync(Tree().WhereHasAncestor(n => n.Code == tag && n.Description == "anchor")
.WhereHasAncestor(n => n.Code == tag && n.Description == "mid"));
both.Ids.Should().BeEquivalentTo([b, c], "under the anchor and under a");
// The walk reads the page in the statement that walks down; the listing adds the statement of the descendants.
var counts = $"statements: no maxDepth {any.Statements}, maxDepth 1 {children.Statements}, maxDepth 2 {two.Statements}, " +
$"rooted {rooted.Statements}, two filters {both.Statements}";
two.Statements.Should().Be(any.Statements, "maxDepth 2 walks down from the ancestors too; {0}", counts);
rooted.Statements.Should().BeGreaterThan(any.Statements, "a rooted query keeps the listing; {0}", counts);
both.Statements.Should().BeGreaterThan(any.Statements, "another tree filter keeps the listing; {0}", counts);
}
///
/// Two matching ancestors, one under the other: root (marked) -> a (marked) -> x -> y, and root -> d. With maxDepth 2,
/// x is one level below a and two below the root and comes once; y is two levels below a, three below the root, and is
/// in: the depth counts from the nearest matching ancestor.
///
[Fact]
public async Task MaxDepth_NestedAncestors_NodeOnce_DepthFromTheNearest()
{
var tag = "han-" + Guid.NewGuid().ToString("N")[..8];
var root = TestDataFactory.CreateTreeNode($"{tag}-root", tag); root.Props.Description = "anchor";
root.id = await _redb.SaveAsync(root);
var a = TestDataFactory.CreateTreeNode($"{tag}-a", tag); a.Props.Description = "anchor"; a.id = await _redb.CreateChildAsync(a, root);
var x = TestDataFactory.CreateTreeNode($"{tag}-x", tag); x.id = await _redb.CreateChildAsync(x, a);
var y = TestDataFactory.CreateTreeNode($"{tag}-y", tag); y.id = await _redb.CreateChildAsync(y, x);
var z = TestDataFactory.CreateTreeNode($"{tag}-z", tag); z.id = await _redb.CreateChildAsync(z, y);
var d = TestDataFactory.CreateTreeNode($"{tag}-d", tag); d.id = await _redb.CreateChildAsync(d, root);
IRedbQueryable Under(int maxDepth) => _redb.TreeQuery().Where(n => n.Code == tag)
.WhereHasAncestor(n => n.Code == tag && n.Description == "anchor", maxDepth);
var two = (await Under(2).ToListAsync()).Select(o => o.id).ToList();
two.Should().OnlyHaveUniqueItems("x is under both anchors");
two.Should().BeEquivalentTo([a.id, x.id, y.id, d.id], "z is three levels below a");
var three = (await Under(3).ToListAsync()).Select(o => o.id).ToList();
three.Should().OnlyHaveUniqueItems();
three.Should().BeEquivalentTo([a.id, x.id, y.id, z.id, d.id]);
(await Under(2).CountAsync()).Should().Be(4, "the count reads the same walk");
}
///
/// More matching ancestors than the provider sends one parameter each (ProSqlBuilder.InListParameterLimit, 500): the
/// parents travel as one list. 520 marked roots, each with a child and a grandchild, one great-grandchild under the
/// first; maxDepth 2 returns the children and the grandchildren.
///
[Fact]
public async Task MaxDepth_ParentsAboveTheParameterLimit()
{
const int anchors = 520;
var tag = "hap-" + Guid.NewGuid().ToString("N")[..8];
IRedbObject Node(string name, long? parent, string? description = null) => new RedbObject
{
name = name, parent_id = parent,
Props = new TreeNodeProps { Name = name, Code = tag, Description = description ?? string.Empty, IsActive = true }
};
var rootIds = await _redb.SaveAsync(Enumerable.Range(0, anchors).Select(i => Node($"{tag}-r{i}", null, "anchor")).ToList());
var childIds = await _redb.SaveAsync(rootIds.Select((r, i) => Node($"{tag}-c{i}", r)).ToList());
var grandchildIds = await _redb.SaveAsync(childIds.Select((c, i) => Node($"{tag}-g{i}", c)).ToList());
await _redb.SaveAsync(new List { Node($"{tag}-deep", grandchildIds[0]) });
var ids = (await _redb.TreeQuery().Where(n => n.Code == tag)
.WhereHasAncestor(n => n.Code == tag && n.Description == "anchor", 2).ToListAsync())
.Select(o => o.id).ToList();
ids.Should().OnlyHaveUniqueItems();
ids.Should().BeEquivalentTo(childIds.Concat(grandchildIds));
}
}