|
| 1 | +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing |
| 2 | + |
| 3 | +exports[`fixture round-trip tests exception-handler.sql deparsed output matches snapshot 1`] = ` |
| 4 | +"-- Function with comments in exception handler |
| 5 | +CREATE FUNCTION safe_divide( |
| 6 | + a numeric, |
| 7 | + b numeric |
| 8 | +) RETURNS numeric LANGUAGE plpgsql AS $$ |
| 9 | +DECLARE |
| 10 | + v_result numeric; |
| 11 | +BEGIN |
| 12 | + -- Attempt the division |
| 13 | + v_result := a / b; |
| 14 | + RETURN v_result; |
| 15 | +EXCEPTION |
| 16 | + WHEN division_by_zero THEN |
| 17 | + -- Log the error and return null |
| 18 | + RAISE NOTICE 'Division by zero: % / %', a, b; |
| 19 | + RETURN NULL; |
| 20 | +END; |
| 21 | +$$;" |
| 22 | +`; |
| 23 | + |
| 24 | +exports[`fixture round-trip tests loop-with-comments.sql deparsed output matches snapshot 1`] = ` |
| 25 | +"-- Function with comments inside loops |
| 26 | +CREATE FUNCTION process_batch( |
| 27 | + p_batch_size int |
| 28 | +) RETURNS int LANGUAGE plpgsql AS $$ |
| 29 | +DECLARE |
| 30 | + v_processed integer := 0; |
| 31 | + r RECORD; |
| 32 | +BEGIN |
| 33 | + -- Process items in batches |
| 34 | + FOR r IN SELECT id, data FROM pending_items LIMIT p_batch_size LOOP |
| 35 | + -- Process each item |
| 36 | + PERFORM process_item(r.id, r.data); |
| 37 | + v_processed := v_processed + 1; |
| 38 | + END LOOP; |
| 39 | +
|
| 40 | + -- Return the count of processed items |
| 41 | + RETURN v_processed; |
| 42 | +END; |
| 43 | +$$;" |
| 44 | +`; |
| 45 | + |
| 46 | +exports[`fixture round-trip tests multi-function.sql deparsed output matches snapshot 1`] = ` |
| 47 | +"-- Multiple functions in one file |
| 48 | +-- with outer SQL comments preserved |
| 49 | +
|
| 50 | +CREATE FUNCTION add_numbers( |
| 51 | + a int, |
| 52 | + b int |
| 53 | +) RETURNS int LANGUAGE plpgsql AS $$ |
| 54 | +BEGIN |
| 55 | + -- Simple addition |
| 56 | + RETURN a + b; |
| 57 | +END; |
| 58 | +$$; |
| 59 | +
|
| 60 | +-- Second function with its own body comments |
| 61 | +CREATE FUNCTION multiply_numbers( |
| 62 | + a int, |
| 63 | + b int |
| 64 | +) RETURNS int LANGUAGE plpgsql AS $$ |
| 65 | +BEGIN |
| 66 | + -- Multiply the inputs |
| 67 | + RETURN a * b; |
| 68 | +END; |
| 69 | +$$;" |
| 70 | +`; |
| 71 | + |
| 72 | +exports[`fixture round-trip tests multiple-comments.sql deparsed output matches snapshot 1`] = ` |
| 73 | +"-- Function with multiple comment groups |
| 74 | +CREATE FUNCTION process_order( |
| 75 | + p_order_id int |
| 76 | +) RETURNS void LANGUAGE plpgsql AS $$ |
| 77 | +DECLARE |
| 78 | + v_total numeric; |
| 79 | + v_status text; |
| 80 | +BEGIN |
| 81 | + -- First, calculate the order total |
| 82 | + SELECT sum(amount) INTO v_total FROM order_items WHERE order_id = p_order_id; |
| 83 | +
|
| 84 | + -- Then update the order status |
| 85 | + -- based on the total amount |
| 86 | + IF v_total > 1000 THEN |
| 87 | + v_status := 'premium'; |
| 88 | + ELSE |
| 89 | + v_status := 'standard'; |
| 90 | + END IF; |
| 91 | +
|
| 92 | + -- Finally, record the result |
| 93 | + UPDATE orders SET status = v_status, total = v_total WHERE id = p_order_id; |
| 94 | +END; |
| 95 | +$$;" |
| 96 | +`; |
| 97 | + |
| 98 | +exports[`fixture round-trip tests nested-blocks.sql deparsed output matches snapshot 1`] = ` |
| 99 | +"-- Function with nested blocks and comments |
| 100 | +CREATE FUNCTION complex_logic( |
| 101 | + p_id int |
| 102 | +) RETURNS text LANGUAGE plpgsql AS $$ |
| 103 | +DECLARE |
| 104 | + v_result text; |
| 105 | +BEGIN |
| 106 | + -- Initialize result |
| 107 | + v_result := 'unknown'; |
| 108 | +
|
| 109 | + -- Try the main logic |
| 110 | + BEGIN |
| 111 | + -- Fetch and process |
| 112 | + SELECT status INTO v_result FROM items WHERE id = p_id; |
| 113 | + EXCEPTION |
| 114 | + WHEN no_data_found THEN |
| 115 | + -- Handle missing item |
| 116 | + v_result := 'not_found'; |
| 117 | + END; |
| 118 | +
|
| 119 | + RETURN v_result; |
| 120 | +END; |
| 121 | +$$;" |
| 122 | +`; |
| 123 | + |
| 124 | +exports[`fixture round-trip tests no-comments.sql deparsed output matches snapshot 1`] = ` |
| 125 | +"CREATE FUNCTION get_one() RETURNS int LANGUAGE plpgsql AS $$ |
| 126 | +BEGIN |
| 127 | + RETURN 1; |
| 128 | +END; |
| 129 | +$$;" |
| 130 | +`; |
| 131 | + |
| 132 | +exports[`fixture round-trip tests simple-function.sql deparsed output matches snapshot 1`] = ` |
| 133 | +"-- Simple function with body comments |
| 134 | +CREATE FUNCTION get_user_count() RETURNS int LANGUAGE plpgsql AS $$ |
| 135 | +DECLARE |
| 136 | + v_count integer; |
| 137 | +BEGIN |
| 138 | + -- Count all active users |
| 139 | + SELECT count(*) INTO v_count FROM users WHERE is_active = true; |
| 140 | + RETURN v_count; |
| 141 | +END; |
| 142 | +$$;" |
| 143 | +`; |
| 144 | + |
| 145 | +exports[`fixture round-trip tests trigger-function.sql deparsed output matches snapshot 1`] = ` |
| 146 | +"-- Trigger function with comments in body |
| 147 | +CREATE FUNCTION audit_trigger() RETURNS trigger LANGUAGE plpgsql AS $$ |
| 148 | +BEGIN |
| 149 | + -- Set the updated_at timestamp |
| 150 | + NEW.updated_at := now(); |
| 151 | +
|
| 152 | + -- Record the change in audit log |
| 153 | + IF TG_OP = 'UPDATE' THEN |
| 154 | + INSERT INTO audit_log (table_name, operation, old_data, new_data) |
| 155 | + VALUES (TG_TABLE_NAME, TG_OP, row_to_json(OLD), row_to_json(NEW)); |
| 156 | + END IF; |
| 157 | +
|
| 158 | + RETURN NEW; |
| 159 | +END; |
| 160 | +$$;" |
| 161 | +`; |
0 commit comments