pgtest

package
v0.0.0-...-6d4bf48 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2023 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package pgtest provides support functions for tests that need to use Postgres. Most clients will just call NewTx; those that need more control can call NewDB.

func TestSimple(t *testing.T) {
    dbtx := pgtest.NewTx(t)
    ...
}

func TestComplex(t *testing.T) {
    _, db := pgtest.NewDB(t, pgtest.SchemaPath)
    ...
    dbtx, err := db.Begin(ctx)
    ...
}

Prefer NewTx when the caller (usually a test function) can run in exactly one transaction. It's significantly faster than NewDB.

Index

Constants

View Source
const DefaultURL = "postgres:///postgres?sslmode=disable"

DefaultURL is used by NewTX and NewDB if DBURL is the empty string.

Variables

View Source
var (
	// DBURL should be a URL of the form "postgres://...".
	// If it is the empty string, DefaultURL will be used.
	// The functions NewTx and NewDB use it to create and connect
	// to new databases by replacing the database name component
	// with a randomized name.
	DBURL = os.Getenv("DB_URL_TEST")

	// SchemaPath is a file containing a schema to initialize
	// a database in NewTx.
	SchemaPath = os.Getenv("CHAIN") + "/core/schema.sql"
)

Functions

func CloneDB

func CloneDB(ctx context.Context, baseURL string) (newURL string, err error)

CloneDB creates a new database, using the database at the provided URL as a template. It returns the URL of the database clone.

func Dump

func Dump(t testing.TB, dbURL string, includeSchema bool, excludingTables ...string) string

Dump performs a full pg_dump of the data in the database at the provided URL.

func Exec

func Exec(ctx context.Context, db pg.DB, t testing.TB, q string, args ...interface{})

Exec executes q in the database or transaction in ctx. If there is an error, it fails t.

func NewDB

func NewDB(f Fataler, schemaPath string) (url string, db *sql.DB)

NewDB creates a database initialized with the schema in schemaPath. It returns the resulting *sql.DB with its URL.

It also registers a finalizer for the DB, so callers can discard it without closing it explicitly, and the test program is nevertheless unlikely to run out of connection slots in the server.

Prefer NewTx whenever the caller can do its work in exactly one transaction.

func NewTx

func NewTx(f Fataler) *sql.Tx

NewTx returns a new transaction on a database initialized with the schema in SchemaPath.

It also registers a finalizer for the Tx, so callers can discard it without rolling back explicitly, and the test program is nevertheless unlikely to run out of connection slots in the server. The caller should not commit the returned Tx; doing so will prevent the underlying database from being reused and so cause future calls to NewTx to be slower.

func WrapDB

func WrapDB(t testing.TB, url string, wrapFn func(string)) *sql.DB

WrapDB opens a new connection to the database at the provided URL, but with a driver that calls wrapFn on every driver.Stmt.Exec call and driver.Stmt.Query call.

It also registers a finalizer for the DB, so callers can discard it without closing it explicitly.

Types

type Fataler

type Fataler interface {
	Fatal(...interface{})
}

Fataler lets NewTx and NewDB signal immediate failure. It is satisfied by *testing.T, *testing.B, and *log.Logger.

Jump to

Keyboard shortcuts

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