postgres

package
v0.7.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: 13 Imported by: 0

Documentation

Overview

Package postgres provides opt-in PostgreSQL acceptance helpers. Callers supply only their isolated test database. Passing tests remove only their own schemas; failed tests retain them for inspection. No helper resets or drops a database.

Index

Constants

View Source
const (
	URLVariable         = "FOUNDRY_TEST_POSTGRES_URL"
	RequiredVariable    = "FOUNDRY_TEST_POSTGRES_REQUIRED"
	KeepSchemasVariable = "FOUNDRY_TEST_KEEP_SCHEMAS"
)

Variables

This section is empty.

Functions

func Config

func Config(t testing.TB) postgres.Config

Config reads the explicitly opted-in URL. Ordinary test runs skip without it; the required acceptance command fails if it is missing. Errors omit credentials.

func KeepSchemas added in v0.7.0

func KeepSchemas(t testing.TB) bool

KeepSchemas reports whether test-owned PostgreSQL artifacts must be retained. Call it during cleanup so subtest and already-run cleanup failures are included. A cleanup registered before the namespace runs after its drop and cannot retain it.

func Namespace

func Namespace(t testing.TB, db *database.DB) string

Namespace creates a unique schema in the caller's isolated test database. The returned identifier contains only safe ASCII letters, digits and underscores. Cleanup removes only this schema when the test passes and KeepSchemas is false. The caller owns db and its closure. Register application cleanup later so LIFO cleanup shuts it down before schema cleanup.

func Open

func Open(t testing.TB, adjust ...func(*postgres.Config)) *database.DB

Open owns one test pool and registers bounded cleanup immediately. Optional adjustments operate on this test's independent settings before construction.

func PrepareNamespace added in v0.7.0

func PrepareNamespace(t testing.TB) (name string, create func(*database.DB))

PrepareNamespace reserves a generated name and registers cleanup before any application or pool cleanup. Call create once, with a pool in the configured test database, when setup is ready to create the schema. Until then cleanup owns nothing. This permits assertions that startup does not create schemas.

Types

type Scope

type Scope struct {
	// contains filtered or unexported fields
}

Scope is one owned namespace in the explicitly opted-in test database. Configuration is immutable; each opened pool is independent and bounded.

func Isolate

func Isolate(t testing.TB) *Scope

Isolate allocates only a fresh schema and removes it after a passing test, unless KeepSchemas is true. The administration pool closes before this returns. Later pool/application cleanups run first in testing's LIFO cleanup order.

func (*Scope) Config

func (s *Scope) Config() postgres.Config

Config returns a caller-owned typed configuration for this owned scope. Scope creation uses the explicit URL, so no mutable TLS pointer is shared.

func (Scope) Format

func (Scope) Format(s fmt.State, _ rune)

Format writes a fixed label without exposing scope configuration.

func (*Scope) Open

func (s *Scope) Open(t testing.TB, options ...database.Option) *database.DB

Open owns a standalone scoped pool for fixtures/assertions. Prefer an app's actual database when testing application behavior or transaction ownership.

func (*Scope) Schema

func (s *Scope) Schema() string

Schema returns the test schema name, or an empty string for a nil scope.

Directories

Path Synopsis
Package application binds test-owned PostgreSQL scopes to production application settings.
Package application binds test-owned PostgreSQL scopes to production application settings.

Jump to

Keyboard shortcuts

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