Documentation
¶
Index ¶
Constants ¶
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 ¶
SkipIfNotIntegration skips the test if INTEGRATION != "true". Call this at the start of TestXxxSuite functions.
Types ¶
type BaseDBTestSuite ¶
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.