Testing the database: pgTAP and v1 as data
Owner: @zuki · Written by the Backend Engineer · Last reviewed: 2026-10-12 · Conforms to the decisions through D32
How the database's rules are tested: pgTAP on the real local Supabase stack, for every rule and every business action, and v1's behaviour captured as data for the migration to match. The Backend Engineer keeps this guide; the decisions and the rules outrank it (how we work).
It is the database half of the Google doc [Guide] Testing v2: SQL tests, Playwright and staff testing, moved here on 2026-10-12 (#55, D28). Its section 1 now describes the pgTAP files packs B to E built (D12, D25).
Where each kind of test is
Section titled “Where each kind of test is”| Layer | Checks | Where it is written up |
|---|---|---|
| pgTAP | Every rule and every business action (each RPC); every error key asserted | This page |
| v1 behaviour as data | The inputs v1 took, and the rows and balances it produced | This page, then the data phase |
| Reconcile | v2's migrated data against v1's, every night | The database: reconcile |
| Playwright | The per-role menu check and D19's journeys | The Frontend Engineer moves it under build/frontend (#55) |
| Staff testing and go/no-go | Real people doing their real day on staging; the gate | Cutover weekend and rollback |
The rules themselves are pgTAP's. A Playwright journey checks what a page shows, never a rule a pgTAP test already proves (D19).
Where the tests are and how to run them
Section titled “Where the tests are and how to run them”-
Files: supabase/tests/database/NN_area.test.sql, numbered after the migration area they test, such as 17_rentals.test.sql.
-
Run on the local stack, after a reset when the schema changed:
Terminal window pnpm supabase db resetpnpm supabase test db -
A stack with no night loaded:
pnpm supabase test dbfails beside a data-phase night's rows, so runpnpm supabase db resetfirst (zuki, #306, 2026-11-04). -
Every file runs in its own transaction and is rolled back, so it leaves nothing behind and needs no reset afterwards (D12). The stack is the real Supabase one, Postgres 17, with Auth and Storage; there is no plain-Postgres stub (D25).
-
In CI, the database job (TL-M0-18) will run them on every pull request that touches supabase/ or tools/db/. What it must run is on the M0 page.
What gets a test
Section titled “What gets a test”- Every rule and every business action gets pgTAP tests in the same pull request that builds it (D19).
- Every error key gets a test that raises it and asserts the key (D10, D30).
- Every description starts with the ID of the rule it proves, for example
'R-RNT-01: a car can''t be booked twice'.pnpm docs:rulesfails when a test cites a rule that doesn't exist or is retired, or a built rule has no test (how rules are written). The rule is written first (D27). - The rule's Status moves from planned to built in the pull request that adds its test and its code.
The shape of a file
Section titled “The shape of a file”-- BE-M0-17 · Rentals (pack B).-- Each description starts with the ID of the rule it proves (D27).begin;select plan(14); -- the exact number of assertions below
-- fixtures: plain inserts, as the test's owner, with fixed uuids
select is(…, …, 'R-RNT-01: …');
select * from finish();rollback;- plan(n) must equal the number of assertions, or the file fails.
- Fixtures are inserted as the test's owner, which bypasses RLS and the status guards. Use fixed uuids so a failure names the same rows every time.
- Helpers for a file go in pg_temp, as in 21_rls.test.sql, and vanish with the transaction.
Acting as a user
Section titled “Acting as a user”A test acts as a login by setting the request's claims and switching to the authenticated role, then switches back:
select set_config('request.jwt.claims', json_build_object('sub', '<profile uuid>', 'role', 'authenticated')::text, true);select set_config('role', 'authenticated', true);-- the query under test, as that userselect set_config('role', 'none', true);select set_config('request.jwt.claims', '', true);21_rls.test.sql wraps this in pg_temp.count_as(), try_as() and val_as(): run a query as person n and return a count, the outcome or the first value. A person who holds one test role with exactly the permissions under test, at one pool or at all pools, shows what that one permission allows.
Asserting a refusal
Section titled “Asserting a refusal”A refusal raises its key as the error message (D10): SQLSTATE P0001 for a broken rule, from app.refuse(), and 42501 for a missing permission, from app.deny().
select throws_ok( $$ select app.set_vehicle_status(…) $$, '42501', 'vehicle.status_forbidden', 'R-VEH-08: a user with no status permission at the car''s pool can''t change its status');- A refused read is not an error. RLS hides the rows, so assert a count of zero.
- A refused write through RLS raises 42501 on insert, and changes zero rows on update or delete. try_as() returns either outcome as text, so one results_eq() can check several.
- The detail is a JSON object with the values the message needs (R-CONV-14); 12_foundation.test.sql shows how to read it.
The 44 acceptance checks
Section titled “The 44 acceptance checks”The delivered M0 had 44 checks in a plain-Postgres harness, which never entered the repository (D25). Each one now maps to a rule and its pgTAP tests; the table is on BE-M0-27, and the timeline's M0 exit check requires them all to pass.
v1 behaviour as data
Section titled “v1 behaviour as data”v1's behaviour is captured as data, not as Playwright specs against v1's pages (D19). In the data phase, each domain gets fixtures taken from the legacy snapshot: the inputs v1 took, and the rows and balances it produced from them. pgTAP tests and the reconcile checks compare v2 with these.
- v1's repair cases become data cases, checked by pgTAP or reconcile: overwritten and duplicate leads, the plate change, the rental anchor shift, double handover, swapped photos, ledger gaps and orphan bills.
- Fixtures hold personal data when they come from the snapshot. They stay out of the repository unless anonymized (D15); a test in supabase/tests uses made-up rows of the same shape.
- A v1 bug fix goes into the port log and arrives in v2 as a rule and its test (plan).
The E2E organization
Section titled “The E2E organization”A sealed test tenant in local and staging databases, from supabase/seed.sql (BE-M0-23): organization E2E Test, pools E2A (Jakarta) and E2B (Surabaya), legal entity E2E, one test bank account and its own document numbering. Never on production. Tests never touch a real organization, and the nightly rehearsal leaves it alone.
- Logins are ordinary accounts, one per role, made by
pnpm db:e2e-users(BE-M0-25); it refuses any URL that isn't the local stack or, with--staging, the staging project. It writes their emails and password to the git-ignored .env.e2e. - A test about a role signs in as that role, never as super_admin.
- Whether the E2E organization is removed from production after go-live or kept sealed is OQ14.
Troubleshooting
Section titled “Troubleshooting”| Problem | Fix |
|---|---|
| "Looks like you planned n tests but ran m" | plan(n) doesn't match the assertions. Count again. |
| "no plan" | The file lacks select plan(n); or select * from finish();. |
| A test passes alone but fails after another | A file must not depend on another's rows. Every file is rolled back; insert what it needs itself. |
| throws_ok fails with the right key but the wrong code | P0001 is app.refuse (a broken rule), 42501 is app.deny or RLS (no permission). Check which the rule's Refusal line calls for. |
pnpm docs:rules fails on a test |
The description cites an ID no rule defines, or a retired one. Fix the ID, or ask the Docs Engineer for the rule. |
| A query as a user sees every row | The role wasn't switched: the test is still the owner, which bypasses RLS. |