factory

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 4 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type F

type F struct {
	*gofakeit.Faker
	// contains filtered or unexported fields
}

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.

func (*F) Seq

func (f *F) Seq() int

Seq returns 1, 2, 3, ... across builds of the same factory.

func (*F) Seqf

func (f *F) Seqf(format string) string

Seqf renders format with the current sequence number, e.g. f.Seqf("user-%d@example.com").

type Factory

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

Factory builds instances of T.

func Define

func Define[T any](build func(f *F) T) *Factory[T]

Define creates a factory with a randomly seeded faker.

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.

func (*Factory[T]) MakeMany

func (fa *Factory[T]) MakeMany(n int, overrides ...func(*T)) []T

MakeMany builds n instances.

func (*Factory[T]) Seeded

func (fa *Factory[T]) Seeded(seed uint64) *Factory[T]

Seeded returns a new factory sharing the build function but with a deterministic faker and a reset sequence, for reproducible tests.

Jump to

Keyboard shortcuts

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