DatabaseAdapter.postgresTestDatabase constructor

DatabaseAdapter.postgresTestDatabase({
  1. String? host,
  2. int? port,
  3. String? database,
  4. String? user,
  5. String? password,
})

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 if null, and defaults to '127.0.0.1',
  • port, read from $PGPORT if null, and defaults to 5432,
  • database, read from $PGDATABASE if null, and defaults to 'postgres',
  • user, read from $PGUSER if null, and defaults to 'postgres',
  • password, read from $PGPASSWORD if null, 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,
    ));