testutil

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package testutil provides testing utilities for the Opik SDK. This mirrors the Python SDK's testlib package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertFloatNear

func AssertFloatNear(t *testing.T, expected, actual, tolerance float64)

AssertFloatNear checks if two floats are within tolerance.

func AssertJSONEqual

func AssertJSONEqual(t *testing.T, expected, actual any)

AssertJSONEqual compares two values as JSON.

func AssertMapHasKeys

func AssertMapHasKeys(t *testing.T, m map[string]any, keys ...string)

AssertMapHasKeys checks if a map has all required keys.

func AssertMatch

func AssertMatch(t *testing.T, matcher Matcher, value any)

AssertMatch checks if a value matches a matcher.

Types

type AnyButNilMatcher

type AnyButNilMatcher struct{}

AnyButNilMatcher matches any non-nil value.

func (AnyButNilMatcher) Match

func (m AnyButNilMatcher) Match(value any) bool

func (AnyButNilMatcher) String

func (m AnyButNilMatcher) String() string

type AnyFloatMatcher

type AnyFloatMatcher struct {
	Min       float64
	Max       float64
	Tolerance float64
	Expected  float64
}

AnyFloatMatcher matches floats within a range or tolerance.

func AnyFloat

func AnyFloat() *AnyFloatMatcher

AnyFloat returns a matcher for floats.

func (*AnyFloatMatcher) Between

func (m *AnyFloatMatcher) Between(min, max float64) *AnyFloatMatcher

Between sets min/max range.

func (AnyFloatMatcher) Match

func (m AnyFloatMatcher) Match(value any) bool

func (*AnyFloatMatcher) Near

func (m *AnyFloatMatcher) Near(expected, tolerance float64) *AnyFloatMatcher

Near sets expected value with tolerance.

func (AnyFloatMatcher) String

func (m AnyFloatMatcher) String() string

type AnyMapMatcher

type AnyMapMatcher struct {
	RequiredKeys []string
}

AnyMapMatcher matches any map, optionally requiring specific keys.

func AnyMap

func AnyMap(requiredKeys ...string) AnyMapMatcher

AnyMap returns a matcher for maps.

func (AnyMapMatcher) Match

func (m AnyMapMatcher) Match(value any) bool

func (AnyMapMatcher) String

func (m AnyMapMatcher) String() string

type AnyMatcher

type AnyMatcher struct{}

AnyMatcher matches any value including nil.

func (AnyMatcher) Match

func (m AnyMatcher) Match(value any) bool

func (AnyMatcher) String

func (m AnyMatcher) String() string

type AnySliceMatcher

type AnySliceMatcher struct {
	MinLen int
	MaxLen int
}

AnySliceMatcher matches any slice.

func AnySlice

func AnySlice() AnySliceMatcher

AnySlice returns a matcher for slices.

func (AnySliceMatcher) Match

func (m AnySliceMatcher) Match(value any) bool

func (AnySliceMatcher) String

func (m AnySliceMatcher) String() string

type AnyStringMatcher

type AnyStringMatcher struct {
	Prefix   string
	Suffix   string
	Contains string
	MinLen   int
	MaxLen   int
}

AnyStringMatcher matches any string, optionally with constraints.

func AnyString

func AnyString() *AnyStringMatcher

AnyString returns a matcher for strings.

func (*AnyStringMatcher) Containing

func (m *AnyStringMatcher) Containing(substr string) *AnyStringMatcher

Containing adds a contains requirement.

func (AnyStringMatcher) Match

func (m AnyStringMatcher) Match(value any) bool

func (AnyStringMatcher) String

func (m AnyStringMatcher) String() string

func (*AnyStringMatcher) WithPrefix

func (m *AnyStringMatcher) WithPrefix(prefix string) *AnyStringMatcher

WithPrefix adds a prefix requirement.

func (*AnyStringMatcher) WithSuffix

func (m *AnyStringMatcher) WithSuffix(suffix string) *AnyStringMatcher

WithSuffix adds a suffix requirement.

type Matcher

type Matcher interface {
	Match(value any) bool
	String() string
}

Matcher is an interface for flexible value matching in tests.

func Any

func Any() Matcher

Any returns a matcher that matches any value.

func AnyButNil

func AnyButNil() Matcher

AnyButNil returns a matcher that matches any non-nil value.

type MockEnv

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

MockEnv temporarily sets environment variables for a test.

func NewMockEnv

func NewMockEnv(t *testing.T) *MockEnv

NewMockEnv creates a new environment mocker.

func (*MockEnv) Set

func (m *MockEnv) Set(key, value string)

Set sets an environment variable and records it for cleanup.

func (*MockEnv) Unset

func (m *MockEnv) Unset(key string)

Unset unsets an environment variable.

type MockServer

type MockServer struct {
	Server *httptest.Server
	// contains filtered or unexported fields
}

MockServer provides an HTTP test server that records requests and returns configured responses. This is similar to Python's respx library.

func NewMockServer

func NewMockServer() *MockServer

NewMockServer creates a new mock server.

func (*MockServer) Close

func (ms *MockServer) Close()

Close shuts down the mock server.

func (*MockServer) LastRequest

func (ms *MockServer) LastRequest() *RecordedRequest

LastRequest returns the most recent request.

func (*MockServer) On

func (ms *MockServer) On(method, path string) *Route

On registers a route for any method.

func (*MockServer) OnDelete

func (ms *MockServer) OnDelete(path string) *Route

OnDelete registers a DELETE route.

func (*MockServer) OnGet

func (ms *MockServer) OnGet(path string) *Route

OnGet registers a GET route.

func (*MockServer) OnPatch

func (ms *MockServer) OnPatch(path string) *Route

OnPatch registers a PATCH route.

func (*MockServer) OnPost

func (ms *MockServer) OnPost(path string) *Route

OnPost registers a POST route.

func (*MockServer) OnPut

func (ms *MockServer) OnPut(path string) *Route

OnPut registers a PUT route.

func (*MockServer) RequestCount

func (ms *MockServer) RequestCount() int

RequestCount returns the number of recorded requests.

func (*MockServer) Requests

func (ms *MockServer) Requests() []*RecordedRequest

Requests returns all recorded requests.

func (*MockServer) RequestsForPath

func (ms *MockServer) RequestsForPath(path string) []*RecordedRequest

RequestsForPath returns requests matching a specific path.

func (*MockServer) Reset

func (ms *MockServer) Reset()

Reset clears all recorded requests.

func (*MockServer) RouteCallCount

func (ms *MockServer) RouteCallCount(method, path string) int

RouteCallCount returns how many times a route was called.

func (*MockServer) URL

func (ms *MockServer) URL() string

URL returns the mock server URL.

type RecordedRequest

type RecordedRequest struct {
	Method  string
	Path    string
	Headers http.Header
	Body    []byte
}

RecordedRequest captures details of an incoming request.

type Route

type Route struct {
	Method     string
	Path       string
	StatusCode int
	Response   any
	Headers    map[string]string
	Handler    http.HandlerFunc
	CallCount  int
}

Route defines a mock route with its response.

func (*Route) Respond

func (r *Route) Respond(statusCode int, body any) *Route

Respond sets the response for a route.

func (*Route) RespondJSON

func (r *Route) RespondJSON(statusCode int, body any) *Route

RespondJSON sets a JSON response.

func (*Route) WithHandler

func (r *Route) WithHandler(handler http.HandlerFunc) *Route

WithHandler sets a custom handler.

func (*Route) WithHeaders

func (r *Route) WithHeaders(headers map[string]string) *Route

WithHeaders adds response headers.

Jump to

Keyboard shortcuts

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