Skip to content

Replace per-test database reseeding with a snapshot restore in backend tests#910

Merged
tjementum merged 1 commit into
mainfrom
account-test-snapshot-seeding
Jun 23, 2026
Merged

Replace per-test database reseeding with a snapshot restore in backend tests#910
tjementum merged 1 commit into
mainfrom
account-test-snapshot-seeding

Conversation

@tjementum

@tjementum tjementum commented Jun 22, 2026

Copy link
Copy Markdown
Member

Summary & Motivation

Backend endpoint tests recreated the schema with EnsureCreated() and reran the full DatabaseSeeder on a fresh in-memory SQLite connection for every test, so the cost of seeding was paid again on every test and grew with every fixture added to the seeder. This change seeds the schema and fixtures once into a template database and fills each test's connection with a binary copy through SqliteConnection.BackupDatabase. The restore cost is effectively constant in the number of fixtures, so seeding no longer scales with the size of the seed data.

  • The account endpoint, back-office, and external-authentication base tests now restore from a shared seeded template (SeededDatabaseTemplate) instead of calling EnsureCreated() and reseeding per test. The template is seeded once through the real dependency injection graph so Entity Framework interceptors behave as in production, and a lock serializes the binary copies that feed each test.
  • The main test infrastructure is brought to parity with account: a single host shared per test class (MainWebApplicationFactory, with per-test state carried through an AsyncLocal MainTestContext) plus the same snapshot seeding, replacing the per-test host rebuild. The main system has no endpoint tests yet, so this establishes the shared pattern for the first one.
  • The size of the gain tracks the size of the fixtures. With today's small account fixtures the full backend suite drops from about 22.0 to about 20.9 seconds averaged over five runs. A controlled measurement that added 1,000 seed rows raised the per-test reseed approach to about 49 seconds while the snapshot held at about 20, so the gap grows from a few percent to well over half as fixtures expand.

Checklist

  • I have added tests, or done manual regression tests
  • I have updated the documentation, if necessary

@tjementum tjementum added the Enhancement New feature or request label Jun 22, 2026
@tjementum tjementum self-assigned this Jun 22, 2026
@tjementum tjementum moved this to 🏗 In Progress in Kanban board Jun 22, 2026
@sonarqubecloud

Copy link
Copy Markdown

@tjementum tjementum changed the title Seed backend endpoint test databases from a snapshot and align main with account Replace per-test database reseeding with a snapshot restore in backend tests Jun 23, 2026
@linear

linear Bot commented Jun 23, 2026

Copy link
Copy Markdown

PP-1321

@tjementum tjementum merged commit ec1859a into main Jun 23, 2026
24 of 28 checks passed
@github-project-automation github-project-automation Bot moved this from 🏗 In Progress to ✅ Done in Kanban board Jun 23, 2026
@tjementum tjementum deleted the account-test-snapshot-seeding branch June 23, 2026 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement New feature or request

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

1 participant