Documentation
¶
Overview ¶
Package pgtest gives each test its own PostgreSQL database on the Docker PostgreSQL server started with `docker compose up -d --wait` (ADR-0028).
func TestInsertUser(t *testing.T) {
pool := pgtest.New(t, pgtest.WithMigrations(migrations.FS))
store := repository.NewUserStore(pool)
// ...
}
The server URL comes from GORBITAL_TEST_DATABASE_URL. When it is unset, tests are skipped with instructions; set GORBITAL_REQUIRE_DB=1 (as CI does) to fail them instead.
Migrations are applied once per distinct set of files into a template database, and each test's database is cloned from it, so tests stay fast and fully isolated. Old templates remain until `docker compose down -v`.
Stability: stable, for tests only: the API follows the compatibility promise; what the helpers do inside a test may change (ADR-0015, ADR-0054).
Index ¶
Constants ¶
const ( EnvURL = "GORBITAL_TEST_DATABASE_URL" EnvRequire = "GORBITAL_REQUIRE_DB" )
Environment variables read by this package.
Variables ¶
This section is empty.
Functions ¶
func New ¶
New creates a database for this test and returns a pool connected to it. When the test ends, the pool is closed and the database dropped.
func NewDatabase ¶
NewDatabase creates a database for this test and returns its connection URL, for tests that start a whole application from configuration. The database is dropped when the test ends; close every connection first. EnvURL must be in URL form (postgres://…).
Types ¶
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
An Option configures New and NewDatabase.
func WithMaxConns ¶
WithMaxConns sets the returned pool's size. Default: 4. NewDatabase ignores it.
func WithMigrations ¶
WithMigrations applies the goose migrations in fsys to the new database.