-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlString_test.ts
392 lines (320 loc) · 11.3 KB
/
sqlString_test.ts
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
import SqlString from "./sqlString.ts";
import { test, assertStrictEq, assertThrows } from "./test_deps.ts";
test("SqlString escapeId - value is quoted", () => {
assertStrictEq(SqlString.escapeId("id"), "`id`");
});
test("SqlString escapeId - value can be a number", () => {
assertStrictEq(SqlString.escapeId(42), "`42`");
});
test("SqlString escapeId - value can be an object", () => {
assertStrictEq(SqlString.escapeId({}), "`[object Object]`");
});
test("SqlString escapeId - value toString is called", () => {
assertStrictEq(
SqlString.escapeId({
toString: () => "foo",
}),
"`foo`",
);
});
test("SqlString escapeId - value toString is quoted", () => {
assertStrictEq(
SqlString.escapeId({
toString: () => "f`oo",
}),
"`f``oo`",
);
});
test("SqlString escapeId - value containing escapes is quoted", () => {
assertStrictEq(SqlString.escapeId("i`d"), "`i``d`");
});
test("SqlString escapeId - value containing separator is quoted", () => {
assertStrictEq(SqlString.escapeId("id1.id2"), "`id1`.`id2`");
});
test("SqlString escapeId - value containing separator and escapes is quoted", () => {
assertStrictEq(SqlString.escapeId("id`1.i`d2"), "`id``1`.`i``d2`");
});
test("SqlString escapeId - value containing separator is fully escaped when forbidQualified", () => {
assertStrictEq(SqlString.escapeId("id1.id2", true), "`id1.id2`");
});
test("SqlString escapeId - arrays are turned into lists", () => {
assertStrictEq(SqlString.escapeId(["a", "b", "t.c"]), "`a`, `b`, `t`.`c`");
});
test("SqlString escapeId - nested arrays are flattened", () => {
assertStrictEq(
SqlString.escapeId(["a", ["b", ["t.c"]]]),
"`a`, `b`, `t`.`c`",
);
});
test("SqlString escape - undefined -> NULL", () => {
assertStrictEq(SqlString.escape(undefined), "NULL");
});
test("SqlString escape - null -> NULL", () => {
assertStrictEq(SqlString.escape(null), "NULL");
});
test("SqlString escape - booleans convert to strings", () => {
assertStrictEq(SqlString.escape(false), "false");
assertStrictEq(SqlString.escape(true), "true");
});
test("SqlString escape - numbers convert to strings", () => {
assertStrictEq(SqlString.escape(5), "5");
});
test("SqlString escape - raw not escaped", () => {
assertStrictEq(SqlString.escape(SqlString.raw("NOW()")), "NOW()");
});
test("SqlString escape - objects are turned into key value pairs", () => {
assertStrictEq(SqlString.escape({ a: "b", c: "d" }), "`a` = 'b', `c` = 'd'");
});
test("SqlString escape - objects function properties are ignored", () => {
assertStrictEq(SqlString.escape({ a: "b", c: () => {} }), "`a` = 'b'");
});
test("SqlString escape - object values toSqlString is called", () => {
assertStrictEq(
SqlString.escape({
id: {
toSqlString: () => "LAST_INSERT_ID()",
},
}),
"`id` = LAST_INSERT_ID()",
);
});
test("SqlString escape - objects toSqlString is called", () => {
assertStrictEq(
SqlString.escape({
toSqlString: () => "@foo_id",
}),
"@foo_id",
);
});
test("SqlString escape - objects toSqlString is not quoted", () => {
assertStrictEq(
SqlString.escape({
toSqlString: () => "CURRENT_TIMESTAMP()",
}),
"CURRENT_TIMESTAMP()",
);
});
test("SqlString escape - nested objects are cast to strings", () => {
assertStrictEq(
SqlString.escape({ a: { nested: true } }),
"`a` = '[object Object]'",
);
});
test("SqlString escape - nested objects use toString", () => {
assertStrictEq(
SqlString.escape({
a: {
toString: () => "foo",
},
}),
"`a` = 'foo'",
);
});
test("SqlString escape - nested objects use toString is quoted", () => {
assertStrictEq(
SqlString.escape({
a: {
toString: () => "f'oo",
},
}),
"`a` = 'f\\'oo'",
);
});
test("SqlString escape - arrays are turned into lists", () => {
assertStrictEq(SqlString.escape([1, 2, "c"]), "1, 2, 'c'");
});
test("SqlString escape - nested arrays are turned into grouped lists", () => {
assertStrictEq(
SqlString.escape([
[1, 2, 3],
[4, 5, 6],
["a", "b", { nested: true }],
]),
"(1, 2, 3), (4, 5, 6), ('a', 'b', '[object Object]')",
);
});
test("SqlString escape - nested objects inside arrays are cast to strings", () => {
assertStrictEq(
SqlString.escape([1, { nested: true }, 2]),
"1, '[object Object]', 2",
);
});
test("SqlString escape - nested objects inside arrays use toString", () => {
assertStrictEq(
SqlString.escape([
1,
{ toString: () => "foo" },
2,
]),
"1, 'foo', 2",
);
});
test("SqlString escape - strings are quoted", () => {
assertStrictEq(SqlString.escape("Super"), "'Super'");
});
test("SqlString escape - \\0 gets escaped", () => {
assertStrictEq(SqlString.escape("Sup\0er"), "'Sup\\0er'");
assertStrictEq(SqlString.escape("Super\0"), "'Super\\0'");
});
test("SqlString escape - \\b gets escaped", () => {
assertStrictEq(SqlString.escape("Sup\ber"), "'Sup\\ber'");
assertStrictEq(SqlString.escape("Super\b"), "'Super\\b'");
});
test("SqlString escape - \\n gets escaped", () => {
assertStrictEq(SqlString.escape("Sup\ner"), "'Sup\\ner'");
assertStrictEq(SqlString.escape("Super\n"), "'Super\\n'");
});
test("SqlString escape - \\r gets escaped", () => {
assertStrictEq(SqlString.escape("Sup\rer"), "'Sup\\rer'");
assertStrictEq(SqlString.escape("Super\r"), "'Super\\r'");
});
test("SqlString escape - \\t gets escaped", () => {
assertStrictEq(SqlString.escape("Sup\ter"), "'Sup\\ter'");
assertStrictEq(SqlString.escape("Super\t"), "'Super\\t'");
});
test("SqlString escape - \\ gets escaped", () => {
assertStrictEq(SqlString.escape("Sup\\er"), "'Sup\\\\er'");
assertStrictEq(SqlString.escape("Super\\"), "'Super\\\\'");
});
test("SqlString escape - \\u001a (ascii 26) gets replaced with \\Z", () => {
assertStrictEq(SqlString.escape("Sup\u001aer"), "'Sup\\Zer'");
assertStrictEq(SqlString.escape("Super\u001a"), "'Super\\Z'");
});
test("SqlString escape - single quotes get escaped", () => {
assertStrictEq(SqlString.escape("Sup'er"), "'Sup\\'er'");
assertStrictEq(SqlString.escape("Super'"), "'Super\\''");
});
test("SqlString escape - double quotes get escaped", () => {
assertStrictEq(SqlString.escape('Sup"er'), "'Sup\\\"er'");
assertStrictEq(SqlString.escape('Super"'), "'Super\\\"'");
});
test("SqlString escape - dates are converted to YYYY-MM-DD HH:II:SS.sss", () => {
var expected = "2012-05-07 11:42:03.002";
var date = new Date(2012, 4, 7, 11, 42, 3, 2);
var string = SqlString.escape(date);
assertStrictEq(string, "'" + expected + "'");
});
test('SqlString escape - dates are converted to specified time zone "Z"', () => {
var expected = "2012-05-07 11:42:03.002";
var date = new Date(Date.UTC(2012, 4, 7, 11, 42, 3, 2));
var string = SqlString.escape(date, false, "Z");
assertStrictEq(string, "'" + expected + "'");
});
test('SqlString escape - dates are converted to specified time zone "+01"', () => {
var expected = "2012-05-07 12:42:03.002";
var date = new Date(Date.UTC(2012, 4, 7, 11, 42, 3, 2));
var string = SqlString.escape(date, false, "+01");
assertStrictEq(string, "'" + expected + "'");
});
test('SqlString escape - dates are converted to specified time zone "+0200"', () => {
var expected = "2012-05-07 13:42:03.002";
var date = new Date(Date.UTC(2012, 4, 7, 11, 42, 3, 2));
var string = SqlString.escape(date, false, "+0200");
assertStrictEq(string, "'" + expected + "'");
});
test('SqlString escape - dates are converted to specified time zone "-05:00"', () => {
var expected = "2012-05-07 06:42:03.002";
var date = new Date(Date.UTC(2012, 4, 7, 11, 42, 3, 2));
var string = SqlString.escape(date, false, "-05:00");
assertStrictEq(string, "'" + expected + "'");
});
test("SqlString escape - dates are converted to UTC for unknown time zone", () => {
var date = new Date(Date.UTC(2012, 4, 7, 11, 42, 3, 2));
var expected = SqlString.escape(date, false, "Z");
var string = SqlString.escape(date, false, "foo");
assertStrictEq(string, expected);
});
test("SqlString escape - invalid dates are converted to null", () => {
var date = new Date(NaN);
var string = SqlString.escape(date);
assertStrictEq(string, "NULL");
});
test("SqlString escape - NaN -> NaN", () => {
assertStrictEq(SqlString.escape(NaN), "NaN");
});
test("SqlString escape - Infinity -> Infinity", () => {
assertStrictEq(SqlString.escape(Infinity), "Infinity");
});
// test('SqlString.format', {
test("SqlString format - question marks are replaced with escaped array values", () => {
var sql = SqlString.format("? and ?", ["a", "b"]);
assertStrictEq(sql, "'a' and 'b'");
});
test("SqlString format - double quest marks are replaced with escaped id", () => {
var sql = SqlString.format("SELECT * FROM ?? WHERE id = ?", ["table", 42]);
assertStrictEq(sql, "SELECT * FROM `table` WHERE id = 42");
});
test("SqlString format - triple question marks are ignored", () => {
var sql = SqlString.format("? or ??? and ?", ["foo", "bar", "fizz", "buzz"]);
assertStrictEq(sql, "'foo' or ??? and 'bar'");
});
test("SqlString format - extra question marks are left untouched", () => {
var sql = SqlString.format("? and ?", ["a"]);
assertStrictEq(sql, "'a' and ?");
});
test("SqlString format - extra arguments are not used", () => {
var sql = SqlString.format("? and ?", ["a", "b", "c"]);
assertStrictEq(sql, "'a' and 'b'");
});
test("SqlString format - question marks within values do not cause issues", () => {
var sql = SqlString.format("? and ?", ["hello?", "b"]);
assertStrictEq(sql, "'hello?' and 'b'");
});
test("SqlString format - undefined is ignored", () => {
var sql = SqlString.format("?", undefined, false);
assertStrictEq(sql, "?");
});
test("SqlString format - objects is converted to values", () => {
var sql = SqlString.format("?", { hello: "world" }, false);
assertStrictEq(sql, "`hello` = 'world'");
});
test("SqlString format - objects is not converted to values", () => {
var sql = SqlString.format("?", { hello: "world" }, true);
assertStrictEq(sql, "'[object Object]'");
var sql = SqlString.format(
"?",
{
toString: () => "hello",
},
true,
);
assertStrictEq(sql, "'hello'");
var sql = SqlString.format(
"?",
{
toSqlString: () => "@foo",
},
true,
);
assertStrictEq(sql, "@foo");
});
test("SqlString format - sql is untouched if no values are provided", () => {
var sql = SqlString.format("SELECT ??");
assertStrictEq(sql, "SELECT ??");
});
test("SqlString format - sql is untouched if values are provided but there are no placeholders", () => {
var sql = SqlString.format("SELECT COUNT(*) FROM table", ["a", "b"]);
assertStrictEq(sql, "SELECT COUNT(*) FROM table");
});
test("SqlString raw - creates object", () => {
assertStrictEq(typeof SqlString.raw("NOW()"), "object");
});
test("SqlString raw - rejects number", () => {
assertThrows(() => {
SqlString.raw(42 as any);
});
});
test("SqlString raw - rejects undefined", () => {
assertThrows(() => {
(SqlString as any).raw();
});
});
test("SqlString raw - object has toSqlString", () => {
assertStrictEq(typeof SqlString.raw("NOW()").toSqlString, "function");
});
test("SqlString raw - toSqlString returns sql as-is", () => {
assertStrictEq(
SqlString.raw("NOW() AS 'current_time'").toSqlString(),
"NOW() AS 'current_time'",
);
});