Documentation
¶
Overview ¶
Package testharness is the single source of Postgres + Redis wiring for the API service's integration tests.
It consolidates the two near-identical pool constructors duplicated across api/internal/services/database (getIntegrationPool, newIntegrationDB) and adds the migration runner the codebase was missing — so an integration test never has to reinvent Postgres/Redis setup or assume the schema is pre-migrated. The third constructor, getTestPool in pkg/secrets, cannot be consolidated here because Go's internal-package visibility forbids pkg/ imports of api/internal/; that duplication is documented and deferred (see README.md).
Isolation model ¶
The harness connects to a single, shared, externally-provisioned test Postgres (TEST_DATABASE_URL; skipped if unreachable). This matches the project's existing integration-test contract. Per-test isolation follows the project convention: use unique IDs/markers per test so parallel tests do not collide. Reset() is provided for non-parallel tests that need a clean slate; it never touches schema_migrations.
See README.md in this package for when to use the harness vs. unit-test mocks.
Index ¶
- func MigrationFiles() ([]string, error)
- type Harness
- func (h *Harness) Close()
- func (h *Harness) DSN() string
- func (h *Harness) ID() string
- func (h *Harness) Logger() *zap.Logger
- func (h *Harness) Logs() *observer.ObservedLogs
- func (h *Harness) MigrateDown() error
- func (h *Harness) MigrateUp() error
- func (h *Harness) Miniredis() *miniredis.Miniredis
- func (h *Harness) NewContext() context.Context
- func (h *Harness) Pool() *pgxpool.Pool
- func (h *Harness) Redis() *redis.Client
- func (h *Harness) Reset() error
- func (h *Harness) SQLDB() *sql.DB
- func (h *Harness) Seed(table string, row map[string]any) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MigrationFiles ¶
MigrationFiles returns the embedded migration file names (the .up.sql set), sorted ascending by name so callers see versions in apply order.
Types ¶
type Harness ¶
type Harness struct {
// contains filtered or unexported fields
}
Harness holds the Postgres and Redis handles for one integration test.
Construct with New; never zero-value it. New registers a t.Cleanup that closes every handle, so tests do not manage teardown themselves.
func New ¶
New connects to the test Postgres and a fresh miniredis. If Postgres is unreachable it skips the calling test (matching the project's existing integration-test contract, so a dev without Docker still gets a green `go test ./...`). Migrations are applied if the schema is not current.
All handles are torn down via t.Cleanup.
func (*Harness) Close ¶
func (h *Harness) Close()
Close releases every handle. Idempotent. New registers this as the test's t.Cleanup; tests may also call it directly to verify teardown behavior.
func (*Harness) DSN ¶
DSN returns the resolved connection string, for tests that construct their own pool (e.g. to exercise pool-level behavior).
func (*Harness) ID ¶
ID returns a short, process-unique identifier for this harness instance, for generating unique test markers (the project's parallel-isolation convention).
func (*Harness) Logger ¶
Logger returns a zap.Logger whose output is captured in memory. Assert on emitted entries via Logs().
func (*Harness) Logs ¶
func (h *Harness) Logs() *observer.ObservedLogs
Logs returns the captured log entries for log-based assertions (e.g. "no ERROR was emitted").
func (*Harness) MigrateDown ¶
MigrateDown reverts all migrations. Use only against a throwaway database; on the shared test DB it destroys the schema for every other test.
func (*Harness) MigrateUp ¶
MigrateUp applies all pending migrations. It is idempotent: a no-op (returns nil) when the schema is already current.
func (*Harness) Miniredis ¶
Miniredis returns the underlying miniredis for advanced assertions (TTL, key scans) that the redis client cannot express.
func (*Harness) NewContext ¶
NewContext returns a context carrying a test-scoped deadline, derived from the harness root context so Close cancels it. Each call's cancel is wired to t.Cleanup so timers never leak.
func (*Harness) Reset ¶
Reset truncates every user table in the public schema, restarting identity sequences, and never touches schema_migrations. Use it in non-parallel tests that need a clean slate; for parallel tests, prefer unique per-test IDs (see ID()).
func (*Harness) SQLDB ¶
SQLDB returns a database/sql handle to the same Postgres, for stores built on database/sql (e.g. the email-token store). It is a distinct handle from the one used internally for migrations.