Path: redb/redb.Tests.Integration/Tests/Base/CollectionFilterAnalyticsTestsBase.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/CollectionFilterAnalyticsTestsBase.cs
----
using System.Linq.Expressions;
using redb.Core;
using redb.Core.Models.Entities;
using redb.Core.Query;
using redb.Core.Query.Aggregation;
using redb.Core.Query.Window;
using redb.Tests.Integration.Helpers;
using redb.Tests.Integration.Models;
namespace redb.Tests.Integration.Tests.Base;
///
/// Every analytic form over a filter that reads an array, a dictionary entry or a field of a nested class, flat and
/// over a tree, compared with the same LINQ over the seeded objects in memory: SumAsync, AggregateAsync, GroupBy,
/// WithWindow, GroupBy.WithWindow, GroupByArray, and for the tree also ToListAsync and CountAsync.
///
/// Pro answers such a filter with the list statement of the same filter inside the analytic statement (a subquery on
/// PostgreSQL and SQLite, a temporary table of the same batch on SQL Server); it used to run that statement first and
/// inline the ids it returned. A filter that matches nothing and a filter that matches one object are part of the set:
/// the first must not turn into "every object", the second must give exactly one.
///
public abstract class CollectionFilterAnalyticsTestsBase
{
protected readonly IRedbService Redb;
protected CollectionFilterAnalyticsTestsBase(IRedbService redb) => Redb = redb;
private sealed record Seeded(long RootId, string Tag, List Children);
/// A root and eight children of one department, varied in skills, phone keys, floors and contacts.
private async Task SeedAsync()
{
await Redb.SyncSchemeAsync();
var tag = "cf-" + Guid.NewGuid().ToString("N")[..8];
var root = new TreeRedbObject
{
name = $"{tag}-root",
Props = TestDataFactory.CreateEmployee(0, department: $"{tag}-root", salary: 90_000m).Props
};
root.id = await Redb.SaveAsync(root);
var children = new List();
for (var i = 1; i <= 8; i++)
{
var p = TestDataFactory.CreateEmployee(i, firstName: $"{tag}-n{i}", department: tag, salary: 1000m * i,
position: i % 2 == 0 ? "Even" : "Odd").Props;
p.Skills = i % 3 == 0 ? ["sql", "x"] : ["x"];
p.PhoneDirectory = i % 2 == 0 ? new Dictionary { ["desk"] = $"{i}" } : new Dictionary { ["home"] = $"{i}" };
p.HomeAddress!.Building!.Floor = i;
// Email verified, phone not: the shape every seeded employee has, which ArrayGroupByDeepTestsBase counts on
// over the shared database.
p.Contacts =
[
new TestContact { Type = i % 2 == 0 ? "email" : "phone", IsVerified = i % 2 == 0, Value = $"{tag}-{i}-a" },
new TestContact { Type = "email", IsVerified = true, Value = $"{tag}-{i}-b" }
];
await Redb.CreateChildAsync(new TreeRedbObject { name = $"{tag}-c{i}", Props = p }, root);
children.Add(p);
}
return new Seeded(root.id, tag, children);
}
/// The filter of each case, always within the seeded department so other tests' data stays out.
private static Expression> Filter(string kind, string tag)
{
var narrowName = tag + "-n4";
return kind switch
{
"nested" => e => e.Department == tag && e.HomeAddress!.Building!.Floor > 4,
"array" => e => e.Department == tag && e.Skills!.Contains("sql"),
"dict" => e => e.Department == tag && e.PhoneDirectory!.ContainsKey("desk"),
"none" => e => e.Department == tag && e.Skills!.Contains("absent"),
"narrow" => e => e.Department == tag && e.FirstName == narrowName && e.Skills!.Contains("x"),
_ => throw new ArgumentOutOfRangeException(nameof(kind))
};
}
public static IEnumerable