pgtest

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package pgtest gives a test package a real PostgreSQL, so that no test fakes the database.

The server comes from DATABASE_URL when it is set, otherwise from a testcontainers postgres:17-alpine started for the package. Each test package gets one schema of its own, created and set up from TestMain before any test and dropped after the last, so packages running in parallel never see each other; the tests of a package share it and isolate by their own data.

func TestMain(m *testing.M) {
	os.Exit(pgtest.Main(m, setup))
}

The package knows nothing about the schema it sets up: the caller passes the set-up, typically applying its migrations.

Index

Constants

View Source
const Image = "postgres:17-alpine"

Image is the PostgreSQL started when DATABASE_URL is not set.

Variables

This section is empty.

Functions

func Main

func Main(m *testing.M, setup func(ctx context.Context, pool *pgxpool.Pool) error, checks ...func(ctx context.Context, pool *pgxpool.Pool) error) int

Main creates the package schema, sets it up with setup through a pool whose search_path points at it, runs the tests, then runs each check on what the tests left in the schema, drops the schema and returns the exit code for os.Exit. A failing check fails the package as a failing test does.

func NewSchema

func NewSchema(t testing.TB) string

NewSchema creates an empty schema for one test, dropped when the test ends, and returns a connection string whose search_path points at it. Tests that need a database before any set-up, such as a migration runner's, use it.

func OtherCluster added in v0.3.0

func OtherCluster(t testing.TB) string

OtherCluster starts a second PostgreSQL server, a cluster of its own with its own system identifier and a transaction counter that starts from the beginning, and returns the connection string of its empty database. It is stopped when the test ends. Tests of what a logical dump or logical replication moves between clusters use it; it always needs Docker, since DATABASE_URL names the one server of the package.

func Pool

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

Pool returns the pool on the package schema that every test of the package shares. It is closed after the last test.

func URL

func URL(t testing.TB) string

URL returns the connection string of the package schema.

Types

This section is empty.

Jump to

Keyboard shortcuts

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