testutil

package
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package testutil provides test utilities and fixtures for go-mink.

Package testutil provides test utilities and fixtures for go-mink.

Package testutil provides test utilities and fixtures for testing go-mink applications.

Package testutil provides test utilities and fixtures for testing go-mink applications.

Package testutil provides test utilities and fixtures for testing go-mink applications.

Package testutil provides utilities for integration testing. It provides helpers for connecting to test infrastructure and waiting for services to be ready.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanupSchema

func CleanupSchema(ctx context.Context, db *sql.DB, schema string) error

CleanupSchema drops a schema and all its objects. The schema name should come from UniqueSchema() which generates safe names.

func MustPostgresDB

func MustPostgresDB(ctx context.Context, connStr string) *sql.DB

MustPostgresDB returns a database connection or panics.

func PostgresDB

func PostgresDB(ctx context.Context, connStr string) (*sql.DB, error)

PostgresDB returns a database connection for PostgreSQL testing. It waits for the database to be ready with retries.

func RegisterTestEvents

func RegisterTestEvents(store *mink.EventStore)

RegisterTestEvents registers test event types with the store.

func UniqueSchema

func UniqueSchema(prefix string) string

UniqueSchema generates a unique schema name for testing. The generated names contain only alphanumeric characters and underscores, making them safe for use in SQL queries.

Types

type ItemAdded

type ItemAdded struct {
	OrderID  string  `json:"orderId"`
	SKU      string  `json:"sku"`
	Quantity int     `json:"quantity"`
	Price    float64 `json:"price"`
}

ItemAdded event for testing.

type MockAdapter

type MockAdapter struct {
	AppendErr           error
	LoadErr             error
	GetStreamInfoErr    error
	GetLastPositionErr  error
	LoadFromPositionErr error
	Events              []adapters.StoredEvent
}

MockAdapter is a mock implementation of adapters.EventStoreAdapter for testing.

func (*MockAdapter) Append

func (m *MockAdapter) Append(ctx context.Context, streamID string, events []adapters.EventRecord, expectedVersion int64) ([]adapters.StoredEvent, error)

Append implements adapters.EventStoreAdapter.

func (*MockAdapter) Close

func (m *MockAdapter) Close() error

Close implements adapters.EventStoreAdapter.

func (*MockAdapter) GetLastPosition

func (m *MockAdapter) GetLastPosition(ctx context.Context) (uint64, error)

GetLastPosition implements adapters.EventStoreAdapter.

func (*MockAdapter) GetStreamInfo

func (m *MockAdapter) GetStreamInfo(ctx context.Context, streamID string) (*adapters.StreamInfo, error)

GetStreamInfo implements adapters.EventStoreAdapter.

func (*MockAdapter) Initialize

func (m *MockAdapter) Initialize(ctx context.Context) error

Initialize implements adapters.EventStoreAdapter.

func (*MockAdapter) Load

func (m *MockAdapter) Load(ctx context.Context, streamID string, fromVersion int64) ([]adapters.StoredEvent, error)

Load implements adapters.EventStoreAdapter.

func (*MockAdapter) LoadFromPosition

func (m *MockAdapter) LoadFromPosition(ctx context.Context, fromPosition uint64, limit int) ([]adapters.StoredEvent, error)

LoadFromPosition implements adapters.SubscriptionAdapter.

func (*MockAdapter) SubscribeAll

func (m *MockAdapter) SubscribeAll(ctx context.Context, fromPosition uint64, opts ...adapters.SubscriptionOptions) (<-chan adapters.StoredEvent, error)

SubscribeAll implements adapters.SubscriptionAdapter.

func (*MockAdapter) SubscribeCategory

func (m *MockAdapter) SubscribeCategory(ctx context.Context, category string, fromPosition uint64, opts ...adapters.SubscriptionOptions) (<-chan adapters.StoredEvent, error)

SubscribeCategory implements adapters.SubscriptionAdapter.

func (*MockAdapter) SubscribeStream

func (m *MockAdapter) SubscribeStream(ctx context.Context, streamID string, fromVersion int64, opts ...adapters.SubscriptionOptions) (<-chan adapters.StoredEvent, error)

SubscribeStream implements adapters.SubscriptionAdapter.

type MockProjection

type MockProjection struct {
	ProjectionName string
	EventTypes     []string
	ApplyErr       error
	Applied        []mink.StoredEvent
}

MockProjection is a mock implementation of mink.InlineProjection for testing.

func (*MockProjection) Apply

func (p *MockProjection) Apply(ctx context.Context, event mink.StoredEvent) error

Apply implements mink.InlineProjection.

func (*MockProjection) HandledEvents

func (p *MockProjection) HandledEvents() []string

HandledEvents implements mink.InlineProjection.

func (*MockProjection) Name

func (p *MockProjection) Name() string

Name implements mink.InlineProjection.

type MockT

