Documentation
¶
Overview ¶
Package testserver runs a lightweight Postgres server for testing.
A Server manages the full lifecycle of a temporary Postgres instance: initialization, configuration, startup, and shutdown. The data directory is removed on shutdown, ensuring test isolation.
RunWithPostgres is a convenience function for TestMain that starts a server, runs migrations, executes tests, and cleans up:
func TestMain(m *testing.M) {
testserver.RunWithPostgres(ctx, m, logger, &client,
extensionLoader, extensionDirectory,
migrationLoader, migrationDirectory,
)
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClearTables ¶
ClearTables truncates the given tables and restarts identity columns (e.g. auto-increment sequences). Useful in test setup/teardown.
func DropTables ¶
DropTables drops the migration tracking table and each of the given tables if they exist.
func RunWithPostgres ¶
func RunWithPostgres( ctx context.Context, m *testing.M, logger *slog.Logger, client **postgres.Client, extensionLoader migrations.FileLoader, extensionDirectory string, migrationLoader migrations.FileLoader, migrationDirectory string, )
RunWithPostgres starts a temporary Postgres instance, runs migrations, executes the test suite, then shuts everything down. It writes the connected client to the provided pointer so tests can access it. Intended for use in TestMain.
Types ¶
type Config ¶
type Config struct {
Host string
Port int
User string
Database string
Password string
MaxConns int
DataDirectory string
}
Config holds configuration for a test Postgres Server.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server controls the lifecycle of a temporary Postgres instance.
func NewServer ¶
NewServer returns a new Server with the given configuration. Zero-valued config fields are populated with defaults.
func (*Server) Client ¶
Client returns a new postgres.Client configured for this server.
func (*Server) Opts ¶
Opts returns the postgres.Opts needed to connect to this server.