postgrestest

package
v0.0.0-...-124bb09 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2024 License: MIT Imports: 22 Imported by: 1

Documentation

Overview

Package postgrestest creates a temporary Postgres instance. Tests should call New(). Other code should call NewInstance() or NewInstanceWithOptions().

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(t testing.TB) string

New creates a new Postgres instance and returns a connection string URL in the form "postgresql:..." to connect to using sql.Open(). After the test completes, the Postgres instance will be shut down. New will call t.Fatal if an error happens initializing Postgres. See NewInstanceWithOptions for more details.

Types

type Instance

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

Instance contains the state of a new temporary Postgres instance.

func NewInstance

func NewInstance() (*Instance, error)

NewInstance calls NewInstanceWithOptions() with the default options. The caller must call Close() to ensure it is stopped and the temporary space is deleted. Tests should prefer to call New().

func NewInstanceWithOptions

func NewInstanceWithOptions(options Options) (*Instance, error)

NewInstanceWithOptions creates a new Postgres instance in a temporary directory. The caller must call Close() to ensure it is stopped and the temporary space is deleted. Tests should prefer to call New().

Postgres will use the "C" locale to ensure that tests don't depend on the local environment.

func (*Instance) BinPath

func (i *Instance) BinPath(commandName string) string

BinPath returns the absolute path to commandName in the Postgres binary directory.

func (*Instance) Close

func (i *Instance) Close() error

Close shuts down Postgres and deletes the temporary directory.

func (*Instance) LocalhostURL

func (i *Instance) LocalhostURL() string

LocalhostURL returns the Postgres connection URL using a localhost TCP socket. This will only work if using Options.ListenOnLocalhost=true. Most callers should use URL() instead.

func (*Instance) RemoteURL

func (i *Instance) RemoteURL() string

RemoteURL returns the firs Postgres connection URL using a localhost TCP socket. This will only work if using Options.ListenOnLocalhost=true. Most callers should use URL() instead.

func (*Instance) RemoteURLForAddress

func (i *Instance) RemoteURLForAddress(address string) string

func (*Instance) URL

func (i *Instance) URL() string

URL returns the Postgres connection URL using a Unix socket in the form "postgresql://...". See: https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING

type Options

type Options struct {
	// If true, Postgres will listen on localhost for network connections.
	ListenOnLocalhost bool

	// If not nil, verbose information will be logged.
	Logger *slog.Logger

	// If not 0, listen globally on InsecureGlobalPort. This is insecure because it will allow
	// connections from any IP address, although it will require a password. The default Postgres
	// port is 5432. InsecureGlobalPort cannot be set together with ListenOnLocalhost, since this
	// overrides ListenOnLocalhost, so just set this option.
	InsecureGlobalPort int

	// Set Postgres's shared_buffers for the buffer pool cache in bytes. See:
	// https://www.postgresql.org/docs/current/runtime-config-resource.html
	SharedBuffers int

	// Create or use Postgres in this directory. If empty, it will create a temporary directory
	// that will be deleted when done. If this is set, the directory will not be deleted.
	DirPath string
}

Options configures the Postgres instance.

Jump to

Keyboard shortcuts

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