19.5 The tests migrate too
The test database is migrated as the hub's is, so the tests run against the schema the migrations build, and a model change without a migration fails them.
tests/Shopfront.Tests/TestDatabase.cs:
using Microsoft.EntityFrameworkCore;
using Shopfront.Data;
namespace Shopfront.Tests;
// The tests' own database, beside the application's, so a test never touches its rows.
internal static class TestDatabase
{
public const string ConnectionString =
"Host=localhost;Database=shopfront_tests;Username=postgres;Password=shopfront";
// A context for a caller; staff, who read every order, unless the test says otherwise.
public static ShopfrontDbContext NewContext(IShopfrontCaller? caller = null) =>
new(
new DbContextOptionsBuilder<ShopfrontDbContext>()
.UseNpgsql(ConnectionString, ShopfrontDatabase.Npgsql)
.Options,
caller ?? TestCaller.Staff
);
// Drops the database and migrates a new one, as the hub does: the seeded catalogue, and no
// orders or runs.
public static async Task Reset()
{
var db = NewContext();
await using (db.ConfigureAwait(false))
{
await db.Database.EnsureDeletedAsync().ConfigureAwait(false);
await db.Database.MigrateAsync().ConfigureAwait(false);
}
}
public static async Task<int> StockOf(string sku)
{
var db = NewContext();
await using (db.ConfigureAwait(false))
{
return await db
.Products.Where(p => p.Sku == sku)
.Select(p => p.Stock)
.SingleAsync()
.ConfigureAwait(false);
}
}
}The returns features' hub gets its database the same way, migrated rather than created:
tests/Shopfront.Tests/FeatureHooks.cs:
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Reqnroll;
using Shopfront.Auth;
using Shopfront.Data;
using Trax.Core.Extensions;
using Trax.StateMachine;
using Trax.StateMachine.Gherkin;
using Trax.StateMachine.Gherkin.Reqnroll;
namespace Shopfront.Tests;
// Starts the hub once for the features and points the Trax bindings at it. Every person of every
// scenario signs in as a user of their own, so no scenario sees another's returns.
[Binding]
public sealed class FeatureHooks
{
// A database of their own, new for each run: the other tests drop theirs before each test,
// which would pull it out from under a running hub.
private const string Database =
"Host=localhost;Database=shopfront_features;Username=postgres;Password=shopfront";
private static WebApplicationFactory<Program>? hub;
[BeforeTestRun]
public static async Task StartTheHub()
{
var db = Context();
await using (db.ConfigureAwait(false))
{
await db.Database.EnsureDeletedAsync().ConfigureAwait(false);
await db.Database.MigrateAsync().ConfigureAwait(false);
}
hub = new WebApplicationFactory<Program>().WithWebHostBuilder(host =>
host.UseEnvironment("Development").UseSetting("ConnectionStrings:Shopfront", Database)
);
var client = hub.CreateClient();
var features = Path.Combine(AppContext.BaseDirectory, "features");
// The hub runs only the documents it registered, so the bindings send each step by the
// persisted id the storefront's client manifest holds for it.
var manifest = ClientManifest.Read(
await File.ReadAllTextAsync(Path.Combine(AppContext.BaseDirectory, "manifest.json"))
.ConfigureAwait(false)
);
// The lexicon is read with the trains' records, as the hub reads them.
var lexicon = Lexicon
.ReadDirectory(
features,
[typeof(Shopfront.Trains.ReturnOrder.IReturnOrderTrain).Assembly]
)
.NotNull();
TraxFeatures.Configure(trax =>
{
trax.Host = hub.Services;
trax.Client = client;
trax.Glossary = lexicon.GlossaryOrThrow();
trax.Manifest = manifest;
trax.Credential = (actor, person, scenario) =>
new FeatureCredential(
new Dictionary<string, string>(StringComparer.Ordinal)
{
["Authorization"] =
"Bearer "
+ DevJwt.Mint(
$"{person}-{scenario}",
actor.Roles[0],
TimeProvider.System
),
}
);
});
}
// The hub hands every job to the worker, and these tests start none: a scenario that waits for
// the check's answer runs in memory only (ReturnFeaturesTests).
[BeforeScenario("in-memory")]
public void InMemoryOnly() =>
Assert.Ignore("Runs in memory only: no worker answers the check here.");
[AfterTestRun]
public static async Task StopTheHub()
{
if (hub is not null)
await hub.DisposeAsync().ConfigureAwait(false);
}
private static ShopfrontDbContext Context() =>
new(
new DbContextOptionsBuilder<ShopfrontDbContext>()
.UseNpgsql(Database, ShopfrontDatabase.Npgsql)
.Options,
TestCaller.Staff
);
}The architecture guards check the same in CI, without a database: a context listed in
MigrationContexts must match its latest migration.
tests/Shopfront.Tests/ArchitectureGuards.cs:
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Shopfront.Data;
using Shopfront.Data.Models;
using Shopfront.Trains.PlaceOrder;
using Trax.Core.Testing;
using Trax.Core.Testing.Fixtures;
using Trax.Effect.Data.Testing;
using Trax.Mediator.Testing;
namespace Shopfront.Tests;
// The guard fixtures this project adopts, one nested class each, so the file holds one type.
public static class ArchitectureGuards
{
[TestFixture]
public sealed class ShopfrontTrainGuards : TrainGuardFixture
{
protected override IReadOnlyList<Assembly> TrainAssemblies =>
[typeof(IPlaceOrderTrain).Assembly];
}
[TestFixture]
public sealed class ShopfrontHygieneGuards : HygieneGuardFixture;
[TestFixture]
public sealed class ShopfrontDataLayerGuards : DomainDataLayerGuardFixture
{
protected override ArchitectureGuardOptions Options =>
new() { SourceScanRoots = ["Shopfront.Core/Data"] };
protected override IReadOnlyList<Type> DomainContexts => [typeof(ShopfrontDbContext)];
// The contexts EF migrations keep: each model must match its latest migration.
protected override IReadOnlyList<Type> MigrationContexts => [typeof(ShopfrontDbContext)];
// The model, built with no database and no caller, for the census to read.
protected override IReadOnlyList<IReadOnlyModel> OwnerScopedModels
{
get
{
var options = new DbContextOptionsBuilder<ShopfrontDbContext>()
.UseNpgsql("Host=localhost;Database=model_only")
.Options;
using var context = new ShopfrontDbContext(options, NoShopfrontCaller.Instance);
return [context.Model];
}
}
protected override OwnerScopeCensusOptions OwnerScope =>
new()
{
OwnerType = typeof(Order),
PrincipalAccessorType = typeof(IShopfrontCaller),
OwnerIdProperties = new HashSet<string>(StringComparer.Ordinal)
{
nameof(Order.CustomerId),
},
};
}
}Start the hub and worker with RabbitMQ running, and both log that they joined it:
info: Trax.Effect.Broadcaster.RabbitMQ.RabbitMqTrainEventReceiver[0]
RabbitMQ receiver started on exchange trax.lifecycle, queue amq.gen-cr_Uh-wKacH-AdfGlPjXyA.dotnet testPassed! - Failed: 0, Passed: 76, Skipped: 0, Total: 76 - Shopfront.Tests.dll (net10.0)SDK Reference
Deployment topologies | Move a domain data context to EF migrations | UseRabbitMq | AddTraxHealthCheck | Health and liveness | Startup refusals | UseTraxApiDashboard
Next: 19.6 Containers