fixtures

package
v0.0.50 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package fixtures provides thin, hand-written factories over the GENERATED gomock mocks in the sibling mocks package. It is the shared test-support layer: the mocks package stays generated-only (mock_*.go), and every configured double a test needs is built here from those generated mocks.

Why the factories take no *testing.T

The no-op doubles (NopLogger/NopMetrics/NopTracer) build their gomock controller from a no-op TestReporter, so callers get a ready no-op mock with zero ceremony — `fixtures.NopLogger()` drops in wherever `mocks.NopLogger{}` used to. Every method is registered AnyTimes, so an unmet/absent expectation can never fire and the controller never needs Finish().

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllowingRateLimiter added in v0.0.50

func AllowingRateLimiter() *mocks.MockRateLimiter

AllowingRateLimiter returns a generated MockRateLimiter that admits every request.

func Build added in v0.0.50

func Build[T any](set func(*T)) T

Build returns a T holding only what set assigns, so a table row names the fields its test is about and leaves the rest zero.

func BuildRef added in v0.0.50

func BuildRef[T any](set func(*T)) *T

BuildRef is Build for a row held by pointer.

func BuildRouteFDS added in v0.0.22

func BuildRouteFDS(
	tb testing.TB,
	fileOpts *descriptorpb.FileOptions,
	messages []*descriptorpb.DescriptorProto,
	methods []*descriptorpb.MethodDescriptorProto,
) []byte

BuildRouteFDS assembles a marshaled, self-contained FileDescriptorSet from the synthetic jsonapi annotations file + google/api + a single test.v1 service file (messages + methods), the exact input openapi.BuildRouteTable consumes.

func ClosedCircuitBreaker added in v0.0.50

func ClosedCircuitBreaker() *mocks.MockCircuitBreaker

ClosedCircuitBreaker returns a generated MockCircuitBreaker whose Execute runs the function.

func Decorations added in v0.0.14

func Decorations() decorate.Decorations

Decorations returns the Decorations that resolves every component and operation to no layers, for tests that build a base of any tier undecorated (recovery only): fixtures.Must(service.NewBase[C](fixtures.Decorations(), "test")).

func DenyingRateLimiter added in v0.0.50

func DenyingRateLimiter() *mocks.MockRateLimiter

DenyingRateLimiter returns a generated MockRateLimiter that refuses every request: Allow is false and Wait returns an error.

func EmptyMessage added in v0.0.22

func EmptyMessage(name string) *descriptorpb.DescriptorProto

EmptyMessage is a no-field response message (e.g. a delete response) carrying no (jsonapi.v1.resource) option.

func ErrPipeline

func ErrPipeline(err error) *mocks.MockPipeline[string, string]

ErrPipeline returns a MockPipeline whose Execute returns ("", err).

func ErrWorkflow

func ErrWorkflow(err error) *mocks.MockWorkflow[string, string]

ErrWorkflow returns a MockWorkflow whose Execute returns ("", err).

func InReexec added in v0.0.50

func InReexec() bool

InReexec reports whether this process is the child ReexecTest started, where the test owns the whole process and may change process-global state.

func JSONAPIAnnotations added in v0.0.22

func JSONAPIAnnotations(tb testing.TB) protoreflect.FileDescriptor

JSONAPIAnnotations builds the protoreflect.FileDescriptor for a hand-authored jsonapi/v1/annotations.proto — the three messages (JsonApiService / JsonApiResource / JsonApiOperation) and their three extends (FileOptions / MessageOptions / MethodOptions, all field 50001) — resolved against the linked descriptor.proto. It lets the tests set the jsonapi extension VALUES on synthetic options via dynamicpb without depending on the generated stubs.

func Method added in v0.0.22

func Method(
	name, input, output string,
	opts *descriptorpb.MethodOptions,
) *descriptorpb.MethodDescriptorProto

Method builds a MethodDescriptorProto wiring input/output messages (by unqualified name in package test.v1) and the given http/operation options.

func Must added in v0.0.10

func Must[T any](v T, err error) T

Must returns v, panicking when err is non-nil: a test's wiring of a constructor that returns (T, error), such as a base built from decorations, fails loudly.

func NopLogger

func NopLogger() interfaces.Logger

NopLogger returns a generated MockLogger whose every method is a no-op and whose With/WithContext return the same logger, for tests that must pass a logger but never assert on log output.

func NopMetrics

func NopMetrics() interfaces.Metrics

NopMetrics returns a generated MockMetrics whose Counter/Histogram/Gauge yield no-op sub-metrics, for tests that must pass a metrics provider but never assert on recorded values.

func NopTracer

func NopTracer() interfaces.Tracer

NopTracer returns a generated MockTracer whose spans discard every operation.

func OpenCircuitBreaker added in v0.0.50

func OpenCircuitBreaker() *mocks.MockCircuitBreaker

