Path: redb/redb.Tests.Integration/Tests/Base/ArrayGroupMetadataCacheTestsBase.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/ArrayGroupMetadataCacheTestsBase.cs ---- using redb.Core; using redb.Core.Models.Entities; using redb.Core.Query; using redb.Core.Query.Aggregation; using redb.Tests.Integration.Helpers; using redb.Tests.Integration.Models; namespace redb.Tests.Integration.Tests.Base; /// /// Pro GroupByArray reads the metadata of the array path and of the element fields once: a repeated call runs the data /// statement alone, and its SQL preview runs nothing. The commands are counted through the redb.Core ActivitySource, /// one span per database command (CommandCounter). /// public abstract class ArrayGroupMetadataCacheTestsBase { protected readonly IRedbService Redb; protected ArrayGroupMetadataCacheTestsBase(IRedbService redb) => Redb = redb; // The data statement and any metadata read. Not the scalar side commands of the // PostgreSQL statistics guard, which a call meets or not by timing (CommandCounter.CountWithoutScalarsAsync). private static Task CommandsAsync(Func action) => CommandCounter.CountWithoutScalarsAsync(action); private static Task> GroupAsync(IRedbQueryable source) => source.GroupByArray(e => e.Contacts!.SelectMany(c => c.Metadata!), m => m.Label) .SelectAsync(g => new Row { Label = g.Key, Count = Agg.Count(g), Values = Agg.Count(g, m => m.Value == "d") }); private static Task PreviewAsync(IRedbQueryable source) => source.GroupByArray(e => e.Contacts!.SelectMany(c => c.Metadata!), m => m.Label) .ToSqlStringAsync(g => new Row { Label = g.Key, Count = Agg.Count(g), Values = Agg.Count(g, m => m.Value == "d") }); private sealed class Row { public string? Label { get; set; } public int Count { get; set; } public int Values { get; set; } } private static EmployeeProps Employee(int i, string marker) { var p = TestDataFactory.CreateEmployee(i, firstName: $"{marker}-{i:D2}").Props; p.Contacts![0].Metadata = [new TestContactDetail { Label = "work", Value = $"v{i}" }]; p.Contacts[1].Metadata = i % 2 == 0 ? [new TestContactDetail { Label = "night", Value = "d" }] : null; return p; } [Fact] public async Task FlatGrouping_RepeatedCall_RunsTheDataStatementAlone() { await Redb.SyncSchemeAsync(); var marker = "mc" + Guid.NewGuid().ToString("N")[..10]; for (var i = 0; i < 6; i++) await Redb.SaveAsync(new RedbObject { name = $"{marker}-{i}", Props = Employee(i, marker) }); IRedbQueryable Source() => Redb.Query().Where(e => e.FirstName.StartsWith(marker)); var first = await GroupAsync(Source()); first.Should().HaveCount(2, "work and night"); (await CommandsAsync(() => GroupAsync(Source()))).Should().Be(1, "the metadata of the path is cached after the first call"); (await CommandsAsync(() => PreviewAsync(Source()))).Should().Be(0, "the preview builds the statement from cached metadata"); } [Fact] public async Task TreeGrouping_RepeatedCall_RunsTheDataStatementAlone() { await Redb.SyncSchemeAsync(); var marker = "mt" + Guid.NewGuid().ToString("N")[..10]; var root = new TreeRedbObject { name = marker, Props = Employee(0, marker) }; root.id = await Redb.SaveAsync(root); for (var i = 1; i < 6; i++) await Redb.CreateChildAsync(new TreeRedbObject { name = $"{marker}-{i}", Props = Employee(i, marker) }, root); IRedbQueryable Source() => Redb.TreeQuery(root.id); var first = await GroupAsync(Source()); first.Should().HaveCount(2, "work and night under the root"); (await CommandsAsync(() => GroupAsync(Source()))).Should().Be(1, "the metadata of the path is cached after the first call"); (await CommandsAsync(() => PreviewAsync(Source()))).Should().Be(0, "the preview builds the statement from cached metadata"); } }