testdb

package
v0.0.0-...-19ca9d9 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package testdb provides helpers for creating isolated PostgreSQL databases in integration tests. Each test gets its own database with migrations applied, and the database is automatically dropped when the test completes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateTestDB

func CreateTestDB(ctx context.Context, t testing.TB, conf DBConfig, opts ...Option) (*sqlx.DB, error)

CreateTestDB provisions an isolated PostgreSQL database for a single test.

It creates a uniquely-named database, optionally applies migrations, and returns a connection to it. The database is automatically dropped when the test completes.

Types

type DBConfig

type DBConfig struct {
	Host     string
	Port     string
	User     string
	Password string
	DBName   string
}

DBConfig holds the connection parameters for a PostgreSQL database.

func ParseDSN

func ParseDSN(dsn string) (DBConfig, error)

ParseDSN parses a PostgreSQL DSN into a DBConfig.

func (DBConfig) DSN

func (dc DBConfig) DSN() string

DSN returns a PostgreSQL connection string.

type Option

type Option func(*createConfig)

Option customises the behaviour of CreateTestDB.

func WithMigrationsPath

func WithMigrationsPath(path string) Option

WithMigrationsPath overrides the default migrations directory.

func WithSkipMigrations

func WithSkipMigrations() Option

WithSkipMigrations skips running migrations — useful for testing missing-table error paths.

Jump to

Keyboard shortcuts

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