SQL and PostgreSQL
trimtest runs SQL tests in two forms and selects them from the static graph only (no recorded tier). The safety rules apply: what it cannot trace runs more tests, never fewer.
| Form | How it is recognised | Command |
|---|---|---|
| pgTAP | the script calls plan(), no_plan(), runtests() or finish() |
pg_prove <files> |
| Plain script | any other test script | psql -X -q -v ON_ERROR_STOP=1 -f <file>, one process per file |
A script is a test when its name ends in _test.sql, _tests.sql or .test.sql, starts with test_, sits in
a test/, tests/ or t/ directory (not under fixtures/, data/, helpers/, support/, lib/ and similar, and
not called setup*, teardown* or schema*), or is a pgTAP script. Add [tests] globs for
other conventions.
Detection
Section titled “Detection”The directory above the test/, tests/ or t/ folder is the project root, and pg_prove and psql run there.
A project is detected when a script in such a folder is a pgTAP test or has a test-looking name. A plain script in
tests/ with neither only counts when no other project (Python, Ruby, …) shares its root, because those
repositories keep SQL fixtures there. Force a project with:
[[project]]kind = "sql"dir = "db"The database
Section titled “The database”trimtest never creates, migrates or drops a database. Tests run against whatever the usual PG* variables
(PGHOST, PGPORT, PGDATABASE, PGUSER, PGPASSWORD, PGSERVICE) or a ~/.pgpass entry describe, exactly as if
you had typed the command. A failed connection makes psql or pg_prove exit non-zero, so those tests count as
failed. To run inside a container, prefix the commands:
[runner]sql = "docker compose exec -T db"What selects what
Section titled “What selects what”A file provides the objects it creates, alters, drops or writes to: create table users, alter function f,
insert into items, create index .. on t, comment on and grant .. on. It depends on every identifier it
mentions, including names inside strings and function bodies (has_table('users')). Tests that name users are
selected when a file providing users changes, and so are tests of files that reference it in turn (a foreign key or
a view over the table).
\i pathand\ir pathare file edges.\iis relative to the project root (where psql runs),\irto the including file.COPY .. FROM 'file'and\copy .. from 'file'with a literal path make the script depend on that file.- Comments and whitespace do not count as a change. Strings, quoted identifiers, dollar-quoted bodies (
$$ .. $$), psql meta-command lines and/*+ hint */comments do. - A script that runs as
begin; .. rollback;(the pgTAP idiom) leaves nothing behind, so other files do not depend on its objects. Temp objects are never providers, and a test that does not roll back still provides what it creates. - A new test always runs.
What runs everything
Section titled “What runs everything”migrations/,migrate/,db/migrate/,deploy/,revert/andverify/(sqitch) below the project root.sqitch.plan,sqitch.conf,.proverc,pg_prove.conf,.psqlrc,schema.sqlandstructure.sql.
What makes a file depend on everything
Section titled “What makes a file depend on everything”Anything the static graph cannot see makes the file depend on every non-test file of the project, so a change
anywhere selects what uses it: EXECUTE of built SQL (EXECUTE format(..), but not GRANT EXECUTE or
EXECUTE FUNCTION in a trigger), \gexec, and a variable used as an include path (\i :file).
Limits
Section titled “Limits”- Static only. A name used only through
search_pathtricks, a role-dependent default, or an extension’s own objects is not traced; the same name in two files links the test to both. - A SQL file read by another project’s tests (a seed loaded by a Python test) still selects those tests: they are matched by file name as for any data file.
- Results come from the exit status. A failing
pg_proverun marks every file in that command as failed.