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 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 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
}
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 TestCase ¶
type TestCase struct {
// contains filtered or unexported fields
}
TestCase provides test helpers
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 drops tables and runs migrations before each test
Usage:
func TestExample(t *testing.T) {
tc := testing.NewTestCase(t)
tc.LazyRefreshDatabase()
// Test code - fresh database
}
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
Usage:
func TestExample(t *testing.T) {
tc := testing.NewTestCase(t)
tc.RefreshDatabase()
// Test code - starts with completely fresh database
}