forked from sql-formatter-org/sql-formatter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgresql.test.ts
More file actions
401 lines (374 loc) · 10.1 KB
/
postgresql.test.ts
File metadata and controls
401 lines (374 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import dedent from 'dedent-js';
import { format as originalFormat, FormatFn } from '../src/sqlFormatter.js';
import behavesLikeSqlFormatter from './behavesLikeSqlFormatter.js';
import supportsAlterTable from './features/alterTable.js';
import supportsBetween from './features/between.js';
import supportsCreateTable from './features/createTable.js';
import supportsDropTable from './features/dropTable.js';
import supportsJoin from './features/join.js';
import supportsOperators from './features/operators.js';
import supportsSchema from './features/schema.js';
import supportsStrings from './features/strings.js';
import supportsReturning from './features/returning.js';
import supportsConstraints from './features/constraints.js';
import supportsDeleteFrom from './features/deleteFrom.js';
import supportsComments from './features/comments.js';
import supportsCommentOn from './features/commentOn.js';
import supportsIdentifiers from './features/identifiers.js';
import supportsParams from './options/param.js';
import supportsArrayAndMapAccessors from './features/arrayAndMapAccessors.js';
import supportsWindow from './features/window.js';
import supportsSetOperations from './features/setOperations.js';
import supportsLimiting from './features/limiting.js';
import supportsInsertInto from './features/insertInto.js';
import supportsUpdate from './features/update.js';
import supportsTruncateTable from './features/truncateTable.js';
import supportsCreateView from './features/createView.js';
import supportsOnConflict from './features/onConflict.js';
import supportsIsDistinctFrom from './features/isDistinctFrom.js';
import supportsArrayLiterals from './features/arrayLiterals.js';
import supportsDataTypeCase from './options/dataTypeCase.js';
describe('PostgreSqlFormatter', () => {
const language = 'postgresql';
const format: FormatFn = (query, cfg = {}) => originalFormat(query, { ...cfg, language });
behavesLikeSqlFormatter(format);
supportsComments(format, { nestedBlockComments: true });
supportsCommentOn(format);
supportsCreateView(format, { orReplace: true, materialized: true, ifNotExists: true });
supportsCreateTable(format, { ifNotExists: true });
supportsDropTable(format, { ifExists: true });
supportsConstraints(format, ['NO ACTION', 'RESTRICT', 'CASCADE', 'SET NULL', 'SET DEFAULT']);
supportsArrayLiterals(format, { withArrayPrefix: true });
supportsArrayAndMapAccessors(format);
supportsAlterTable(format, {
addColumn: true,
dropColumn: true,
renameTo: true,
renameColumn: true,
});
supportsDeleteFrom(format);
supportsInsertInto(format);
supportsOnConflict(format);
supportsUpdate(format, { whereCurrentOf: true });
supportsTruncateTable(format, { withoutTable: true });
supportsStrings(format, ["''-qq", "U&''", "X''", "B''"]);
supportsIdentifiers(format, [`""-qq`, 'U&""']);
supportsBetween(format);
supportsSchema(format);
// Missing: '::' type cast (tested separately)
supportsOperators(
format,
[
// Arithmetic
'%',
'^',
'|/',
'||/',
'@',
// Assignment
':=',
// Bitwise
'&',
'|',
'#',
'~',
'<<',
'>>',
// Byte comparison
'~>~',
'~<~',
'~>=~',
'~<=~',
// Geometric
'@-@',
'@@',
'##',
'<->',
'&&',
'&<',
'&>',
'<<|',
'&<|',
'|>>',
'|&>',
'<^',
'^>',
'?#',
'?-',
'?|',
'?-|',
'?||',
'@>',
'<@',
'~=',
// JSON
'?',
'@?',
'?&',
'->',
'->>',
'#>',
'#>>',
'#-',
// Named function params
'=>',
// Network address
'>>=',
'<<=',
// Pattern matching
'~~',
'~~*',
'!~~',
'!~~*',
// POSIX RegExp
'~',
'~*',
'!~',
'!~*',
// Range/multirange
'-|-',
// String concatenation
'||',
// Text search
'@@@',
'!!',
'^@',
// Trigram/trigraph
'<%',
'<<%',
'%>',
'%>>',
'<<->',
'<->>',
'<<<->',
'<->>>',
// Custom operators: from pgvector extension
'<#>',
'<=>',
'<+>',
'<~>',
'<%>',
],
{ any: true }
);
supportsIsDistinctFrom(format);
supportsJoin(format);
supportsSetOperations(format);
supportsReturning(format);
supportsParams(format, { numbered: ['$'] });
supportsWindow(format);
supportsLimiting(format, { limit: true, offset: true, fetchFirst: true, fetchNext: true });
supportsDataTypeCase(format);
it('allows $ character as part of identifiers', () => {
expect(format('SELECT foo$, some$$ident')).toBe(dedent`
SELECT
foo$,
some$$ident
`);
});
// Postgres-specific string types
it("supports E'' strings with C-style escapes", () => {
expect(format("E'blah blah'")).toBe("E'blah blah'");
expect(format("E'some \\' FROM escapes'")).toBe("E'some \\' FROM escapes'");
expect(format("SELECT E'blah' FROM foo")).toBe(dedent`
SELECT
E'blah'
FROM
foo
`);
expect(format("E'blah''blah'")).toBe("E'blah''blah'");
});
it('supports dollar-quoted strings', () => {
expect(format('$xxx$foo $$ LEFT JOIN $yyy$ bar$xxx$')).toBe(
'$xxx$foo $$ LEFT JOIN $yyy$ bar$xxx$'
);
expect(format('$$foo JOIN bar$$')).toBe('$$foo JOIN bar$$');
expect(format('$$foo $ JOIN bar$$')).toBe('$$foo $ JOIN bar$$');
expect(format('$$foo \n bar$$')).toBe('$$foo \n bar$$');
expect(format('SELECT $$where$$ FROM $$update$$')).toBe(dedent`
SELECT
$$where$$
FROM
$$update$$
`);
});
it('formats type-cast operator without spaces', () => {
expect(format('SELECT 2 :: numeric AS foo;')).toBe(dedent`
SELECT
2::numeric AS foo;
`);
});
// issue #144 (unsolved)
// This is currently far from ideal.
it('formats SELECT DISTINCT ON () syntax', () => {
expect(format('SELECT DISTINCT ON (c1, c2) c1, c2 FROM tbl;')).toBe(dedent`
SELECT DISTINCT
ON (c1, c2) c1,
c2
FROM
tbl;
`);
});
// Regression test for issue #447
it('formats empty SELECT', () => {
expect(format('SELECT;')).toBe(dedent`
SELECT;
`);
});
// Regression test for issues #391 and #618
it('formats TIMESTAMP WITH TIME ZONE syntax', () => {
expect(
format(`
CREATE TABLE time_table (id INT,
created_at TIMESTAMP WITH TIME ZONE,
deleted_at TIME WITH TIME ZONE,
modified_at TIMESTAMP(0) WITH TIME ZONE);`)
).toBe(dedent`
CREATE TABLE time_table (
id INT,
created_at TIMESTAMP WITH TIME ZONE,
deleted_at TIME WITH TIME ZONE,
modified_at TIMESTAMP(0) WITH TIME ZONE
);
`);
});
// Regression test for issue #624
it('supports array slice operator', () => {
expect(format('SELECT foo[:5], bar[1:], baz[1:5], zap[:];')).toBe(dedent`
SELECT
foo[:5],
bar[1:],
baz[1:5],
zap[:];
`);
});
it('formats ALTER TABLE ... ALTER COLUMN', () => {
expect(
format(
`ALTER TABLE t ALTER COLUMN foo SET DATA TYPE VARCHAR;
ALTER TABLE t ALTER COLUMN foo SET DEFAULT 5;
ALTER TABLE t ALTER COLUMN foo DROP DEFAULT;
ALTER TABLE t ALTER COLUMN foo SET NOT NULL;
ALTER TABLE t ALTER COLUMN foo DROP NOT NULL;`
)
).toBe(dedent`
ALTER TABLE t
ALTER COLUMN foo
SET DATA TYPE VARCHAR;
ALTER TABLE t
ALTER COLUMN foo
SET DEFAULT 5;
ALTER TABLE t
ALTER COLUMN foo
DROP DEFAULT;
ALTER TABLE t
ALTER COLUMN foo
SET NOT NULL;
ALTER TABLE t
ALTER COLUMN foo
DROP NOT NULL;
`);
});
it('formats FOR UPDATE clause', () => {
expect(
format(`
SELECT * FROM tbl FOR UPDATE;
SELECT * FROM tbl FOR UPDATE OF tbl.salary;
`)
).toBe(dedent`
SELECT
*
FROM
tbl
FOR UPDATE;
SELECT
*
FROM
tbl
FOR UPDATE OF
tbl.salary;
`);
});
// Issue #685
it('allows TYPE to be used as an identifier', () => {
expect(format(`SELECT type, modified_at FROM items;`)).toBe(dedent`
SELECT
type,
modified_at
FROM
items;
`);
});
// Issue #156, #709
it('does not recognize common fields names as keywords', () => {
expect(
format(`SELECT id, type, name, location, label, password FROM release;`, {
keywordCase: 'upper',
})
).toBe(dedent`
SELECT
id,
type,
name,
location,
label,
password
FROM
release;
`);
});
it('formats DEFAULT VALUES clause', () => {
expect(
format(`INSERT INTO items default values RETURNING id;`, {
keywordCase: 'upper',
})
).toBe(dedent`
INSERT INTO
items
DEFAULT VALUES
RETURNING
id;
`);
});
// Issue #726
it('treats TEXT as data-type (not as plain keyword)', () => {
expect(
format(`CREATE TABLE foo (items text);`, {
dataTypeCase: 'upper',
})
).toBe(dedent`
CREATE TABLE foo (items TEXT);
`);
expect(
format(`CREATE TABLE foo (text VARCHAR(100));`, {
keywordCase: 'upper',
})
).toBe(dedent`
CREATE TABLE foo (text VARCHAR(100));
`);
});
// Issue #711
it('supports OPERATOR() syntax', () => {
expect(format(`SELECT foo OPERATOR(public.===) bar;`)).toBe(dedent`
SELECT
foo OPERATOR(public.===) bar;
`);
expect(format(`SELECT foo operator ( !== ) bar;`)).toBe(dedent`
SELECT
foo operator ( !== ) bar;
`);
});
// Issue #813
it('supports OR REPLACE in CREATE FUNCTION', () => {
expect(format(`CREATE OR REPLACE FUNCTION foo ();`)).toBe(dedent`
CREATE OR REPLACE FUNCTION foo ();
`);
});
it('formats JSON and JSONB data types', () => {
expect(
format(`CREATE TABLE foo (bar json, baz jsonb);`, {
dataTypeCase: 'upper',
})
).toBe('CREATE TABLE foo (bar JSON, baz JSONB);');
});
});