Skip to content

Commit 30f0030

Browse files
Replace expectAstMatch with expectParseDeparse helper for direct AST comparison
- Add standalone expectParseDeparse helper function in test-utils/index.ts (not on class) - Implement round-trip validation: parse(sql1) → deparse(sql2) → parse(sql2) → compare cleanTree(ast1) vs cleanTree(ast2) - Replace expectAstMatch usage with expectParseDeparse in all pretty formatting tests: - SELECT pretty tests - CREATE TABLE pretty tests - Constraints pretty tests - CREATE POLICY pretty tests - Use direct expect(ast2).toEqual(ast1) comparison for semantic validation - All tests pass with simplified, more direct AST validation approach Co-Authored-By: Dan Lynch <[email protected]>
1 parent 82877d0 commit 30f0030

File tree

5 files changed

+35
-36
lines changed

5 files changed

+35
-36
lines changed

packages/deparser/__tests__/pretty/constraints-pretty.test.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { deparseSync } from '../../src';
22
import { parse } from 'libpg-query';
3-
import { TestUtils } from '../../test-utils';
3+
import { expectParseDeparse } from '../../test-utils';
44

55
describe('Pretty constraint formatting', () => {
66
const foreignKeyConstraintSql = `ALTER TABLE products ADD CONSTRAINT fk_category FOREIGN KEY (category_id) REFERENCES categories(id) ON UPDATE CASCADE ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED;`;
@@ -56,17 +56,14 @@ describe('Pretty constraint formatting', () => {
5656
});
5757

5858
it('should validate AST equivalence between original and pretty-formatted SQL', async () => {
59-
const testUtils = new TestUtils();
6059
const testCases = [
61-
{ name: 'foreign key constraint', sql: foreignKeyConstraintSql },
62-
{ name: 'check constraint', sql: checkConstraintSql },
63-
{ name: 'complex table with constraints', sql: complexTableSql }
60+
foreignKeyConstraintSql,
61+
checkConstraintSql,
62+
complexTableSql
6463
];
6564

66-
for (const testCase of testCases) {
67-
const originalParsed = await parse(testCase.sql);
68-
const prettyFormatted = deparseSync(originalParsed, { pretty: true });
69-
await testUtils.expectAstMatch(`pretty-${testCase.name}`, prettyFormatted);
65+
for (const sql of testCases) {
66+
await expectParseDeparse(sql, { pretty: true });
7067
}
7168
});
7269
});

packages/deparser/__tests__/pretty/create-policy-pretty.test.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { deparseSync } from '../../src';
22
import { parse } from 'libpg-query';
3-
import { TestUtils } from '../../test-utils';
3+
import { expectParseDeparse } from '../../test-utils';
44

55
describe('Pretty CREATE POLICY formatting', () => {
66
const basicPolicySql = `CREATE POLICY user_policy ON users FOR ALL TO authenticated_users USING (user_id = current_user_id());`;
@@ -50,17 +50,14 @@ describe('Pretty CREATE POLICY formatting', () => {
5050
});
5151

5252
it('should validate AST equivalence between original and pretty-formatted SQL', async () => {
53-
const testUtils = new TestUtils();
5453
const testCases = [
55-
{ name: 'basic CREATE POLICY', sql: basicPolicySql },
56-
{ name: 'complex CREATE POLICY', sql: complexPolicySql },
57-
{ name: 'simple CREATE POLICY', sql: simplePolicySql }
54+
basicPolicySql,
55+
complexPolicySql,
56+
simplePolicySql
5857
];
5958

60-
for (const testCase of testCases) {
61-
const originalParsed = await parse(testCase.sql);
62-
const prettyFormatted = deparseSync(originalParsed, { pretty: true });
63-
await testUtils.expectAstMatch(`pretty-${testCase.name}`, prettyFormatted);
59+
for (const sql of testCases) {
60+
await expectParseDeparse(sql, { pretty: true });
6461
}
6562
});
6663
});

packages/deparser/__tests__/pretty/create-table-pretty.test.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { deparseSync } from '../../src';
22
import { parse } from 'libpg-query';
3-
import { TestUtils } from '../../test-utils';
3+
import { expectParseDeparse } from '../../test-utils';
44

55
describe('Pretty CREATE TABLE formatting', () => {
66
const basicTableSql = `CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE);`;
@@ -49,16 +49,13 @@ describe('Pretty CREATE TABLE formatting', () => {
4949
});
5050

5151
it('should validate AST equivalence between original and pretty-formatted SQL', async () => {
52-
const testUtils = new TestUtils();
5352
const testCases = [
54-
{ name: 'basic CREATE TABLE', sql: basicTableSql },
55-
{ name: 'complex CREATE TABLE', sql: complexTableSql }
53+
basicTableSql,
54+
complexTableSql
5655
];
5756

58-
for (const testCase of testCases) {
59-
const originalParsed = await parse(testCase.sql);
60-
const prettyFormatted = deparseSync(originalParsed, { pretty: true });
61-
await testUtils.expectAstMatch(`pretty-${testCase.name}`, prettyFormatted);
57+
for (const sql of testCases) {
58+
await expectParseDeparse(sql, { pretty: true });
6259
}
6360
});
6461
});

packages/deparser/__tests__/pretty/select-pretty.test.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { deparseSync } from '../../src';
22
import { parse } from 'libpg-query';
3-
import { TestUtils } from '../../test-utils';
3+
import { expectParseDeparse } from '../../test-utils';
44

55
describe('Pretty SELECT formatting', () => {
66
const basicSelectSql = `SELECT id, name, email FROM users WHERE active = true;`;
@@ -58,18 +58,15 @@ describe('Pretty SELECT formatting', () => {
5858
});
5959

6060
it('should validate AST equivalence between original and pretty-formatted SQL', async () => {
61-
const testUtils = new TestUtils();
6261
const testCases = [
63-
{ name: 'basic SELECT', sql: basicSelectSql },
64-
{ name: 'complex SELECT', sql: complexSelectSql },
65-
{ name: 'SELECT with subquery', sql: selectWithSubquerySql },
66-
{ name: 'SELECT with UNION', sql: selectUnionSql }
62+
basicSelectSql,
63+
complexSelectSql,
64+
selectWithSubquerySql,
65+
selectUnionSql
6766
];
6867

69-
for (const testCase of testCases) {
70-
const originalParsed = await parse(testCase.sql);
71-
const prettyFormatted = deparseSync(originalParsed, { pretty: true });
72-
await testUtils.expectAstMatch(`pretty-${testCase.name}`, prettyFormatted);
68+
for (const sql of testCases) {
69+
await expectParseDeparse(sql, { pretty: true });
7370
}
7471
});
7572
});

packages/deparser/test-utils/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ import * as path from 'path';
66
import { expect } from '@jest/globals';
77
import { diff } from 'jest-diff'
88

9+
export async function expectParseDeparse(sql1: string, options = { pretty: false }) {
10+
const parsed = await parse(sql1);
11+
12+
const sql2 = deparse(parsed, options);
13+
14+
const ast1 = cleanTree(parsed);
15+
const ast2 = cleanTree(await parse(sql2));
16+
17+
expect(ast2).toEqual(ast1);
18+
}
19+
920
type ParseErrorType =
1021
| 'PARSE_FAILED'
1122
| 'INVALID_STATEMENT'

0 commit comments

Comments
 (0)