Documentation
¶
Overview ¶
Package dbtest provides PostgreSQL test helpers: a real Postgres container and a fake server that rejects logins.
Index ¶
- Constants
- func CloneT(t *testing.T, templateConnStr string) string
- func LoginError(t *testing.T, ctx context.Context, address string) error
- func Migrate(connStr string, vectorEnabled bool) error
- func MigrateT(t *testing.T, connStr string, vectorEnabled bool)
- func PoolConfig(connStr string) *pgxpool.Config
- func RefuseConnections(t *testing.T) string
- func RejectLogins(t *testing.T, sqlstate string) string
- func Start(ctx context.Context) (connStr string, cleanup func(), err error)
- func StartT(ctx context.Context, t *testing.T) string
Constants ¶
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 ¶
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 ¶
LoginError logs in to address and returns the *pgconn.ConnectError it fails with.
func Migrate ¶
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 ¶
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 ¶
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 ¶
RefuseConnections returns a local address with no listener, so a dial to it is refused.
func RejectLogins ¶
RejectLogins starts a server that answers every login with a FATAL error carrying sqlstate, such as 28P01. It stops when t ends.
Types ¶
This section is empty.