Documentation
¶
Overview ¶
Package postgrestest provides real PostgreSQL containers for integration tests. It never substitutes a fake for PostgreSQL semantics.
Index ¶
Constants ¶
const ( // DefaultIsolationCleanupTimeout bounds the rollback that restores test // isolation after cancellation, panic, or goroutine termination. DefaultIsolationCleanupTimeout = 5 * time.Second )
Variables ¶
var ( // ErrNilIsolationCallback reports a missing isolated test body. ErrNilIsolationCallback = errors.New("postgrestest: isolation callback is nil") )
Functions ¶
func RunIsolated ¶
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 ¶
Open creates a PostgreSQL container, waits for readiness, obtains a DSN, and invokes the optional deterministic setup hook.
func (*Database) Container ¶
func (d *Database) Container() *testcontainerspostgres.PostgresContainer
Container exposes the native testcontainers PostgreSQL container.