Path: redb/redb.Tests.Integration/Tests/Base/AggregatePreviewTestsBase.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/AggregatePreviewTestsBase.cs ---- using redb.Core; 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; /// /// The aggregate SQL preview shows the query that runs. The Pro previews built it with no filter while /// AggregateAsync passed the query's Where: the preview of a filtered sum showed a sum over the whole scheme. Free asked /// for aggregate_batch_preview, a function of the removed legacy module, and threw NotSupportedException. /// public abstract class AggregatePreviewTestsBase { protected readonly IRedbService Redb; protected AggregatePreviewTestsBase(IRedbService redb) => Redb = redb; [Fact] public async Task AggregatePreview_CarriesTheWhere() { await Redb.SyncSchemeAsync(); var department = $"preview-{Guid.NewGuid():N}"; var sql = await Redb.Query() .Where(e => e.Department == department && e.Salary > 1m) .ToAggregateSqlStringAsync(x => new { Total = Agg.Sum(x.Props.Salary), Headcount = Agg.Count() }); sql.Should().Contain(department, "the preview must carry the Where the aggregate runs with"); } } /// /// On Free the preview is the statement the aggregate runs: pvt_build_aggregate_sql compiles it with every value inline, /// so it runs as it is and gives what AggregateAsync returns. /// public abstract class FreeAggregatePreviewTestsBase : AggregatePreviewTestsBase { protected FreeAggregatePreviewTestsBase(IRedbService redb) : base(redb) { } [Fact] public async Task AggregatePreview_RunAsItIs_GivesWhatTheAggregateReturns() { await Redb.SyncSchemeAsync(); var department = $"preview-{Guid.NewGuid():N}"; await Redb.SaveAsync(Enumerable.Range(1, 3) .Select(i => TestDataFactory.CreateEmployee(i, salary: 100m * i, department: department)).ToList()); IRedbQueryable Tagged() => Redb.Query().Where(e => e.Department == department); var aggregate = await Tagged().AggregateAsync(x => new { Total = Agg.Sum(x.Props.Salary) }); var sql = await Tagged().ToAggregateSqlStringAsync(x => new { Total = Agg.Sum(x.Props.Salary) }); aggregate.Total.Should().Be(600m); (await Redb.Context.ExecuteScalarAsync(sql)).Should().Be(aggregate.Total, "the preview is the statement that runs"); } } /// /// The Pro preview reads the selector with the parser the execution uses. Its own parser took a dictionary's .Values /// and an indexer key for nothing, and the preview showed SELECT 1 AS "_empty" for an aggregate that runs. /// public abstract class ProAggregatePreviewTestsBase : AggregatePreviewTestsBase { protected ProAggregatePreviewTestsBase(IRedbService redb) : base(redb) { } [Fact] public async Task AggregatePreview_DictionaryValues_AggregatesTheValues() { await Redb.SyncSchemeAsync(); var sql = await Redb.Query() .ToAggregateSqlStringAsync(x => new { Total = Agg.Sum(x.Props.BonusByYear!.Values) }); sql.ToUpperInvariant().Should().Contain("SUM("); sql.Should().NotContain("_empty", "the values of the dictionary are aggregated"); } [Fact] public async Task AggregatePreview_DictionaryKey_AggregatesThatKey() { await Redb.SyncSchemeAsync(); var sql = await Redb.Query() .ToAggregateSqlStringAsync(x => new { Bonus = Agg.Sum(x.Props.BonusByYear![2023]) }); sql.Should().ContainEquivalentOf("_array_index", "the key selects one entry of the dictionary"); sql.Should().Contain("2023"); sql.Should().NotContain("_empty"); } }