OpenCircuitBreaker returns a generated MockCircuitBreaker whose Execute rejects without invoking the function.

func PanicPipeline

func PanicPipeline() *mocks.MockPipeline[string, string]

PanicPipeline returns a MockPipeline whose Execute panics (for recovery-decorator tests).

func PanicWorkflow

func PanicWorkflow() *mocks.MockWorkflow[string, string]

PanicWorkflow returns a MockWorkflow whose Execute panics (for recovery-decorator tests).

func PassPipeline

func PassPipeline() *mocks.MockPipeline[string, string]

PassPipeline returns a MockPipeline that echoes "mock-output-"+input.

func PassWorkflow

func PassWorkflow() *mocks.MockWorkflow[string, string]

PassWorkflow returns a MockWorkflow that echoes "mock-workflow-output-"+input.

func ReexecTest added in v0.0.50

func ReexecTest(tb testing.TB, name string)

ReexecTest runs the test named name again, alone, in a child process of the current test binary, and fails tb with the child's output if it fails. A test that swaps the process stdio or counts every goroutine in the process cannot run beside parallel tests; the parent calls this and returns, and the child (where InReexec is true) runs the real body.

func ResourceMessage added in v0.0.22

func ResourceMessage(
	jsonapiSetter *JSONAPISetter,
	name, resType, single, collection string,
) *descriptorpb.DescriptorProto

ResourceMessage is a response message carrying a (jsonapi.v1.resource) option. s must not be nil.

func SpyTracer

func SpyTracer(onSetAttribute func(key string, value any)) interfaces.Tracer

SpyTracer returns a generated MockTracer whose span forwards each SetAttribute call to onSetAttribute (nil = discard), for tests that assert which span attributes were set.

func StubClientStream

func StubClientStream(
	ctx context.Context,
	sendErr error,
	recvErrs ...error,
) grpc.ClientStream

