test_util

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: MIT Imports: 28 Imported by: 0

Documentation

Overview

Package testing provides model factories for use in tests and seed data. Factories use a fluent builder API: Set field values, then Make (in-memory) or Create (insert into DB).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewTestDB

func NewTestDB(t *testing.T, cfg config.DatabaseConfig) *pgxpool.Pool

NewTestDB creates a database connection that rolls back automatically.

func WithORMTransaction

func WithORMTransaction(t *testing.T, db *database.DB, fn func(txDB *database.DB))

WithORMTransaction runs a function inside an ORM transaction that is rolled back.

func WithTestTransaction

func WithTestTransaction(t *testing.T, pool *pgxpool.Pool, fn func(tx pgx.Tx))

WithTestTransaction runs a function inside a transaction that is rolled back.

Types

type BaseFactory

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

BaseFactory provides helper methods for test.

func NewBaseFactory

func NewBaseFactory(t *testing.T) *BaseFactory

NewBaseFactory creates a new BaseFactory.

func (*BaseFactory) RequireNoError

func (f *BaseFactory) RequireNoError(err error)

RequireNoError asserts that err is nil.

type Factory

type Factory[T any] interface {
	Create(ctx context.Context, db *database.DB) (*T, error)
	Make() *T
}

Factory defines an interface for creating models in tests or seeders.

type FakeMailer

type FakeMailer struct {
	Messages []*mail.Message
}

FakeMailer is a test mailer that collects sent messages in memory.

func NewFakeMailer

func NewFakeMailer() *FakeMailer

NewFakeMailer creates a new FakeMailer.

func (*FakeMailer) AssertNotSent

func (m *FakeMailer) AssertNotSent(t *testing.T)

AssertNotSent asserts that no emails were sent.

func (*FakeMailer) AssertSent

func (m *FakeMailer) AssertSent(t *testing.T, to string)

AssertSent asserts that an email was sent to the given address.

func (*FakeMailer) Send

func (m *FakeMailer) Send(ctx context.Context, msg *mail.Message) error

Send implements the mail.Mailer interface.

type MemoryStorage

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

MemoryStorage is an in-memory file storage driver for test_util.

func NewMemoryStorage

func NewMemoryStorage() *MemoryStorage

NewMemoryStorage creates a new MemoryStorage.

func (*MemoryStorage) Copy

func (s *MemoryStorage) Copy(ctx context.Context, src, dest string) error

Copy copies a file from source to destination.

func (*MemoryStorage) Delete

func (s *MemoryStorage) Delete(ctx context.Context, path string) error

Delete removes content from memory.

func (*MemoryStorage) Exists

func (s *MemoryStorage) Exists(ctx context.Context, path string) (bool, error)

Exists checks if the file exists in memory.

func (*MemoryStorage) Get

func (s *MemoryStorage) Get(ctx context.Context, path string) ([]byte, error)

Get retrieves content from memory.

func (*MemoryStorage) Move

func (s *MemoryStorage) Move(ctx context.Context, src, dest string) error

Move moves a file from source to destination.

func (*MemoryStorage) Put

func (s *MemoryStorage) Put(ctx context.Context, path string, content []byte) error

Put stores content in memory.

func (*MemoryStorage) SignedURL

func (s *MemoryStorage) SignedURL(ctx context.Context, path string, expiresIn time.Duration) (string, error)

SignedURL returns a fake signed URL for the file.

func (*MemoryStorage) URL

func (s *MemoryStorage) URL(path string) (string, error)

URL returns a fake URL for the file.

type ModelFactory

type ModelFactory[T any] struct {
	// contains filtered or unexported fields
}

func NewFactory

func NewFactory[T any](table string) *ModelFactory[T]

NewFactory creates a new ModelFactory[T] for the given database table.

Example:

factory := test_util.NewFactory[User]("users").
    Set("name", faker.Name()).
    Set("email", faker.Email()).
    Count(10)

users := factory.CreateMany(ctx, db)

func (*ModelFactory[T]) Count

func (f *ModelFactory[T]) Count(n int) *ModelFactory[T]

Count sets the number of instances to create/make.

func (*ModelFactory[T]) Create

func (f *ModelFactory[T]) Create(ctx context.Context, db *database.DB) (*T, error)

Create inserts a single instance into the database and returns the created record.

func (*ModelFactory[T]) CreateMany

func (f *ModelFactory[T]) CreateMany(ctx context.Context, db *database.DB) ([]*T, error)

CreateMany inserts count instances into the database and returns all created records.

func (*ModelFactory[T]) Make

func (f *ModelFactory[T]) Make() *T

Make returns a single in-memory instance (no DB call).

func (*ModelFactory[T]) MakeMany

func (f *ModelFactory[T]) MakeMany() []*T

MakeMany returns count in-memory instances.

func (*ModelFactory[T]) Set

func (f *ModelFactory[T]) Set(field string, value any) *ModelFactory[T]

Set registers a fixed value for the given field name. The field name should match the struct's db/json tag name.

