dbtest

package
v0.0.0-...-956fd44 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package dbtest provides PostgreSQL test helpers: a real Postgres container and a fake server that rejects logins.

Index

Constants

View Source
const Role = "postgres"

Role is the test database's login user. Test connections assume it as their role, which PostgreSQL allows for any login.

Variables

This section is empty.

Functions

func CloneT

func CloneT(t *testing.T, templateConnStr string) string

CloneT creates an isolated database from a template prepared using Start and Migrate. Close all connections to the template before cloning it. Connections to the clone must close before its registered cleanup runs.

func LoginError

func LoginError(t *testing.T, ctx context.Context, address string) error

LoginError logs in to address and returns the *pgconn.ConnectError it fails with.

func Migrate

func Migrate(connStr string, vectorEnabled bool) error

Migrate installs pgvector for the test database when enabled, then runs the embedded migrations against connStr. Use MigrateT in tests that have a *testing.T; use Migrate in TestMain where no T is available.

func MigrateT

func MigrateT(t *testing.T, connStr string, vectorEnabled bool)

MigrateT runs the embedded migrations against connStr and calls t.Fatal on error. If vectorEnabled is true the vector pass is also applied.

func PoolConfig

func PoolConfig(connStr string) *pgxpool.Config

PoolConfig returns the pool configuration for connStr that assumes Role. It panics on a malformed connection string, which only a broken test can produce.

func RefuseConnections

func RefuseConnections(t *testing.T) string

RefuseConnections returns a local address with no listener, so a dial to it is refused.

func RejectLogins

func RejectLogins(t *testing.T, sqlstate string) string

RejectLogins starts a server that answers every login with a FATAL error carrying sqlstate, such as 28P01. It stops when t ends.

func Start

func Start(ctx context.Context) (connStr string, cleanup func(), err error)

Start starts a pgvector Postgres container and returns the connection string and a cleanup function. Callers are responsible for calling cleanup when done.

func StartT

func StartT(ctx context.Context, t *testing.T) string

StartT starts a pgvector Postgres container and registers cleanup with t.Cleanup. Suitable for use in individual tests or test helpers that have a *testing.T.

Types

This section is empty.

Jump to

Keyboard shortcuts

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