Documentation
¶
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. For tests that must perform DDL, e.g. replaying a shipped migration's data transform against a seeded pre-migration shape (the app role can't drop/recreate indexes it doesn't 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 via the app connection (bypass RLS). Since tenants table has no RLS, this 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 (RLS-enforced) connection to the test database whose every statement passes through tracer. Call it after SetupTestDB (which migrates and cleans). 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 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.
It replaces 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: pass AdminPool(t). Postgres keeps only the first track_activity_query_size bytes (1 KiB by default) of a statement in pg_stat_activity, so 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.