postgrestest

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2020 License: LGPL-3.0 Imports: 7 Imported by: 0

README

Simple support for Go testing with a live Postgres database
----

This package makes it straightforward to write tests that connect
to a fresh Postgres database created for the tests.

Documentation

Overview

Package postgrestest provides a package intended for running tests which require a Postgres backend.

Index

Constants

This section is empty.

Variables

View Source
var ErrDisabled = errgo.New("postgres testing is disabled")

ErrDisabled is returned by New when postgres testing has been explicitly disabled.

Functions

func PgTestDisable added in v1.1.1

func PgTestDisable() bool

PgTestDisable returns whether Postgres should be disabled based on the PGTESTDISABLE environment variable.

Types

type DB

type DB struct {
	*sql.DB
	// contains filtered or unexported fields
}

DB holds a connection to a schema within a Postgres database. The schema is created by New and deleted (along with all the tables) when the DB is closed.

func New

func New() (*DB, error)

New connects to a Postgres instance and returns a database connection that uses a newly created schema with a random name. The PG* environment variables may be used to configure the connection parameters (see https://www.postgresql.org/docs/9.3/static/libpq-envars.html).

The returned DB instance must be closed after it's finished with.

If the environment variable PGTESTDISABLE is non-empty ErrDisabled will be returned.

If the environment variable PGTESTKEEPDB is non-empty, the name of the test schema will be printed and the data will not be deleted.

For optimal test performance, we recommend setting the following Postgres config values in your testing or development environment (BUT NEVER IN PRODUCTION):

fsync = off
synchronous_commit = off
full_page_writes = off

Be aware that these settings may lead to data loss and corruption. However, they should not have any negative impact on ephemeral tests.

func (*DB) Close

func (pg *DB) Close() error

Close removes the test database and closes the database connection. This method should not be called from multiple goroutines.

func (*DB) Schema

func (pg *DB) Schema() string

Schema returns the test schema name.

Jump to

Keyboard shortcuts

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