postgrestest

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package postgrestest provides real PostgreSQL containers for integration tests. It never substitutes a fake for PostgreSQL semantics.

Index

Constants

View Source
const (
	// DefaultIsolationCleanupTimeout bounds the rollback that restores test
	// isolation after cancellation, panic, or goroutine termination.
	DefaultIsolationCleanupTimeout = 5 * time.Second
)

Variables

View Source
var (
	// ErrNilIsolationCallback reports a missing isolated test body.
	ErrNilIsolationCallback = errors.New("postgrestest: isolation callback is nil")
)

Functions

func RunIsolated added in v1.0.1

func RunIsolated(
	ctx context.Context,
	beginner IsolationBeginner,
	fn func(context.Context, pgx.Tx) error,
) (err error)

RunIsolated executes fn once in a transaction that is always rolled back. It is suitable only when the code under test does not commit, open other connections, execute non-transactional DDL, or depend on external side effects. Callback and rollback errors remain inspectable through errors.Is and errors.As. A panic triggers bounded rollback before the original value is re-panicked, and goroutine termination cannot skip rollback. A cleanup panic cannot replace a returned callback error or either terminal callback path. With no earlier callback cause, a rollback panic propagates rather than reporting successful isolation.

Types

type Config

type Config struct {
	Image    string
	Database string
	Username string
	Password string
	// HostPort optionally binds PostgreSQL to a stable loopback port. It is
	// useful for stop/start tests whose client endpoint must not move.
	HostPort string
	// CleanupTimeout bounds container termination even when the setup context
	// has already been canceled.
	CleanupTimeout time.Duration
	// Setup runs once after startup. An error, panic, or goroutine termination
	// triggers bounded container cleanup without replacing the original cause.
	Setup func(context.Context, string) error
}

Config controls an isolated PostgreSQL container. Setup runs exactly once after PostgreSQL is accepting connections and receives its complete DSN. Setup errors, panics, or goroutine termination trigger bounded container cleanup. The original setup panic is preserved even if termination also panics.

type Database

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

Database owns a PostgreSQL test container and its connection string.

func Open added in v1.1.0

func Open(ctx context.Context, config Config) (*Database, error)

Open creates a PostgreSQL container, waits for readiness, obtains a DSN, and invokes the optional deterministic setup hook.

func Start deprecated

func Start(ctx context.Context, config Config) (*Database, error)

Start delegates to Open.

Deprecated: use Open to make resource acquisition explicit.

func (*Database) Close deprecated

func (d *Database) Close(ctx context.Context) error

Close delegates to Shutdown.

Deprecated: use Shutdown for complete bounded owned shutdown.

func (*Database) Container

Container exposes the native testcontainers PostgreSQL container.

func (*Database) DSN

func (d *Database) DSN() string

DSN returns the complete pgx-compatible connection string. Treat it as a secret because it contains the configured test credentials.

func (*Database) Shutdown added in v1.1.0

func (d *Database) Shutdown(ctx context.Context) error

Shutdown starts one caller-independent, bounded container termination and lets each caller wait with its own context. Repeated calls return the shared terminal termination result.

type IsolationBeginner added in v1.0.1

type IsolationBeginner interface {
	Begin(context.Context) (pgx.Tx, error)
}

IsolationBeginner is implemented by pgx.Conn, pgxpool.Pool, pgxpool.Conn, and pgx.Tx values that can begin a transaction.

Jump to

Keyboard shortcuts

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