Testing fixtures

From constructive-io/pgsql-parser · View interactive version

Testing fixtures is an AI agent skill from the GitHub repository constructive-io/pgsql-parser. This skill documents how the fixture-based testing pipeline works in pgsql-parser and how to add new test fixtures.

How to use this skill

Skill file

pgsql-parser Fixtures & Testing System

This skill documents how the fixture-based testing pipeline works in pgsql-parser and how to add new test fixtures.

Architecture Overview

The testing pipeline has three layers:

  1. SQL fixture files — Raw SQL statements in __fixtures__/kitchen-sink/
  2. Generated fixture JSON — __fixtures__/generated/generated.json (individual statements keyed by path)
  3. Generated test files — packages/deparser/__tests__/kitchen-sink/*.test.ts

Directory Structure

__fixtures__/
  kitchen-sink/           # Source SQL fixture files
    latest/postgres/      # PostgreSQL regression test extracts
    misc/                 # Miscellaneous test cases (issues, features)
    original/             # Original upstream PostgreSQL test files
    pretty/               # Pretty-print specific test cases
  generated/
    generated.json        # Auto-generated: individual statements keyed by relative path
  plpgsql/                # PL/pgSQL fixture SQL files
  plpgsql-generated/
    generated.json        # Auto-generated: PL/pgSQL fixtures

packages/deparser/
  __tests__/
    kitchen-sink/         # Auto-generated test files from kitchen-sink fixtures
    misc/                 # Hand-written test files for specific features
    pretty/               # Pretty-print tests using PrettyTest utility
  scripts/
    make-fixtures.ts      # Splits SQL files -> generated.json
    make-kitchen-sink.ts  # Generates kitchen-sink test files
    statement-splitter.ts # Extracts individual statements from multi-statement SQL
  test-utils/
    index.ts              # Core test utilities (expectParseDeparse, FixtureTestUtils, TestUtils)
    PrettyTest.ts         # Pretty-print test helper
packages/plpgsql-deparser/
  scripts/
    make-fixtures.ts      # Extracts PL/pgSQL statements -> plpgsql-generated/generated.json
packages/transform-ast/
  scripts/
    make-kitchen-sink.ts  # Generates transform kitchen-sink tests
    test-ast.ts           # AST round-trip validation for transform

How Fixtures Work

Step 1: Write SQL fixture file

Create a .sql file in __fixtures__/kitchen-sink/. Organize by category:

Each SQL statement in the file becomes a separate test case. Use comments for context:

-- Brief description of what's being tested
-- Ref: constructive-io/pgsql-parser#289
SELECT EXTRACT(EPOCH FROM now());
SELECT EXTRACT(YEAR FROM TIMESTAMP '2001-02-16 20:38:40');

Step 2: Regenerate fixtures

From packages/deparser/:

# Generate generated.json from all kitchen-sink SQL files
npm run fixtures

# Generate kitchen-sink test files from generated.json
npm run fixtures:kitchen-sink

# Or do both at once:
npm run kitchen-sink

What make-fixtures.ts does:

What make-kitchen-sink.ts does:

Step 3: Run tests

# Run all deparser tests
cd packages/deparser && npx jest

# Run only kitchen-sink tests
npx jest __tests__/kitchen-sink/

# Run a specific fixture test
npx jest __tests__/kitchen-sink/misc-extract.test.ts

Test Verification

The FixtureTestUtils.runFixtureTests() method (via TestUtils.expectAstMatch()) performs this verification for each statement:

  1. Parse the original SQL -> AST
  2. Deparse (non-pretty) the AST -> SQL string
  3. Reparse the deparsed SQL -> new AST
  4. Compare the cleaned ASTs — they must match
  5. Repeat steps 2-4 with pretty: true

This ensures full round-trip fidelity: parse(sql) -> deparse(ast) -> parse(deparsed) === original AST

The expectParseDeparse() utility in test-utils/index.ts does the same thing for hand-written tests.

Pretty-Print Tests

Pretty-print tests use a different pattern via PrettyTest class in test-utils/PrettyTest.ts:

import { PrettyTest } from '../../test-utils/PrettyTest';

const testCases = [
  'pretty/misc-1.sql',
  'pretty/misc-2.sql',
];

const prettyTest = new PrettyTest(testCases);
prettyTest.generateTests();

This generates two tests per case (pretty and non-pretty) and uses Jest snapshots. Pretty tests read from generated.json and compare output via toMatchSnapshot().

Adding a New Fixture (Quick Reference)

  1. Create/edit a .sql file in __fixtures__/kitchen-sink/{category}/
  2. Run cd packages/deparser && npm run kitchen-sink
  3. Run npx jest to verify all tests pass
  4. Commit the .sql file, generated.json, and any new generated test files in __tests__/kitchen-sink/

PostgreSQL Regression Corpus (`__fixtures__/regress/`)

A ratchet over the SQL that libpg_query vendors from PostgreSQL (test/sql/postgres_regress,

test/sql/plpgsql_regress) plus its own test SQL (test/sql/deparse*, test/plpgsql_samples.sql):

(postgres_regress/arrays-12.sql), plus the libpg_query commit they came from. Built by

cd packages/deparser && npm run fixtures:regress -- /path/to/libpg_query. Check out the

x-publish.libpgQueryRepo at the x-publish.libpgQueryCommit of the pinned libpg-query release

first (both are in its package.json).

round-trip yet.

pretty and non-pretty, via TestUtils.expectAstMatch) and

packages/plpgsql-deparser/__tests__/regress-corpus.test.ts. They run in the normal test script.

A new failure fails the test, and so does a known failure that now passes. After fixing deparser

bugs (or bumping libpg-query and regenerating the corpus), refresh the lists with

UPDATE_KNOWN_FAILURES=1 npx jest __tests__/regress (deparser) and

UPDATE_KNOWN_FAILURES=1 npx jest __tests__/regress-corpus (plpgsql-deparser), then review the diff.

Bug fixes still get a kitchen-sink fixture; the corpus only guards against regressions.

PL/pgSQL Fixtures (`plpgsql-deparser`)

The PL/pgSQL deparser has its own fixture pipeline:

cd packages/plpgsql-deparser && npm run fixtures

This runs scripts/make-fixtures.ts which:

Transform Kitchen-Sink (`transform`)

The transform package has its own kitchen-sink and AST test scripts:

cd packages/transform-ast
npm run kitchen-sink   # generate transform-specific kitchen-sink tests
npm run test:ast       # run AST round-trip validation

make-kitchen-sink.ts writes __tests__/kitchen-sink/fixtures.json (fixture file

-> statement keys, shared by every version pair) plus one test file per adjacent

version pair (13-14.test.ts … 17-18.test.ts), each iterating the manifest with

it.each. Keep it that way: a suite per fixture file meant every fixture paid for

its own Jest module registry, generated.json parse, and six WASM parser inits.

Changing a Walker

@pgsql/transform drives its whole pipeline (schema mapping, routing, qualify,

role and extension transforms, round-trip validation) through the walkers in

@pgsql/traverse — see the ast-traversal skill. Its fixtures are therefore the

regression gate for any traversal change, including ones made in another package:

pnpm --filter @pgsql/traverse test
pnpm --filter plpgsql-parser test
pnpm --filter @pgsql/transform test   # fixtures + snapshots must be unchanged

A traversal change that alters fixture output is a behavior change, not a

refactor. Two failure modes to look for specifically:

are only found via the runtime schema, so a hand-rolled recursion silently drops

them and a fixture loses a rename.

one double-applies edits.

Never edit a fixture or snapshot to make a walker change pass.

Package Scripts Reference

`packages/deparser` (primary fixture pipeline)

| Script | Command | Description |

|--------|---------|-------------|

| fixtures | ts-node scripts/make-fixtures.ts | Regenerate generated.json |

| fixtures:kitchen-sink | ts-node scripts/make-kitchen-sink.ts | Regenerate kitchen-sink test files |

| kitchen-sink | npm run fixtures && npm run fixtures:kitchen-sink | Both steps combined |

| fixtures:ast | ts-node scripts/make-fixtures-ast.ts | Generate AST JSON fixtures |

| fixtures:sql | ts-node scripts/make-fixtures-sql.ts | Generate SQL fixtures via native deparse |

| fixtures:upstream-diff | ts-node scripts/make-upstream-diff.ts | Generate diff comparing upstream (libpg-query) vs our deparser output |

| test | jest | Run all tests |

| test:watch | jest --watch | Run tests in watch mode |

`packages/plpgsql-deparser`

| Script | Command | Description |

|--------|---------|-------------|

| fixtures | ts-node scripts/make-fixtures.ts | Extract PL/pgSQL fixtures to plpgsql-generated/generated.json |

`packages/transform-ast`

| Script | Command | Description |

|--------|---------|-------------|

| kitchen-sink | ts-node scripts/make-kitchen-sink.ts | Generate transform kitchen-sink tests |

| test:ast | ts-node scripts/test-ast.ts | AST round-trip validation |

`packages/parser`

| Script | Command | Description |

|--------|---------|-------------|

| prepare-versions | ts-node scripts/prepare-versions.ts | Generate version-specific sub-packages from config/versions.json |

| test:ast | ts-node scripts/test-ast.ts | AST round-trip validation |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.