|
| 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 | +}); |
0 commit comments