|
| 1 | +import { expectParseDeparse } from '../../test-utils'; |
| 2 | + |
| 3 | +describe('TypeCast with negative numbers', () => { |
| 4 | + it('should handle negative integer with CAST syntax', async () => { |
| 5 | + const sql = `SELECT -1::integer`; |
| 6 | + const result = await expectParseDeparse(sql); |
| 7 | + expect(result).toMatchSnapshot(); |
| 8 | + }); |
| 9 | + |
| 10 | + it('should handle parenthesized negative integer', async () => { |
| 11 | + const sql = `SELECT (-1)::integer`; |
| 12 | + const result = await expectParseDeparse(sql); |
| 13 | + expect(result).toMatchSnapshot(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should handle negative float with CAST syntax', async () => { |
| 17 | + const sql = `SELECT -1.5::numeric`; |
| 18 | + const result = await expectParseDeparse(sql); |
| 19 | + expect(result).toMatchSnapshot(); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should handle parenthesized negative float', async () => { |
| 23 | + const sql = `SELECT (-1.5)::numeric`; |
| 24 | + const result = await expectParseDeparse(sql); |
| 25 | + expect(result).toMatchSnapshot(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should handle negative bigint', async () => { |
| 29 | + const sql = `SELECT -9223372036854775808::bigint`; |
| 30 | + const result = await expectParseDeparse(sql); |
| 31 | + expect(result).toMatchSnapshot(); |
| 32 | + }); |
| 33 | +}); |
| 34 | + |
| 35 | +describe('TypeCast with complex expressions', () => { |
| 36 | + it('should handle arithmetic expression with CAST syntax', async () => { |
| 37 | + const sql = `SELECT (1 + 2)::integer`; |
| 38 | + const result = await expectParseDeparse(sql); |
| 39 | + expect(result).toMatchSnapshot(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should handle subtraction expression', async () => { |
| 43 | + const sql = `SELECT (a - b)::integer FROM t`; |
| 44 | + const result = await expectParseDeparse(sql); |
| 45 | + expect(result).toMatchSnapshot(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should handle CASE expression with CAST syntax', async () => { |
| 49 | + const sql = `SELECT (CASE WHEN a > 0 THEN 1 ELSE 2 END)::integer FROM t`; |
| 50 | + const result = await expectParseDeparse(sql); |
| 51 | + expect(result).toMatchSnapshot(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should handle boolean expression', async () => { |
| 55 | + const sql = `SELECT (a IS NULL)::boolean FROM t`; |
| 56 | + const result = await expectParseDeparse(sql); |
| 57 | + expect(result).toMatchSnapshot(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should handle comparison expression', async () => { |
| 61 | + const sql = `SELECT (a > b)::boolean FROM t`; |
| 62 | + const result = await expectParseDeparse(sql); |
| 63 | + expect(result).toMatchSnapshot(); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +describe('TypeCast with function calls', () => { |
| 68 | + it('should handle function call with :: syntax and parentheses', async () => { |
| 69 | + const sql = `SELECT substring('test', 1, 2)::text`; |
| 70 | + const result = await expectParseDeparse(sql); |
| 71 | + expect(result).toMatchSnapshot(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should handle qualified function call', async () => { |
| 75 | + const sql = `SELECT pg_catalog.substring('test', 1, 2)::text`; |
| 76 | + const result = await expectParseDeparse(sql); |
| 77 | + expect(result).toMatchSnapshot(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should handle aggregate function', async () => { |
| 81 | + const sql = `SELECT sum(x)::numeric FROM t`; |
| 82 | + const result = await expectParseDeparse(sql); |
| 83 | + expect(result).toMatchSnapshot(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should handle nested function calls', async () => { |
| 87 | + const sql = `SELECT upper(lower('TEST'))::text`; |
| 88 | + const result = await expectParseDeparse(sql); |
| 89 | + expect(result).toMatchSnapshot(); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +describe('TypeCast with pg_catalog.bpchar', () => { |
| 94 | + it('should preserve CAST syntax for qualified bpchar', async () => { |
| 95 | + const sql = `SELECT 'x'::pg_catalog.bpchar`; |
| 96 | + const result = await expectParseDeparse(sql); |
| 97 | + // Should use CAST() syntax for round-trip fidelity |
| 98 | + expect(result).toBe(`SELECT CAST('x' AS pg_catalog.bpchar)`); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should use :: syntax for unqualified bpchar', async () => { |
| 102 | + const sql = `SELECT 'x'::bpchar`; |
| 103 | + const result = await expectParseDeparse(sql); |
| 104 | + expect(result).toBe(`SELECT 'x'::bpchar`); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should handle bpchar with length modifier', async () => { |
| 108 | + const sql = `SELECT 'hello'::bpchar(10)`; |
| 109 | + const result = await expectParseDeparse(sql); |
| 110 | + expect(result).toMatchSnapshot(); |
| 111 | + }); |
| 112 | +}); |
| 113 | + |
| 114 | +describe('TypeCast with string literals containing special characters', () => { |
| 115 | + it('should handle string literal with parenthesis', async () => { |
| 116 | + const sql = `SELECT '('::text`; |
| 117 | + const result = await expectParseDeparse(sql); |
| 118 | + // Should use :: syntax (not CAST) - improvement over old string-based heuristic |
| 119 | + expect(result).toBe(`SELECT '('::text`); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should handle string literal starting with minus', async () => { |
| 123 | + const sql = `SELECT '-hello'::text`; |
| 124 | + const result = await expectParseDeparse(sql); |
| 125 | + // Should use :: syntax (not CAST) - improvement over old string-based heuristic |
| 126 | + expect(result).toBe(`SELECT '-hello'::text`); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should handle string literal with multiple special chars', async () => { |
| 130 | + const sql = `SELECT '(-)'::text`; |
| 131 | + const result = await expectParseDeparse(sql); |
| 132 | + expect(result).toBe(`SELECT '(-)'::text`); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should handle empty string', async () => { |
| 136 | + const sql = `SELECT ''::text`; |
| 137 | + const result = await expectParseDeparse(sql); |
| 138 | + expect(result).toBe(`SELECT ''::text`); |
| 139 | + }); |
| 140 | +}); |
| 141 | + |
| 142 | +describe('TypeCast with simple constants', () => { |
| 143 | + it('should handle positive integer with :: syntax', async () => { |
| 144 | + const sql = `SELECT 123::integer`; |
| 145 | + const result = await expectParseDeparse(sql); |
| 146 | + expect(result).toBe(`SELECT 123::integer`); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should handle positive float with :: syntax', async () => { |
| 150 | + const sql = `SELECT 3.14::numeric`; |
| 151 | + const result = await expectParseDeparse(sql); |
| 152 | + expect(result).toBe(`SELECT 3.14::numeric`); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should handle boolean true', async () => { |
| 156 | + const sql = `SELECT true::boolean`; |
| 157 | + const result = await expectParseDeparse(sql); |
| 158 | + expect(result).toBe(`SELECT TRUE::boolean`); |
| 159 | + }); |
| 160 | + |
| 161 | + it('should handle boolean false', async () => { |
| 162 | + const sql = `SELECT false::boolean`; |
| 163 | + const result = await expectParseDeparse(sql); |
| 164 | + expect(result).toBe(`SELECT FALSE::boolean`); |
| 165 | + }); |
| 166 | + |
| 167 | + it('should handle NULL cast', async () => { |
| 168 | + const sql = `SELECT NULL::integer`; |
| 169 | + const result = await expectParseDeparse(sql); |
| 170 | + expect(result).toMatchSnapshot(); |
| 171 | + }); |
| 172 | +}); |
| 173 | + |
| 174 | +describe('TypeCast with column references', () => { |
| 175 | + it('should handle simple column reference with :: syntax', async () => { |
| 176 | + const sql = `SELECT a::integer FROM t`; |
| 177 | + const result = await expectParseDeparse(sql); |
| 178 | + expect(result).toBe(`SELECT a::integer FROM t`); |
| 179 | + }); |
| 180 | + |
| 181 | + it('should handle qualified column reference', async () => { |
| 182 | + const sql = `SELECT t.a::integer FROM t`; |
| 183 | + const result = await expectParseDeparse(sql); |
| 184 | + expect(result).toBe(`SELECT t.a::integer FROM t`); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should handle fully qualified column reference', async () => { |
| 188 | + const sql = `SELECT schema.t.a::integer FROM schema.t`; |
| 189 | + const result = await expectParseDeparse(sql); |
| 190 | + expect(result).toMatchSnapshot(); |
| 191 | + }); |
| 192 | +}); |
| 193 | + |
| 194 | +describe('TypeCast with pg_catalog types', () => { |
| 195 | + it('should handle pg_catalog.int4 with :: syntax', async () => { |
| 196 | + const sql = `SELECT 123::pg_catalog.int4`; |
| 197 | + const result = await expectParseDeparse(sql); |
| 198 | + // Should strip pg_catalog prefix and use :: syntax |
| 199 | + expect(result).toBe(`SELECT 123::int4`); |
| 200 | + }); |
| 201 | + |
| 202 | + it('should handle pg_catalog.varchar', async () => { |
| 203 | + const sql = `SELECT 'hello'::pg_catalog.varchar`; |
| 204 | + const result = await expectParseDeparse(sql); |
| 205 | + expect(result).toBe(`SELECT 'hello'::varchar`); |
| 206 | + }); |
| 207 | + |
| 208 | + it('should handle pg_catalog.numeric', async () => { |
| 209 | + const sql = `SELECT 3.14::pg_catalog.numeric`; |
| 210 | + const result = await expectParseDeparse(sql); |
| 211 | + expect(result).toBe(`SELECT 3.14::numeric`); |
| 212 | + }); |
| 213 | +}); |
| 214 | + |
| 215 | +describe('TypeCast with arrays', () => { |
| 216 | + it('should handle array literal cast', async () => { |
| 217 | + const sql = `SELECT ARRAY[1, 2, 3]::integer[]`; |
| 218 | + const result = await expectParseDeparse(sql); |
| 219 | + expect(result).toMatchSnapshot(); |
| 220 | + }); |
| 221 | + |
| 222 | + it('should handle array string literal cast', async () => { |
| 223 | + const sql = `SELECT '{1,2,3}'::integer[]`; |
| 224 | + const result = await expectParseDeparse(sql); |
| 225 | + expect(result).toMatchSnapshot(); |
| 226 | + }); |
| 227 | +}); |
| 228 | + |
| 229 | +describe('TypeCast with ROW expressions', () => { |
| 230 | + it('should handle ROW cast', async () => { |
| 231 | + const sql = `SELECT ROW(1, 2)::record`; |
| 232 | + const result = await expectParseDeparse(sql); |
| 233 | + expect(result).toMatchSnapshot(); |
| 234 | + }); |
| 235 | + |
| 236 | + it('should handle implicit row cast', async () => { |
| 237 | + const sql = `SELECT (1, 2)::record`; |
| 238 | + const result = await expectParseDeparse(sql); |
| 239 | + expect(result).toMatchSnapshot(); |
| 240 | + }); |
| 241 | +}); |
0 commit comments