Help us shape the future of Go! Take the 2026 Go Developer Survey

testutil

package
v0.0.0-...-d63d8f9 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: MIT Imports: 36 Imported by: 0

Documentation

Overview

Package testutil provides shared test infrastructure for integration tests. Use testutil.Main in TestMain functions to compose infrastructure options.

Index

Constants

This section is empty.

Variables

View Source
var AdminCreds struct {
	Email    string
	Password string
}

AdminCreds holds the email and password of the single admin user created by WithAdminUser. Tests that need an authenticated session should use these credentials via NewAuthState, which logs in using them.

Functions

func MustMarshalJSON

func MustMarshalJSON(v any) string

MustMarshalJSON marshals v to a JSON string, panicking on error. Useful for building approval test inputs inline.

func RequireStatus

func RequireStatus(t *testing.T, err error, expectedCode int)

RequireStatus asserts that err is a *StatusError with the expected code, or that err is nil when expectedCode is 2xx. Fails the test immediately.

func ScrubField

func ScrubField(data map[string]any, field string)

ScrubField replaces the value of a key in a decoded JSON map with "[SCRUBBED]". This is used before snapshot assertions to mask non-deterministic fields like IDs and timestamps.

func ScrubFields

func ScrubFields(data map[string]any, fields ...string)

ScrubFields scrubs multiple fields from a decoded JSON map.

func UniqueID

func UniqueID() string

UniqueID returns a unique string safe for use across parallel tests. Each call returns a different value within a test binary invocation.

Types

type Fixtures

type Fixtures struct {
	Workspace db.Workspace
}

Fixtures holds seed data created before tests run.

func LoadFixtures

func LoadFixtures(ctx context.Context) (*Fixtures, error)

LoadFixtures creates a standard workspace used as the base for tests.

type M

type M struct {
	// contains filtered or unexported fields
}

M wraps *testing.M and manages ordered infrastructure setup and teardown.

func Main

func Main(m *testing.M, opts ...Option) *M

Main creates a new M with the given options, running each option's setup immediately. Teardowns are run in reverse order after m.Run().

func (*M) Run

func (m *M) Run() int

Run executes the test suite, then runs all teardowns in reverse order.

type Option

type Option func() Teardown

Option sets up a piece of test infrastructure and returns its teardown.

func WithAdminUser

func WithAdminUser() Option

WithAdminUser returns an Option that creates the single admin user directly in the database (bypassing the signup endpoint's one-user guard). It must be composed after WithPostgres() so the DB pool is ready.

func WithApprovals

func WithApprovals() Option

WithApprovals returns an Option that configures the go-approval-tests reporter and snapshot folder. When the -accept-changes flag is set, all diffs are auto-approved.

func WithEncryptionKey

func WithEncryptionKey() Option

WithEncryptionKey returns an Option that generates a random encryption key and stores it in the settings table. Use this in TestMain for tests that interact with git tokens or other encrypted data but do not call WithAdminUser.

func WithPostgres

func WithPostgres() Option

WithPostgres returns an Option that starts a Postgres 17.7 container, runs migrations, and injects the pool into the db package. The container is terminated on teardown.

func WithQdrant

func WithQdrant() Option

WithQdrant returns an Option that starts a Qdrant v1.17.0 container, ensures the collection exists, and injects the gRPC connection into the qdrant package. The container is terminated on teardown.

type State

type State struct {
	// contains filtered or unexported fields
}

State holds the test Echo server and provides HTTP helper methods.

func NewAuthState

func NewAuthState(t *testing.T) *State

NewAuthState creates a State with a session cookie pre-set by logging in as the admin user created by WithAdminUser(). Tests using this helper must include WithAdminUser() in their TestMain options.

func NewState

func NewState(t *testing.T) *State

NewState creates a State backed by a minimal Echo server with all routes registered.

func (*State) Delete

func (s *State) Delete(path string) (map[string]any, error)

Delete performs a DELETE request.

func (*State) DeleteStatus

func (s *State) DeleteStatus(path string) error

DeleteStatus performs a DELETE request and returns only the error.

func (*State) Get

func (s *State) Get(path string) (map[string]any, error)

Get performs a GET request against the test server.

func (*State) GetStatus

func (s *State) GetStatus(path string) error

GetStatus performs a GET request and returns only the error (useful for asserting non-2xx status codes without caring about the body).

func (*State) Post

func (s *State) Post(path string, body any) (map[string]any, error)

Post performs a POST request with a JSON body.

func (*State) PostStatus

func (s *State) PostStatus(path string, body any) error

PostStatus performs a POST request and returns only the error.

func (*State) Put

func (s *State) Put(path string, body any) (map[string]any, error)

Put performs a PUT request with a JSON body.

type StatusError

type StatusError struct {
	Code int
	Body []byte
}

StatusError is returned when a response has a non-2xx status code.

func (*StatusError) Error

func (e *StatusError) Error() string

type Teardown

type Teardown func()

Teardown is a cleanup function returned by an Option.

Jump to

Keyboard shortcuts

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