testutil

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package testutil provides doubles and fixture helpers for the paystack-go test suite. It lives under internal/ so it cannot be imported outside this module.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertErrorMatrix

func AssertErrorMatrix(t *testing.T, invoke func(paystack.Backend) error)

AssertErrorMatrix runs DefaultErrorMatrix against invoke. invoke is called once per row with a FixtureBackend wired to that row's fixture and must return the service method's error.

func LoadFixture

func LoadFixture(t testing.TB, name string) []byte

LoadFixture reads a file from testdata/ relative to the module root. It fails the test immediately if the fixture cannot be read.

func RunConcurrent

func RunConcurrent(t *testing.T, n int, fn func() error)

RunConcurrent invokes fn n times in parallel and fails the test if any call returns an error. Ensures service methods are safe for concurrent use.

Types

type CallRecord

type CallRecord struct {
	Method string
	Path   string
	Params interface{}
	Out    interface{}
}

CallRecord captures one invocation of MockBackend.Call or .CallRaw.

type ErrorCase

type ErrorCase struct {
	Name       string
	Fixture    string
	Status     int
	Header     http.Header
	WantCode   paystack.ErrorCode
	WantRetry  time.Duration
	WantFields bool
}

ErrorCase captures one row of the standard error matrix every service method must pass. Use AssertErrorMatrix to run the whole table in a single test.

func DefaultErrorMatrix

func DefaultErrorMatrix() []ErrorCase

DefaultErrorMatrix returns the seven error rows every service method is required to handle by the spec.

type FixtureBackend

type FixtureBackend struct {
	// Fixture is a filename under testdata/ (e.g. "transaction_verify.json").
	Fixture string

	// Status is the HTTP status to simulate. Defaults to 200.
	Status int

	// Header lets tests inject response headers (e.g. Retry-After).
	Header http.Header
}

FixtureBackend serves a fixture file as if it were a live Paystack response. When Status >= 400, the body is passed through paystack.ParseError so the returned *Error matches what HTTPBackend would produce.

func (*FixtureBackend) Call

func (f *FixtureBackend) Call(ctx context.Context, method, path string, params, out interface{}) error

Call loads the fixture, either unmarshals it into out (2xx) or converts it to *paystack.Error (4xx/5xx).

func (*FixtureBackend) CallRaw

func (f *FixtureBackend) CallRaw(ctx context.Context, method, path string, params interface{}) (*http.Response, error)

CallRaw returns a fresh *http.Response built from the fixture.

type MockBackend

type MockBackend struct {
	// Response is JSON-encoded into the caller's `out` when non-nil and Err
	// is nil. It can be a ready-made Response[T] / ListResponse[T] envelope
	// or a plain struct — the bytes are marshalled then unmarshalled.
	Response interface{}

	// Err, when set, is returned as-is from Call.
	Err error

	// RawResponse drives CallRaw. When nil, an empty 200 is returned.
	RawResponse *http.Response
	// contains filtered or unexported fields
}

MockBackend is a Backend double with fully controllable responses. Use it when the test is about call shape (method, path, params) rather than response parsing.

func (*MockBackend) Call

func (m *MockBackend) Call(ctx context.Context, method, path string, params, out interface{}) error

Call records the invocation and either returns Err or JSON-round-trips Response into out.

func (*MockBackend) CallRaw

func (m *MockBackend) CallRaw(ctx context.Context, method, path string, params interface{}) (*http.Response, error)

CallRaw records the invocation and returns RawResponse or a default 200.

func (*MockBackend) Calls

func (m *MockBackend) Calls() []CallRecord

Calls returns a copy of all recorded invocations.

func (*MockBackend) LastCall

func (m *MockBackend) LastCall() CallRecord

LastCall returns the most recent recorded invocation. Panics if none.

func (*MockBackend) Reset

func (m *MockBackend) Reset()

Reset clears recorded calls.

Jump to

Keyboard shortcuts

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