Documentation
¶
Overview ¶
Package dbtest spins up disposable databases for integration tests using testcontainers. NewPostgres starts a Postgres container, opens a connection, optionally applies migrations, and registers teardown with t.Cleanup.
pg := dbtest.NewPostgres(ctx, t, dbtest.Config{Migrations: migrationsFS})
repo := widget.NewStore(pg.DB)
Importing this package pulls in testcontainers and the Docker client, so it is intended for test binaries only. Tests using it should be guarded with `if testing.Short() { t.Skip(...) }` since they require a Docker daemon.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Image is the container image (default "postgres:17-alpine").
Image string
// Database, User, Password name the bootstrapped database and superuser
// (defaults "servicekit", "postgres", "postgres").
Database string
User string
Password string
// Migrations, when non-nil, is applied with goose after the database is
// ready — typically an embed.FS of `*.sql` files.
Migrations fs.FS
// StartupTimeout bounds waiting for the database to accept connections
// (default 60s).
StartupTimeout time.Duration
}
Config tunes the Postgres container. The zero value is valid and uses the defaults documented on each field.
type Postgres ¶
type Postgres struct {
// DB is an open, migrated connection pool to the container.
DB *sqlx.DB
// Config is an sqldb.Config pointing at the container, suitable for handing
// to application code that opens its own pool.
Config sqldb.Config
}
Postgres is a running Postgres test container with an open connection.
func NewPostgres ¶
NewPostgres starts a Postgres container, opens a connection, applies cfg.Migrations (when set), and registers cleanup. It fails the test on any error. Skip the calling test under `go test -short`, as it needs Docker.