-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests.js
299 lines (187 loc) · 12.9 KB
/
tests.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
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
'use strict';
const expect = require('chai').expect;
const sql = require('./index.js');
/*
These are tests for the extension to 'sql-bricks' library, which is meant to add mysql-specific functionality to the general
'sql-bricks' library. The 'sql-bricks' library is well tested, so the tests below cover only the extension functionality, which includes
only a few functions
*/
describe("MySQL Query Builder Tests", function () {
describe("insert", function () {
it("should return correct insert query when using '.onDuplicateKeyUpdate' option with single column update", function () {
let values = [[123, 'Moshe', 41, 92], [456, 'David', 34, 87], [789, 'Rachel', 22, 98]];
let expectedQuery = 'INSERT INTO main.some_table (id, name, age, grade) ' +
'VALUES (123, \'Moshe\', 41, 92), (456, \'David\', 34, 87), (789, \'Rachel\', 22, 98) ' +
'ON DUPLICATE KEY UPDATE grade = VALUES(grade)';
let actualQuery = sql.insert('main.some_table', 'id', 'name', 'age', 'grade')
.values(values)
.onDuplicateKeyUpdate(['grade'])
.toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
it("should return correct insert query when using '.onDuplicateKeyUpdate' option with multiple column update", function () {
let values = [[123, 'Moshe', 41, 92], [456, 'David', 34, 87], [789, 'Rachel', 22, 98]];
let expectedQuery = 'INSERT INTO main.some_table (id, name, age, grade) ' +
'VALUES (123, \'Moshe\', 41, 92), (456, \'David\', 34, 87), (789, \'Rachel\', 22, 98) ' +
'ON DUPLICATE KEY UPDATE age = VALUES(age), grade = VALUES(grade)';
let actualQuery = sql.insert('main.some_table', 'id', 'name', 'age', 'grade')
.values(values)
.onDuplicateKeyUpdate(['age', 'grade'])
.toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
it("should return correct insert query when using '.onDuplicateKeyUpdate' option with column-value pair", function () {
let values = [[123, 'Moshe', 41, 92, 0], [456, 'David', 34, 87, 0], [789, 'Rachel', 22, 98, 0]];
let expectedQuery = 'INSERT INTO main.some_table (id, name, age, grade, counter) ' +
'VALUES (123, \'Moshe\', 41, 92, 0), (456, \'David\', 34, 87, 0), (789, \'Rachel\', 22, 98, 0) ' +
'ON DUPLICATE KEY UPDATE age = VALUES(age), grade = VALUES(grade), counter = counter + 1';
let actualQuery = sql.insert('main.some_table', 'id', 'name', 'age', 'grade', 'counter')
.values(values)
.onDuplicateKeyUpdate(['age', 'grade', { counter: 'counter + 1' }])
.toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
it("should ignore '.onDuplicateKeyUpdate' when cols is null", function () {
let values = [[123, 'Moshe', 41, 92], [456, 'David', 34, 87], [789, 'Rachel', 22, 98]];
let expectedQuery = 'INSERT INTO main.some_table (id, name, age, grade) ' +
'VALUES (123, \'Moshe\', 41, 92), (456, \'David\', 34, 87), (789, \'Rachel\', 22, 98)';
let actualQuery = sql.insert('main.some_table', 'id', 'name', 'age', 'grade')
.values(values)
.onDuplicateKeyUpdate()
.toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
it("should ignore '.onDuplicateKeyUpdate' when cols is not array", function () {
let values = [[123, 'Moshe', 41, 92], [456, 'David', 34, 87], [789, 'Rachel', 22, 98]];
let expectedQuery = 'INSERT INTO main.some_table (id, name, age, grade) ' +
'VALUES (123, \'Moshe\', 41, 92), (456, \'David\', 34, 87), (789, \'Rachel\', 22, 98)';
let actualQuery = sql.insert('main.some_table', 'id', 'name', 'age', 'grade')
.values(values)
.onDuplicateKeyUpdate('grade')
.toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
it("should ignore '.onDuplicateKeyUpdate' when cols empty array", function () {
let values = [[123, 'Moshe', 41, 92], [456, 'David', 34, 87], [789, 'Rachel', 22, 98]];
let expectedQuery = 'INSERT INTO main.some_table (id, name, age, grade) ' +
'VALUES (123, \'Moshe\', 41, 92), (456, \'David\', 34, 87), (789, \'Rachel\', 22, 98)';
let actualQuery = sql.insert('main.some_table', 'id', 'name', 'age', 'grade')
.values(values)
.onDuplicateKeyUpdate([])
.toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
it("should return correct insert query when using 'ignore'", function () {
let values = [[123, 'Moshe', 41, 92], [456, 'David', 34, 87], [789, 'Rachel', 22, 98]];
let expectedQuery = 'INSERT IGNORE INTO main.some_table (id, name, age, grade) ' +
'VALUES (123, \'Moshe\', 41, 92), (456, \'David\', 34, 87), (789, \'Rachel\', 22, 98)';
let actualQuery = sql.insert('main.some_table', 'id', 'name', 'age', 'grade')
.ignore()
.values(values)
.toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
it("should quote reserved words with grave accent (backtick)", function () {
let values = [[123, 'Moshe', 41, 92], [456, 'David', 34, 87], [789, 'Rachel', 22, 98]];
let expectedQuery = 'INSERT INTO main.some_table (`user`, name, `order`, grade) ' +
'VALUES (123, \'Moshe\', 41, 92), (456, \'David\', 34, 87), (789, \'Rachel\', 22, 98)';
let actualQuery = sql.insert('main.some_table', 'user', 'name', 'order', 'grade')
.values(values)
.toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
});
describe("select", function () {
it("should return correct select query using limit", function () {
let expectedQuery = 'SELECT * FROM main.some_table LIMIT 100';
let actualQuery = sql.select().from('main.some_table').limit(100).toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
it("should ignore limit when value is null", function () {
let expectedQuery = 'SELECT * FROM main.some_table';
let actualQuery = sql.select().from('main.some_table').limit().toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
it("should return correct select query using limit and offset", function () {
let expectedQuery = 'SELECT * FROM main.some_table LIMIT 100 OFFSET 50';
let actualQuery = sql.select().from('main.some_table').limit(100).offset(50).toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
it("should ignore offset when value is null", function () {
let expectedQuery = 'SELECT * FROM main.some_table LIMIT 100';
let actualQuery = sql.select().from('main.some_table').limit(100).offset().toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
});
describe("delete", function() {
it("should return correct delete query with limit", function () {
let expectedQuery = 'DELETE FROM main.some_table LIMIT 100';
let actualQuery = sql.delete('main.some_table').limit(100).toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
it("should return correct delete query with where and limit", function () {
let expectedQuery = 'DELETE FROM main.some_table WHERE id > 200 LIMIT 100';
let actualQuery = sql.delete('main.some_table').where(sql.gt('id', 200)).limit(100).toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
it("should return correct delete query with order by when passing columns as array", function () {
let expectedQuery = 'DELETE FROM main.some_table ORDER BY name,age desc,grade';
let actualQuery = sql.delete('main.some_table').orderBy(['name', 'age desc', 'grade']).toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
it("should return correct delete query with order by when passing columns as separate arguments", function () {
let expectedQuery = 'DELETE FROM main.some_table ORDER BY name, age desc, grade';
let actualQuery = sql.delete('main.some_table').orderBy('name', 'age desc', 'grade').toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
it("should return correct delete query without order by when passing null", function () {
let expectedQuery = 'DELETE FROM main.some_table';
let actualQuery = sql.delete('main.some_table').orderBy().toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
it("should return correct delete query without order by when passing empty array", function () {
let expectedQuery = 'DELETE FROM main.some_table';
let actualQuery = sql.delete('main.some_table').orderBy([]).toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
it("should return correct delete query without order by when passing empty string", function () {
let expectedQuery = 'DELETE FROM main.some_table';
let actualQuery = sql.delete('main.some_table').orderBy('').toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
it("should return correct delete query with order by and limit", function () {
let expectedQuery = 'DELETE FROM main.some_table ORDER BY name, age desc, grade LIMIT 100';
let actualQuery = sql.delete('main.some_table')
.orderBy('name', 'age desc', 'grade')
.limit(100)
.toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
})
describe("update", function() { // not repeating similar tests for limit and order by in "delete" class
it("should return correct update query with limit clause", function () {
let expectedQuery = 'UPDATE main.some_table SET grade = 90 LIMIT 100';
let actualQuery = sql.update('main.some_table', { grade: 90}).limit(100).toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
it("should return correct update query with where, limit clauses", function () {
let expectedQuery = 'UPDATE main.some_table SET grade = 90 WHERE grade > 90 LIMIT 100';
let actualQuery = sql.update('main.some_table', { grade: 90}).where(sql.gt('grade', 90)).limit(100).toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
it("should return correct update query with order by clause", function () {
let expectedQuery = 'UPDATE main.some_table SET grade = 90 ORDER BY age desc';
let actualQuery = sql.update('main.some_table', { grade: 90}).orderBy('age desc').toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
it("should return correct update query with where, order by, limit clauses", function () {
let expectedQuery = 'UPDATE main.some_table SET grade = 90 WHERE grade > 90 ORDER BY age desc LIMIT 100';
let actualQuery = sql.update('main.some_table', { grade: 90})
.where(sql.gt('grade', 90))
.orderBy('age desc')
.limit(100)
.toString();
return expect(actualQuery).to.equal(expectedQuery.replace(/\n/g, ' ').replace(/\s\s+/g, ' '));
});
});
});