type MockT struct {
	testing.TB // embed to satisfy unexported methods
	Failed_    bool
	Fatal_     bool
	Message    string
	Logs       []string
}

MockT is a mock testing.TB that captures test failures for testing. It is used to test functions that call testing.T methods like Fatal, Error, etc.

func NewMockT

func NewMockT() *MockT

NewMockT creates a new MockT instance.

func RunWithMockT

func RunWithMockT(fn func(m *MockT)) *MockT

RunWithMockT runs a function with a MockT and waits for completion. This handles runtime.Goexit() calls from Fatal/FailNow.

func (*MockT) Error

func (m *MockT) Error(args ...any)

Error implements testing.TB.

func (*MockT) Errorf

func (m *MockT) Errorf(format string, args ...any)

Errorf implements testing.TB.

func (*MockT) Fail

func (m *MockT) Fail()

Fail implements testing.TB.

func (*MockT) FailNow

func (m *MockT) FailNow()

FailNow implements testing.TB.

func (*MockT) Failed

func (m *MockT) Failed() bool

Failed implements testing.TB.

func (*MockT) Fatal

func (m *MockT) Fatal(args ...any)

Fatal implements testing.TB.

func (*MockT) Fatalf

func (m *MockT) Fatalf(format string, args ...any)

Fatalf implements testing.TB.

func (*MockT) Helper

func (m *MockT) Helper()

Helper implements testing.TB.

type Order

type Order struct {
	mink.AggregateBase

	CustomerID     string
	Items          []OrderItem
	Status         string
	TrackingNumber string
	CancelReason   string
}

Order is a test aggregate for E2E tests.

func NewOrder

func NewOrder(id string) *Order

NewOrder creates a new Order aggregate for testing.

func (*Order) AddItem

func (o *Order) AddItem(sku string, qty int, price float64) error

AddItem adds an item to the order.

func (*Order) ApplyEvent

func (o *Order) ApplyEvent(event interface{}) error

ApplyEvent applies historical events to rebuild state.

func (*Order) Cancel

func (o *Order) Cancel(reason string) error

Cancel cancels the order.

func (*Order) Create

func (o *Order) Create(customerID string) error

Create initializes the order.

func (*Order) Ship

func (o *Order) Ship(trackingNumber string) error

Ship marks the order as shipped.

func (*Order) TotalAmount

func (o *Order) TotalAmount() float64

TotalAmount calculates the total order amount.

type OrderCancelled

type OrderCancelled struct {
	OrderID string `json:"orderId"`
	Reason  string `json:"reason"`
}

OrderCancelled event for testing.

type OrderCreated

type OrderCreated struct {
	OrderID    string `json:"orderId"`
	CustomerID string `json:"customerId"`
}

OrderCreated event for testing.

type OrderItem

type OrderItem struct {
	SKU      string
	Quantity int
	Price    float64
}

OrderItem represents an item in an order.

type OrderReadModel

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

OrderReadModel maintains a collection of order summaries.

func NewOrderReadModel

func NewOrderReadModel() *OrderReadModel

NewOrderReadModel creates a new read model.

func (*OrderReadModel) Apply

func (rm *OrderReadModel) Apply(event mink.StoredEvent) error

Apply processes an event and updates the read model.

func (*OrderReadModel) Count

func (rm *OrderReadModel) Count() int

Count returns the number of orders in the read model.

func (*OrderReadModel) Get

func (rm *OrderReadModel) Get(orderID string) *OrderSummary

Get returns an order summary by ID.

func (*OrderReadModel) UpdateCount

func (rm *OrderReadModel) UpdateCount() int

UpdateCount returns the number of updates processed.

type OrderShipped

type OrderShipped struct {
	OrderID        string `json:"orderId"`
	TrackingNumber string `json:"trackingNumber"`
}

OrderShipped event for testing.

type OrderSummary

type OrderSummary struct {
	OrderID        string
	CustomerID     string
	ItemCount      int
	TotalAmount    float64
	Status         string
	TrackingNumber string
}

OrderSummary is a simple read model for orders.

type TestCommand

type TestCommand struct {
	ID         string
	ShouldFail bool
}

TestCommand is a mock command for testing middleware.

func (*TestCommand) AggregateID

func (c *TestCommand) AggregateID() string

AggregateID implements mink.Command.

func (*TestCommand) AggregateType

func (c *TestCommand) AggregateType() string

AggregateType implements mink.Command.

func (*TestCommand) CommandType

func (c *TestCommand) CommandType() string

CommandType implements mink.Command.

func (*TestCommand) Validate

func (c *TestCommand) Validate() error

Validate implements mink.Command.

type TestConfig

type TestConfig struct {
	PostgresURL string
}

TestConfig holds configuration for test infrastructure.

func DefaultConfig

func DefaultConfig() *TestConfig

DefaultConfig returns the default test configuration from environment variables.

Jump to

Keyboard shortcuts

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