EF Core store
Wire up integration tests
A district report reads its schools from the database, so its integration test needs to save the district and schools first.
BuildAsync passes the rows to an IBiomeStore, with seed rows followed by created entities. The EF Core package supplies EfCoreStore, so you can use the DbContext your test already owns.
using Microsoft.EntityFrameworkCore;
using Zeeq.Biome.EntityFrameworkCore;
public static partial class TestBiome
{
// Use the test's existing context for the save.
public static BiomeBuilder<SchoolSeed, SchoolSeed> Start(
DbContext db,
out SchoolSeed seed,
TimeProvider? time = null
) => Start(out seed, db.AsBiomeStore(), time);
}
db.AsBiomeStore() and new EfCoreStore(db) are equivalent. Your test creates, reuses and disposes the context.
await TestBiome
.Start(_db, out _)
.AddDistrict(out var district)
.AddSchools(out var schools, 2)
.BuildAsync();
// Clear tracked rows so the report uses the values read from the database.
_db.ChangeTracker.Clear();
var now = DateTimeOffset.UtcNow;
var district = new District
{
Id = $"dist_{Guid.CreateVersion7():N}",
Name = "Riverside Unified",
TimeZoneId = "America/Chicago",
CreatedAtUtc = now,
UpdatedAtUtc = now,
};
var first = new School
{
Id = $"sch_{Guid.CreateVersion7():N}",
DistrictId = district.Id,
Name = "School 1",
Code = "S1",
CreatedAtUtc = now,
UpdatedAtUtc = now,
};
var second = new School
{
Id = $"sch_{Guid.CreateVersion7():N}",
DistrictId = district.Id,
Name = "School 2",
Code = "S2",
CreatedAtUtc = now,
UpdatedAtUtc = now,
};
_db.AddRange(district, first, second);
await _db.SaveChangesAsync();
// Clear tracked rows before running the report.
_db.ChangeTracker.Clear();
_db.ChangeTracker.Clear() after BuildAsync().What the store does
db.AddRange(entities)in the order Biome supplies.- One
SaveChangesAsync, honoring the cancellation token.
EF Core sorts inserts by foreign-key dependency, so the order matters less than the keys.
Database-generated keys
Suppose the school app uses database-generated integer IDs for District and School. Omit the ID assignments in both factories. The district's final key is unavailable while the school blueprint runs, so set the School.District navigation. EF Core uses the relationship to populate School.DistrictId with the generated key.
public static readonly ChildBlueprint<SchoolSeed, District, School> School = Blueprint.ChildOf<
SchoolSeed,
District,
School
>(
(district, ctx) =>
new()
{
// Set the navigation because the district ID is assigned during the save.
District = district,
Name = $"School {ctx.Index + 1}",
Code = $"S{ctx.Index + 1}",
CreatedAtUtc = ctx.Now,
UpdatedAtUtc = ctx.Now,
}
);
await using var db = sqlite.CreateContext();
await TestBiome.Start(db, out _).AddDistrict(out var district).AddSchools(out _, 2).BuildAsync();
await using var read = sqlite.CreateContext();
// Query by the generated ID to verify that EF filled both foreign keys.
var savedSchools = await read.Schools.Where(s => s.DistrictId == district.Id).ToListAsync();
await Assert.That(savedSchools.Count).IsEqualTo(2);
SQLite and Postgres
SQLite makes it quick to check relationships. Use Postgres when timestamp precision is part of the behavior under test. Both fixtures below expose the same SchoolDb so the test setup chain stays the same.
public sealed class SqliteDb : IAsyncInitializer, IAsyncDisposable
{
private readonly SqliteConnection connection = new("DataSource=:memory:");
public async Task InitializeAsync()
{
// An open connection keeps the in-memory database available across contexts.
await connection.OpenAsync();
await using var db = CreateContext();
await db.Database.EnsureCreatedAsync();
}
internal SchoolDb CreateContext() =>
new(new DbContextOptionsBuilder<SchoolDb>().UseSqlite(connection).Options);
public ValueTask DisposeAsync() => connection.DisposeAsync();
}
public sealed class PostgresFixture : IAsyncInitializer, IAsyncDisposable
{
private readonly PostgreSqlContainer container = new PostgreSqlBuilder(
"postgres:17-alpine"
).Build();
public async Task InitializeAsync()
{
await container.StartAsync();
await using var db = CreateContext();
await db.Database.EnsureCreatedAsync();
}
// Point each context at the database started for this fixture.
internal SchoolDb CreateContext() =>
new(
new DbContextOptionsBuilder<SchoolDb>()
.UseNpgsql(container.GetConnectionString())
.Options
);
public ValueTask DisposeAsync() => container.DisposeAsync();
}
Rows seen by the database
Seed rows, aggregate parts and BeforeSave rows are all saved. In the database-generated ID example, a transient school stays in memory and never receives an ID, as long as a saved row does not reference it through a navigation.
await using var db = sqlite.CreateContext();
await TestBiome
.Start(db, out _)
.AddDistrict(out var district)
.AddSchool(out var north, s => s.Code = "NH")
.Under(district)
// Keep the proposed school available to the test without inserting it.
.AddSchool(out var proposed, s => s.Code = "SH")
.Transient()
.BuildAsync();
await using var read = sqlite.CreateContext();
var saved = await read.Schools.SingleAsync(s => s.DistrictId == district.Id);
// The proposed school remains unsaved and has no generated key.
await Assert.That(saved.Code).IsEqualTo(north.Code);
await Assert.That(proposed.Id).IsEqualTo(0);
Other databases
Anything else implements IBiomeStore, a single method:
public sealed class MyStore : IBiomeStore
{
public ValueTask SaveAsync(IReadOnlyList<object> entities, CancellationToken cancellationToken)
{
// Persist the supplied rows after aggregate expansion and BeforeSave.
return ValueTask.CompletedTask;
}
}