-
Notifications
You must be signed in to change notification settings - Fork 0
/
queryGenerator.test.js
101 lines (84 loc) · 3.37 KB
/
queryGenerator.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
const sequery = require('../../src');
const expect = require('chai').expect;
const config = require('../__config/default');
describe('MySQL - queryGenerator', async () => {
it('init sequery', () => {
sequery.init(config.use('mysql'));
});
it(`.getWhereConditions(where) // where = {"id": 2}`, () => {
const where = {"id": 2};
const whereStr = sequery.getWhereConditions(where);
expect(whereStr === '`id` = 2').to.be.true;
});
it(`.getWhereConditions(where, tableAs) // where = {"id": 2}, tableAs = 'm'`, () => {
const where = {"id": 2};
const tableAs = 'm';
const whereStr = sequery.getWhereConditions(where, tableAs);
expect(whereStr === '`m`.`id` = 2').to.be.true;
});
it(`.getWhereConditions(where) // where = '{"id": 2}'`, () => {
const where = '{"id": 2}';
const whereStr = sequery.getWhereConditions(where);
expect(whereStr === '`id` = 2').to.be.true;
});
it(`.getWhereConditions(where) // where = {"id": {$gt: 2}}`, () => {
const where = {"id": {$gt: 2}};
const whereStr = sequery.getWhereConditions(where);
expect(whereStr === '`id` > 2').to.be.true;
});
it(`.getWhereConditions(where) // where = {id: {[Op.or]: {[Op.lt]: 1000, [Op.eq]: null}}}`, () => {
const Op = sequery.Sequelize.Op;
const where = {
id: {
[Op.or]: {
[Op.lt]: 1000,
[Op.eq]: null
}
}
};
const whereStr = sequery.getWhereConditions(where);
expect(whereStr === '(`id` < 1000 OR `id` IS NULL)').to.be.true;
});
it(`.getOrderClause(order) // order = 'id'`, () => {
const order = 'id';
const orderStr = sequery.getOrderClause(order);
expect(orderStr === ' order by `id`').to.be.true;
});
it(`.getOrderClause(order) // order = 'type, name desc'`, () => {
const order = 'type, name desc';
const orderStr = sequery.getOrderClause(order);
expect(orderStr === ' order by `type`, `name` desc').to.be.true;
});
it(`.getOrderClause(order) // order = ['type', 'name desc']`, () => {
const order = ['type', 'name desc'];
const orderStr = sequery.getOrderClause(order);
expect(orderStr === ' order by `type`, `name` desc').to.be.true;
});
it(`.getOrderClause(order) // order = [['type'], ['name', 'desc']]`, () => {
const order = [['type'], ['name', 'desc']];
const orderStr = sequery.getOrderClause(order);
expect(orderStr === ' order by `type`, `name` desc').to.be.true;
});
it(`.getOrderClause(order, tableAs) // order = ['type', 'name desc'], tableAs = 'm'`, () => {
const order = ['type', 'name desc'];
const tableAs = 'm';
const orderStr = sequery.getOrderClause(order, tableAs);
expect(orderStr === ' order by `m`.`type`, `m`.`name` desc').to.be.true;
});
it(`.getGroupClause(group) // group = 'id'`, () => {
const group = 'id';
const groupStr = sequery.getGroupClause(group);
expect(groupStr === ' group by `id`').to.be.true;
});
it(`.getLimitClause(options) // options = {order: 'id', limit: 10, offset: 5}`, () => {
const options = {order: 'id', limit: 10, offset: 5};
const limitStr = sequery.getLimitClause(options);
expect(limitStr === ' order by `id` limit 5, 10').to.be.true;
});
it(`.getLimitClause(options, tableAs) // options = {order: 'id', limit: 10, offset: 5}, tableAs = 'm'`, () => {
const options = {order: 'id', tableAs: 'm', limit: 10, offset: 5};
const tableAs = 'm';
const limitStr = sequery.getLimitClause(options, tableAs);
expect(limitStr === ' order by `m`.`id` limit 5, 10').to.be.true;
});
});