testcontainerz

package
v19.1.0-rc2 Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetupPostgres

func SetupPostgres(t testing.TB, name string, migrationsFS fs.FS) *pgxpool.Pool

func SetupRedis

func SetupRedis(t testing.TB) url.URL

Types

type PostgresContainer

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

PostgresContainer wraps a single Postgres testcontainer with one or more pre-migrated template databases. Each call to WithLogicalDB(s) clones every template into a fresh logical database via CREATE DATABASE ... TEMPLATE, giving every test its own isolated schemas without paying the cost of starting a new container or rerunning migrations.

The container terminates when the testing.TB passed to NewPostgresContainer cleans up. Individual logical databases are dropped when their test ends so disk usage in the container stays bounded by concurrent (not cumulative) test count.

func NewPostgresContainer

func NewPostgresContainer(t testing.TB, schemas ...Schema) *PostgresContainer

NewPostgresContainer starts a Postgres container and creates one template database per schema, named "<schema.Name>_template", applying the schema's migrations to it. Subsequent WithLogicalDB(s) calls clone these templates.

At least one schema is required. Typically called once from a testify suite's SetupSuite.

func (*PostgresContainer) WithLogicalDB

func (c *PostgresContainer) WithLogicalDB(t *testing.T, fn func(t *testing.T, pool *pgxpool.Pool))

WithLogicalDB is a shorthand for WithLogicalDBs when the container holds a single schema.

func (*PostgresContainer) WithLogicalDBs

func (c *PostgresContainer) WithLogicalDBs(t *testing.T, fn func(t *testing.T, pools map[string]*pgxpool.Pool))

WithLogicalDBs clones every template into a freshly named database, opens a pgxpool.Pool against each, and invokes fn with pools keyed by Schema.Name. When fn returns, every pool is closed and every database is dropped. Cleanups run LIFO, so any cleanups the test registers inside fn (e.g. tx rollback) run while the pools are still open.

type Schema

type Schema struct {
	// Name is used both as the migration tracking key (passed to
	// pgtool.RunMigrations) and as the lookup key for the cloned pool returned
	// by WithLogicalDBs. Must be unique within a container.
	Name string
	// Migrations is the FS of migration files applied to the template.
	Migrations fs.FS
}

Schema describes one template database to create on a PostgresContainer.

Jump to

Keyboard shortcuts

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