Skip to content

Commit a579c20

Browse files
committed
jest
1 parent d63d6a7 commit a579c20

File tree

9 files changed

+118
-8
lines changed

9 files changed

+118
-8
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { parse, parseSync, deparse, deparseSync, loadModule } from '../src';
2+
3+
describe('pgsql-parser', () => {
4+
describe('Async API', () => {
5+
it('should parse a simple SELECT statement', async () => {
6+
const sql = 'SELECT * FROM test_table';
7+
const parseResult = await parse(sql);
8+
9+
expect(parseResult).toBeDefined();
10+
expect(parseResult.stmts).toBeDefined();
11+
expect(Array.isArray(parseResult.stmts)).toBe(true);
12+
expect(parseResult.stmts!.length).toBeGreaterThan(0);
13+
expect(parseResult.stmts![0]).toHaveProperty('stmt');
14+
});
15+
16+
it('should deparse an AST back to SQL', async () => {
17+
const sql = 'SELECT * FROM test_table';
18+
const parseResult = await parse(sql);
19+
const deparsed = await deparse(parseResult);
20+
21+
expect(deparsed).toBeDefined();
22+
expect(typeof deparsed).toBe('string');
23+
expect(deparsed.toLowerCase()).toContain('select');
24+
expect(deparsed.toLowerCase()).toContain('test_table');
25+
});
26+
27+
it('should support AST manipulation and round-trip', async () => {
28+
const sql = 'SELECT * FROM test_table';
29+
const parseResult = await parse(sql);
30+
31+
// Manipulate the AST to change the table name
32+
const stmt = parseResult.stmts![0].stmt as any;
33+
stmt.SelectStmt.fromClause[0].RangeVar.relname = 'another_table';
34+
35+
const deparsed = await deparse(parseResult);
36+
37+
expect(deparsed.toLowerCase()).toContain('another_table');
38+
expect(deparsed.toLowerCase()).not.toContain('test_table');
39+
});
40+
41+
it('should parse and deparse more complex queries', async () => {
42+
const sql = 'SELECT id, name FROM users WHERE age > 18 ORDER BY name';
43+
const parseResult = await parse(sql);
44+
const deparsed = await deparse(parseResult);
45+
46+
expect(deparsed).toBeDefined();
47+
expect(deparsed.toLowerCase()).toContain('select');
48+
expect(deparsed.toLowerCase()).toContain('users');
49+
expect(deparsed.toLowerCase()).toContain('where');
50+
});
51+
});
52+
53+
describe('Sync API', () => {
54+
beforeAll(async () => {
55+
// Initialize module for sync methods
56+
await loadModule();
57+
});
58+
59+
it('should parse a simple SELECT statement synchronously', () => {
60+
const sql = 'SELECT * FROM test_table';
61+
const parseResult = parseSync(sql);
62+
63+
expect(parseResult).toBeDefined();
64+
expect(parseResult.stmts).toBeDefined();
65+
expect(Array.isArray(parseResult.stmts)).toBe(true);
66+
expect(parseResult.stmts!.length).toBeGreaterThan(0);
67+
expect(parseResult.stmts![0]).toHaveProperty('stmt');
68+
});
69+
70+
it('should deparse an AST back to SQL synchronously', () => {
71+
const sql = 'SELECT * FROM test_table';
72+
const parseResult = parseSync(sql);
73+
const deparsed = deparseSync(parseResult);
74+
75+
expect(deparsed).toBeDefined();
76+
expect(typeof deparsed).toBe('string');
77+
expect(deparsed.toLowerCase()).toContain('select');
78+
expect(deparsed.toLowerCase()).toContain('test_table');
79+
});
80+
81+
it('should support AST manipulation with sync methods', () => {
82+
const sql = 'SELECT * FROM test_table';
83+
const parseResult = parseSync(sql);
84+
85+
// Manipulate the AST to change the table name
86+
const stmt = parseResult.stmts![0].stmt as any;
87+
stmt.SelectStmt.fromClause[0].RangeVar.relname = 'modified_table';
88+
89+
const deparsed = deparseSync(parseResult);
90+
91+
expect(deparsed.toLowerCase()).toContain('modified_table');
92+
expect(deparsed.toLowerCase()).not.toContain('test_table');
93+
});
94+
});
95+
96+
describe('Error handling', () => {
97+
it('should handle invalid SQL gracefully', async () => {
98+
const invalidSql = 'SELECT * FROM';
99+
100+
await expect(parse(invalidSql)).rejects.toThrow();
101+
});
102+
103+
it('should handle invalid SQL gracefully with sync methods', async () => {
104+
await loadModule();
105+
const invalidSql = 'SELECT * FROM';
106+
107+
expect(() => parseSync(invalidSql)).toThrow();
108+
});
109+
});
110+
});

packages/parser/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ module.exports = {
1414
transformIgnorePatterns: [`/node_modules/*`],
1515
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
1616
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
17-
modulePathIgnorePatterns: ["dist/*"]
17+
modulePathIgnorePatterns: ["dist/*", "versions/*"]
1818
};

packages/proto-parser/__tests__/__snapshots__/enum-maps.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`enum-maps disabled enum maps 1`] = `
44
[

packages/proto-parser/__tests__/__snapshots__/enum-utils.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`enum utils nested objects format generates nested objects with custom filenames 1`] = `
44
[

packages/proto-parser/__tests__/__snapshots__/enums.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`pure enums 1`] = `
44
[

packages/proto-parser/__tests__/__snapshots__/json.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`enums enumMap json enabled 1`] = `
44
[

packages/proto-parser/__tests__/__snapshots__/runtime-schema.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`runtime-schema custom filename json 1`] = `
44
[

packages/proto-parser/__tests__/__snapshots__/types.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`types fieldsRequired 1`] = `
44
[

packages/proto-parser/__tests__/__snapshots__/utils.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`utils astHelpers enabled 1`] = `
44
[

0 commit comments

Comments
 (0)