Skip to content

Commit acfcec5

Browse files
Consolidate pretty formatting tests with expectParseDeparse helper
- Update expectParseDeparse to return deparsed SQL for snapshot testing - Add DeparserOptions type support for newline and tab options - Replace all deparseSync calls with expectParseDeparse in pretty tests - Remove separate AST validation tests - now consolidated into snapshot tests - Each test now gets both AST validation and snapshot testing in single call - All 4 pretty test suites (24 tests, 24 snapshots) pass with consolidated approach Co-Authored-By: Dan Lynch <[email protected]>
1 parent 30f0030 commit acfcec5

File tree

5 files changed

+28
-98
lines changed

5 files changed

+28
-98
lines changed

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

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,54 +16,36 @@ describe('Pretty constraint formatting', () => {
1616
);`;
1717

1818
it('should format foreign key constraint with pretty option enabled', async () => {
19-
const parsed = await parse(foreignKeyConstraintSql);
20-
const result = deparseSync(parsed, { pretty: true });
19+
const result = await expectParseDeparse(foreignKeyConstraintSql, { pretty: true });
2120
expect(result).toMatchSnapshot();
2221
});
2322

2423
it('should maintain single-line format when pretty option disabled', async () => {
25-
const parsed = await parse(foreignKeyConstraintSql);
26-
const result = deparseSync(parsed, { pretty: false });
24+
const result = await expectParseDeparse(foreignKeyConstraintSql, { pretty: false });
2725
expect(result).toMatchSnapshot();
2826
});
2927

3028
it('should format check constraint with pretty option enabled', async () => {
31-
const parsed = await parse(checkConstraintSql);
32-
const result = deparseSync(parsed, { pretty: true });
29+
const result = await expectParseDeparse(checkConstraintSql, { pretty: true });
3330
expect(result).toMatchSnapshot();
3431
});
3532

3633
it('should format complex table with constraints with pretty option enabled', async () => {
37-
const parsed = await parse(complexTableSql);
38-
const result = deparseSync(parsed, { pretty: true });
34+
const result = await expectParseDeparse(complexTableSql, { pretty: true });
3935
expect(result).toMatchSnapshot();
4036
});
4137

4238
it('should maintain single-line format for complex table when pretty disabled', async () => {
43-
const parsed = await parse(complexTableSql);
44-
const result = deparseSync(parsed, { pretty: false });
39+
const result = await expectParseDeparse(complexTableSql, { pretty: false });
4540
expect(result).toMatchSnapshot();
4641
});
4742

4843
it('should use custom newline and tab characters in pretty mode', async () => {
49-
const parsed = await parse(foreignKeyConstraintSql);
50-
const result = deparseSync(parsed, {
44+
const result = await expectParseDeparse(foreignKeyConstraintSql, {
5145
pretty: true,
5246
newline: '\r\n',
5347
tab: ' '
5448
});
5549
expect(result).toMatchSnapshot();
5650
});
57-
58-
it('should validate AST equivalence between original and pretty-formatted SQL', async () => {
59-
const testCases = [
60-
foreignKeyConstraintSql,
61-
checkConstraintSql,
62-
complexTableSql
63-
];
64-
65-
for (const sql of testCases) {
66-
await expectParseDeparse(sql, { pretty: true });
67-
}
68-
});
6951
});

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

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,54 +10,36 @@ describe('Pretty CREATE POLICY formatting', () => {
1010
const simplePolicySql = `CREATE POLICY simple_policy ON posts FOR SELECT TO public USING (published = true);`;
1111

1212
it('should format basic CREATE POLICY with pretty option enabled', async () => {
13-
const parsed = await parse(basicPolicySql);
14-
const result = deparseSync(parsed, { pretty: true });
13+
const result = await expectParseDeparse(basicPolicySql, { pretty: true });
1514
expect(result).toMatchSnapshot();
1615
});
1716

1817
it('should maintain single-line format when pretty option disabled', async () => {
19-
const parsed = await parse(basicPolicySql);
20-
const result = deparseSync(parsed, { pretty: false });
18+
const result = await expectParseDeparse(basicPolicySql, { pretty: false });
2119
expect(result).toMatchSnapshot();
2220
});
2321

2422
it('should format complex CREATE POLICY with pretty option enabled', async () => {
25-
const parsed = await parse(complexPolicySql);
26-
const result = deparseSync(parsed, { pretty: true });
23+
const result = await expectParseDeparse(complexPolicySql, { pretty: true });
2724
expect(result).toMatchSnapshot();
2825
});
2926

3027
it('should maintain single-line format for complex policy when pretty disabled', async () => {
31-
const parsed = await parse(complexPolicySql);
32-
const result = deparseSync(parsed, { pretty: false });
28+
const result = await expectParseDeparse(complexPolicySql, { pretty: false });
3329
expect(result).toMatchSnapshot();
3430
});
3531

3632
it('should format simple CREATE POLICY with pretty option enabled', async () => {
37-
const parsed = await parse(simplePolicySql);
38-
const result = deparseSync(parsed, { pretty: true });
33+
const result = await expectParseDeparse(simplePolicySql, { pretty: true });
3934
expect(result).toMatchSnapshot();
4035
});
4136

4237
it('should use custom newline and tab characters in pretty mode', async () => {
43-
const parsed = await parse(basicPolicySql);
44-
const result = deparseSync(parsed, {
38+
const result = await expectParseDeparse(basicPolicySql, {
4539
pretty: true,
4640
newline: '\r\n',
4741
tab: ' '
4842
});
4943
expect(result).toMatchSnapshot();
5044
});
51-
52-
it('should validate AST equivalence between original and pretty-formatted SQL', async () => {
53-
const testCases = [
54-
basicPolicySql,
55-
complexPolicySql,
56-
simplePolicySql
57-
];
58-
59-
for (const sql of testCases) {
60-
await expectParseDeparse(sql, { pretty: true });
61-
}
62-
});
6345
});

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

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,31 @@ describe('Pretty CREATE TABLE formatting', () => {
1515
);`;
1616

1717
it('should format basic CREATE TABLE with pretty option enabled', async () => {
18-
const parsed = await parse(basicTableSql);
19-
const result = deparseSync(parsed, { pretty: true });
18+
const result = await expectParseDeparse(basicTableSql, { pretty: true });
2019
expect(result).toMatchSnapshot();
2120
});
2221

2322
it('should maintain single-line format when pretty option disabled', async () => {
24-
const parsed = await parse(basicTableSql);
25-
const result = deparseSync(parsed, { pretty: false });
23+
const result = await expectParseDeparse(basicTableSql, { pretty: false });
2624
expect(result).toMatchSnapshot();
2725
});
2826

2927
it('should format complex CREATE TABLE with pretty option enabled', async () => {
30-
const parsed = await parse(complexTableSql);
31-
const result = deparseSync(parsed, { pretty: true });
28+
const result = await expectParseDeparse(complexTableSql, { pretty: true });
3229
expect(result).toMatchSnapshot();
3330
});
3431

3532
it('should maintain single-line format for complex table when pretty disabled', async () => {
36-
const parsed = await parse(complexTableSql);
37-
const result = deparseSync(parsed, { pretty: false });
33+
const result = await expectParseDeparse(complexTableSql, { pretty: false });
3834
expect(result).toMatchSnapshot();
3935
});
4036

4137
it('should use custom newline and tab characters in pretty mode', async () => {
42-
const parsed = await parse(basicTableSql);
43-
const result = deparseSync(parsed, {
38+
const result = await expectParseDeparse(basicTableSql, {
4439
pretty: true,
4540
newline: '\r\n',
4641
tab: ' '
4742
});
4843
expect(result).toMatchSnapshot();
4944
});
50-
51-
it('should validate AST equivalence between original and pretty-formatted SQL', async () => {
52-
const testCases = [
53-
basicTableSql,
54-
complexTableSql
55-
];
56-
57-
for (const sql of testCases) {
58-
await expectParseDeparse(sql, { pretty: true });
59-
}
60-
});
6145
});

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

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,61 +12,41 @@ describe('Pretty SELECT formatting', () => {
1212
const selectUnionSql = `SELECT name FROM customers UNION ALL SELECT name FROM suppliers ORDER BY name;`;
1313

1414
it('should format basic SELECT with pretty option enabled', async () => {
15-
const parsed = await parse(basicSelectSql);
16-
const result = deparseSync(parsed, { pretty: true });
15+
const result = await expectParseDeparse(basicSelectSql, { pretty: true });
1716
expect(result).toMatchSnapshot();
1817
});
1918

2019
it('should maintain single-line format when pretty option disabled', async () => {
21-
const parsed = await parse(basicSelectSql);
22-
const result = deparseSync(parsed, { pretty: false });
20+
const result = await expectParseDeparse(basicSelectSql, { pretty: false });
2321
expect(result).toMatchSnapshot();
2422
});
2523

2624
it('should format complex SELECT with pretty option enabled', async () => {
27-
const parsed = await parse(complexSelectSql);
28-
const result = deparseSync(parsed, { pretty: true });
25+
const result = await expectParseDeparse(complexSelectSql, { pretty: true });
2926
expect(result).toMatchSnapshot();
3027
});
3128

3229
it('should maintain single-line format for complex SELECT when pretty disabled', async () => {
33-
const parsed = await parse(complexSelectSql);
34-
const result = deparseSync(parsed, { pretty: false });
30+
const result = await expectParseDeparse(complexSelectSql, { pretty: false });
3531
expect(result).toMatchSnapshot();
3632
});
3733

3834
it('should format SELECT with subquery with pretty option enabled', async () => {
39-
const parsed = await parse(selectWithSubquerySql);
40-
const result = deparseSync(parsed, { pretty: true });
35+
const result = await expectParseDeparse(selectWithSubquerySql, { pretty: true });
4136
expect(result).toMatchSnapshot();
4237
});
4338

4439
it('should format SELECT with UNION with pretty option enabled', async () => {
45-
const parsed = await parse(selectUnionSql);
46-
const result = deparseSync(parsed, { pretty: true });
40+
const result = await expectParseDeparse(selectUnionSql, { pretty: true });
4741
expect(result).toMatchSnapshot();
4842
});
4943

5044
it('should use custom newline and tab characters in pretty mode', async () => {
51-
const parsed = await parse(basicSelectSql);
52-
const result = deparseSync(parsed, {
45+
const result = await expectParseDeparse(basicSelectSql, {
5346
pretty: true,
5447
newline: '\r\n',
5548
tab: ' '
5649
});
5750
expect(result).toMatchSnapshot();
5851
});
59-
60-
it('should validate AST equivalence between original and pretty-formatted SQL', async () => {
61-
const testCases = [
62-
basicSelectSql,
63-
complexSelectSql,
64-
selectWithSubquerySql,
65-
selectUnionSql
66-
];
67-
68-
for (const sql of testCases) {
69-
await expectParseDeparse(sql, { pretty: true });
70-
}
71-
});
7252
});

packages/deparser/test-utils/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { parse } from 'libpg-query';
2-
import { deparseSync as deparse } from '../src';
2+
import { deparseSync as deparse, DeparserOptions } from '../src';
33
import { cleanTree } from '../src/utils';
44
import { readFileSync } from 'fs';
55
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 }) {
9+
export async function expectParseDeparse(sql1: string, options: DeparserOptions = { pretty: false }) {
1010
const parsed = await parse(sql1);
1111

1212
const sql2 = deparse(parsed, options);
@@ -15,6 +15,8 @@ export async function expectParseDeparse(sql1: string, options = { pretty: false
1515
const ast2 = cleanTree(await parse(sql2));
1616

1717
expect(ast2).toEqual(ast1);
18+
19+
return sql2;
1820
}
1921

2022
type ParseErrorType =

0 commit comments

Comments
 (0)