Documentation
¶
Overview ¶
Package testhelpers provides shared utilities for tests across all packages.
Index ¶
- Constants
- func ErrorForUUID(id uuid.UUID) error
- func NewEcho() *echo.Echo
- func NewFixedClock() *clock.Fake
- func NewUUIDWithSuffix(suffix string) uuid.UUID
- func SetupTestDB(t *testing.T) *sql.DB
- func WithTxRollback(t *testing.T, database *sql.DB, fn func(ctx context.Context, q *db.Queries))
- type APIResponse
Constants ¶
const ( // SuffixOK simulates the happy path — no error. SuffixOK = "00" // SuffixForbidden simulates data.ErrForbidden (HTTP 403). SuffixForbidden = "03" // SuffixNotFound simulates data.ErrNotFound (HTTP 404). SuffixNotFound = "04" // SuffixInternal simulates data.ErrInternal (HTTP 500). SuffixInternal = "05" // SuffixConflict simulates data.ErrConflict (HTTP 409). SuffixConflict = "09" )
Suffix* constants define the project-wide convention for simulating repo-layer outcomes from a test UUID: the trailing two hex characters of the UUID select which sentinel error (if any) a stub or mock should return.
Variables ¶
This section is empty.
Functions ¶
func ErrorForUUID ¶
ErrorForUUID returns the sentinel error a stub should simulate for id, based on its trailing two hex characters, or nil for the happy path.
func NewEcho ¶
NewEcho returns an Echo instance with the DTO validator registered, suitable for use in HTTP handler tests.
func NewFixedClock ¶ added in v0.5.1
NewFixedClock returns a *clock.Fake pinned to the real current instant at call time, for tests that need a non-nil clock but don't care about its exact value. It's deliberately anchored to "now" rather than an arbitrary fixed date so it stays compatible with code that legitimately compares against real wall-clock time (e.g. JWT libraries' own expiry checks, or hand-built fixtures using time.Now()) — the returned clock is still frozen at that instant for the rest of the test unless advanced. Tests that care about timing relationships should construct their own clock.NewFake(t0) and drive it with Set/Advance instead.
func NewUUIDWithSuffix ¶
NewUUIDWithSuffix returns a random UUID whose trailing two hex characters are suffix, for building deterministic test fixtures, e.g.:
testhelpers.NewUUIDWithSuffix(testhelpers.SuffixNotFound)
func SetupTestDB ¶
SetupTestDB opens an in-memory SQLite database and applies all migrations. The database is closed automatically when the test ends.
func WithTxRollback ¶
WithTxRollback runs fn inside a transaction that is always rolled back, giving each sub-test a clean, isolated view of the database.
The *db.Queries passed to fn are bound to the transaction — pass them to repo constructors (e.g. repo.NewUserRepo) rather than calling sqlc methods directly, to keep tests free of raw SQL.