Help us shape the future of Go! Take the 2026 Go Developer Survey

dbtest

package
v1.0.0-rc.12 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package dbtest supplies isolated PostgreSQL clones for database-backed tests. A suite owns its template and accepts initialization from the schema's owner.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Count

func Count(t testing.TB, db *database.DB, want int, q string, args ...any)

Count fails the test unless q, a single-count query, answers want. It is for storage tests whose guarantee is the stored representation; tests of behaviour ask the owner instead.

func FailNextStatement

func FailNextStatement(t testing.TB, db *database.DB, fragment string)

FailNextStatement makes the next statement whose text contains fragment fail, for the rest of the test: the fault a test injects to prove a failed write or read is reported, never mistaken for success.

func Restore

func Restore(ctx context.Context, db *database.DB, schemaSQL string, rows []Row) error

Restore makes db hold a release's database: its public schema emptied, the release's own schema run, and its rows put back with triggers, foreign-key checks among them, switched off while they load (session_replication_role). That is a load exemption, not a deferral: nothing checks the rows afterwards, so they must be what Snapshot read from a consistent database. A generated column is left to compute itself.

func Shape

func Shape(ctx context.Context, db *database.DB) (string, error)

Shape is a database's public schema as text: every column with its type, nullability, default and generation; every constraint with its definition; every index with its definition. Two databases with the same Shape have the same schema, whatever order their objects were made in.

func WaitForLock

func WaitForLock(t testing.TB, db *database.DB, query string)

WaitForLock observes that the named statement is blocked in PostgreSQL. Tests use it to order concurrent operations without guessing a sleep duration.

func WriteRows

func WriteRows(path string, rows []Row) error

WriteRows keeps rows at path as gzipped JSON lines, one row each.

Types

type Row

type Row struct {
	Table string          `json:"table"`
	Row   json.RawMessage `json:"row"`
}

Row is one table row as a release left it: the table and the row as JSON (row_to_json), so a later build can put it back without that release's code.

func ReadRows

func ReadRows(path string) ([]Row, error)

ReadRows reads rows WriteRows kept.

func Snapshot

func Snapshot(ctx context.Context, db *database.DB, skip func(table string) bool) ([]Row, error)

Snapshot reads every row of every table in the public schema, sorted, except the tables skip names. It is how an upgrade fixture freezes a release's database; Restore puts it back.

type Suite

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

func NewSuite

func NewSuite(initialize func(context.Context, string) (*database.DB, error)) *Suite

func (*Suite) Close

func (s *Suite) Close() error

Close waits for background drops, then releases the suite's template.

func (*Suite) New

func (s *Suite) New(t testing.TB) *database.DB

func (*Suite) NewWith

func (s *Suite) NewWith(t testing.TB, wrap func(net.Conn) net.Conn) *database.DB

NewWith is New with each of the database's connections passed through wrap (nil for none): for a test of how the pool copes with a connection that behaves as one on a loaded machine does.

func (*Suite) Run

func (s *Suite) Run(m *testing.M) int

Run preserves the suite's eight-fixture default; an explicit -parallel wins.

Jump to

Keyboard shortcuts

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