testutil

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: MIT Imports: 12 Imported by: 2

Documentation

Overview

Package testutil provides generic, domain-agnostic test helpers for services built on go-foundation: in-memory SQLite databases, ready-made test configurations, a programmable HTTP doer double, a log-recording slog handler, and a free-port helper. All are dependency-light — stdlib plus gorm/sqlite.

The heavier, testcontainers-backed PostgreSQL harness lives in the testutil/pgtest subpackage, so importing testutil does not pull the container runtime into a consumer's module graph.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FreeAddr added in v0.4.0

func FreeAddr(t testing.TB) string

FreeAddr reserves and releases an ephemeral loopback port and returns its address (host:port), so a test can bind a server to a port known to be free. The listener is closed before returning, so there is an inherent race window before the caller re-binds; it is a pragmatic helper for tests, not a guarantee.

func NewTestConfig

func NewTestConfig(opts ...ConfigOption) *config.Config

NewTestConfig creates a generic test configuration backed by SQLite. Consuming services can override fields via functional options.

func NewTestDB

func NewTestDB(t testing.TB) *gorm.DB

NewTestDB creates an in-memory SQLite database connection for testing. The database is created fresh for each test and does not persist.

Types

type ConfigOption

type ConfigOption func(*config.Config)

ConfigOption is a functional option for configuring test config.

func WithEnvironment

func WithEnvironment(env string) ConfigOption

WithEnvironment sets the environment in test config.

func WithVersion

func WithVersion(version string) ConfigOption

WithVersion sets the version in test config.

type FakeClock added in v0.7.0

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

FakeClock is a concurrency-safe, manually controlled clock for tests. Pass its Now method into a component's clock seam (for example cache.WithNowFunc) and drive time deterministically with Advance and Set instead of depending on the wall clock.

func NewFakeClock added in v0.7.0

func NewFakeClock(start time.Time) *FakeClock

NewFakeClock returns a FakeClock started at the given time.

func (*FakeClock) Advance added in v0.7.0

func (c *FakeClock) Advance(d time.Duration)

Advance moves the clock forward by d.

func (*FakeClock) Now added in v0.7.0

func (c *FakeClock) Now() time.Time

Now returns the clock's current time.

func (*FakeClock) Set added in v0.7.0

func (c *FakeClock) Set(t time.Time)

Set moves the clock to the absolute time t.

type FakeHTTPDoer added in v0.4.0

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

FakeHTTPDoer is a programmable HTTP test double. It satisfies any interface with a Do(*http.Request) (*http.Response, error) method structurally, so a caller substitutes it wherever an HTTP client seam is defined. Replies are stubbed per request URL with an optional fallback. It is safe for concurrent use.

func NewFakeHTTPDoer added in v0.4.0

func NewFakeHTTPDoer() *FakeHTTPDoer

NewFakeHTTPDoer returns an empty FakeHTTPDoer. Unstubbed requests get a 200 OK with an empty body unless StubDefault overrides the fallback.

func (*FakeHTTPDoer) Calls added in v0.4.0

func (f *FakeHTTPDoer) Calls() int

Calls reports how many requests have been made.

func (*FakeHTTPDoer) Do added in v0.4.0

func (f *FakeHTTPDoer) Do(req *http.Request) (*http.Response, error)

Do records the request and returns its programmed reply.

func (*FakeHTTPDoer) StubBodyReadError added in v0.4.0

func (f *FakeHTTPDoer) StubBodyReadError(url string, status int, readErr error) *FakeHTTPDoer

StubBodyReadError programs requests to url to return a response whose Body fails every Read with readErr — modeling a truncated stream, where the response headers arrive and the connection dies mid-body. It is the case a status-code-only stub cannot reach: the request looks successful right up to the point the caller tries to consume it.

func (*FakeHTTPDoer) StubDefault added in v0.4.0

func (f *FakeHTTPDoer) StubDefault(status int, body string) *FakeHTTPDoer

StubDefault programs the fallback reply for any unstubbed request URL.

func (*FakeHTTPDoer) StubDefaultBodyReadError added in v0.4.0

func (f *FakeHTTPDoer) StubDefaultBodyReadError(status int, readErr error) *FakeHTTPDoer

StubDefaultBodyReadError programs the fallback so unstubbed requests return a response whose Body fails every Read with readErr.

func (*FakeHTTPDoer) StubError added in v0.4.0

func (f *FakeHTTPDoer) StubError(url string, err error) *FakeHTTPDoer

StubError programs requests to url to fail with err.

func (*FakeHTTPDoer) StubURL added in v0.4.0

func (f *FakeHTTPDoer) StubURL(url string, status int, body string) *FakeHTTPDoer

StubURL programs the reply for requests to url.

type RecordingHandler added in v0.4.0

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

RecordingHandler captures the structured attributes of every log record, so a test can assert on the log's cadence and content rather than on its absence. Each record is a map with "msg" and "level" keys plus one key per attribute.

func (*RecordingHandler) Enabled added in v0.4.0

Enabled reports the handler is always enabled.

func (*RecordingHandler) Handle added in v0.4.0

func (h *RecordingHandler) Handle(_ context.Context, rec slog.Record) error

Handle records one log record's message, level, and attributes.

func (*RecordingHandler) Records added in v0.4.0

func (h *RecordingHandler) Records() []map[string]any

Records returns a copy of what has been logged so far.

func (*RecordingHandler) WithAttrs added in v0.4.0

func (h *RecordingHandler) WithAttrs([]slog.Attr) slog.Handler

WithAttrs returns the handler unchanged.

func (*RecordingHandler) WithGroup added in v0.4.0

func (h *RecordingHandler) WithGroup(string) slog.Handler

WithGroup returns the handler unchanged.

Directories

Path Synopsis
Package pgtest provides a testcontainers-backed PostgreSQL harness for integration tests built on go-foundation.
Package pgtest provides a testcontainers-backed PostgreSQL harness for integration tests built on go-foundation.

Jump to

Keyboard shortcuts

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