-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathtest-SqlString.js
145 lines (112 loc) · 4.29 KB
/
test-SqlString.js
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
var common = require('../../common');
var test = require('utest');
var assert = require('assert');
var SqlString = require(common.lib + '/protocol/SqlString');
test('SqlString.escape', {
'undefined -> NULL': function() {
assert.equal(SqlString.escape(undefined), 'NULL');
},
'null -> NULL': function() {
assert.equal(SqlString.escape(null), 'NULL');
},
'booleans convert to strings': function() {
assert.equal(SqlString.escape(false), 'false');
assert.equal(SqlString.escape(true), 'true');
},
'numbers convert to strings': function() {
assert.equal(SqlString.escape(5), '5');
},
'objects are turned into key value pairs': function() {
assert.equal(SqlString.escape({a: 'b', c: 'd'}), "`a` = 'b', `c` = 'd'");
},
'objects function properties are ignored': function() {
assert.equal(SqlString.escape({a: 'b', c: function() {}}), "`a` = 'b'");
},
'nested objects are cast to strings': function() {
assert.equal(SqlString.escape({a: {nested: true}}), "`a` = '[object Object]'");
},
'arrays are turned into lists': function() {
assert.equal(SqlString.escape([1, 2, 'c']), "1, 2, 'c'");
},
'nested arrays are turned into grouped lists': function() {
assert.equal(SqlString.escape([[1,2,3], [4,5,6], ['a', 'b', {nested: true}]]), "(1, 2, 3), (4, 5, 6), ('a', 'b', '[object Object]')");
},
'nested objects inside arrays are cast to strings': function() {
assert.equal(SqlString.escape([1, {nested: true}, 2]), "1, '[object Object]', 2");
},
'strings are quoted': function() {
assert.equal(SqlString.escape('Super'), "'Super'");
},
'\0 gets escaped': function() {
assert.equal(SqlString.escape('Sup\0er'), "'Sup\\0er'");
},
'\b gets escaped': function() {
assert.equal(SqlString.escape('Sup\ber'), "'Sup\\ber'");
},
'\n gets escaped': function() {
assert.equal(SqlString.escape('Sup\ner'), "'Sup\\ner'");
},
'\r gets escaped': function() {
assert.equal(SqlString.escape('Sup\rer'), "'Sup\\rer'");
},
'\t gets escaped': function() {
assert.equal(SqlString.escape('Sup\ter'), "'Sup\\ter'");
},
'\\ gets escaped': function() {
assert.equal(SqlString.escape('Sup\\er'), "'Sup\\\\er'");
},
'\u001a (ascii 26) gets replaced with \\Z': function() {
assert.equal(SqlString.escape('Sup\u001aer'), "'Sup\\Zer'");
},
'single quotes get escaped': function() {
assert.equal(SqlString.escape('Sup\'er'), "'Sup\\'er'");
},
'double quotes get escaped': function() {
assert.equal(SqlString.escape('Sup"er'), "'Sup\\\"er'");
},
'dates are converted to YYYY-MM-DD HH:II:SS.sss': function() {
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);
assert.strictEqual(string, "'" + expected + "'");
},
'buffers are converted to hex': function() {
var buffer = new Buffer([0, 1, 254, 255]);
var string = SqlString.escape(buffer);
assert.strictEqual(string, "X'0001feff'");
},
'NaN -> NaN': function() {
assert.equal(SqlString.escape(NaN), 'NaN');
},
'Infinity -> Infinity': function() {
assert.equal(SqlString.escape(Infinity), 'Infinity');
}
});
test('SqlString.format', {
'question marks are replaced with escaped array values': function() {
var sql = SqlString.format('? and ?', ['a', 'b']);
assert.equal(sql, "'a' and 'b'");
},
'extra question marks are left untouched': function() {
var sql = SqlString.format('? and ?', ['a']);
assert.equal(sql, "'a' and ?");
},
'extra arguments are not used': function() {
var sql = SqlString.format('? and ?', ['a', 'b', 'c']);
assert.equal(sql, "'a' and 'b'");
},
'question marks within values do not cause issues': function() {
var sql = SqlString.format('? and ?', ['hello?', 'b']);
assert.equal(sql, "'hello?' and 'b'");
},
'objects is converted to values': function () {
var sql = SqlString.format('?', { 'hello': 'world' }, false)
assert.equal(sql, "`hello` = 'world'")
},
'objects is not converted to values': function () {
var sql = SqlString.format('?', { 'hello': 'world' }, true)
assert.equal(sql, "'[object Object]'")
var sql = SqlString.format('?', { toString: function () { return 'hello' } }, true)
assert.equal(sql, "'hello'")
}
});