testpostgres

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package postgres provides test helpers for integration tests that require a real PostgreSQL database. It is intended to be imported only from _test.go files or test binaries.

Usage:

func TestSomething(t *testing.T) {
    db := testpostgres.Open(t)
    // db is closed and all connections released when t ends.
}

Index

Constants

View Source
const (
	// EnvDatabaseURL is the environment variable used to locate the test database.
	// Set it to a postgres:// DSN before running integration tests.
	// Example: postgres://user:pass@localhost:5432/testdb?sslmode=disable
	EnvDatabaseURL = "TEST_DATABASE_URL"
)

Variables

This section is empty.

Functions

func NewIsolatedDatabase

func NewIsolatedDatabase(t *testing.T) *postgres.DB

NewIsolatedDatabase creates a fresh PostgreSQL database for the test and returns a pool connected to it. The database is dropped when t ends.

This gives each test a clean, isolated schema so tests can run in parallel without interfering with each other.

func Open

func Open(t *testing.T) *postgres.DB

Open opens a *postgres.DB for the test database specified by TEST_DATABASE_URL. If the environment variable is not set the test is skipped. The connection is automatically closed when t ends.

func RunMigrations

func RunMigrations(t *testing.T, db *postgres.DB, sources []migrate.Source)

RunMigrations applies migrations to db using the provided sources. It fails the test immediately if migrations fail.

Types

This section is empty.

Jump to

Keyboard shortcuts

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