func (*ModelFactory[T]) SetFunc

func (f *ModelFactory[T]) SetFunc(field string, fn func() any) *ModelFactory[T]

SetFunc registers a generator function for the given field. The function is called once per created instance, enabling unique values.

func (*ModelFactory[T]) WithFakes

func (f *ModelFactory[T]) WithFakes() *ModelFactory[T]

WithFakes enables automatic fake data generation for unassigned fields based on struct tags.

type Suite

type Suite struct {
	suite.Suite
	App         *engine.App
	Postgres    *postgres.PostgresContainer
	Redis       *redis.RedisContainer
	Ctx         context.Context
	DB          *database.DB
	RedisClient redisclient.UniversalClient
	// contains filtered or unexported fields
}

Suite is a base testing suite providing real service containers.

func (*Suite) SetupSuite

func (s *Suite) SetupSuite()

SetupSuite starts the required containers and initializes the Astra app.

func (*Suite) SetupTest

func (s *Suite) SetupTest()

SetupTest starts a database transaction before each test. It temporarily replaces the App's "db" with a transaction-scoped DB.

func (*Suite) TearDownSuite

func (s *Suite) TearDownSuite()

TearDownSuite stops all containers.

func (*Suite) TearDownTest

func (s *Suite) TearDownTest()

TearDownTest rolls back the transaction after each test, restoring the database to its pristine state.

type SyncDispatcher

type SyncDispatcher struct{}

SyncDispatcher executes jobs immmediately in the same goroutine instead of queueing.

func NewSyncDispatcher

func NewSyncDispatcher() *SyncDispatcher

NewSyncDispatcher creates a new synchronous dispatcher.

func (*SyncDispatcher) Dispatch

func (d *SyncDispatcher) Dispatch(ctx context.Context, job queue.Job, name string) error

Dispatch executes the job immediately.

func (*SyncDispatcher) DispatchAt

func (d *SyncDispatcher) DispatchAt(ctx context.Context, job queue.Job, name string, at time.Time) error

DispatchAt executes the job immediately (ignoring scheduled time) for testing purposes.

func (*SyncDispatcher) DispatchIn

func (d *SyncDispatcher) DispatchIn(ctx context.Context, job queue.Job, name string, delay time.Duration) error

DispatchIn executes the job immediately (ignoring delay) for testing purposes.

type TestApp

type TestApp struct {
	App    *engine.App
	Router *astrahttp.Router
	// contains filtered or unexported fields
}

TestApp is a test wrapper around the Astra app.

func NewTestApp

func NewTestApp(t *testing.T, setup func(app *engine.App, router *astrahttp.Router)) *TestApp

NewTestApp creates a new application for testing.

func (*TestApp) GET

func (a *TestApp) GET(path string) *TestResponse

func (*TestApp) POST

func (a *TestApp) POST(path string, body any) *TestResponse

POST is a generic helper for making POST requests.

func (*TestApp) PUT

func (a *TestApp) PUT(path string, body any) *TestResponse

PUT is a generic helper for making PUT requests.

func (*TestApp) WithHeader

func (a *TestApp) WithHeader(key, value string) *TestApp

type TestResponse

type TestResponse struct {
	Recorder *httptest.ResponseRecorder
	// contains filtered or unexported fields
}

TestResponse wraps httptest.ResponseRecorder with fluent assertions.

func POST

func POST[T any](a *TestApp, path string, body T) *TestResponse

func PUT

func PUT[T any](a *TestApp, path string, body T) *TestResponse

func (*TestResponse) AssertBodyContains

func (r *TestResponse) AssertBodyContains(s string) *TestResponse

AssertBodyContains checks if the response body contains the given string.

func (*TestResponse) AssertContract

func (r *TestResponse) AssertContract(model any) *TestResponse

AssertContract verifies that the response body matches the structural contract defined by the provided model (Go struct).

func (*TestResponse) AssertHeader

func (r *TestResponse) AssertHeader(key, expected string) *TestResponse

AssertHeader asserts the response contains a specific header value.

func (*TestResponse) AssertJSON

func (r *TestResponse) AssertJSON(key string, expected any) *TestResponse

AssertJSON asserts the response body contains a specific JSON value at the given key.

func (*TestResponse) AssertJSONCount

func (r *TestResponse) AssertJSONCount(key string, count int) *TestResponse

AssertJSONCount asserts the length of a JSON array at the given key.

func (*TestResponse) AssertStatus

func (r *TestResponse) AssertStatus(code int) *TestResponse

AssertStatus asserts the response status code.

func (*TestResponse) Debug

func (r *TestResponse) Debug() *TestResponse

func (*TestResponse) ExpectJSON

func (r *TestResponse) ExpectJSON(key string, expected any) *TestResponse

ExpectJSON is an alias for AssertJSON.

func (*TestResponse) ExpectStatus

func (r *TestResponse) ExpectStatus(code int) *TestResponse

ExpectStatus is an alias for AssertStatus.

Jump to

Keyboard shortcuts

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