postgrestest

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 11 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 Start

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

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

func (*Database) Close

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

Close terminates the container. Failed termination may be retried; after a successful termination, later calls return nil without repeating it.

func (*Database) Container

func (d *Database) Container() *postgres.PostgresContainer

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.

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