pgsql-parser-testing
Original:🇺🇸 English
Translated
Test the pgsql-parser repository (SQL parser/deparser). Use when working in the pgsql-parser repo, fixing deparser issues, running parser tests, or validating SQL round-trips. Scoped specifically to the constructive-io/pgsql-parser repository.
1installs
Added on
NPX Install
npx skill4agent add constructive-io/constructive-skills pgsql-parser-testingTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →PGSQL Parser Testing
Testing workflow for the pgsql-parser repository. This skill is scoped specifically to the monorepo.
constructive-io/pgsql-parserWhen to Apply
Use this skill when:
- Working in the pgsql-parser repository
- Fixing deparser or parser issues
- Running parser/deparser tests
- Validating SQL round-trip correctness
- Adding new SQL syntax support
Repository Structure
pgsql-parser/
packages/
parser/ # SQL parser (libpg_query bindings)
deparser/ # SQL deparser (AST to SQL)
plpgsql-parser/ # PL/pgSQL parser
plpgsql-deparser/ # PL/pgSQL deparser
types/ # TypeScript type definitions
utils/ # Utility functions
traverse/ # AST traversal utilities
transform/ # AST transformation utilitiesTesting Strategy
The pgsql-parser uses AST-level equality for correctness, not string equality:
parse(sql1) → ast1 → deparse(ast1) → sql2 → parse(sql2) → ast2While textually, a correct round-trip means .
sql2 !== sql1ast1 === ast2Key Principle
Exact SQL string equality is not required. The focus is on comparing resulting ASTs. Use (deparser) or (ast package) to validate correctness.
expectAstMatchexpectPGParseDevelopment Workflow
Initial Setup
bash
pnpm install
pnpm buildRunning Tests
Run all tests:
bash
pnpm testRun tests for a specific package:
bash
cd packages/deparser
pnpm testWatch mode for rapid iteration:
bash
cd packages/deparser
pnpm test:watchRun a specific test:
bash
pnpm test --testNamePattern="specific-test-name"Fixing Deparser Issues
Systematic Approach
-
One test at a time: Focus on individual failing testsbash
pnpm test --testNamePattern="specific-test" -
Always check for regressions: After each fix, run full test suitebash
pnpm test -
Build before testing: Always rebuild after code changesbash
pnpm build && pnpm test -
Clean commits: Stage files explicitlybash
git add packages/deparser/src/specific-file.ts
Workflow Loop
Make changes → pnpm build → pnpm test --testNamePattern="target" → pnpm test (full) → commitTest Utilities
Deparser Tests
Location:
packages/deparser/test-utils/index.tstypescript
import { expectAstMatch } from '../test-utils';
it('deparses SELECT correctly', () => {
expectAstMatch('SELECT * FROM users');
});AST Package Tests
Location:
packages/ast/test/utils/index.tsUses database deparser for validation:
typescript
import { expectPGParse } from '../test/utils';
it('round-trips through database deparser', async () => {
await expectPGParse('SELECT * FROM users WHERE id = 1');
});Note: AST tests require the database to have function available.
deparser.expressions_arrayCommon Commands
| Command | Description |
|---|---|
| Build all packages |
| Run all tests |
| Run tests in watch mode |
| Run linter |
| Clean build artifacts |
Package-Specific Testing
Parser Package
Tests libpg_query bindings and SQL parsing:
bash
cd packages/parser
pnpm testDeparser Package
Tests AST-to-SQL conversion:
bash
cd packages/deparser
pnpm testPL/pgSQL Packages
Tests PL/pgSQL parsing and deparsing:
bash
cd packages/plpgsql-parser
pnpm test
cd packages/plpgsql-deparser
pnpm testDebugging Tips
-
Use isolated debug scripts for complex issues (don't commit them)
-
Check the AST structure when tests fail:typescript
import { parse } from 'pgsql-parser'; console.log(JSON.stringify(parse('SELECT 1'), null, 2)); -
Compare ASTs visually to understand differences:typescript
const ast1 = parse(sql1); const ast2 = parse(deparse(ast1)); console.log('Original:', JSON.stringify(ast1, null, 2)); console.log('Round-trip:', JSON.stringify(ast2, null, 2));
Troubleshooting
| Issue | Solution |
|---|---|
| Tests fail after changes | Run |
| Type errors | Check |
| Shared code changes | Rebuild dependent packages |
| Snapshot mismatches | Review changes, update with |
Important Notes
- Changes to or
typespackages may require rebuilding dependent packagesutils - Each package can be developed and tested independently
- The project uses Lerna for monorepo management
- Always verify no regressions before committing
References
- Deparser testing docs:
packages/deparser/TESTING.md - Quoting rules:
packages/deparser/QUOTING-RULES.md - Deparser usage:
packages/deparser/DEPARSER_USAGE.md - PL/pgSQL deparser:
packages/plpgsql-deparser/AGENTS.md