testdb

package
v0.19.766 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2026 License: AGPL-3.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const TestDBName = "ctl_api_test"

Variables

This section is empty.

Functions

func CreateTestDatabase

func CreateTestDatabase() error

CreateTestDatabase creates the test database if it doesn't exist and runs migrations. Uses the same config system as the service to get connection parameters.

func SkipIfNotIntegration

func SkipIfNotIntegration(t *testing.T)

SkipIfNotIntegration skips the test if INTEGRATION != "true". Call this at the start of TestXxxSuite functions.

func TruncateAllTables

func TruncateAllTables(ctx context.Context, db *gorm.DB) error

TruncateAllTables truncates all tables in the database except service accounts. Service accounts are preserved to avoid re-running data migrations.

Types

type BaseDBTestSuite

type BaseDBTestSuite struct {
	suite.Suite
	// contains filtered or unexported fields
}

BaseDBTestSuite provides automatic test database setup and truncation. Embed this in your test suites and call SetDB() in SetupSuite after creating your DB connection.

Example:

type MyTestSuite struct {
    testdb.BaseDBTestSuite
    // your fields
}

func TestMySuite(t *testing.T) {
    testdb.SkipIfNotIntegration(t)
    suite.Run(t, new(MyTestSuite))
}

func (s *MyTestSuite) SetupSuite() {
    s.BaseDBTestSuite.SetupSuite() // creates test DB and sets env
    // create your fx app and get DB
    s.SetDB(db)
}

Tables are automatically truncated before each test via SetupTest.

func (*BaseDBTestSuite) DB

func (s *BaseDBTestSuite) DB() *gorm.DB

DB returns the database connection.

func (*BaseDBTestSuite) SetDB

func (s *BaseDBTestSuite) SetDB(db *gorm.DB)

SetDB stores the database connection for use in truncation. Call this in your SetupSuite after creating the DB connection.

func (*BaseDBTestSuite) SetupSuite

func (s *BaseDBTestSuite) SetupSuite()

SetupSuite creates the test database if needed and sets DB_NAME env var. Call this at the start of your SetupSuite if you override it.

func (*BaseDBTestSuite) SetupTest

func (s *BaseDBTestSuite) SetupTest()

SetupTest truncates all tables before each test and re-runs migrations. If you override SetupTest in your suite, call s.BaseDBTestSuite.SetupTest() first.

Jump to

Keyboard shortcuts

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