testutil

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AdminPool

func AdminPool(t *testing.T) *sql.DB

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

func CreateTestTenant(t *testing.T, db *postgres.DB, name string) string

CreateTestTenant inserts a tenant via the app connection (bypass RLS). Since tenants table has no RLS, this works directly.

func MustLivemode

func MustLivemode(ctx context.Context) bool

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

func OpenExecModeDB(t *testing.T, mode pgx.QueryExecMode) *sql.DB

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

func OpenTracedDB(t *testing.T, tracer pgx.QueryTracer) *postgres.DB

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 SetupTestDB

func SetupTestDB(t *testing.T) *postgres.DB

func WaitForLockWaiter

func WaitForLockWaiter(t *testing.T, ctx context.Context, admin *sql.DB, queryLike string)

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.

func WaitForLockWaiters

func WaitForLockWaiters(t *testing.T, ctx context.Context, admin *sql.DB, queryLike string, n int)

WaitForLockWaiters is WaitForLockWaiter for a test that sends several requests behind one lock: it returns once at least n backends wait.

Types

This section is empty.

Directories

Path Synopsis
Package pdftest reads back what Hardcount's PDF renderers drew, for tests.
Package pdftest reads back what Hardcount's PDF renderers drew, for tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL