Help us shape the future of Go! Take the 2026 Go Developer Survey

factory

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package factory builds models with fake data for tests and seeders:

var Users = factory.New(func(f *factory.Faker) models.User {
	return models.User{Name: f.Name(), Email: f.Email()}
})

admins, err := Users.Count(3).State(func(u *models.User) { u.Role = "admin" }).Create(ctx, db)

Factories are values: State, Count, Seed and AfterCreate return copies, so a base factory can be shared. Make builds values in memory; Create also inserts them through orm.Create. A Faker seeded per factory makes the data reproducible (Seed), which seeders and golden tests rely on.

See https://github.com/AlexGalhardo/vukem/blob/main/docs/database.md#factories-and-seeders.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Pick

func Pick[T any](f *Faker, items ...T) T

Pick returns one of items.

Types

type Factory

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

Factory builds T values.

func New

func New[T any](def func(f *Faker) T) Factory[T]

New returns a factory whose definition returns one model with fake data.

func (Factory[T]) AfterCreate

func (f Factory[T]) AfterCreate(fn func(ctx context.Context, ex database.Executor, m *T) error) Factory[T]

AfterCreate runs fn for every created model, inside Create; use it to create related models:

Users.AfterCreate(func(ctx context.Context, ex database.Executor, u *User) error {
	_, err := Posts.Count(2).State(func(p *Post) { p.UserID = u.ID }).Create(ctx, ex)
	return err
})

func (Factory[T]) Count

func (f Factory[T]) Count(n int) Factory[T]

Count sets how many models Make and Create build.

func (Factory[T]) Create

func (f Factory[T]) Create(ctx context.Context, ex database.Executor) ([]T, error)

Create builds and inserts Count models (in a transaction when ex is a *database.DB), then runs AfterCreate hooks.

Example
package main

import (
	"context"
	"fmt"

	"github.com/AlexGalhardo/vukem/database"
	"github.com/AlexGalhardo/vukem/factory"
	"github.com/AlexGalhardo/vukem/orm"
)

type User struct {
	orm.Model
	Name string
	Role string
}

func main() {
	ctx := context.Background()

	db, _ := database.Open(ctx, ":memory:")
	defer db.Close()

	_, _ = db.ExecContext(ctx, `CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT,
		created_at TIMESTAMP, updated_at TIMESTAMP, name TEXT, role TEXT)`)

	users := factory.New(func(f *factory.Faker) User {
		return User{Name: f.Name(), Role: "member"}
	})

	created, err := users.Count(3).Create(ctx, db)
	if err != nil {
		fmt.Println(err)
		return
	}

	n, _ := orm.QueryOf[User](db).Count(ctx)
	fmt.Println(len(created), n, created[0].ID > 0)
}
Output:
3 3 true

func (Factory[T]) CreateOne

func (f Factory[T]) CreateOne(ctx context.Context, ex database.Executor) (T, error)

CreateOne builds and inserts one model.

func (Factory[T]) Make

func (f Factory[T]) Make() []T

Make builds Count models without saving them.

Example
package main

import (
	"fmt"

	"github.com/AlexGalhardo/vukem/factory"
	"github.com/AlexGalhardo/vukem/orm"
)

type User struct {
	orm.Model
	Name string
	Role string
}

func main() {
	users := factory.New(func(f *factory.Faker) User {
		return User{Name: f.Name(), Role: "member"}
	})

	admins := users.Count(2).State(func(u *User) { u.Role = "admin" }).Make()
	for _, u := range admins {
		fmt.Println(u.Role, u.Name != "")
	}
}
Output:
admin true
admin true

func (Factory[T]) MakeOne

func (f Factory[T]) MakeOne() T

MakeOne builds one model without saving it.

func (Factory[T]) Seed

func (f Factory[T]) Seed(seed uint64) Factory[T]

Seed makes the fake data reproducible.

func (Factory[T]) State

func (f Factory[T]) State(fn func(*T)) Factory[T]

State applies fn to every built model, after the definition (and after earlier states).

type Faker

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

Faker generates fake data. It is deterministic for a given seed; it is not cryptographically secure and must never produce secrets.

func NewFaker

func NewFaker(seed uint64) *Faker

NewFaker returns a Faker seeded with seed.

func (*Faker) Bool

func (f *Faker) Bool() bool

Bool returns true or false.

func (*Faker) Email

func (f *Faker) Email() string

Email returns an address on a reserved example domain, unique within one Make and unlikely to collide across Makes.

func (*Faker) FirstName

func (f *Faker) FirstName() string

FirstName returns a first name.

func (*Faker) Float

func (f *Faker) Float(lo, hi float64) float64

Float returns a number in [lo, hi).

func (*Faker) Int

func (f *Faker) Int(lo, hi int) int

Int returns a number in [lo, hi].

func (*Faker) LastName

func (f *Faker) LastName() string

LastName returns a last name.

func (*Faker) Name

func (f *Faker) Name() string

Name returns a full name.

func (*Faker) Paragraph

func (f *Faker) Paragraph() string

Paragraph returns 3–6 sentences.

func (*Faker) Sentence

func (f *Faker) Sentence() string

Sentence returns a capitalized sentence of 6–12 words.

func (*Faker) Seq

func (f *Faker) Seq() int

Seq is the 1-based index of the model being built (unique per Make).

func (*Faker) Slug

func (f *Faker) Slug() string

Slug returns a lowercase, dash-separated slug of 3 words.

func (*Faker) Time

func (f *Faker) Time(from, to time.Time) time.Time

Time returns a time between from and to.

func (*Faker) URL

func (f *Faker) URL() string

URL returns an https URL on an example domain.

func (*Faker) UUID

func (f *Faker) UUID() string

UUID returns a random version 4 UUID (not for secrets).

func (*Faker) Username

func (f *Faker) Username() string

Username returns a lowercase handle, unique within one Make.

func (*Faker) Word

func (f *Faker) Word() string

Word returns a lorem ipsum word.

func (*Faker) Words

func (f *Faker) Words(n int) string

Words returns n words separated by spaces.

Jump to

Keyboard shortcuts

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