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 ¶
- func FreeAddr(t testing.TB) string
- func NewTestConfig(opts ...ConfigOption) *config.Config
- func NewTestDB(t testing.TB) *gorm.DB
- type ConfigOption
- type FakeHTTPDoer
- func (f *FakeHTTPDoer) Calls() int
- func (f *FakeHTTPDoer) Do(req *http.Request) (*http.Response, error)
- func (f *FakeHTTPDoer) StubBodyReadError(url string, status int, readErr error) *FakeHTTPDoer
- func (f *FakeHTTPDoer) StubDefault(status int, body string) *FakeHTTPDoer
- func (f *FakeHTTPDoer) StubDefaultBodyReadError(status int, readErr error) *FakeHTTPDoer
- func (f *FakeHTTPDoer) StubError(url string, err error) *FakeHTTPDoer
- func (f *FakeHTTPDoer) StubURL(url string, status int, body string) *FakeHTTPDoer
- type RecordingHandler
- func (*RecordingHandler) Enabled(context.Context, slog.Level) bool
- func (h *RecordingHandler) Handle(_ context.Context, rec slog.Record) error
- func (h *RecordingHandler) Records() []map[string]any
- func (h *RecordingHandler) WithAttrs([]slog.Attr) slog.Handler
- func (h *RecordingHandler) WithGroup(string) slog.Handler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FreeAddr ¶ added in v0.4.0
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.
Types ¶
type ConfigOption ¶
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 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) 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) Handle ¶ added in v0.4.0
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.