pgtest

package
v0.6.38 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package pgtest runs a throwaway PostgreSQL server for one test binary.

A suite that claims to hold on PostgreSQL has to have run on PostgreSQL. So this finds the server binaries installed on the machine, makes a cluster in a fresh temporary directory, and starts it listening on a Unix socket in that directory and nowhere else. Nothing is mocked and nothing is shared with another run.

When a machine has no server to find, Start says so instead of failing: a suite reports the backend it did not reach by name, which is a fact a reader can act on, and still runs everything else.

A store built on orm/tenant runs its own tests on both engines with this — start one server in TestMain, make its login with tenant.Provision, and open the store on a schema of the test's own:

func TestMain(m *testing.M) {
	pg, why = pgtest.Start()
	code := m.Run()
	if pg != nil {
		pg.Stop()
	}
	os.Exit(code)
}

Index

Constants

View Source
const Super = "postgres"

Super is the cluster's superuser. Tests use it only to make roles and databases; a statement under test never runs as it, because a superuser ignores row-level security and would prove nothing.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	// Major is the server's major version.
	Major int
	// contains filtered or unexported fields
}

Server is one running cluster.

func Start

func Start() (*Server, string)

Start makes and starts a cluster from the newest PostgreSQL on this machine. It returns the reason instead when there is none, or when it will not start.

func (*Server) DSN

func (s *Server) DSN(user, db string) string

DSN opens a connection as user to database db over the cluster's socket.

func (*Server) Exec

func (s *Server) Exec(db string, stmts ...string) error

Exec runs each statement as the superuser in database db, in order.

func (*Server) Open

func (s *Server) Open(user, db string) (*sql.DB, error)

Open returns a pool connected as user to database db.

func (*Server) Stop

func (s *Server) Stop()

Stop shuts the server down and removes the cluster.

Jump to

Keyboard shortcuts

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