tests

package
v0.19.771 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2026 License: AGPL-3.0 Imports: 56 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 CtlApiFXOptions

func CtlApiFXOptions() []fx.Option

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

func CtlApiFXOptionsWithValidator() []fx.Option

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

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.

type CapturedSignal added in v0.19.770

type CapturedSignal struct {
	ID     string
	Signal eventloop.Signal
}

CapturedSignal holds information about a signal that was sent.

type FakeEventLoopClient added in v0.19.770

type FakeEventLoopClient struct {
	// contains filtered or unexported fields
}

FakeEventLoopClient is a test implementation of eventloop.Client that records signals.

func NewFakeEventLoopClient added in v0.19.770

func NewFakeEventLoopClient() *FakeEventLoopClient

NewFakeEventLoopClient creates a new fake event loop client for testing.

func (*FakeEventLoopClient) Cancel added in v0.19.770

func (f *FakeEventLoopClient) Cancel(ctx context.Context, namespace, id string) error

Cancel implements eventloop.Client (no-op for testing).

func (*FakeEventLoopClient) GetSignals added in v0.19.770

func (f *FakeEventLoopClient) GetSignals() []CapturedSignal

GetSignals returns all captured signals.

func (*FakeEventLoopClient) GetWorkflowCount added in v0.19.770

func (f *FakeEventLoopClient) GetWorkflowCount(ctx context.Context, namespace string, workflowID string) (int64, error)

GetWorkflowCount implements eventloop.Client (returns 1 for testing).

func (*FakeEventLoopClient) GetWorkflowStatus added in v0.19.770

func (f *FakeEventLoopClient) GetWorkflowStatus(ctx context.Context, namespace string, workflowID string) (enumsv1.WorkflowExecutionStatus, error)

GetWorkflowStatus implements eventloop.Client (returns completed for testing).

func (*FakeEventLoopClient) Reset added in v0.19.770

func (f *FakeEventLoopClient) Reset()

Reset clears all captured signals.

func (*FakeEventLoopClient) Send added in v0.19.770

func (f *FakeEventLoopClient) Send(ctx context.Context, id string, signal eventloop.Signal)

Send implements eventloop.Client by recording the signal.

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.

Jump to

Keyboard shortcuts

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