testserver

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func ClearTables(ctx context.Context, client *postgres.Client, tables ...string)

ClearTables truncates the given tables and restarts identity columns (e.g. auto-increment sequences). Useful in test setup/teardown.

func DropTables

func DropTables(ctx context.Context, client *postgres.Client, tables ...string)

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

func NewServer(config Config) *Server

NewServer returns a new Server with the given configuration. Zero-valued config fields are populated with defaults.

func (*Server) Client

func (s *Server) Client() *postgres.Client

Client returns a new postgres.Client configured for this server.

func (*Server) Opts

func (s *Server) Opts() *postgres.Opts

Opts returns the postgres.Opts needed to connect to this server.

func (*Server) Shutdown

func (s *Server) Shutdown() error

Shutdown stops the Postgres instance and removes the data directory.

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

Start initializes and starts the Postgres instance. It creates the data directory via initdb, writes a tuned-for-testing configuration, and starts the server. The call blocks until Postgres is accepting connections.

func (*Server) WithLogger

func (s *Server) WithLogger(log *slog.Logger) *Server

Jump to

Keyboard shortcuts

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