testdb

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: AGPL-3.0 Imports: 26 Imported by: 0

Documentation

Overview

Package testdb gives integration tests a fresh, migrated Postgres database.

The first call starts Postgres 18 with testcontainers (or uses the server in REPUTECAST_TEST_DATABASE_URL, which is faster locally: point it at the docker compose Postgres), migrates a template database once, and then clones it per test with CREATE DATABASE … TEMPLATE, which takes a few ms. Each run first drops what finished runs left behind (see sweep).

Index

Constants

This section is empty.

Variables

View Source
var Image = "postgres:18-alpine"

Image is the Postgres image used when no server URL is configured.

Functions

func Empty

func Empty(t testing.TB) string

Empty creates an empty database (no migrations) and returns its URL; it is dropped when the test ends. For tests of migrating itself.

func New

func New(t testing.TB) *pgxpool.Pool

New returns a pool connected to a fresh database cloned from the migrated template. The database is dropped when the test ends.

func NewFrom

func NewFrom(t testing.TB, template string) *pgxpool.Pool

NewFrom is New cloning template, a database made by Template ("" for the migrated template).

func Shared

func Shared(ctx context.Context, key string, fill func(pool *pgxpool.Pool, dir string) error) (name, dir string, err error)

Shared is Template filled once per `go test` run rather than once per test binary: the first binary to ask fills it, the others wait for it and reuse it. fill writes the database through pool and any files into dir, which the caller copies for each clone. key names the fill and must change with anything that changes what fill writes.

func Template

func Template(ctx context.Context, key string, fill func(*pgxpool.Pool) error) (string, error)

Template builds a database on top of the migrated template with fill (the demo data, say) and returns its name for NewFrom. Call it once per test binary: cloning is a few milliseconds where filling may take seconds.

func URL

func URL(pool *pgxpool.Pool) string

URL returns the connection URL of the database behind pool's config.

Types

This section is empty.

Jump to

Keyboard shortcuts

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