Connected apps with 3rd-party data hosts jsr.io/@civility
civility docs spec testing.md
2.3 kB · 79 lines
Markdown
at main

Testing #

Postgres #

For testing the postgresql adapter, we need to be running a database server.

If you already have a database setup, simply add PG_TEST_URL to your environment, or to root/.env. If this environment variable is not set, PG tests will simply be skipped.

Initializing a Postgres DB #

If you don't have a postgres database available, you will have to set one up.

1. Download PG #

Follow instructions from https://www.postgresql.org/download/

For macOS, I am using homebrew:

brew install postgresql@18 # macOS, using homebrew

2. Add data path #

Add a location for storing data to ~/.zprofile or ~/.zshrc:

export PGDATA="~/.data/pg"

3. Create a database for Civility #

initdb # initialize a database
pg_ctl -D ~/.data/pg -l logfile start # start the database server

# Optionally, add a user:
createuser ben -P
createdb -U postgres -O ben my-password

# Or, just create db as superuser:
createdb civility # add a db to the database

4. Add the access url to the .env file #

cp .env.example .env
# Edit .env:
PG_TEST_URL=postgres://ben:my-password@localhost:5432/civility

5. Run the tests #

# Full suite (includes postgres tests)
deno task test

# Or just the hono package with postgres tests, skipping fmt/lint
PG_TEST_URL=postgres://civility_test:civility_test@localhost:5432/civility_test \
  deno test packages/hono -A --quiet

Each test run calls runMigrations(db) before executing, so the schema is created automatically on first run and migration idempotency is verified by the runMigrations is idempotent test.

4. Tear down (optional) #

The tests use random UUID prefixes for all user/app IDs so runs are isolated and additive. You can drop the database between major schema changes:

dropdb -U postgres civility
createdb -U postgres -O ben my-password

Notes #

  • PostgreSQL 14+ recommended — the adapter uses DISTINCT ON, ON CONFLICT DO UPDATE, and RETURNING, all standard since 9.5.
  • Tests skip cleanly when PG_TEST_URL is unset — CI passes without a database.
  • The rate_limits table is shared between auth rate limiting (login/MFA) and sync rate limiting; cleanupExpiredAuth prunes both by expiry.