testutil

package
v0.0.57 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: AGPL-3.0 Imports: 18 Imported by: 0

README

testutil

Shared infrastructure for database-backed and container-backed tests across the Quincy module. This package exists solely for use in _test.go files and is never compiled into production binaries. It is not under src/api/ or src/agent/ because both import it and placing it inside either would create a circular dependency.

The package provides four things. A test database constructor that spins up a disposable postgres:18.4 container via testcontainers-go, applies the full production schema, and returns a connection pool along with a cleanup function — no manual setup, no persistent state between test runs. A skip helper for use in TestMain: when the database constructor returns an error (Docker unavailable or broken), calling this helper emits a proper go test SKIP record and exits 0, so the package reports as skipped rather than failing; the skip reason is visible with -v. A set of fixture helpers that produce a minimal but realistic configuration (two teams, one box, two services, one userlist) and seed the corresponding rows into the database, including the competition pause state — which route and sink tests need seeded even when they never pause anything, because the score write path reads it on every submission and errors when no state has been recorded yet. A test router constructor that builds a Gin engine registered with the full production route tree, bypassing the global middleware that normally reads from environment state and instead injecting a connection pool and config directly into each request's context — this includes panic recovery so routes that call log.Panic on errors don't crash the test process.

The container/ sub-package provides a lower-level container launcher for integration tests that need containers other than Postgres (for example, an OTel collector). It wraps testcontainers.GenericContainer in a panic-recovery closure — testcontainers can panic (rather than return an error) when the Docker daemon is unreachable — and converts any failure into a t.Skip with a visible warning. It also registers container termination as a t.Cleanup so tests need not manage lifecycle manually.

Docker must be available for any test that uses this package. On non-standard socket paths (Colima, Podman, etc.) set DOCKER_HOST before running. Testcontainers-go handles container lifecycle automatically; each package that calls the database constructor gets its own isolated Postgres instance that is torn down after the test binary exits.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MinimalConfig

func MinimalConfig() *config.APIConfigSpec

MinimalConfig returns a minimal APIConfigSpec for tests: 2 teams, 1 box, 2 services.

func NewTestDB

func NewTestDB(ctx context.Context) (*pgxpool.Pool, func(), error)

NewTestDB starts a postgres:18.4 container, applies the schema, and returns a pool and cleanup func. Returns an error (rather than panicking) if Docker is unavailable or broken.

func NewTestRouter

func NewTestRouter(pool *pgxpool.Pool, cfg *config.APIConfigSpec) *gin.Engine

NewTestRouter builds a Gin engine using the real application routes, with DB and config injected directly (no globals required).

func SeedPauses added in v0.0.56

func SeedPauses(ctx context.Context, db *pgxpool.Pool, cfg *config.APIConfigSpec) error

SeedPauses seeds pause_states with the config's starting state. Score writes read this table, so a DB-backed test that records a score must call this first.

func SeedScoring

func SeedScoring(ctx context.Context, db *pgxpool.Pool, cfg *config.APIConfigSpec) error

SeedScoring inserts zero-count rows into final_scores. Requires SetupConfig to have been called.

func SeedUsers

func SeedUsers(ctx context.Context, db *pgxpool.Pool, cfg *config.APIConfigSpec) error

SeedUsers inserts default users into scoring_users. Requires SetupConfig to have been called.

func SetupConfig

func SetupConfig(cfg *config.APIConfigSpec)

SetupConfig sets config.TeamRange from cfg without calling config.SetConfig.

func SkipDBTests

func SkipDBTests(err error)

SkipDBTests emits a proper go test SKIP record and exits 0 when Docker or the DB is unavailable. Call in TestMain when NewTestDB returns an error. Output is visible with -v; without -v go test shows "ok ... [no tests to run]" (same behavior as any t.Skip call in a TestMain-only binary).

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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