Documentation
¶
Index ¶
- Constants
- Variables
- type GolangMigrateLogger
- type GooseLogger
- type Informer
- func GetMongoDatabase(tb testing.TB, dsn string, opt ...Option) (*mongo.Database, Informer)
- func GetMySQLConn(tb testing.TB, dsn string, opt ...Option) (*sql.DB, Informer)
- func GetPgxPool(tb testing.TB, dsn string, opt ...Option) (*pgxpool.Pool, Informer)
- func GetPqConn(ctx context.Context, tb testing.TB, dsn string, opt ...Option) (*sql.DB, Informer)
- func GetSQLConn(tb testing.TB, driver, dsn string, opt ...Option) (*sql.DB, Informer)
- type MigrateFactory
- type Migrator
- type Option
- func WithConnectDatabase(connectDatabase string) Option
- func WithDockerEnv(dockerEnv []string) Option
- func WithDockerImage(dockerImage string) Option
- func WithDockerPort(dockerPort int) Option
- func WithDockerRepository(dockerRepository string) Option
- func WithDockerSocketEndpoint(dockerSocketEndpoint string) Option
- func WithLogger(logger ctxlog.ILogger) Option
- func WithMigrations(migrationsDir string, migrateFactory MigrateFactory) Option
- func WithMode(mode RunMode) Option
- func WithPrepareCleanUp(prepareCleanUp PrepareCleanUp) Option
- func WithRetryTimeout(retryTimeout time.Duration) Option
- func WithTotalRetryDuration(totalRetryDuration time.Duration) Option
- func WithUnsetProxyEnv(unsetProxyEnv bool) Option
- type PrepareCleanUp
- type RunMode
Constants ¶
const ( // DefaultRetryTimeout is the default retry timeout. DefaultRetryTimeout = time.Second * 3 // DefaultTotalRetryDuration is the default total retry duration. DefaultTotalRetryDuration = time.Second * 30 )
const ( // DefaultMongoDSN - default mongodb connection string. DefaultMongoDSN = "mongodb://testuser:secret@127.0.0.1:27017/testdb?authSource=admin" // DefaultMySQLDSN - default mysql connection string. DefaultMySQLDSN = "root:secret@tcp(127.0.0.1:3306)/test_db" // DefaultPostgresDSN - default postgres connection string. DefaultPostgresDSN = "postgres://postgres:secret@127.0.0.1:5432/postgres?sslmode=disable" )
Variables ¶
var ( // GooseMigrateFactoryPGX is a migrator for https://github.com/pressly/goose with pgx driver. GooseMigrateFactoryPGX = GooseMigrateFactory(goose.DialectPostgres, "pgx") // GooseMigrateFactoryPQ is a migrator for https://github.com/pressly/goose with pq driver. GooseMigrateFactoryPQ = GooseMigrateFactory(goose.DialectPostgres, "postgres") // GooseMigrateFactoryMySQL is a migrator for https://github.com/pressly/goose with mysql driver. GooseMigrateFactoryMySQL = GooseMigrateFactory(goose.DialectMySQL, "mysql") )
Functions ¶
This section is empty.
Types ¶
type GolangMigrateLogger ¶
type GolangMigrateLogger struct {
// contains filtered or unexported fields
}
GolangMigrateLogger is a logger for golang-migrate.
func NewGolangMigrateLogger ¶
func NewGolangMigrateLogger(l ctxlog.ILogger) *GolangMigrateLogger
NewGolangMigrateLogger creates a new golang-migrate logger.
func (*GolangMigrateLogger) Printf ¶
func (g *GolangMigrateLogger) Printf(format string, v ...any)
Printf logs a message.
func (*GolangMigrateLogger) Verbose ¶
func (g *GolangMigrateLogger) Verbose() bool
Verbose returns true.
type GooseLogger ¶
type GooseLogger struct {
// contains filtered or unexported fields
}
GooseLogger is a logger for goose.
func NewGooseLogger ¶
func NewGooseLogger(t testing.TB, l ctxlog.ILogger) *GooseLogger
NewGooseLogger creates a new goose logger.
func (GooseLogger) Fatalf ¶
func (l GooseLogger) Fatalf(format string, v ...any)
Fatalf logs a fatal error.
func (GooseLogger) Printf ¶
func (l GooseLogger) Printf(format string, v ...any)
Printf logs a message.
type Informer ¶ added in v2.1.0
type Informer interface { // DSN returns the real database connection string. DSN() string // Host returns the host of the database server. Host() string // Port returns the port of the database server. Port() int // DatabaseName returns the database name for testing. DatabaseName() string }
Informer interface for database information.
func GetMongoDatabase ¶
GetMongoDatabase initializes a test MongoDB database, applies migrations, and returns a database connection.
func GetMySQLConn ¶ added in v2.0.2
GetMySQLConn inits a test mysql database, applies migrations. Use user root for docker test database.
func GetPgxPool ¶
GetPgxPool inits a test postgresql (pgx driver) database, applies migrations, and returns pgx connection pool to the database.
func GetPqConn ¶
GetPqConn inits a test postgresql (pq driver) database, applies migrations, and returns sql connection to the database.
func GetSQLConn ¶
GetSQLConn inits a test database, applies migrations, and returns sql connection to the database. driver: https://go.dev/wiki/SQLDrivers. Do not forget to import corresponding driver package.
type MigrateFactory ¶
type MigrateFactory func(t testing.TB, dsn string, migrationsDir string, logger ctxlog.ILogger) (Migrator, error)
MigrateFactory creates a new migrator.
func GooseMigrateFactory ¶
func GooseMigrateFactory(dialect goose.Dialect, driver string) MigrateFactory
GooseMigrateFactory creates a new migrator for https://github.com/pressly/goose.
type Migrator ¶
Migrator interface for applying migrations.
func GolangMigrateFactory ¶
func GolangMigrateFactory(_ testing.TB, dsn, migrationsDir string, logger ctxlog.ILogger) (Migrator, error)
GolangMigrateFactory creates a new migrator for https://github.com/golang-migrate/migrate.
type Option ¶
type Option func(*testDB)
Option option for creating a test database.
func WithConnectDatabase ¶
WithConnectDatabase sets the name of the database to connect to. The default will be take from the DSN.
func WithDockerEnv ¶
WithDockerEnv sets the environment variables for the docker container. The default is empty.
func WithDockerImage ¶
WithDockerImage sets the name of the docker image. The default is `latest`.
func WithDockerPort ¶
WithDockerPort sets the port for connecting to database in docker. The default is the port from the DSN.
func WithDockerRepository ¶
WithDockerRepository sets the name of docker hub repository. Required for RunModeDocker or RunModeAuto with empty environment variable TESTDOCK_DSN_[DRIVER].
func WithDockerSocketEndpoint ¶
WithDockerSocketEndpoint sets the docker socket endpoint for connecting to the docker daemon. The default is autodetect.
func WithLogger ¶
WithLogger sets the logger for the test database. The default is logger from testing.TB.
func WithMigrations ¶
func WithMigrations(migrationsDir string, migrateFactory MigrateFactory) Option
WithMigrations sets the directory and factory for the migrations.
func WithPrepareCleanUp ¶
func WithPrepareCleanUp(prepareCleanUp PrepareCleanUp) Option
WithPrepareCleanUp sets the function for prepare to delete temporary test database. The default is empty, but `GetPgxPool` and `GetPqConn` use it to automatically apply cleanup handlers to disconnect all users from the database before cleaning up.
func WithRetryTimeout ¶
WithRetryTimeout sets the timeout for connecting to the database. The default is 3 second. Must be less than totalRetryDuration.
func WithTotalRetryDuration ¶ added in v2.0.3
WithTotalRetryDuration sets the total retry duration. The default is 30 seconds. Must be greater than retryTimeout.
func WithUnsetProxyEnv ¶
WithUnsetProxyEnv unsets the proxy environment variables. The default is false.
type PrepareCleanUp ¶
PrepareCleanUp - function for prepare to delete temporary test database. For example, disconnect users.
type RunMode ¶
type RunMode int
RunMode defines the run mode of the test database.
const ( // RunModeUnknown - unknown run mode RunModeUnknown RunMode = 0 // RunModeDocker - run the tests in docker RunModeDocker RunMode = 1 // RunModeExternal - run the tests in external database RunModeExternal RunMode = 2 // RunModeAuto - checks the environment variable TESTDOCK_DSN_[DRIVER]. If it is set, // then RunModeExternal, otherwise RunModeDocker. // If TESTDOCK_DSN_[DRIVER] is set and RunModeAuto, WithDSN option is ignored. // For example, for postgres pgx driver: // TESTDOCK_DSN_PGX=postgres://postgres:secret@localhost:5432/postgres&sslmode=disable RunModeAuto RunMode = 3 )