pgtest

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package pgtest provides a shared real-Postgres test harness usable from any package in the module (core/migrate, cmd/gofastr, …) without importing framework/internal/testdb, which is import-restricted to the framework tree.

Resolution order (memoised once per process):

  1. TEST_POSTGRES_DSN env var — point CI at an existing server.
  2. testcontainers postgres:16-alpine — spun up on demand (needs Docker).
  3. neither reachable → tests call t.Skip via DB/DSN.

Each DB(t) hands back a connection scoped to a unique schema (search_path), so concurrent tests don't collide, with cleanup registered on t.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BaseDSN

func BaseDSN(t *testing.T) string

BaseDSN returns the resolved base DSN (the maintenance/default database), or skips the test if Postgres is unreachable. Use for EnsureDatabase / CLI round-trips that need a raw connection string.

func DB

func DB(t *testing.T) *sql.DB

DB returns a *sql.DB scoped to a fresh, uniquely-named schema (via search_path) on the shared Postgres, or skips if Postgres is unreachable. The schema and connection are dropped/closed on t.Cleanup.

func FreshDatabaseDSN

func FreshDatabaseDSN(t *testing.T) string

FreshDatabaseDSN creates a uniquely-named database on the shared Postgres and returns a URL DSN pointing at it, dropped on t.Cleanup. Use for CLI / tooling tests that connect by URL string and expect to own the database (vs DB(t), which schema-scopes a shared one). Skips if Postgres is unreachable, or if the base DSN isn't URL-form.

func RedactDSN

func RedactDSN(dsn string) string

RedactDSN masks the password in a DSN for safe logging.

func UnusedDSN

func UnusedDSN(t *testing.T) (string, func())

UnusedDSN returns a URL DSN pointing at a uniquely-named database that does NOT yet exist, plus a cleanup that drops it if something created it. Use to test database-creation paths (EnsureDatabase / migrate up --create-db). Skips if Postgres is unreachable or the base DSN isn't URL-form.

Types

This section is empty.

Jump to

Keyboard shortcuts

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