Path: redb-route/tests/redb.Route.Tests.Llm/Storage/StorageProFixture.cs
Snapshot: redb-route, 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-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.Llm/Storage/StorageProFixture.cs
----
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using redb.Core;
using redb.Core.Models.Configuration;
using redb.Core.Pro.Extensions;
using redb.MSSql.Pro.Extensions;
using redb.Postgres.Pro.Extensions;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Llm.Storage.Redb.Schemas;
using redb.SQLite.Pro.Extensions;
namespace redb.Route.Tests.Llm.Storage;
///
/// Provider-substituted Pro fixture for the redb.Route.Llm storage integration tests: one
/// fixture, one set of test classes, the provider slid underneath by REDB_PROVIDER
/// (sqlite — default, since it needs no stand and is where the dialect's own planner guards
/// matter — or postgres / mssql), following the redb.Tests.Integration matrix convention.
/// Uses the Pro free tier (1024 queries, no JWT license required).
///
/// Cleanup is scoped to the LLM schemes only — never DELETE FROM _objects,
/// which would nuke unrelated data shared with other test runs against the same DB.
/// We enumerate rows of each LLM *Props scheme through the redb query API
/// and remove them via the bulk DeleteAsync(ids) primitive (which cascades
/// to _values and _tree).
///
///
public sealed class StorageProFixture : IAsyncLifetime
{
public IRedbService Redb { get; private set; } = null!;
public ServiceProvider ServiceProvider { get; private set; } = null!;
public IServiceScopeFactory ScopeFactory => ServiceProvider.GetRequiredService();
///
/// Real with the fixture's
/// registered as the default unnamed instance. Stores resolve it via
/// context.GetRedbService(name, exchange) — when name is empty
/// (the test path), the extension falls back to context.GetService<IRedbService>()
/// which returns the fixture's redb. No per-exchange scoping is needed for
/// these unit tests — they call store methods with exchange: null.
///
public IRouteContext RouteContext { get; private set; } = null!;
/// Which backend this run rides on: sqlite (default), postgres or mssql.
public string Provider { get; } =
Environment.GetEnvironmentVariable("REDB_PROVIDER")?.ToLowerInvariant() is { Length: > 0 } p
? p
: "sqlite";
private static string RequireConnectionString(IConfiguration config, string name)
=> config.GetConnectionString(name)
?? throw new InvalidOperationException($"ConnectionStrings:{name} missing in appsettings.json");
public async Task InitializeAsync()
{
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var services = new ServiceCollection();
services.AddLogging(b => b.AddConsole().SetMinimumLevel(LogLevel.Warning));
services.AddRedbPro(options =>
{
// The provider is slid underneath, not copy-pasted around: the same test classes run
// against whichever backend the environment names.
var builder = Provider switch
{
"mssql" => options.UseMsSql(global::redb.Route.Tests.Shared.TestDatabases.MsSql(RequireConnectionString(config, "MsSql"))),
"postgres" => options.UsePostgres(global::redb.Route.Tests.Shared.TestDatabases.Postgres(RequireConnectionString(config, "Postgres"))),
// SQLite needs no stand: the file lives next to the test assembly. Full provider parity
// with the other two, so the same claims are checked on it rather than assumed.
"sqlite" => options.UseSqlite(RequireConnectionString(config, "Sqlite")),
_ => throw new InvalidOperationException(
$"REDB_PROVIDER '{Provider}' is not one of: postgres, mssql, sqlite.")
};
builder.Configure(c =>
{
c.PropsSaveStrategy = PropsSaveStrategy.ChangeTracking;
c.SkipHashValidationOnCacheCheck = false;
c.EnablePropsCache = false;
// PVT prefilter: a cutting step that narrows the value-side scan before the aggregation.
// It is in all three Pro providers, so it is on here for all
// three — SQLite included, whose dialect guards the planner itself (SqlitePrefilterGuards).
c.EnablePvtPrefilter = true;
});
// Free tier: 1024 queries — no .WithLicense() needed.
});
ServiceProvider = services.BuildServiceProvider();
Redb = ServiceProvider.GetRequiredService();
RouteContext = new RouteContext();
RouteContext.SetServiceProvider(ServiceProvider);
RouteContext.AddService(typeof(IRedbService), Redb);
try { await Redb.InitializeAsync(ensureCreated: true); }
catch { await Redb.InitializeAsync(); }
await SyncSchemes();
await Cleanup();
}
private async Task SyncSchemes()
{
await Redb.SyncSchemeAsync();
await Redb.SyncSchemeAsync();
await Redb.SyncSchemeAsync();
await Redb.SyncSchemeAsync();
await Redb.SyncSchemeAsync();
await Redb.SyncSchemeAsync();
await Redb.SyncSchemeAsync();
await Redb.SyncSchemeAsync();
await Redb.SyncSchemeAsync();
await Redb.SyncSchemeAsync();
await Redb.SyncSchemeAsync();
}
///
/// Removes only LLM-scheme rows; never touches unrelated objects.
/// DeleteWithPurgeAsync cascades automatically — soft-delete on a
/// conversation root pulls every descendant under
/// the same trash container, so we only enumerate the roots / flat tables.
///
public async Task Cleanup()
{
var ids = new List();
ids.AddRange((await Redb.Query().ToListAsync()).Select(o => o.id));
ids.AddRange((await Redb.Query().ToListAsync()).Select(o => o.id));
ids.AddRange((await Redb.Query().ToListAsync()).Select(o => o.id));
ids.AddRange((await Redb.Query().ToListAsync()).Select(o => o.id));
ids.AddRange((await Redb.Query().ToListAsync()).Select(o => o.id));
ids.AddRange((await Redb.Query().ToListAsync()).Select(o => o.id));
if (ids.Count > 0)
await Redb.DeleteWithPurgeAsync(ids);
}
public async Task DisposeAsync()
{
RouteContext?.Dispose();
if (ServiceProvider is IAsyncDisposable ad)
await ad.DisposeAsync();
else
ServiceProvider?.Dispose();
}
}
[CollectionDefinition("StoragePro")]
public sealed class StorageProCollection : ICollectionFixture { }