postgrestest

package module
v0.0.0-...-eb8d15a Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

README

github.com/StevenACoffman/pgxtest

Reference Contributor Covenant

NOTE: This is a fork of https://github.com/zombiezen/postgrestest to use https://github.com/jackc/pgx

The readme below has not been updated to reflect recent code changes. PRs welcome!

TODO(steve): I would further like to make the changes in https://github.com/gokrazy/gus/commit/b97c652fd03754ba817bd3c13f18ea6e2e154ef4#diff-7829468e86c1cc5d5133195b5cb48e1ff6c75e3e9203777f6b2e379d9e4882b3 from https://michael.stapelberg.ch/posts/2024-11-19-testing-with-go-and-postgresql-ephemeral-dbs/

Package pgxtest provides a test harness that starts an ephemeral PostgreSQL server. It is tested on macOS, Linux, and Windows. It can cut down the overhead of PostgreSQL in tests up to 90% compared to spinning up a postgres Docker container: starting a server with this package takes roughly 650 milliseconds and creating a database takes roughly 20 milliseconds.

Example

func TestApp(t *testing.T) {
	// Start up the PostgreSQL server. This can take a few seconds, so better to
	// do it once per test run.
	ctx := context.Background()
	srv, err := postgrestest.Start(ctx)
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(srv.Cleanup)

	// Each of your subtests can have their own database:
	t.Run("Test1", func(t *testing.T) {
		db, err := srv.NewDatabase(ctx)
		if err != nil {
			t.Fatal(err)
		}
		if _, err := db.Exec(`CREATE TABLE foo (id SERIAL PRIMARY KEY);`); err != nil {
			t.Fatal(err)
		}
		// ...
	})

	t.Run("Test2", func(t *testing.T) {
		db, err := srv.NewDatabase(ctx)
		if err != nil {
			t.Fatal(err)
		}
		if _, err := db.Exec(`CREATE TABLE foo (id SERIAL PRIMARY KEY);`); err != nil {
			t.Fatal(err)
		}
		// ...
	})
}

Installation

PostgreSQL must be installed locally for this package to work. See the PostgreSQL Downloads page for instructions on how to obtain PostgreSQL for your operating system.

To install the package:

go get github.com/StevenACoffman/pgxtest

License

Apache 2.0

Documentation

Overview

Package postgrestest provides a test harness that starts an ephemeral PostgreSQL server. PostgreSQL must be installed locally for this package to work.

Example
var t *testing.T // passed into your testing function

// Start up the PostgreSQL server. This can take a few seconds, so better to
// do it once per test run.
ctx := context.Background()
srv, err := postgrestest.Start(ctx)
if err != nil {
	t.Fatal(err)
}
t.Cleanup(srv.Cleanup)

// Each of your subtests can have their own database:
t.Run("Test1", func(t *testing.T) {
	db, err := srv.NewPGXConn(ctx)
	if err != nil {
		t.Fatal(err)
	}
	if _, err := db.Exec(ctx, `CREATE TABLE foo (id SERIAL PRIMARY KEY);`); err != nil {
		t.Fatal(err)
	}
	// ...
})

t.Run("Test2", func(t *testing.T) {
	db, err := srv.NewPGXConn(ctx)
	if err != nil {
		t.Fatal(err)
	}
	if _, err := db.Exec(ctx, `CREATE TABLE foo (id SERIAL PRIMARY KEY);`); err != nil {
		t.Fatal(err)
	}
	// ...
})

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	// contains filtered or unexported fields
}

A Server represents a running PostgreSQL server.

func Start

func Start(ctx context.Context) (_ *Server, err error)

Start starts a PostgreSQL server with an empty database and waits for it to accept connections.

Start looks for the programs "pg_ctl" and "initdb" in PATH. If these are not found, then Start searches for them in /usr/lib/postgresql/*/bin, preferring the highest version found.

func (*Server) Cleanup

func (srv *Server) Cleanup()

Cleanup shuts down the server and deletes any on-disk files the server used.

func (*Server) CreateDatabase

func (srv *Server) CreateDatabase(ctx context.Context) (string, error)

CreateDatabase creates a new database on the server and returns its data source name.

func (*Server) DefaultDatabase

func (srv *Server) DefaultDatabase() string

DefaultDatabase returns the data source name of the default "postgres" database.

func (*Server) NewDatabaseDB

func (srv *Server) NewDatabaseDB(ctx context.Context) (*sql.DB, error)

NewDatabaseDB opens a connection to a freshly created database on the server.

func (*Server) NewPGXConn

func (srv *Server) NewPGXConn(ctx context.Context) (*pgx.Conn, error)

NewPGXConn opens a connection to a freshly created database on the server.

func (*Server) NewPGXPool

func (srv *Server) NewPGXPool(ctx context.Context) (*pgxpool.Pool, error)

NewPGXPool opens a connection pool to a freshly created database on the server.

Jump to

Keyboard shortcuts

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