DatabaseAdapter.postgresTestDatabase constructor
DatabaseAdapter.postgresTestDatabase({})
Create an ephemeral postgres database for testing.
This assumes that a postgres server is running and admin priviledges are available when connecting with:
host
, read from$PGHOST
ifnull
, and defaults to'127.0.0.1'
,port
, read from$PGPORT
ifnull
, and defaults to5432
,database
, read from$PGDATABASE
ifnull
, and defaults to'postgres'
,user
, read from$PGUSER
ifnull
, and defaults to'postgres'
,password
, read from$PGPASSWORD
ifnull
, and defaults to'postgres'
.
This will connect to postgres, use CREATE DATABASE "testdb-<uuid>"
to
create an empty database for testing, and return a DatabaseAdapter for
that database. When close is called the test database will be deleted.
You can launch a postgres database for local testing with:
docker run \
-ti --rm \
-e POSTGRES_PASSWORD=postgres \
-p 127.0.0.1:5432:5432 \
postgres:17
If running tests on Github Actions you can add a postgres service to your workflows jobs with:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:17
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
steps:
- ...
See Github Actions documentation on creating PostgreSQL service containers for details.
Implementation
factory DatabaseAdapter.postgresTestDatabase({
String? host,
int? port,
String? database,
String? user,
String? password,
}) =>
futureDatabaseAdapter(postgresTestingDatabaseAdapter(
host: host,
port: port,
database: database,
user: user,
password: password,
));