Documentation
¶
Overview ¶
Package testutil holds helpers that tests share: the integration test database (SetupTestDB, AdminPool), a wait for a backend blocked on a lock (WaitForLockWaiter), and small readers for credit balances and the mode on a context.
Index ¶
- func AdminPool(t *testing.T) *sql.DB
- func BalanceUSD(balances []domain.CreditBalance, err error) (domain.CreditBalance, error)
- func CreateTestTenant(t *testing.T, db *postgres.DB, name string) string
- func MustLivemode(ctx context.Context) bool
- func OpenExecModeDB(t *testing.T, mode pgx.QueryExecMode) *sql.DB
- func OpenTracedDB(t *testing.T, tracer pgx.QueryTracer) *postgres.DB
- func SetupTestDB(t *testing.T) *postgres.DB
- func WaitForLockWaiter(t *testing.T, ctx context.Context, admin *sql.DB, queryLike string)
- func WaitForLockWaiters(t *testing.T, ctx context.Context, admin *sql.DB, queryLike string, n int)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AdminPool ¶
AdminPool opens the owner-role connection, the same role that runs migrations. It is for tests that must run DDL, for example to replay a shipped migration's data transform against a seeded pre-migration shape. The app role cannot drop or recreate indexes it does not own.
func BalanceUSD ¶
func BalanceUSD(balances []domain.CreditBalance, err error) (domain.CreditBalance, error)
BalanceUSD picks the USD row out of a credit GetBalance result, which holds one balance per currency (ADR-129). A customer with no USD credit reads as a zero USD balance. Wrap the call: BalanceUSD(svc.GetBalance(...)).
func CreateTestTenant ¶
CreateTestTenant inserts a tenant through the app connection, bypassing RLS. The tenants table has no RLS, so the insert works directly.
func MustLivemode ¶
MustLivemode returns the mode on ctx, for test fakes that record it. It panics when ctx has none, so a missing postgres.WithLivemode fails the test.
func OpenExecModeDB ¶
OpenExecModeDB opens an app-user connection to the test database that sends every statement in the given pgx exec mode. An operator can pick the mode with default_query_exec_mode in the database URL (docs/ops/postgres-requirements.md), so a test of how a parameter is encoded runs in each one. It needs no migrations or clean tables.
func OpenTracedDB ¶
OpenTracedDB opens a second app-user connection to the test database, with RLS enforced, whose every statement passes through tracer. Call it after SetupTestDB, which migrates and cleans. It is for tests that assert how many queries or transactions a read path costs. An N+1 regression shows up as a count that grows with the row count (issue #1142).
func SetupTestDB ¶
SetupTestDB runs migrations as superuser, cleans data, and returns an app-user connection where RLS is enforced.
func WaitForLockWaiter ¶
WaitForLockWaiter returns once a backend in the test database is waiting on a lock while it runs a statement whose text contains queryLike (ILIKE, so case does not matter). A race test calls it after starting the goroutine that must queue behind a lock the test holds, and before the test lets that lock go.
Use it instead of a fixed sleep. On a slow runner, a sleep can end before the goroutine reaches the lock. The two sides then run one after the other, and the test passes without the interleaving it says it proves.
admin must be able to see other sessions' queries, so pass AdminPool(t). Postgres keeps only the first track_activity_query_size bytes (1 KiB by default) of a statement in pg_stat_activity. For a long statement, match text near its start.
If no such backend appears within 15 seconds, the test fails with "0 of 1 backends blocked". That usually means the code under test no longer takes the lock the test is about, or takes a different one first.
Types ¶
This section is empty.