testhelpers

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package testhelpers provides shared utilities for tests across all packages.

Index

Constants

View Source
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

func ErrorForUUID(id uuid.UUID) error

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

func NewEcho() *echo.Echo

NewEcho returns an Echo instance with the DTO validator registered, suitable for use in HTTP handler tests.

func NewUUIDWithSuffix

func NewUUIDWithSuffix(suffix string) uuid.UUID

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

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

SetupTestDB opens an in-memory SQLite database and applies all migrations. The database is closed automatically when the test ends.

func WithTxRollback

func WithTxRollback(t *testing.T, database *sql.DB, fn func(ctx context.Context, q *db.Queries))

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.

Types

type APIResponse

type APIResponse[T any] struct {
	RequestID uuid.UUID `json:"request_id,omitempty"`
	Status    int       `json:"status"`
	Data      T         `json:"data,omitempty"`
	Error     string    `json:"error,omitempty"`
}

APIResponse mirrors model.APIResponse for test decoding with a typed Data field.

Jump to

Keyboard shortcuts

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