MagicCSharp.Testing.Database
1.1.1
See the version list below for details.
dotnet add package MagicCSharp.Testing.Database --version 1.1.1
NuGet\Install-Package MagicCSharp.Testing.Database -Version 1.1.1
<PackageReference Include="MagicCSharp.Testing.Database" Version="1.1.1" />
<PackageVersion Include="MagicCSharp.Testing.Database" Version="1.1.1" />
<PackageReference Include="MagicCSharp.Testing.Database" />
paket add MagicCSharp.Testing.Database --version 1.1.1
#r "nuget: MagicCSharp.Testing.Database, 1.1.1"
#:package MagicCSharp.Testing.Database@1.1.1
#addin nuget:?package=MagicCSharp.Testing.Database&version=1.1.1
#tool nuget:?package=MagicCSharp.Testing.Database&version=1.1.1
MagicCSharp.Testing.Database
Repository integration testing against a real PostgreSQL, via Testcontainers. One container for the whole suite, a database per test class, a fake clock and key generator already wired. Requires Docker.
Add it to the test project beside your data project — the one with the DALs and repositories. Split out from
MagicCSharp.Testing on purpose: the fakes there have no heavy dependencies, and a project that only wants
FakeTimeProvider should not pull in Testcontainers, xUnit and Npgsql.
dotnet add package MagicCSharp.Testing.Database
Why a real database
A repository is mostly translation — a filter into SQL, a row into an entity. The in-memory and SQLite
providers translate differently from Postgres, so a test that passes against them proves the C# and not the
query, which is the half that breaks. Enum-as-string columns, jsonb round-trips and timestamptz offset
rules have no equivalent anywhere else.
MagicCSharp's own repository bases are tested this way — tests/MagicCSharp.Data.EntityFramework.Tests is
fifty tests over a real Postgres, covering create, filter, update, delete, pagination, soft delete and the
search column. It is the worked example of everything below.
Cost control
One container for the whole suite. One logical database per test class, so classes run in parallel without seeing each other's rows. Schema created once per database. Tables truncated between tests rather than recreated.
Use
[Trait("Category", "Integration")]
public class OrdersRepositoryTests : TestRepositoryBase<MagicShopContext>
{
private OrdersEfRepository OrdersRepository =>
new OrdersEfRepository(DbContextFactory, KeyGen, TimeProvider, NullLoggerFactory.Instance);
[Fact]
public async Task Create_assigns_created_and_updated_from_the_clock()
{
TimeProvider.SetUtcNow(new DateTimeOffset(2026, 3, 1, 0, 0, 0, TimeSpan.Zero));
var order = await OrdersRepository.Create(new OrderEdit
{
CustomerId = 7,
Total = 42.50m,
Status = OrderStatus.Pending,
});
Assert.Equal(TimeProvider.GetUtcNow(), order.Created);
}
}
TimeProvider is a FakeTimeProvider starting at a fixed instant (1 January 2026, UTC), KeyGen a
FakeKeyGen derived from it, and DbContextFactory the pooled factory for your context — the same three
things the repository takes in production. The clock only moves forward: set it in the test's constructor,
then advance it.
Override ContainerImage to match production, or to get an extension — a context with a vector column
needs pgvector/pgvector:pg17. Override InitializeDatabase to Database.MigrateAsync() when you want the
test to prove the migrations produce a working schema, rather than building it from the model.
ConfigureDataSource and ConfigureNpgsql take the same hooks as AddPostgresDbContextFactory, so a type
plugin registered in production is registered here too.
Run without Docker by filtering: dotnet test --filter "Category!=Integration".
A repository whose constructor takes RepositoryServices<TContext> is built from the same three plus KeyGen:
new RepositoryServices<MagicShopContext>(DbContextFactory, KeyGen, TimeProvider, NullLoggerFactory.Instance).
Migrations upgrade from the last release
A migration that builds an empty schema can still fail on the database production has. One test per context checks the upgrade:
[Trait("Category", "Database")]
public class BoilersMigrationTests(ITestOutputHelper output) : MigrationTestBase<BoilersDbContext>(output);
MigrationsUpgradeToLatest creates a fresh database named by the context's [MigrationsDatabase], restores
Migrations/{name}_baseline.dump from the test's output folder when there is one (a pg_dump of the released
schema), applies the migrations, and fails unless every migration in the code was applied — and again after a second
migrate that must find nothing pending. The database is dropped afterwards. pg_restore's output is withheld, as a
dump can hold data.
MigrationTestBase uses xUnit v2, like TestRepositoryBase. A suite on another test framework calls the check itself,
MigrationUpgradeCheck.Verify<TContext>(container, options, cancellationToken), which throws
MigrationUpgradeException and carries no test framework types. MigrationUpgradeOptions sets BaselineDirectory,
ConfigureNpgsql (pgvector's UseVector(), say) and a WriteLine for progress.
Related packages
- MagicCSharp.Testing
—
FakeTimeProvider,FakeKeyGen,SyncEventDispatcher, without Docker - MagicCSharp.Data.EntityFramework — the repositories under test
The whole picture, and the optional repository layout: github.com/MagicDoorInc/MagicCSharp. MIT.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- MagicCSharp.Data.Postgres (>= 1.1.1)
- MagicCSharp.Testing (>= 1.1.1)
- Testcontainers.PostgreSql (>= 4.15.0)
- xunit.abstractions (>= 2.0.3)
- xunit.extensibility.core (>= 2.9.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.