Path: redb/redb.Tests.Integration/Tests/Base/ProWindowTestsBase.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/ProWindowTestsBase.cs
----
using redb.Core;
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;
///
/// Windows of the Pro providers: the shape that reads the shown fields after Take/Skip, and the SQL preview (the Free
/// providers run windows through a SQL function and have no preview).
///
public abstract class ProWindowTestsBase : WindowTestsBase
{
protected ProWindowTestsBase(IRedbService redb) : base(redb) { }
[Fact]
public async Task Window_SkipTake_ShownFieldsAreReadForTheRowsKept()
{
// The window is computed over the fields it needs; the fields only shown (FirstName, HomeAddress.City) are read
// afterwards for the rows Skip/Take keep. Each row must carry its own fields, and the window values must be
// those of the whole filtered set. Two departments of this test only, so parallel seeding changes no rank.
var tag = Guid.NewGuid().ToString("N")[..8];
var (a, b) = ($"win-a-{tag}", $"win-b-{tag}");
await Redb.SaveAsync(Enumerable.Range(0, 24)
.Select(i => TestDataFactory.CreateEmployee(i, firstName: $"W{tag}{i:D2}", salary: 40000m + (i % 7) * 1000m,
department: i % 2 == 0 ? a : b, city: $"City{i % 5}"))
.ToList());
var truth = await Redb.Query().Where(e => e.Department == a || e.Department == b).ToListAsync();
truth.Should().HaveCount(24);
var byId = truth.ToDictionary(o => o.Id);
var rows = await Redb.Query()
.Where(e => e.Department == a || e.Department == b)
.Skip(5).Take(10)
.WithWindow(w => w.PartitionBy(e => e.Department).OrderByDesc(e => e.Salary))
.SelectAsync(e => new
{
e.Id, e.Props.FirstName, City = e.Props.HomeAddress!.City, e.Props.Department,
Rank = Win.Rank(), Total = Win.Sum(e.Props.Salary)
});
rows.Should().HaveCount(10);
rows.Select(r => r.Id).Should().OnlyHaveUniqueItems();
foreach (var r in rows)
{
var t = byId[r.Id];
var department = truth.Where(o => o.Props.Department == t.Props.Department).Select(o => o.Props.Salary).ToList();
r.FirstName.Should().Be(t.Props.FirstName);
r.City.Should().Be(t.Props.HomeAddress!.City);
r.Department.Should().Be(t.Props.Department);
r.Rank.Should().Be(1 + department.Count(s => s > t.Props.Salary));
r.Total.Should().Be(department.Where(s => s >= t.Props.Salary).Sum(), "a running sum within the department");
}
}
[Fact]
public async Task Window_WithoutBaseFields_ReadsItsFieldsAlone_AndLeavesOutTheTrash()
{
// A window that reads no base field takes its rows from the fields alone: _objects is joined only when the
// window reads one of its columns. The objects of the fields are those of the scheme, so a soft-deleted object
// (moved to the trash, its values kept) stays out as before.
var tag = Guid.NewGuid().ToString("N")[..8];
var department = $"win-t-{tag}";
var ids = await Redb.SaveAsync(Enumerable.Range(0, 6)
.Select(i => TestDataFactory.CreateEmployee(i, firstName: $"T{tag}{i}", salary: 50000m + i * 1000m, department: department))
.ToList());
await Redb.SoftDeleteAsync(new[] { ids[5] });
var window = Redb.Query().Where(e => e.Department == department)
.WithWindow(w => w.PartitionBy(e => e.Department).OrderByDesc(e => e.Salary));
var rows = await window.SelectAsync(e => new { e.Props.Salary, Rank = Win.RowNumber() });
rows.Select(r => r.Salary).Should().BeEquivalentTo(new[] { 50000m, 51000m, 52000m, 53000m, 54000m },
"the soft-deleted object is not in the window");
rows.Select(r => r.Rank).Should().BeEquivalentTo(new long[] { 1, 2, 3, 4, 5 });
(await window.ToSqlStringAsync(e => new { e.Props.Salary, Rank = Win.RowNumber() }))
.Should().Contain("FROM pvt_cte p").And.NotContain("JOIN pvt_cte p");
(await window.ToSqlStringAsync(e => new { e.Id, e.Props.Salary, Rank = Win.RowNumber() }))
.Should().Contain("JOIN pvt_cte p", "a base field of the window is read from _objects");
}
[Fact]
public async Task Window_SqlPreview_CarriesTheFilter()
{
// ToSqlStringAsync of a window after Where used to drop the filter: the preview is the SQL that runs.
var sql = await Redb.Query()
.Where(e => e.Salary > 70123m)
.Take(20)
.WithWindow(w => w.PartitionBy(e => e.Department).OrderByDesc(e => e.Salary))
.ToSqlStringAsync(e => new { e.Props.FirstName, Rank = Win.RowNumber() });
sql.Should().Contain("70123");
}
}