-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.js
399 lines (368 loc) · 12.2 KB
/
test.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
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
393
394
395
396
397
398
399
var DB = require('./index');
var d = new DB();
/**
* 测试增语句
*/
test('add module: 增sql', function() {
var ret = d.add({
table: 'user',
data: {
username: 'asdasd',
password: 12345,
email: 'asd@as.sdf',
sex: 1,
description: 'yoyo',
head: 'asdasd'
}
});
expect(ret.sql).toBe('INSERT INTO `user` ( `username` , `password` , `email` , `sex` , `description` , `head` ) VALUES ( ? , ? , ? , ? , ? , ? )');
expect(ret.data).toEqual(['asdasd', 12345, 'asd@as.sdf', 1, 'yoyo', 'asdasd']);
});
test('add module: 增sql,批量添加', function() {
var ret = d.add({
table: 'user',
data: [{
username: 'asdasd',
password: 12345,
email: 'asd@as.sdf',
sex: 1,
description: 'yoyo',
head: 'asdasd'
}, {
username: 'qqwwrr',
password: 12345,
email: 'qqwwee@yee.sdf',
sex: 0,
description: 'nnn',
head: 'sw'
}, {
username: 'hwerwer',
password: 12345,
email: 'asd@hqwe.sdf',
sex: 1,
description: 'wetttt',
head: 'asdherjhasd'
}]
});
expect(ret.sql).toBe('INSERT INTO `user` ( `username` , `password` , `email` , `sex` , `description` , `head` ) VALUES (? , ? , ? , ? , ? , ?) , (? , ? , ? , ? , ? , ?) , (? , ? , ? , ? , ? , ?)');
expect(ret.data).toEqual(['asdasd', 12345, 'asd@as.sdf', 1, 'yoyo', 'asdasd', 'qqwwrr', 12345, 'qqwwee@yee.sdf', 0, 'nnn', 'sw', 'hwerwer', 12345, 'asd@hqwe.sdf', 1, 'wetttt', 'asdherjhasd']);
});
/**
* 测试删语句
*/
test('del module: 删sql,条件为字符串', function() {
var ret = d.del({
table: 'share',
condition: 'id = 123'
});
expect(ret.sql).toBe('DELETE FROM `share` WHERE id = 123');
expect(ret.data).toEqual([]);
});
test('del module: 删sql,条件为对象', function() {
var ret = d.del({
table: 'share',
condition: {
username: 'gasgg',
create_time: 1322456678112,
parent_id: 14
}
});
expect(ret.sql).toBe('DELETE FROM `share` WHERE `share`.`username` = ? AND `share`.`create_time` = ? AND `share`.`parent_id` = ?');
expect(ret.data).toEqual(['gasgg', 1322456678112, 14]);
});
test('del module: 删sql,条件为对象,条件使用or连接', function() {
var ret = d.del({
table: 'share',
condition: [
{username: 'gasgg'},
{create_time: 1322456678112},
{parent_id: 14}
]
});
expect(ret.sql).toBe('DELETE FROM `share` WHERE `share`.`username` = ? OR `share`.`create_time` = ? OR `share`.`parent_id` = ?');
expect(ret.data).toEqual(['gasgg', 1322456678112, 14]);
});
test('del module: 删sql,条件为对象,存在对象值为数组', function() {
var ret = d.del({
table: 'share',
condition: {
id: [12, 22, 55, 66]
}
});
expect(ret.sql).toBe('DELETE FROM `share` WHERE `share`.`id` in (? , ? , ? , ?)');
expect(ret.data).toEqual([12, 22, 55, 66]);
});
test('del module: 删sql,条件为对象,存在多个对象值为数组', function() {
var ret = d.del({
table: 'share',
condition: {
id: [12, 22, 55, 66],
parent_id: [2, 12]
}
});
expect(ret.sql).toBe('DELETE FROM `share` WHERE `share`.`id` in (? , ? , ? , ?) AND `share`.`parent_id` in (? , ?)');
expect(ret.data).toEqual([12, 22, 55, 66, 2, 12]);
});
test('del module: 删sql,条件为对象,只以对象值作为条件', function() {
var ret = d.del({
table: 'share',
condition: {
'0': 'id < 12',
'11': 'id > 1'
}
});
expect(ret.sql).toBe('DELETE FROM `share` WHERE id < 12 AND id > 1');
});
/**
* 测试改语句
*/
test('update module: 改sql,条件为字符串', function() {
var ret = d.update({
table: 'share',
data: {
username: 'ggsd',
parent_id: 3434
},
condition: 'id = 123'
});
expect(ret.sql).toBe('UPDATE `share` SET `username` = ? , `parent_id` = ? WHERE id = 123');
expect(ret.data).toEqual(['ggsd', 3434]);
});
test('update module: 改sql,条件为对象', function() {
var ret = d.update({
table: 'share',
data: {
username: 'ggsd',
parent_id: 3434
},
condition: {
username: 'gasgg',
create_time: 1322456678112,
parent_id: 14
}
});
expect(ret.sql).toBe('UPDATE `share` SET `username` = ? , `parent_id` = ? WHERE `share`.`username` = ? AND `share`.`create_time` = ? AND `share`.`parent_id` = ?');
expect(ret.data).toEqual(['ggsd', 3434, 'gasgg', 1322456678112, 14]);
});
test('update module: 改sql,条件为对象,存在对象值为数组', function() {
var ret = d.update({
table: 'share',
data: {
username: 'ggsd',
parent_id: 3434
},
condition: {
id: [12, 22, 55, 66]
}
});
expect(ret.sql).toBe('UPDATE `share` SET `username` = ? , `parent_id` = ? WHERE `share`.`id` in (? , ? , ? , ?)');
expect(ret.data).toEqual(['ggsd', 3434, 12, 22, 55, 66]);
});
test('update module: 改sql,条件为对象,存在多个对象值为数组', function() {
var ret = d.update({
table: 'share',
data: {
username: 'ggsd',
parent_id: 3434
},
condition: {
id: [12, 22, 55, 66],
parent_id: [2, 12]
}
});
expect(ret.sql).toBe('UPDATE `share` SET `username` = ? , `parent_id` = ? WHERE `share`.`id` in (? , ? , ? , ?) AND `share`.`parent_id` in (? , ?)');
expect(ret.data).toEqual(['ggsd', 3434, 12, 22, 55, 66, 2, 12]);
});
/**
* 测试查语句
*/
test('find module: 查sql,条件为字符串', function() {
var ret = d.find({
table: 'share',
condition: 'id = 123'
});
expect(ret.sql).toBe('SELECT * FROM `share` WHERE id = 123');
expect(ret.data).toEqual([]);
});
test('find module: 查sql,条件为对象', function() {
var ret = d.find({
table: 'share',
condition: {
username: 'gasgg',
create_time: 1322456678112,
parent_id: 14
}
});
expect(ret.sql).toBe('SELECT * FROM `share` WHERE `share`.`username` = ? AND `share`.`create_time` = ? AND `share`.`parent_id` = ?');
expect(ret.data).toEqual(['gasgg', 1322456678112, 14]);
});
test('find module: 查sql,条件为对象,存在对象值为数组', function() {
var ret = d.find({
table: 'share',
condition: {
id: [12, 22, 55, 66]
}
});
expect(ret.sql).toBe('SELECT * FROM `share` WHERE `share`.`id` in (? , ? , ? , ?)');
expect(ret.data).toEqual([12, 22, 55, 66]);
});
test('find module: 查sql,条件为对象,存在多个对象值为数组', function() {
var ret = d.find({
table: 'share',
condition: {
id: [12, 22, 55, 66],
parent_id: [2, 12]
}
});
expect(ret.sql).toBe('SELECT * FROM `share` WHERE `share`.`id` in (? , ? , ? , ?) AND `share`.`parent_id` in (? , ?)');
expect(ret.data).toEqual([12, 22, 55, 66, 2, 12]);
});
test('find module: 查sql,条件为字符串,存在分页', function() {
var ret = d.find({
table: 'share',
offset: 10,
limit: 20,
condition: 'id = 123'
});
expect(ret.sql).toBe('SELECT * FROM `share` WHERE id = 123 LIMIT ? OFFSET ?');
expect(ret.data).toEqual([20, 10]);
});
test('find module: 查sql,条件为字符串,存在分页,搜索值为对象', function() {
var ret = d.find({
table: 'share',
offset: 10,
limit: 20,
like: {
value: 'xxx',
type: 'front',
attr: ['username', 'id']
},
condition: 'id = 123'
});
expect(ret.sql).toBe('SELECT * FROM `share` WHERE id = 123 OR `share`.`username` LIKE "%xxx" OR `share`.`id` LIKE "%xxx" LIMIT ? OFFSET ?');
expect(ret.data).toEqual([20, 10]);
});
test('find module: 查sql,条件为字符串,存在分页,搜索值为对象数组', function() {
var ret = d.find({
table: 'share',
offset: 10,
limit: 20,
like: [{
value: 'xxx',
type: 'front',
attr: ['username', 'id']
}, {
value: 'yyy',
type: 'all',
attr: 'create_time'
}],
condition: 'id = 123'
});
expect(ret.sql).toBe('SELECT * FROM `share` WHERE id = 123 OR `share`.`username` LIKE "%xxx" OR `share`.`id` LIKE "%xxx" OR `share`.`create_time` LIKE "%yyy%" LIMIT ? OFFSET ?');
expect(ret.data).toEqual([20, 10]);
});
test('find module: 查sql,条件为字符串,排序值为对象数组', function() {
var ret = d.find({
table: 'share',
order: [{
attr: 'other.username',
type: 'desc'
}, {
attr: 'create_time',
type: 'asc'
}],
condition: 'id = 123'
});
expect(ret.sql).toBe('SELECT * FROM `share` WHERE id = 123 ORDER BY `other`.`username` DESC , `share`.`create_time` ASC');
expect(ret.data).toEqual([]);
});
test('find module: 查sql,使用表别名', function() {
var ret = d.find({
table: 'share',
alias: {
share: 's'
},
condition: 's.id = 123'
});
expect(ret.sql).toBe('SELECT * FROM `share` AS `s` WHERE s.id = 123');
expect(ret.data).toEqual([]);
});
test('find module: 查sql,使用表别名,进行表连接,值为对象数组', function() {
var ret = d.find({
table: 'share',
alias: {
share: 's',
user: 'u',
blog: 'b'
},
join: [{
table: 'user',
type: 'full',
on: 'u.username = s.username'
}, {
table: 'blog',
on: {
def: 'b.username = s.username',
condition: {
a: 'sss'
}
}
}],
condition: 's.id = 123'
});
expect(ret.sql).toBe('SELECT * FROM `share` AS `s` FULL JOIN `user` AS `u` ON u.username = s.username LEFT JOIN `blog` AS `b` ON b.username = s.username AND `share`.`a` = ? WHERE s.id = 123');
expect(ret.data).toEqual(['sss']);
});
test('find module: 查sql,使用分组', function() {
var ret = d.find({
table: 'share',
group: {
attr: 'username',
having: 'COUNT(*) > 10'
}
});
expect(ret.sql).toBe('SELECT * FROM `share` GROUP BY `share`.`username` HAVING COUNT(*) > 10');
expect(ret.data).toEqual([]);
});
test('find module: 查sql,条件为字符串,使用表别名,进行表连接,获取方式为自定义', function() {
var ret = d.find({
table: 'share',
alias: {
share: 's',
user: 'u'
},
join: {
table: 'user',
type: 'left',
on: 'u.username = s.username'
},
field: {
attr: ['u.email', 'u.description', 's.id', 's.username'],
map: {
'u.username': 'linkUsername'
}
},
condition: 'id = 123'
});
expect(ret.sql).toBe('SELECT `u`.`email` , `u`.`description` , `s`.`id` , `s`.`username` , `u`.`username` AS `linkUsername` FROM `share` AS `s` LEFT JOIN `user` AS `u` ON u.username = s.username WHERE id = 123');
expect(ret.data).toEqual([]);
});
test('find module: 查sql,字段为字符串', function() {
var ret = d.find({
table: 'share',
field: 'COUNT(*) AS `total`'
});
expect(ret.sql).toBe('SELECT COUNT(*) AS `total` FROM `share`');
expect(ret.data).toEqual([]);
});
test('find module: 查sql,条件为字符串,使用union', function() {
var ret = d.find([{
table: 'share',
condition: 'id = 123'
}, {
table: 'share',
condition: 'id < 10'
}]);
expect(ret.sql).toBe('SELECT * FROM `share` WHERE id = 123 UNION SELECT * FROM `share` WHERE id < 10');
expect(ret.data).toEqual([]);
});