Documentation
¶
Overview ¶
Package testdb is the harness every database-backed test in this module runs on: Start gives a test its own uniquely-named, isolated Postgres database, so tests run in parallel with no shared state and nothing to truncate between them.
This is staged duplication, not divergence ¶
The shared harness belongs in tenancy, because tenant scoping and row-level security are what it exists to test. tenancy does not exist yet, and cas/internal/testdb and audit/internal/testdb are each keeping their own copy until the post-alpha swap. This package is the next copy of the same agreed pattern — same image, same per-test database, same TEST_DATABASE_URL override, same testing.Short() gate — and it converges on tenancy's harness the moment that module lands. It is deliberately shaped to make that swap mechanical: Start's signature is the whole of the contract.
It differs from the other copies in one way, and only because this module differs: there is no default migrator, because this module has no migrations of its own. Start hands back an empty database, which is exactly what a test of a migrator needs.
Duplicating test code for one release is a cost worth paying to avoid creating a module before a second consumer needs it. Duplicating production code would not be.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is an isolated, empty database created for a single test by Start.
func Start ¶
Start creates a uniquely-named, empty Postgres database and registers a t.Cleanup that closes the pool and drops it. Each call gets its own database, so tests using Start may run in parallel without interfering with one another; there is no shared database and no Truncate step because there is nothing shared to reset.
When TEST_DATABASE_URL is set, Start connects to that server and still creates a unique database on it rather than reusing a shared one. Otherwise it starts a pgvector/pgvector:pg18 container via testcontainers-go once per test binary run (not once per test) and shares that single container across every call to Start in the run.
Start skips the test under testing.Short() — this is the module's one chokepoint for that convention, so every database test inherits it without its author needing a gate of their own. -short wins even when TEST_DATABASE_URL is set: a reachable server is not a reason to run, since the convention is "no databases in the inner loop", period. The skip happens before any container or database work, so a skipped test pays no setup cost.
func (*DB) Name ¶
Name is this test's database name, for an error message that has to say which database it was talking about.
func (*DB) Pool ¶
Pool returns the connection pool opened against this test's database. It connects as the container's superuser, which is the migration identity this module's own tests need: Up must be able to create a schema, create roles and grant on them.
A test asserting anything about what a *non*-owner may do has to switch roles explicitly, because a superuser bypasses privileges and row-level security unconditionally, and a test that did not switch would be proving nothing.