Documentation
¶
Index ¶
- Constants
- func CreateTestDatabase() error
- func CtlApiFXOptions() []fx.Option
- func CtlApiFXOptionsWithValidator() []fx.Optiondeprecated
- func NewTestRouter(opts RouterOptions) *gin.Engine
- func SkipIfNotIntegration(t *testing.T)
- func TruncateAllTables(ctx context.Context, db *gorm.DB) error
- type BaseDBTestSuite
- type RouterOptions
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 CtlApiFXOptions ¶
CtlApiFXOptions returns the common FX options used across all ctl-api integration tests. This includes configuration, logging, databases, external services, helpers, and clients.
Usage:
app := fxtest.New(
t,
testfx.CtlApiFXOptions()...,
fx.Provide(MyService), // add your service under test
fx.Populate(&myTestService),
)
func CtlApiFXOptionsWithValidator
deprecated
CtlApiFXOptionsWithValidator returns common test options but uses the standard validator instead of the custom validator. Use this when you don't need custom entity_name validation.
Deprecated: Most tests should use CtlApiFXOptions() with the custom validator.
func NewTestRouter ¶
func NewTestRouter(opts RouterOptions) *gin.Engine
NewTestRouter creates a gin router with standard test middlewares.
Standard middlewares included (in order): 1. stderr - Error handling and JSON error responses (REQUIRED) 2. patcher - PATCH request field extraction for partial updates 3. pagination - Query parameter parsing for paginated endpoints 4. context injection - Injects test org and account into context
Usage:
router := testfx.NewTestRouter(testfx.RouterOptions{
L: s.service.L,
DB: s.service.DB,
TestOrg: s.testOrg,
TestAcc: s.testAcc,
})
With additional middlewares:
router := testfx.NewTestRouter(testfx.RouterOptions{
L: s.service.L,
DB: s.service.DB,
TestOrg: s.testOrg,
TestAcc: s.testAcc,
AdditionalMiddlewares: []gin.HandlerFunc{
myCustomMiddleware.Handler(),
},
})
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.
type RouterOptions ¶
type RouterOptions struct {
// Logger for middlewares
L *zap.Logger
// Database for pagination and patcher middleware
DB *gorm.DB
// TestOrg to inject into context (optional)
TestOrg *app.Org
// TestAccount to inject into context (optional)
TestAcc *app.Account
// AdditionalMiddlewares to add after standard middlewares (optional)
AdditionalMiddlewares []gin.HandlerFunc
}
RouterOptions configures the test router setup.