Path: redb/redb.Tests.Integration/Tests/Base/QueryPlannerBlockTestsBase.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/QueryPlannerBlockTestsBase.cs
----
using Microsoft.Extensions.DependencyInjection;
using redb.Core;
using redb.Core.Models.Entities;
using redb.Core.Query;
using redb.Tests.Integration.Helpers;
using redb.Tests.Integration.Models;
namespace redb.Tests.Integration.Tests.Base;
///
/// The query planner switched for a block of code (redb.WithQueryPlanner(bool)), Pro only. The configuration is
/// one object per process; the block changes the calling flow only, nests, restores on dispose, and leaves concurrent
/// flows alone. The form is told by the SQL: the aggregate pivot folds the value rows with GROUP BY, the streams form of
/// a filter, a sort and a page has none. Runs in both planner modes of the suite: the block wins over either.
///
public abstract class QueryPlannerBlockTestsBase
{
protected readonly IRedbService Redb;
private readonly IServiceProvider _services;
protected QueryPlannerBlockTestsBase(IRedbService redb, IServiceProvider services)
{
Redb = redb;
_services = services;
}
private async Task SeedAsync()
{
await Redb.SyncSchemeAsync();
var tag = "pvb-" + Guid.NewGuid().ToString("N")[..8];
for (var i = 1; i <= 6; i++)
{
var employee = TestDataFactory.CreateEmployee(i, firstName: $"{tag}-{i}", department: tag, salary: 1000m * i);
employee.Props.Age = 20 + i;
await Redb.SaveAsync(employee);
}
return tag;
}
private static IRedbQueryable Page(IRedbService redb, string tag)
=> redb.Query().Where(e => e.Department == tag && e.Age >= 22).OrderBy(e => e.Salary).Take(3);
private static bool IsAggregatePivot(string sql) => sql.Contains("GROUP BY", StringComparison.OrdinalIgnoreCase);
[Fact]
public async Task Block_SwitchesTheFormForTheCallingFlow_AndTheAnswerStaysTheSame()
{
var tag = await SeedAsync();
string sqlOff, sqlOn;
List idsOff, idsOn;
using (Redb.WithQueryPlanner(false))
{
QueryPlannerScope.BlockValue.Should().BeFalse();
sqlOff = await Page(Redb, tag).ToSqlStringAsync();
idsOff = (await Page(Redb, tag).ToListAsync()).Select(o => o.id).ToList();
}
using (Redb.WithQueryPlanner(true))
{
sqlOn = await Page(Redb, tag).ToSqlStringAsync();
idsOn = (await Page(Redb, tag).ToListAsync()).Select(o => o.id).ToList();
}
IsAggregatePivot(sqlOff).Should().BeTrue("with the planner off the query is read through the aggregate pivot:\n" + sqlOff);
IsAggregatePivot(sqlOn).Should().BeFalse("with the planner on this page is read through the streams form:\n" + sqlOn);
idsOn.Should().HaveCount(3).And.Equal(idsOff, "the form changes, the answer does not");
QueryPlannerScope.BlockValue.Should().BeNull("both blocks are disposed");
}
[Fact]
public async Task Blocks_Nest_AndDisposingRestoresTheOuterValue()
{
var tag = await SeedAsync();
using (Redb.WithQueryPlanner(false))
{
using (Redb.WithQueryPlanner(true))
IsAggregatePivot(await Page(Redb, tag).ToSqlStringAsync()).Should().BeFalse("the inner block turns the planner on");
IsAggregatePivot(await Page(Redb, tag).ToSqlStringAsync()).Should().BeTrue("the outer block is in effect again");
}
}
[Fact]
public async Task Block_DoesNotReachAConcurrentFlow()
{
var tag = await SeedAsync();
using var bothInside = new Barrier(2);
async Task InBlock(bool enabled)
{
using var scope = _services.CreateScope();
var redb = scope.ServiceProvider.GetRequiredService();
using (redb.WithQueryPlanner(enabled))
{
bothInside.SignalAndWait(); // both blocks are open at the same time
return await Page(redb, tag).ToSqlStringAsync();
}
}
var off = Task.Run(() => InBlock(false));
var on = Task.Run(() => InBlock(true));
await Task.WhenAll(off, on);
IsAggregatePivot(await off).Should().BeTrue("the flow that switched the planner off reads the pivot");
IsAggregatePivot(await on).Should().BeFalse("the concurrent flow that switched it on is not affected");
}
[Fact]
public async Task OutsideAnyBlock_TheConfigurationDecides()
{
var tag = await SeedAsync();
QueryPlannerScope.BlockValue.Should().BeNull();
var configured = QueryPlannerScope.IsEnabled(Redb.Configuration);
IsAggregatePivot(await Page(Redb, tag).ToSqlStringAsync()).Should().Be(!configured,
$"EnablePvtPrefilter is {configured} for this suite and no block is open");
}
[Fact]
public async Task TheBlockInEffectWhereTheQueryRuns_Decides()
{
var tag = await SeedAsync();
// Composed inside a block, run after it: the value at the run counts.
IRedbQueryable composed;
using (Redb.WithQueryPlanner(false))
composed = Page(Redb, tag);
using (Redb.WithQueryPlanner(true))
IsAggregatePivot(await composed.ToSqlStringAsync()).Should().BeFalse("it runs inside the block that turns the planner on");
// Started inside a block, awaited after it: the call keeps the block it was made in.
Task started;
using (Redb.WithQueryPlanner(false))
started = Page(Redb, tag).ToSqlStringAsync();
IsAggregatePivot(await started).Should().BeTrue("the call was made inside the block that turns the planner off");
}
}