Documentation
¶
Index ¶
- func DropAllTables(db *sql.DB, driver string) error
- func F() *gofakeit.Faker
- func Faker() *gofakeit.Faker
- func GetAllTables(db *sql.DB, driver string) ([]string, error)
- func RefreshDatabase(t *testing.T) *sql.DB
- func ResetSchemaRefresh()
- func TruncateAllTables(db *sql.DB, driver string) error
- type Factory
- func (f *Factory) Count(n int) *Factory
- func (f *Factory) Create(overrides ...map[string]interface{}) interface{}
- func (f *Factory) DefineState(name string, attributes map[string]interface{})
- func (f *Factory) Make(overrides ...map[string]interface{}) interface{}
- func (f *Factory) Sequence(field string, generator func(int) interface{}) *Factory
- func (f *Factory) State(name string) *Factory
- type ModelFactory
- func (f *ModelFactory[T]) Count(n int) *ModelFactory[T]
- func (f *ModelFactory[T]) Create(overrides *T) any
- func (f *ModelFactory[T]) CreateMany(count int, overrides *T) []*T
- func (f *ModelFactory[T]) CreateOne(overrides *T) *T
- func (f *ModelFactory[T]) DefineState(name string, modifier func(*T)) *ModelFactory[T]
- func (f *ModelFactory[T]) Make(overrides *T) any
- func (f *ModelFactory[T]) State(name string) *ModelFactory[T]
- type TestCase
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DropAllTables ¶
DropAllTables drops all tables in the database
func GetAllTables ¶
GetAllTables returns a list of all tables in the database
func RefreshDatabase ¶
RefreshDatabase resets the database to a clean state and runs all migrations
This function: 1. Validates it's safe to run (test database, not production) 2. Drops all existing tables 3. Runs all registered migrations via migrate.Up() 4. Registers cleanup to close the connection after test
Safety checks: - Requires testing.T (only callable from tests) - Checks APP_ENV != "production" - Verifies database name contains "test" or is ":memory:"
Usage:
func TestExample(t *testing.T) {
testing.RefreshDatabase(t)
// Test with clean database
}
func ResetSchemaRefresh ¶ added in v0.7.0
func ResetSchemaRefresh()
ResetSchemaRefresh resets the schema refresh flag (for testing the testing framework)
Types ¶
type Factory ¶
type Factory struct {
// contains filtered or unexported fields
}
Factory represents a model factory for generating test data
func NewFactory ¶
NewFactory creates a new factory for generating test data
func (*Factory) DefineState ¶
DefineState defines a named attribute preset
type ModelFactory ¶ added in v0.6.7
type ModelFactory[T any] struct { // contains filtered or unexported fields }
ModelFactory is a type-safe factory for creating test models Usage:
func (User) Factory() *ModelFactory[User] {
return NewModelFactory(func() *User {
return &User{Name: Faker().Name(), Email: Faker().Email()}
})
}
// In tests:
user := models.User{}.Factory().Create(nil)
admin := models.User{}.Factory().Create(&models.User{Role: "admin"})
users := models.User{}.Factory().Count(3).Create(nil)
func NewModelFactory ¶ added in v0.6.7
func NewModelFactory[T any](definition func() *T) *ModelFactory[T]
NewModelFactory creates a new type-safe model factory
func (*ModelFactory[T]) Count ¶ added in v0.6.7
func (f *ModelFactory[T]) Count(n int) *ModelFactory[T]
Count sets the number of records to generate
func (*ModelFactory[T]) Create ¶ added in v0.6.7
func (f *ModelFactory[T]) Create(overrides *T) any
Create generates and persists model(s) to database Returns *T for single, []*T for multiple (when Count > 1)
func (*ModelFactory[T]) CreateMany ¶ added in v0.6.7
func (f *ModelFactory[T]) CreateMany(count int, overrides *T) []*T
CreateMany is a convenience method that always returns []*T (not any)
func (*ModelFactory[T]) CreateOne ¶ added in v0.6.7
func (f *ModelFactory[T]) CreateOne(overrides *T) *T
CreateOne is a convenience method that always returns *T (not any)
func (*ModelFactory[T]) DefineState ¶ added in v0.6.7
func (f *ModelFactory[T]) DefineState(name string, modifier func(*T)) *ModelFactory[T]
DefineState registers a named state modifier
func (*ModelFactory[T]) Make ¶ added in v0.6.7
func (f *ModelFactory[T]) Make(overrides *T) any
Make generates model(s) without persisting to database Returns *T for single, []*T for multiple (when Count > 1)
func (*ModelFactory[T]) State ¶ added in v0.6.7
func (f *ModelFactory[T]) State(name string) *ModelFactory[T]
State applies a named state modifier to the factory
type TestCase ¶
type TestCase struct {
// contains filtered or unexported fields
}
TestCase provides test helpers for database testing
func NewTestCase ¶
NewTestCase creates a new test case instance
func Setup ¶
Setup creates a TestCase and automatically runs RefreshDatabase This is the recommended way to setup database tests
Usage:
func TestExample(t *testing.T) {
tc := ormtesting.Setup(t)
// Database already refreshed - ready to test
}
If you don't need database refresh, use NewTestCase(t) directly instead
func (*TestCase) LazyRefreshDatabase ¶
func (tc *TestCase) LazyRefreshDatabase()
LazyRefreshDatabase resets the database for testing: 1. Runs migrations ONCE per test suite (not per test) 2. Truncates all tables before each test (fast cleanup)
Usage:
func TestExample(t *testing.T) {
tc := testing.NewTestCase(t)
tc.LazyRefreshDatabase()
// Test code - clean database, fast setup
}
func (*TestCase) RefreshDatabase ¶
func (tc *TestCase) RefreshDatabase()
RefreshDatabase drops all tables and runs migrations for EACH test This is slower but more thorough - use when you need true isolation (e.g., testing migrations themselves)
Usage:
func TestExample(t *testing.T) {
tc := testing.NewTestCase(t)
tc.RefreshDatabase()
// Test code - completely fresh database
}