Path: redb-route/tests/redb.Route.Tests.Core/Integration/PostgresFixture.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/PostgresFixture.cs
----
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using redb.Core;
using redb.Core.Models.Configuration;
using redb.Core.Pro.Extensions;
using redb.Postgres.Pro.Extensions;
using redb.Route.RedbCore.Models;
namespace redb.Route.Tests.Core.Integration;
///
/// Shared xUnit fixture that boots a real Postgres-backed ,
/// creates the database schema and syncs the scheme.
///
public sealed class PostgresFixture : IAsyncLifetime
{
public IRedbService Redb { get; private set; } = null!;
public ServiceProvider ServiceProvider { get; private set; } = null!;
public async Task InitializeAsync()
{
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var connectionString = global::redb.Route.Tests.Shared.TestDatabases.Postgres(config.GetConnectionString("Postgres")!);
var license = config["Redb:License"];
var services = new ServiceCollection();
services.AddLogging();
services.AddRedbPro(options =>
{
options.UsePostgres(connectionString)
.Configure(c =>
{
c.PropsSaveStrategy = PropsSaveStrategy.DeleteInsert;
});
if (!string.IsNullOrWhiteSpace(license))
options.WithLicense(license);
});
ServiceProvider = services.BuildServiceProvider();
Redb = ServiceProvider.GetRequiredService();
try
{
await Redb.InitializeAsync(ensureCreated: true);
}
catch
{
// Schema may already exist from a parallel TFM run or previous session
await Redb.InitializeAsync();
}
await Redb.SyncSchemeAsync();
}
public async Task DisposeAsync()
{
if (ServiceProvider is IAsyncDisposable asyncDisposable)
await asyncDisposable.DisposeAsync();
else
ServiceProvider?.Dispose();
}
}
[CollectionDefinition("Postgres")]
public class PostgresCollection : ICollectionFixture;