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

pgtest

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: AGPL-3.0 Imports: 21 Imported by: 0

Documentation

Overview

Package pgtest gives a test its own Postgres database, migrated to the store's current Postgres schema, with connections as the roles the hosted build runs as. Tests run on Postgres when MAILRULES_TEST_POSTGRES_DSN names a superuser connection, and on SQLite otherwise.

The schema is migrated once into a template database named after a hash of the migration files, so a changed migration gets a new template; each test's database is a copy of it, dropped when the test ends. The package must not import the store: the store's own tests use it.

Index

Constants

View Source
const (
	AsApp    = appRole
	AsWorker = workerRole
	AsOwner  = ownerRole
)

Roles a test can sign in to its database as, by URL.

View Source
const EnvDSN = "MAILRULES_TEST_POSTGRES_DSN"

EnvDSN names the environment variable holding the superuser DSN.

Variables

This section is empty.

Functions

func DSN

func DSN() string

DSN is MAILRULES_TEST_POSTGRES_DSN, a superuser DSN; empty means tests run on SQLite.

Types

type DB

type DB struct {
	// App is connected as mailrules_test_app, a member of mailrules_app: what the store runs on.
	App *sql.DB
	// Worker is connected as mailrules_test_worker, a member of mailrules_worker.
	Worker *sql.DB
	// Admin is connected as the DSN's superuser, which row-level security does not apply
	// to. Its queries may use SQLite's ? and ?N placeholders: they are rewritten to $n
	// (outside quotes and comments), so tests' raw SQL written for SQLite runs unchanged.
	Admin *sql.DB
	// Name is the database's name.
	Name string
}

DB is one test's own database, made from a template migrated to the current Postgres set.

func Open

func Open(t testing.TB) *DB

Open makes the test's database and connects to it; it is dropped when the test ends. Callers check DSN first: Open fails the test when it is empty.

func (*DB) URL

func (d *DB) URL(role string) string

URL is a postgres:// URL to the test's database signed in as role (AsApp, AsWorker or AsOwner): what the daemon is given in MAILRULES_*DATABASE_URL.

Jump to

Keyboard shortcuts

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