Path: redb-route/tests/redb.Route.Tests.Core/Integration/ServerRedbSqlDataSourceTests.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.Core/Integration/ServerRedbSqlDataSourceTests.cs
----
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Npgsql;
using redb.Core;
using redb.Core.Pro.Extensions;
using redb.MSSql.Pro.Extensions;
using redb.Postgres.Pro.Extensions;
using redb.Route.Tests.Core.Sql;
namespace redb.Route.Tests.Core.Integration;
///
/// The two redb services of on a database server: the default one on the test
/// database, the one named orders on that database's name with , created when missing.
///
public abstract class ServerRedbSqlFixture : IAsyncLifetime
{
/// Appended to the test database's name for the named service's database.
public const string Suffix = "_route_sql_orders";
public ServiceProvider Services { get; private set; } = null!;
public ServiceProvider NamedServices { get; private set; } = null!;
protected abstract string ProbeTableSql { get; }
/// Registers redb Pro on the test database, or on its named twin when .
protected abstract Task RegisterAsync(IServiceCollection services, string connectionString, bool named);
protected abstract string ConnectionStringName { get; }
public async Task InitializeAsync()
{
var config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
var cs = config.GetConnectionString(ConnectionStringName)!;
Services = await BuildAsync(cs, named: false);
NamedServices = await BuildAsync(cs, named: true);
}
private async Task BuildAsync(string connectionString, bool named)
{
var services = new ServiceCollection();
services.AddLogging();
await RegisterAsync(services, connectionString, named);
var provider = services.BuildServiceProvider();
var redb = provider.GetRequiredService();
try { await redb.InitializeAsync(ensureCreated: true); }
catch { await redb.InitializeAsync(); }
await redb.SyncSchemeAsync();
await redb.Context.ExecuteAsync(ProbeTableSql);
return provider;
}
public async Task DisposeAsync()
{
await Services.DisposeAsync();
await NamedServices.DisposeAsync();
}
}
public sealed class PostgresRedbSqlFixture : ServerRedbSqlFixture
{
protected override string ConnectionStringName => "Postgres";
protected override string ProbeTableSql => "CREATE TABLE IF NOT EXISTS route_sql_lease_rows (tag text NOT NULL)";
protected override async Task RegisterAsync(IServiceCollection services, string connectionString, bool named)
{
var cs = new NpgsqlConnectionStringBuilder(global::redb.Route.Tests.Shared.TestDatabases.Postgres(connectionString));
if (named)
{
cs.Database += Suffix;
var admin = new NpgsqlConnectionStringBuilder(cs.ConnectionString) { Database = "postgres" };
await using var connection = new NpgsqlConnection(admin.ConnectionString);
await connection.OpenAsync();
await using var exists = new NpgsqlCommand("SELECT 1 FROM pg_database WHERE datname = @n", connection);
exists.Parameters.AddWithValue("n", cs.Database!);
if (await exists.ExecuteScalarAsync() is null)
{
await using var create = new NpgsqlCommand($"CREATE DATABASE \"{cs.Database}\"", connection);
await create.ExecuteNonQueryAsync();
}
}
services.AddRedbPro(o => o.UsePostgres(cs.ConnectionString));
}
}
public sealed class MsSqlRedbSqlFixture : ServerRedbSqlFixture
{
protected override string ConnectionStringName => "MSSql";
protected override string ProbeTableSql =>
"IF OBJECT_ID(N'route_sql_lease_rows') IS NULL CREATE TABLE route_sql_lease_rows (tag nvarchar(64) NOT NULL)";
protected override async Task RegisterAsync(IServiceCollection services, string connectionString, bool named)
{
var cs = new SqlConnectionStringBuilder(global::redb.Route.Tests.Shared.TestDatabases.MsSql(connectionString));
if (named)
{
cs.InitialCatalog += Suffix;
var admin = new SqlConnectionStringBuilder(cs.ConnectionString) { InitialCatalog = "master" };
await using var connection = new SqlConnection(admin.ConnectionString);
await connection.OpenAsync();
await using var create = new SqlCommand(
$"IF DB_ID(N'{cs.InitialCatalog}') IS NULL CREATE DATABASE [{cs.InitialCatalog}]", connection);
await create.ExecuteNonQueryAsync();
}
services.AddRedbPro(o => o.UseMsSql(cs.ConnectionString));
}
}
[Collection("Postgres")]
public sealed class PostgresRedbSqlDataSourceTests(PostgresRedbSqlFixture fixture)
: RedbSqlDataSourceTestsBase, IClassFixture
{
protected override IServiceProvider Services => fixture.Services;
protected override IServiceProvider NamedServices => fixture.NamedServices;
protected override bool ChecksDistributedTransaction => true;
}
[Collection("MsSql")]
public sealed class MsSqlRedbSqlDataSourceTests(MsSqlRedbSqlFixture fixture)
: RedbSqlDataSourceTestsBase, IClassFixture
{
protected override IServiceProvider Services => fixture.Services;
protected override IServiceProvider NamedServices => fixture.NamedServices;
protected override bool ChecksDistributedTransaction => true;
}