-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy pathsqlite.test.ts
More file actions
124 lines (115 loc) · 4.35 KB
/
sqlite.test.ts
File metadata and controls
124 lines (115 loc) · 4.35 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
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 supportsCreateView from './features/createView.js';
import supportsOnConflict from './features/onConflict.js';
import supportsDataTypeCase from './options/dataTypeCase.js';
import supportsNumbers from './features/numbers.js';
import supportsReturning from './features/returning.js';
describe('SqliteFormatter', () => {
const language = 'sqlite';
const format: FormatFn = (query, cfg = {}) => originalFormat(query, { ...cfg, language });
behavesLikeSqlFormatter(format);
supportsNumbers(format);
supportsComments(format);
supportsCreateView(format, { ifNotExists: true });
supportsCreateTable(format, { ifNotExists: true });
supportsDropTable(format, { ifExists: true });
supportsConstraints(format, ['SET NULL', 'SET DEFAULT', 'CASCADE', 'RESTRICT', 'NO ACTION']);
supportsAlterTable(format, {
addColumn: true,
dropColumn: true,
renameTo: true,
renameColumn: true,
});
supportsDeleteFrom(format);
supportsInsertInto(format);
supportsReturning(format);
supportsOnConflict(format);
supportsUpdate(format);
supportsStrings(format, ["''-qq", "X''"]);
supportsIdentifiers(format, [`""-qq`, '``', '[]']);
supportsBetween(format);
supportsSchema(format);
supportsJoin(format);
supportsSetOperations(format, ['UNION', 'UNION ALL', 'EXCEPT', 'INTERSECT']);
supportsOperators(format, ['%', '~', '&', '|', '<<', '>>', '==', '->', '->>', '||']);
supportsParams(format, { positional: true, numbered: ['?'], named: [':', '@'] });
// SQLite has its own Tcl-style $-parameter syntax that is handled as a custom
// param type rather than the default named-param prefix. See sqlite.formatter.ts.
describe('supports SQLite $-style (Tcl) named placeholders', () => {
it('recognizes $name placeholders', () => {
expect(format('SELECT $foo, $bar, $baz;')).toBe(dedent`
SELECT
$foo,
$bar,
$baz;
`);
});
it('recognizes $name(...) placeholders without inserting a space', () => {
expect(format('select $p(x=y);')).toBe(dedent`
select
$p(x=y);
`);
});
it('recognizes $name with :: suffix and (...) trailer', () => {
expect(format('SELECT $foo::bar(extra);')).toBe(dedent`
SELECT
$foo::bar(extra);
`);
});
it('replaces $name placeholders with param values', () => {
expect(
format(`WHERE name = $name AND age > $current_age;`, {
params: { name: "'John'", current_age: '10' },
})
).toBe(dedent`
WHERE
name = 'John'
AND age > 10;
`);
});
});
supportsWindow(format);
supportsLimiting(format, { limit: true, offset: true });
supportsDataTypeCase(format);
it('supports REPLACE INTO syntax', () => {
expect(format(`REPLACE INTO tbl VALUES (1,'Leopard'),(2,'Dog');`)).toBe(dedent`
REPLACE INTO
tbl
VALUES
(1, 'Leopard'),
(2, 'Dog');
`);
});
it('supports ON CONFLICT .. DO UPDATE syntax', () => {
expect(format(`INSERT INTO tbl VALUES (1,'Leopard') ON CONFLICT DO UPDATE SET foo=1;`))
.toBe(dedent`
INSERT INTO
tbl
VALUES
(1, 'Leopard')
ON CONFLICT DO UPDATE
SET
foo = 1;
`);
});
});