Documentation
¶
Index ¶
- Variables
- func NewDefaultDialector(dsn string) (gorm.Dialector, error)
- func NewIAMDialector(dsn string, region string, credProvider aws.CredentialsProvider) (gorm.Dialector, error)
- func NewTracingPlugin(conf *TracingConfig) gorm.Plugin
- func Open(ctx context.Context, conf *DatabaseConfig, opts ...gorm.Option) (*gorm.DB, io.Closer, error)
- func ParseSchema(db *gorm.DB, model any) (*schema.Schema, error)
- func SetupDatabaseFactory(name string, opts ...gorm.Option) ...
- func SetupTestSuiteFactory(opts ...TestSuiteOption) func(ctx context.Context, lc *lifecycle.Lifecycle) (*TestSuite, error)
- func WithMaxQueryLength(maxLength int) func(db *gorm.DB) *gorm.DB
- func WithSpanName(spanName string) func(db *gorm.DB) *gorm.DB
- type AuthMethod
- type Container
- type ContainerConfig
- type DatabaseConfig
- type Model
- type TestSuite
- type TestSuiteOption
- type TracingConfig
Constants ¶
This section is empty.
Variables ¶
var DefaultContainerConfig = func() *ContainerConfig { return &ContainerConfig{ Image: "postgres:17.4-alpine3.21", Username: "postgres", Password: "postgres", DatabaseName: "postgres", HostPort: "", } }
DefaultContainerConfig is the default configuration for starting a PostgreSQL test container.
var DefaultContainerFailCleanupTimeout = 10 * time.Second
DefaultContainerFailCleanupTimeout is used only for abnormal cleanup when container startup fails in OpenContainer. It does not affect normal termination flows managed by the lifecycle or callers.
var DefaultCreateBatchSize = 100
var DefaultDatabaseConfig = func() *DatabaseConfig { def, err := confx.Read[*struct { Database DatabaseConfig `confx:"database"` }]("yaml", strings.NewReader(defaultDatabaseConfigYAML)) if err != nil { panic(err) } return &def.Database }
var OmitAssociationsPlugin gorm.Plugin = &omitAssociationsPlugin{}
OmitAssociationsPlugin automatically omits associations for Create, Update and Delete operations
var SetupDatabase = SetupDatabaseFactory("database")
var TimePrecision = time.Microsecond
Functions ¶
func NewIAMDialector ¶
func NewTracingPlugin ¶
func NewTracingPlugin(conf *TracingConfig) gorm.Plugin
func SetupDatabaseFactory ¶
func SetupTestSuiteFactory ¶ added in v3.2.0
func SetupTestSuiteFactory(opts ...TestSuiteOption) func(ctx context.Context, lc *lifecycle.Lifecycle) (*TestSuite, error)
SetupTestSuiteFactory creates a factory function for creating test suites.
func WithMaxQueryLength ¶ added in v3.2.0
Types ¶
type AuthMethod ¶
type AuthMethod string
const ( AuthMethodPassword AuthMethod = "password" AuthMethodIAM AuthMethod = "iam" )
type Container ¶ added in v3.2.0
type Container struct {
testcontainers.Container
DSN string
}
Container wraps the started test container and exposes a DSN for connecting with PostgreSQL clients.
func OpenContainer ¶ added in v3.2.0
func OpenContainer(ctx context.Context, conf *ContainerConfig) (_ *Container, xerr error)
OpenContainer starts a PostgreSQL test container and returns a Container with a ready-to-use DSN. Call Terminate on the underlying container to stop and clean up resources when finished. If startup fails, the container is terminated using ContainerFailCleanupTimeout.
func SetupContainer ¶ added in v3.2.0
func SetupContainer(ctx context.Context, lc *lifecycle.Lifecycle, conf *ContainerConfig) (*Container, error)
SetupContainer starts a PostgreSQL test container and registers a lifecycle cleanup actor to terminate the container on shutdown.
func (*Container) Terminate ¶ added in v3.2.0
func (c *Container) Terminate(ctx context.Context, opts ...testcontainers.TerminateOption) error
Terminate terminates the PostgreSQL container.
type ContainerConfig ¶ added in v3.2.0
type ContainerConfig struct {
Image string `confx:"image" usage:"PostgreSQL image to use"`
Username string `confx:"username" usage:"PostgreSQL username"`
Password string `confx:"password" usage:"PostgreSQL password"`
DatabaseName string `confx:"databaseName" usage:"PostgreSQL database name"`
HostPort string `confx:"hostPort" usage:"PostgreSQL host port"`
}
ContainerConfig defines the configuration for starting a PostgreSQL test container. If a field is left empty, a sensible default will be used.
HostPort binds the container's 5432/tcp to a specific host port when set. Leave it empty to let Docker allocate a random available port.
type DatabaseConfig ¶
type DatabaseConfig struct {
DSN string `confx:"dsn" usage:"Database connection string" validate:"required"`
Debug bool `confx:"debug" usage:"Enable debug mode"`
Tracing TracingConfig `confx:"tracing" usage:"Tracing configuration"`
MaxIdleConns int `confx:"maxIdleConns" usage:"Maximum number of idle connections" validate:"ltefield=MaxOpenConns"`
MaxOpenConns int `confx:"maxOpenConns" usage:"Maximum number of open connections"`
ConnMaxLifetime time.Duration `confx:"connMaxLifetime" usage:"Maximum connection lifetime"`
ConnMaxIdleTime time.Duration `confx:"connMaxIdleTime" usage:"Maximum idle time for connections" validate:"ltefield=ConnMaxLifetime"`
AuthMethod AuthMethod `confx:"authMethod" usage:"Authentication method: 'password' or 'iam'" validate:"required,oneof=password iam"`
}
type Model ¶
type TestSuite ¶ added in v3.2.0
TestSuite provides a test environment with a PostgreSQL container, database connection, and lifecycle management for integration tests.
func MustStartTestSuite ¶ added in v3.2.0
func MustStartTestSuite(ctx context.Context, opts ...TestSuiteOption) *TestSuite
MustStartTestSuite creates and starts a new test suite, panicking on error. This is a convenience wrapper around StartTestSuite for test code that prefers panics.
func StartTestSuite ¶ added in v3.2.0
func StartTestSuite(ctx context.Context, opts ...TestSuiteOption) (*TestSuite, error)
StartTestSuite creates and starts a new test suite with PostgreSQL container. Configuration can be customized using TestSuiteOption functions.
func (*TestSuite) DSN ¶ added in v3.2.0
DSN returns the database connection string for the test suite.
type TestSuiteOption ¶ added in v3.2.0
type TestSuiteOption func(*testSuiteOptions)
TestSuiteOption configures TestSuite creation
func WithContainerConfig ¶ added in v3.2.0
func WithContainerConfig(config *ContainerConfig) TestSuiteOption
WithContainerConfig sets a custom container configuration
func WithProviders ¶ added in v3.2.0
func WithProviders(providers ...any) TestSuiteOption
WithProviders adds additional dependency injection providers
type TracingConfig ¶
type TracingConfig struct {
ExcludeQuery bool `confx:"excludeQuery" usage:"Exclude query"`
ExcludeQueryVars bool `confx:"excludeQueryVars" usage:"Exclude query vars"`
MaxQueryLength int `confx:"maxQueryLength" usage:"Maximum query length for tracing, 0 uses default (4096)"`
QueryFormatter func(query string) string `confx:"-" json:"-"`
Logger *kitlog.Logger `confx:"-" json:"-" inject:"optional"`
}