Documentation
¶
Overview ¶
Package dbtest resolves the PostgreSQL database that integration tests run against.
Tests do not choose between an embedded and an external server themselves; they call ForT / ForGinkgo (or Open, for a framework-agnostic handle) and get whichever the environment provides:
COMMONS_DB_URL connect to this server instead of embedding one
COMMONS_DB_CREATE "false" uses COMMONS_DB_URL as-is; anything else
(including unset) carves out a fresh database
When COMMONS_DB_URL is unset, dbtest starts or reuses the persistent embedded PostgreSQL server under ~/.config/commons-db and checks out an isolated database from its cross-process pool.
Index ¶
Constants ¶
const ( EnvURL = "COMMONS_DB_URL" EnvCreate = "COMMONS_DB_CREATE" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is a resolved test database. The handle accessors are lazy and memoised, so a test that only needs a DSN never opens a pool. They do not return errors: a database that cannot be reached is a dead test, so they abort through the failure handler the constructing adapter installed.
func ForGinkgo ¶
ForGinkgo resolves a database for a Ginkgo spec and registers cleanup with DeferCleanup.
Call it from BeforeAll or BeforeEach — DeferCleanup binds the teardown to whichever node is running, so a BeforeAll resolution is torn down after the containing Ordered container rather than after each spec.
func ForT ¶
ForT resolves a database for a standard library / testify test and registers cleanup on t.
func Open ¶
Open resolves a database according to the environment. The returned cleanup closes every handle the DB produced and drops the database if Open created one; callers own it.
Handle accessors on the returned DB panic on failure. ForT and ForGinkgo install their framework's failure handler instead.
type Options ¶
type Options struct {
// Name seeds the database name. Required.
Name string
// DataDir overrides where an embedded server keeps its cluster. Ignored
// when COMMONS_DB_URL is set. Defaults to ~/.config/commons-db.
DataDir string
// LogName labels the gorm SQL logger. Defaults to Name.
LogName string
}
Options configures how a test database is resolved.