pgtest

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package pgtest gives integration tests a real, migrated Postgres database.

Each call to New creates a fresh database on the server at TEST_DATABASE_URL and drops it when the test ends, so tests never share rows and can run in parallel. The database is cloned from a template migrated once per set of migrations, so a test pays for a copy, not for running every migration again:

func TestCreateUser(t *testing.T) {
    pool := pgtest.New(t, migrations.FS)
    users := repositories.NewUserRepository(sqlc.New(pool))
    ...
}

Without TEST_DATABASE_URL the test is skipped. Set REQUIRE_DB_TESTS=1 in CI so a missing database fails the run instead of skipping it silently.

Index

Constants

View Source
const (
	// EnvURL points at a Postgres server the tests may create databases on,
	// for example postgres://postgres:postgres@localhost:5432/postgres.
	EnvURL = "TEST_DATABASE_URL"

	// EnvRequire, set to 1, turns a missing EnvURL into a failure instead of
	// a skip.
	EnvRequire = "REQUIRE_DB_TESTS"
)

Variables

This section is empty.

Functions

func New

func New(t testing.TB, migrations fs.FS) *pgxpool.Pool

New returns a pool connected to a new database with every migration in migrations applied (see postgres.Migrate). The pool is closed and the database dropped when t ends.

Types

This section is empty.

Jump to

Keyboard shortcuts

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