Documentation
¶
Overview ¶
Package testutil holds helpers that tests share: the integration test database (SetupTestDB, AdminPool), a database role for one test (CreateRole), 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 AtRealTime() context.Context
- func BalanceUSD(balances []domain.CreditBalance, err error) (domain.CreditBalance, error)
- func CreateRole(t *testing.T, prefix, password, options string) string
- func CreateTestTenant(t *testing.T, db *postgres.DB, name string) string
- func EarlierReleasePlanChange(t *testing.T, ctx context.Context, db *postgres.DB, tenantID, subID 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)
- func WaitForLockWaitersOrDone(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 AtRealTime ¶ added in v0.10.0
AtRealTime is a test-mode context with no instant bound: where a job's tick starts (the scheduler drops any instant, so each row binds its 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 CreateRole ¶ added in v0.10.0
CreateRole creates a login role for one test and returns its name. It works as the owner role in TEST_ADMIN_DATABASE_URL, on that database. The name is prefix plus the database's name, and the role is dropped, with its grants there, when the test ends. prefix holds only lowercase letters, digits and underscores. options are extra CREATE ROLE options, such as "BYPASSRLS", or "".
A role belongs to the whole Postgres server, not to one database, so a fixed name is shared by every database on the server. Two runs of the same test on two test databases at once would then drop or change each other's role, and a grant one run leaves in its database stops the other from dropping the role. A name per database keeps each run's role its own.
func CreateTestTenant ¶
CreateTestTenant inserts a tenant through the app connection, bypassing RLS. The tenants table has no RLS, so the insert works directly.
func EarlierReleasePlanChange ¶ added in v0.12.0
func EarlierReleasePlanChange(t *testing.T, ctx context.Context, db *postgres.DB, tenantID, subID string, at time.Time, planID string)
EarlierReleasePlanChange moves subscription subID to planID at `at`: it closes the subscription's open billing interval there and opens the next one, with the source "plan" the earlier release wrote. A change at the instant the open interval starts leaves it zero-width, as the earlier release did.
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.
func WaitForLockWaiters ¶
WaitForLockWaiters is WaitForLockWaiter for a test that sends several requests behind one lock: it returns once at least n backends wait.
func WaitForLockWaitersOrDone ¶ added in v0.10.0
func WaitForLockWaitersOrDone(t *testing.T, ctx context.Context, admin *sql.DB, queryLike string, n int, done ...<-chan struct{})
WaitForLockWaitersOrDone is WaitForLockWaiters for a race test whose writer may not wait at all: it also returns as soon as any channel in done is closed. Use it when a writer that does not queue is the bug under test. The test then goes on and reads that writer's result, which shows the bug, instead of failing here after 15 seconds with no answer.
Types ¶
This section is empty.