Documentation
¶
Index ¶
- func AssertRecordCount(t *testing.T, db *gorm.DB, model interface{}, expectedCount int64, ...)
- func AssertRecordExists(t *testing.T, db *gorm.DB, model interface{}, where string, ...)
- func AssertRecordNotExists(t *testing.T, db *gorm.DB, model interface{}, where string, ...)
- func AutoMigrate(t *testing.T, db *gorm.DB, models ...interface{})
- func CreateFile(t *testing.T, path string, content string)
- func DirExists(t *testing.T, path string) bool
- func FileExists(t *testing.T, path string) bool
- func LoadFixture(t *testing.T, path string) []byte
- func LoadJSONFixture(t *testing.T, path string, v interface{})
- func MkdirAll(t *testing.T, basePath string, dirs ...string)
- func RunInTransaction(t *testing.T, db *gorm.DB, fn func(tx *gorm.DB))
- func SeedTestData(t *testing.T, db *gorm.DB, data interface{})
- func SetupTestDB(t *testing.T) *gorm.DB
- func SetupTestEnv(t *testing.T, env map[string]string)
- func TempDir(t *testing.T) string
- func TruncateTable(t *testing.T, db *gorm.DB, tableName string)
- func WriteFixture(t *testing.T, dir, filename string, data []byte) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertRecordCount ¶
func AssertRecordCount(t *testing.T, db *gorm.DB, model interface{}, expectedCount int64, where string, args ...interface{})
AssertRecordCount asserts that the database contains the expected number of records matching the given conditions.
Example usage:
testutil.AssertRecordCount(t, db, &User{}, 5, "active = ?", true)
func AssertRecordExists ¶
func AssertRecordExists(t *testing.T, db *gorm.DB, model interface{}, where string, args ...interface{})
AssertRecordExists asserts that a record exists in the database with the given conditions.
Example usage:
testutil.AssertRecordExists(t, db, &User{}, "email = ?", "test@example.com")
func AssertRecordNotExists ¶
func AssertRecordNotExists(t *testing.T, db *gorm.DB, model interface{}, where string, args ...interface{})
AssertRecordNotExists asserts that no record exists in the database with the given conditions.
Example usage:
testutil.AssertRecordNotExists(t, db, &User{}, "email = ?", "deleted@example.com")
func AutoMigrate ¶
AutoMigrate runs database migrations for the provided models. This is a convenience wrapper around GORM's AutoMigrate.
Example usage:
testutil.AutoMigrate(t, db, &User{}, &Post{})
func CreateFile ¶
CreateFile creates a file with the given content in the specified path. Parent directories are created automatically.
func FileExists ¶
FileExists checks if a file exists at the given path.
func LoadFixture ¶
LoadFixture loads a test fixture file from the internal/testutil/fixtures directory. Returns the raw bytes of the fixture file.
func LoadJSONFixture ¶
LoadJSONFixture loads a JSON fixture file and unmarshals it into the provided value.
func MkdirAll ¶
MkdirAll creates a directory structure in the given base path. Useful for creating complex test directory structures.
func RunInTransaction ¶
RunInTransaction runs the provided function within a database transaction that is automatically rolled back after the function completes. This is useful for testing database operations without persisting changes.
Example usage:
testutil.RunInTransaction(t, db, func(tx *gorm.DB) {
user := User{Email: "test@example.com"}
err := tx.Create(&user).Error
require.NoError(t, err)
// Changes are rolled back after this function returns
})
func SeedTestData ¶
SeedTestData seeds the test database with the provided data. The data parameter should be a pointer to a struct or slice of structs.
Example usage:
users := []User{
{Email: "user1@example.com"},
{Email: "user2@example.com"},
}
testutil.SeedTestData(t, db, &users)
func SetupTestDB ¶
SetupTestDB creates a test database. By default, it creates an in-memory SQLite database for fast, isolated testing. If POSTGRES_TEST_DSN environment variable is set, it will use a PostgreSQL database instead.
The database is automatically closed when the test completes.
Example usage:
db := testutil.SetupTestDB(t)
err := db.AutoMigrate(&User{})
require.NoError(t, err)
func SetupTestEnv ¶
SetupTestEnv sets test environment variables and automatically unsets them when the test completes.
func TempDir ¶
TempDir creates a temporary directory for tests that is automatically cleaned up when the test completes.
func TruncateTable ¶
TruncateTable truncates the specified table in the database. Useful for cleaning up between test cases.
Example usage:
testutil.TruncateTable(t, db, "users")
Types ¶
This section is empty.