When adding new test fixtures for the PL/pgSQL deparser, follow this workflow:
Add your PL/pgSQL function/procedure definitions to the appropriate fixture file in __fixtures__/plpgsql/. For deparser-specific fixes, use plpgsql_deparser_fixes.sql.
Example fixture:
-- Test N: Description of what this tests
CREATE FUNCTION test_example(p_input text, OUT result text)
LANGUAGE plpgsql AS $$
BEGIN
result := p_input;
RETURN;
END$$;Run the fixture generation script from the plpgsql-deparser package:
cd packages/plpgsql-deparser
pnpm fixturesThis script (scripts/make-fixtures.ts):
- Reads all
.sqlfiles from__fixtures__/plpgsql/ - Parses each file to extract PL/pgSQL statements (CREATE FUNCTION, CREATE PROCEDURE, DO blocks)
- Validates each statement can be parsed by the PL/pgSQL parser
- Outputs valid fixtures to
__fixtures__/plpgsql-generated/generated.json
Run the test suite to verify your fixtures round-trip correctly:
cd packages/plpgsql-deparser
pnpm testThe round-trip test (__tests__/plpgsql-deparser.test.ts):
- Loads all fixtures from
generated.json - For each fixture: parse -> deparse -> reparse
- Compares the AST from original parse with the AST from reparsed output
- Reports any failures (AST mismatches or reparse failures)
For important deparser fixes, add explicit test cases with snapshots to __tests__/deparser-fixes.test.ts:
it('should handle [description]', async () => {
const sql = `CREATE FUNCTION test_example(...)
LANGUAGE plpgsql AS $$
BEGIN
-- your test case
END$$`;
await testUtils.expectAstMatch('description', sql);
const parsed = parsePlPgSQLSync(sql) as unknown as PLpgSQLParseResult;
const deparsed = deparseSync(parsed);
expect(deparsed).toMatchSnapshot();
// Add specific assertions
expect(deparsed).toContain('expected output');
});Then run tests with snapshot update:
pnpm test --updateSnapshotAlways commit the fixture file, generated.json, test file, AND snapshots together:
git add __fixtures__/plpgsql/plpgsql_deparser_fixes.sql
git add __fixtures__/plpgsql-generated/generated.json
git add packages/plpgsql-deparser/__tests__/deparser-fixes.test.ts
git add packages/plpgsql-deparser/__tests__/__snapshots__/deparser-fixes.test.ts.snap
git commit -m "test: add fixtures for [description]"- The
generated.jsonfile is the source of truth for tests - it must be regenerated when fixtures change - Fixtures that fail PL/pgSQL parsing are skipped (logged as warnings during generation)
- The test suite has a
KNOWN_FAILING_FIXTURESset for fixtures with known issues - avoid adding to this unless necessary - When adding fixtures for new deparser features, ensure the fixture exercises the specific AST pattern you're testing
plpgsql_deparser_fixes.sql- Fixtures for deparser bug fixes and edge casesplpgsql_*.sql- PostgreSQL regression test fixtures (from upstream)- Each fixture should have a comment describing what it tests
- Number fixtures sequentially (Test 1, Test 2, etc.) within each file