Documentation
¶
Overview ¶
Package factory builds fake records (like Laravel model factories).
var UserFactory = factory.New("users", func(f *gofakeit.Faker, i int) User {
return User{Name: f.Name(), Email: f.Email()}
})
UserFactory.Count(50).Create(ctx, c)
Index ¶
- Constants
- type Env
- type Factory
- func (f *Factory[T]) ChunkSize(n int) *Factory[T]
- func (f *Factory[T]) Count(n int) *Factory[T]
- func (f *Factory[T]) Create(ctx context.Context, env Env) ([]T, error)
- func (f *Factory[T]) CreateOne(ctx context.Context, env Env) (T, error)
- func (f *Factory[T]) Make() []T
- func (f *Factory[T]) MakeOne() T
- func (f *Factory[T]) State(fns ...func(*T)) *Factory[T]
- func (f *Factory[T]) Table() string
- func (f *Factory[T]) With(fk *gofakeit.Faker) *Factory[T]
Constants ¶
const DefaultChunkSize = 500
DefaultChunkSize is how many rows go into one INSERT statement.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Env ¶
type Env interface {
Insert(table interface{}) *goqu.InsertDataset
Faker() *gofakeit.Faker
}
Env is what Create needs: an INSERT builder and a faker. *seeder.Context implements it.
type Factory ¶
type Factory[T any] struct { // contains filtered or unexported fields }
Factory produces values of type T. T is usually a struct with `db:"..."` tags; tag auto-increment columns with `goqu:"skipinsert"`.
A Factory is immutable: Count, State and With return a copy, so a shared base factory can be specialised safely.
func New ¶
New creates a factory. def receives a faker and a sequence number that grows with every record this factory (and its copies) builds. Use the sequence for values that must be unique:
Email: fmt.Sprintf("user%d@example.com", i)
func (*Factory[T]) Create ¶
Create builds the records and inserts them (in chunks). It returns the records as built; database-generated columns such as IDs are not filled in, query them afterwards if you need them.
func (*Factory[T]) Make ¶
func (f *Factory[T]) Make() []T
Make builds records in memory without touching the database.
func (*Factory[T]) MakeOne ¶
func (f *Factory[T]) MakeOne() T
MakeOne builds a single record in memory.