-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy pathsql.test.ts
More file actions
117 lines (106 loc) · 3.91 KB
/
sql.test.ts
File metadata and controls
117 lines (106 loc) · 3.91 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
import dedent from 'dedent-js';
import { format as originalFormat, FormatFn } from '../src/sqlFormatter.js';
import behavesLikeSqlFormatter from './behavesLikeSqlFormatter.js';
import supportsCreateTable from './features/createTable.js';
import supportsDropTable from './features/dropTable.js';
import supportsAlterTable from './features/alterTable.js';
import supportsSchema from './features/schema.js';
import supportsStrings from './features/strings.js';
import supportsBetween from './features/between.js';
import supportsJoin from './features/join.js';
import supportsOperators from './features/operators.js';
import supportsConstraints from './features/constraints.js';
import supportsDeleteFrom from './features/deleteFrom.js';
import supportsComments from './features/comments.js';
import supportsIdentifiers from './features/identifiers.js';
import supportsParams from './options/param.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 supportsDataTypeCase from './options/dataTypeCase.js';
import supportsNumbers from './features/numbers.js';
describe('SqlFormatter', () => {
const language = 'sql';
const format: FormatFn = (query, cfg = {}) => originalFormat(query, { ...cfg, language });
behavesLikeSqlFormatter(format);
supportsNumbers(format);
supportsComments(format);
supportsCreateView(format);
supportsCreateTable(format);
supportsDropTable(format);
supportsConstraints(format, ['CASCADE', 'SET NULL', 'SET DEFAULT', 'RESTRICT', 'NO ACTION']);
supportsAlterTable(format, {
addColumn: true,
dropColumn: true,
renameTo: true,
renameColumn: true,
});
supportsDeleteFrom(format);
supportsInsertInto(format);
supportsUpdate(format, { whereCurrentOf: true });
supportsTruncateTable(format);
supportsStrings(format, ["''-qq", "''-bs", "X''", "N''", "U&''"]);
supportsIdentifiers(format, [`""-qq`, '``']);
supportsBetween(format);
supportsSchema(format);
supportsJoin(format);
supportsSetOperations(format);
supportsOperators(format, ['||'], { any: true });
supportsParams(format, { positional: true });
supportsWindow(format);
supportsLimiting(format, { limit: true, offset: true, fetchFirst: true, fetchNext: true });
supportsDataTypeCase(format);
it('throws error when encountering characters or operators it does not recognize', () => {
expect(() => format('SELECT @name, :bar FROM foo;')).toThrowError(
`Parse error: Unexpected "@name, :ba" at line 1 column 8`
);
});
it('crashes when encountering unsupported curly braces', () => {
expect(() =>
format(dedent`
SELECT
{foo};
`)
).toThrowError('Parse error: Unexpected "{foo};" at line 2 column 3');
});
// Issue #702
it('treats ASC and DESC as reserved keywords', () => {
expect(format(`SELECT foo FROM bar ORDER BY foo asc, zap desc`, { keywordCase: 'upper' }))
.toBe(dedent`
SELECT
foo
FROM
bar
ORDER BY
foo ASC,
zap DESC
`);
});
it('formats ALTER TABLE ... ALTER COLUMN', () => {
expect(
format(
`ALTER TABLE t ALTER COLUMN foo SET DEFAULT 5;
ALTER TABLE t ALTER COLUMN foo DROP DEFAULT;
ALTER TABLE t ALTER COLUMN foo DROP SCOPE CASCADE;
ALTER TABLE t ALTER COLUMN foo RESTART WITH 10;`
)
).toBe(dedent`
ALTER TABLE t
ALTER COLUMN foo
SET DEFAULT 5;
ALTER TABLE t
ALTER COLUMN foo
DROP DEFAULT;
ALTER TABLE t
ALTER COLUMN foo
DROP SCOPE CASCADE;
ALTER TABLE t
ALTER COLUMN foo
RESTART WITH 10;
`);
});
});