Documentation
¶
Overview ¶
Package pgtest gives each test its own throwaway PostgreSQL database.
Database creates an empty database named pgtest_<random> on the server behind an admin URL, returns a URL pointing at it and drops it when the test ends. Pass the URL to vukemtest as DATABASE_URL to run an app test against real PostgreSQL instead of the default in-memory SQLite:
func TestCreatePost(t *testing.T) {
app := vukemtest.New(t, bootstrap.New, vukemtest.Options{
Env: map[string]string{"DATABASE_URL": pgtest.Database(t, "")},
})
// ...
}
The server must be running: Database fails the test with a clear message when it cannot connect, so guard Postgres-only tests with testing.Short or a build tag if they must be optional.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Database ¶
Database creates an empty database on the server at adminURL, returns its URL and drops it in tb.Cleanup after terminating its connections. adminURL is a postgres:// or postgresql:// URL (keyword/value DSNs such as "host=... dbname=..." are rejected) whose role may CREATE DATABASE; when empty it is read from TEST_DATABASE_URL, then POSTGRES_URL. The server must be PostgreSQL 13 or later. Any failure fails the test.
Register Database before anything that opens connections to the new database (such as vukemtest.New) so those close first: cleanups run in reverse order.
Types ¶
This section is empty.