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 ¶
- func NewTestDB(t *testing.T, cfg config.DatabaseConfig) *pgxpool.Pool
- func WithORMTransaction(t *testing.T, db *database.DB, fn func(txDB *database.DB))
- func WithTestTransaction(t *testing.T, pool *pgxpool.Pool, fn func(tx pgx.Tx))
- type BaseFactory
- type Factory
- type FakeMailer
- type MemoryStorage
- func (s *MemoryStorage) Copy(ctx context.Context, src, dest string) error
- func (s *MemoryStorage) Delete(ctx context.Context, path string) error
- func (s *MemoryStorage) Exists(ctx context.Context, path string) (bool, error)
- func (s *MemoryStorage) Get(ctx context.Context, path string) ([]byte, error)
- func (s *MemoryStorage) Move(ctx context.Context, src, dest string) error
- func (s *MemoryStorage) Put(ctx context.Context, path string, content []byte) error
- func (s *MemoryStorage) SignedURL(ctx context.Context, path string, expiresIn time.Duration) (string, error)
- func (s *MemoryStorage) URL(path string) (string, error)
- type ModelFactory
- func (f *ModelFactory[T]) Count(n int) *ModelFactory[T]
- func (f *ModelFactory[T]) Create(ctx context.Context, db *database.DB) (*T, error)
- func (f *ModelFactory[T]) CreateMany(ctx context.Context, db *database.DB) ([]*T, error)
- func (f *ModelFactory[T]) Make() *T
- func (f *ModelFactory[T]) MakeMany() []*T
- func (f *ModelFactory[T]) Set(field string, value any) *ModelFactory[T]
- func (f *ModelFactory[T]) SetFunc(field string, fn func() any) *ModelFactory[T]
- func (f *ModelFactory[T]) WithFakes() *ModelFactory[T]
- type Suite
- type SyncDispatcher
- func (d *SyncDispatcher) Dispatch(ctx context.Context, job queue.Job, name string) error
- func (d *SyncDispatcher) DispatchAt(ctx context.Context, job queue.Job, name string, at time.Time) error
- func (d *SyncDispatcher) DispatchIn(ctx context.Context, job queue.Job, name string, delay time.Duration) error
- type TestApp
- type TestResponse
- func (r *TestResponse) AssertBodyContains(s string) *TestResponse
- func (r *TestResponse) AssertContract(model any) *TestResponse
- func (r *TestResponse) AssertHeader(key, expected string) *TestResponse
- func (r *TestResponse) AssertJSON(key string, expected any) *TestResponse
- func (r *TestResponse) AssertJSONCount(key string, count int) *TestResponse
- func (r *TestResponse) AssertStatus(code int) *TestResponse
- func (r *TestResponse) Debug() *TestResponse
- func (r *TestResponse) ExpectJSON(key string, expected any) *TestResponse
- func (r *TestResponse) ExpectStatus(code int) *TestResponse
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithORMTransaction ¶
WithORMTransaction runs a function inside an ORM 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 ¶
FakeMailer is a test mailer that collects sent messages in memory.
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.
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) Move ¶
func (s *MemoryStorage) Move(ctx context.Context, src, dest string) error
Move moves a file from source to destination.
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 ¶
Create inserts a single instance into the database and returns the created record.
func (*ModelFactory[T]) CreateMany ¶
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) 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.
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 ¶
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 ¶
type TestResponse ¶
type TestResponse struct {
Recorder *httptest.ResponseRecorder
// contains filtered or unexported fields
}
TestResponse wraps httptest.ResponseRecorder with fluent assertions.
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.