Documentation
¶
Overview ¶
Package factory provides model factories for tests and seeders: define how a model is built once, then Make in-memory instances or Create persisted ones in tests and seeders. Factories are ORM-agnostic — persistence goes through a caller-supplied function, typically a repository's Create method.
Index ¶
- type F
- type Factory
- func (fa *Factory[T]) Create(ctx context.Context, persist func(context.Context, *T) error, ...) (T, error)
- func (fa *Factory[T]) CreateMany(ctx context.Context, n int, persist func(context.Context, *T) error, ...) ([]T, error)
- func (fa *Factory[T]) Make(overrides ...func(*T)) T
- func (fa *Factory[T]) MakeMany(n int, overrides ...func(*T)) []T
- func (fa *Factory[T]) Seeded(seed uint64) *Factory[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type F ¶
F is the per-factory faker handed to build functions. It embeds the full gofakeit API (f.Email(), f.Name(), f.UUID(), ...) and adds a sequence counter for unique values.
type Factory ¶
type Factory[T any] struct { // contains filtered or unexported fields }
Factory builds instances of T.
func (*Factory[T]) Create ¶
func (fa *Factory[T]) Create(ctx context.Context, persist func(context.Context, *T) error, overrides ...func(*T)) (T, error)
Create builds one instance and persists it, typically with a repository:
ticket, err := factories.NewTicketFactory().Create(ctx, repo.Create)
func (*Factory[T]) CreateMany ¶
func (fa *Factory[T]) CreateMany(ctx context.Context, n int, persist func(context.Context, *T) error, overrides ...func(*T)) ([]T, error)
CreateMany builds and persists n instances, stopping at the first error and returning the instances persisted so far alongside it.
func (*Factory[T]) Make ¶
func (fa *Factory[T]) Make(overrides ...func(*T)) T
Make builds one instance, applying overrides in order after the build function.