|
| 1 | +import { parse, transformSync, deparseSync, loadModule } from '../src'; |
| 2 | + |
| 3 | +beforeAll(async () => { |
| 4 | + await loadModule(); |
| 5 | +}); |
| 6 | + |
| 7 | +const simpleFunctionSql = ` |
| 8 | +CREATE OR REPLACE FUNCTION test_func(p_id int) |
| 9 | +RETURNS void |
| 10 | +LANGUAGE plpgsql |
| 11 | +AS $$ |
| 12 | +DECLARE |
| 13 | + v_count int; |
| 14 | +BEGIN |
| 15 | + v_count := 0; |
| 16 | + RAISE NOTICE 'Count: %', v_count; |
| 17 | +END; |
| 18 | +$$; |
| 19 | +`; |
| 20 | + |
| 21 | +const multiStatementSql = ` |
| 22 | +CREATE TABLE users (id int); |
| 23 | +
|
| 24 | +CREATE OR REPLACE FUNCTION get_user(p_id int) |
| 25 | +RETURNS int |
| 26 | +LANGUAGE plpgsql |
| 27 | +AS $$ |
| 28 | +BEGIN |
| 29 | + RETURN p_id; |
| 30 | +END; |
| 31 | +$$; |
| 32 | +
|
| 33 | +CREATE INDEX idx_users_id ON users(id); |
| 34 | +`; |
| 35 | + |
| 36 | +describe('plpgsql-parser', () => { |
| 37 | + describe('parse', () => { |
| 38 | + it('should parse a simple PL/pgSQL function', () => { |
| 39 | + const result = parse(simpleFunctionSql); |
| 40 | + |
| 41 | + expect(result.sql).toBeDefined(); |
| 42 | + expect(result.sql.stmts).toHaveLength(1); |
| 43 | + expect(result.items).toHaveLength(1); |
| 44 | + expect(result.functions).toHaveLength(1); |
| 45 | + |
| 46 | + const fn = result.functions[0]; |
| 47 | + expect(fn.kind).toBe('plpgsql-function'); |
| 48 | + expect(fn.language).toBe('plpgsql'); |
| 49 | + expect(fn.body.raw).toContain('v_count'); |
| 50 | + expect(fn.plpgsql.hydrated).toBeDefined(); |
| 51 | + expect(fn.plpgsql.stats.totalExpressions).toBeGreaterThan(0); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should parse multi-statement SQL with mixed content', () => { |
| 55 | + const result = parse(multiStatementSql); |
| 56 | + |
| 57 | + expect(result.sql.stmts).toHaveLength(3); |
| 58 | + expect(result.items).toHaveLength(3); |
| 59 | + expect(result.functions).toHaveLength(1); |
| 60 | + |
| 61 | + expect(result.items[0].kind).toBe('stmt'); |
| 62 | + expect(result.items[1].kind).toBe('plpgsql-function'); |
| 63 | + expect(result.items[2].kind).toBe('stmt'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should skip hydration when hydrate=false', () => { |
| 67 | + const result = parse(simpleFunctionSql, { hydrate: false }); |
| 68 | + |
| 69 | + expect(result.functions).toHaveLength(0); |
| 70 | + expect(result.items).toHaveLength(1); |
| 71 | + expect(result.items[0].kind).toBe('stmt'); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('transformSync', () => { |
| 76 | + it('should transform function using callback', () => { |
| 77 | + const result = transformSync(simpleFunctionSql, (ctx) => { |
| 78 | + const fn = ctx.functions[0]; |
| 79 | + fn.stmt.funcname[0].String.sval = 'renamed_func'; |
| 80 | + }); |
| 81 | + |
| 82 | + expect(result).toContain('renamed_func'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should transform function using visitor', () => { |
| 86 | + const result = transformSync(simpleFunctionSql, { |
| 87 | + onFunction: (fn) => { |
| 88 | + fn.stmt.funcname[0].String.sval = 'visitor_renamed'; |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + expect(result).toContain('visitor_renamed'); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('deparseSync', () => { |
| 97 | + it('should deparse parsed script back to SQL', () => { |
| 98 | + const parsed = parse(simpleFunctionSql); |
| 99 | + const result = deparseSync(parsed); |
| 100 | + |
| 101 | + expect(result).toContain('CREATE'); |
| 102 | + expect(result).toContain('FUNCTION'); |
| 103 | + expect(result).toContain('test_func'); |
| 104 | + expect(result).toContain('plpgsql'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should support pretty printing', () => { |
| 108 | + const parsed = parse(simpleFunctionSql); |
| 109 | + const result = deparseSync(parsed, { pretty: true }); |
| 110 | + |
| 111 | + expect(result).toContain('\n'); |
| 112 | + }); |
| 113 | + }); |
| 114 | +}); |
0 commit comments