StubClientStream returns a generated MockClientStream for stream-interceptor tests: Context returns ctx (the caller's, never nil), SendMsg returns sendErr, and RecvMsg yields recvErrs in order then io.EOF. It replaces the former FakeClientStream.

func StubPipeline

func StubPipeline(
	exec func(ctx context.Context, in string) (string, error),
) *mocks.MockPipeline[string, string]

StubPipeline returns a generated MockPipeline whose Execute runs exec.

func StubRetrier

func StubRetrier(
	failUntil int,
) (retrier *mocks.MockRetrier, attempts func() int)

StubRetrier returns a generated MockRetrier that retries the operation up to failUntil times, plus a getter for the number of attempts made.

func StubService

func StubService(getErr error) *mocks.MockService[TestEntity, TestParams, string]

StubService returns a generated MockService whose Get returns getErr, and otherwise echoes a canned entity; List/Create/Update/Delete succeed.

func StubServicePanickingOnGet added in v0.0.50

func StubServicePanickingOnGet() *mocks.MockService[TestEntity, TestParams, string]

StubServicePanickingOnGet returns a StubService whose Get panics.

func StubStore

func StubStore() *mocks.MockStore[TestEntity, TestParams, string]

StubStore returns a generated MockStore backed by an in-memory map, so CRUD through it is observable end-to-end (Create then Get returns the entity, Delete drops it, Exists reflects presence). It replaces the former CustomMockStore.

func StubWorkflow

func StubWorkflow(
	exec func(ctx context.Context, in string) (string, error),
) *mocks.MockWorkflow[string, string]

StubWorkflow returns a generated MockWorkflow whose Execute runs exec.

Types

type JSONAPISetter added in v0.0.22

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

JSONAPISetter carries the dynamic extension types + message descriptors decoded from the synthetic jsonapi annotations file, so a test can stamp jsonapi.v1.service / .resource / .operation onto descriptorpb options without the generated stubs.

func NewJSONAPISetter added in v0.0.22

func NewJSONAPISetter(
	tb testing.TB,
	fileDescriptor protoreflect.FileDescriptor,
) JSONAPISetter

NewJSONAPISetter decodes the extension + message descriptors from the synthetic jsonapi annotations file into a JSONAPISetter.

func (*JSONAPISetter) ActionMethodOpts added in v0.0.50

func (s *JSONAPISetter) ActionMethodOpts(
	verb, path string, additional ...[value]string,
) *descriptorpb.MethodOptions

ActionMethodOpts returns MethodOpts plus the (jsonapi.v1.operation){action:true} marker.

func (*JSONAPISetter) FileOptsWithService added in v0.0.22

func (s *JSONAPISetter) FileOptsWithService() *descriptorpb.FileOptions

FileOptsWithService returns FileOptions carrying (jsonapi.v1.service) for the testsvc service at /api/v1.

func (*JSONAPISetter) MethodOpts added in v0.0.22

func (*JSONAPISetter) MethodOpts(
	verb, path string, additional ...[value]string,
) *descriptorpb.MethodOptions

MethodOpts returns MethodOptions carrying the google.api.http binding (verb + path, plus any additional bindings).

func (*JSONAPISetter) MsgOptsWithResource added in v0.0.22

func (s *JSONAPISetter) MsgOptsWithResource(
	resType, single, collection string,
) *descriptorpb.MessageOptions

MsgOptsWithResource returns MessageOptions carrying (jsonapi.v1.resource) with the given type/single/collection keys (empty keys are omitted).

type Sequence added in v0.0.50

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

Sequence is a deterministic pseudo-random stream for generated test data. The same seed always yields the same values, so a failing run reproduces exactly; it is not a source of security randomness.

func NewSequence added in v0.0.50

func NewSequence(seed uint32) *Sequence

NewSequence returns a Sequence that starts from seed.

func (*Sequence) Float64 added in v0.0.50

func (s *Sequence) Float64() float64

Float64 returns a value in [0, 1).

func (*Sequence) IntN added in v0.0.50

func (s *Sequence) IntN(n int) int

IntN returns a value in [0, n); n must be positive.

type SpyLogger

type SpyLogger struct {
	// MockLogger is the embedded generated mock whose expectations back the spy.
	*mocks.MockLogger

	// ChildDebugCalls records Debug messages logged on the WithContext child loggers,
	// so a test can assert the child actually logged (e.g. an observability decorator
	// that binds WithContext(ctx) for trace correlation and logs at Debug).
	ChildDebugCalls *[]string
	// ChildInfoCalls records Info messages logged on the WithContext child loggers.
	ChildInfoCalls *[]string
	// ChildWarnCalls records Warn messages logged on the WithContext child loggers.
	ChildWarnCalls *[]string
	// ChildErrorCalls records Error messages logged on the WithContext child loggers.
	ChildErrorCalls *[]string

	// DebugCalls records Debug messages logged directly on this logger.
	DebugCalls []string
	// InfoCalls records Info messages logged directly on this logger.
	InfoCalls []string
	// WarnCalls records Warn messages logged directly on this logger.
	WarnCalls []string
	// ErrorCalls records Error messages logged directly on this logger.
	ErrorCalls []string
	// ContextUsed is true when this logger was created by a WithContext call.
	ContextUsed bool
	// contains filtered or unexported fields
}

SpyLogger records log calls made through an embedded generated MockLogger, for tests that assert which messages were logged — directly and through WithContext child loggers. It replaces the former hand-rolled mocks.SpyLogger.

func NewSpyLogger

func NewSpyLogger() *SpyLogger

NewSpyLogger returns a SpyLogger that records log calls through a generated MockLogger, with WithContext yielding a recording child.

type TestEntity

type TestEntity struct {
	// ID is the entity identifier.
	ID string
	// Name is a human-readable label.
	Name string
}

TestEntity is a minimal domain entity for exercising repository and service operations through MockStore/MockService generic instantiations.

type TestParams

type TestParams struct{}

TestParams is a query-parameter type for testing List operations.

Directories

Path Synopsis
dbtest
cassandra
Package cassandra provides a testcontainers-based Apache Cassandra 5 instance for integration tests.
Package cassandra provides a testcontainers-based Apache Cassandra 5 instance for integration tests.
elasticmq
Package elasticmq provides a testcontainers-based ElasticMQ (SQS-compatible) instance for integration tests.
Package elasticmq provides a testcontainers-based ElasticMQ (SQS-compatible) instance for integration tests.
minio
Package minio provides a testcontainers-based MinIO instance for integration tests.
Package minio provides a testcontainers-based MinIO instance for integration tests.
postgres
Package postgres provides a generic testcontainers-based PostgreSQL database for integration tests.
Package postgres provides a generic testcontainers-based PostgreSQL database for integration tests.
redis
Package redis provides a testcontainers-based Redis instance for integration tests.
Package redis provides a testcontainers-based Redis instance for integration tests.
Package e2ekit is the gen-free shared test-kit for endpoint E2E + telemetry suites.
Package e2ekit is the gen-free shared test-kit for endpoint E2E + telemetry suites.
Package outboxtest is the reusable OutboxStore conformance suite (Run) and the routing-sink suite (RunRouting) a consumer runs against its own store and sink over real infrastructure; the engine runs them against the postgres outbox-store kind.
Package outboxtest is the reusable OutboxStore conformance suite (Run) and the routing-sink suite (RunRouting) a consumer runs against its own store and sink over real infrastructure; the engine runs them against the postgres outbox-store kind.
Package store (testutil) provides a interfaces.Store implementation that supports call counting and error injection for use in tests.
Package store (testutil) provides a interfaces.Store implementation that supports call counting and error injection for use in tests.
Package suite provides test suite constructors for unit and integration tests.
Package suite provides test suite constructors for unit and integration tests.
base
Package base provides the base test suite with shared utilities.
Package base provides the base test suite with shared utilities.

Jump to

Keyboard shortcuts

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