-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.crud.select.test.js
194 lines (159 loc) · 6.93 KB
/
query.crud.select.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
const expect = require('chai').expect;
const my = require('../../src');
const config = require('../__config/default');
const tools = require('../__tools');
const {prepareForTesting, _________________} = tools;
describe('MySQL - query/crud/select', () => {
const table = 'users';
my.init(config);
prepareForTesting();
it(`.select(table)`, async () => {
const result = await my.select(table);
expect(result.length === 7).to.be.true;
});
it(`.select(table, where) // where = {}`, async () => {
const where = {};
const result = await my.select(table, where);
expect(result.length > 0).to.be.true;
});
it(`.select(table, where) // where = ''`, async () => {
const where = {};
const result = await my.select(table, where);
expect(result.length > 0).to.be.true;
});
it(`.select(table, where) // where = "username like 'th%'"`, async () => {
const where = "username like 'th%'";
const result = await my.select(table, where);
expect(result.length === 2).to.be.true;
});
it(`.select(table, where) // where = "id in (1, 2, 3)"`, async () => {
const where = "id in (1, 2, 3)";
const result = await my.select(table, where);
expect(result.length === 3).to.be.true;
});
it(`.select(table, where) // where = {username: "thor"}`, async () => {
const where = {username: "thor"};
const result = await my.select(table, where);
expect(result[0].username === "thor").to.be.true;
});
it(`.select(table, where) // where = {username: {[Op.like]: "th%"}}`, async () => {
const Op = my.sequelizeOp;
const where = {username: {[Op.like]: "th%"}};
const result = await my.select(table, where);
expect(result.length === 2).to.be.true;
});
it(`.select(table, fields) // fields = "username"`, async () => {
const fields = "username";
const result = await my.select(table, fields);
expect(Object.keys(result[0]).length === 1).to.be.true;
});
it(`.select(table, fields) // fields = "id, username"`, async () => {
const fields = "id, username";
const result = await my.select(table, fields);
expect(Object.keys(result[0]).length === 2).to.be.true;
});
it(`.select(table, fields) // fields = ["id", "username"]`, async () => {
const fields = ["id", "username"];
const result = await my.select(table, fields);
expect(Object.keys(result[0]).length === 2).to.be.true;
});
it(`.select(table, fields, where) // fields = "username", where = "username like 'th%'"`, async () => {
const fields = "username";
const where = "username like 'th%'";
const result = await my.select(table, fields, where);
expect(Object.keys(result[0]).length === 1 && result.length === 2).to.be.true;
});
it(`.select(table, fields, where) // fields = "id, username", where = "username like 'th%'"`, async () => {
const fields = "id, username";
const where = "username like 'th%'";
const result = await my.select(table, fields, where);
expect(Object.keys(result[0]).length === 2 && result.length === 2).to.be.true;
});
it(`.select(table, fields, where) // fields = ["id", "username"], where = "username like 'th%'"`, async () => {
const fields = ["id", "username"];
const where = "username like 'th%'";
const result = await my.select(table, fields, where);
expect(Object.keys(result[0]).length === 2 && result.length === 2).to.be.true;
});
_________________();
it(`.select({table, where}) // where = "username like 'th%'"`, async () => {
const where = "username like 'th%'";
const result = await my.select({table, where});
expect(result.length === 2).to.be.true;
});
it(`.select({table, fields}) // fields = "username"`, async () => {
const fields = "username";
const result = await my.select({table, fields});
expect(Object.keys(result[0]).length === 1).to.be.true;
});
it(`.select({table, fields}) // fields = "id, username"`, async () => {
const fields = "id, username";
const result = await my.select({table, fields});
expect(Object.keys(result[0]).length === 2).to.be.true;
});
it(`.select({table, fields}) // fields = ["id", "username"]`, async () => {
const fields = ["id", "username"];
const result = await my.select({table, fields});
expect(Object.keys(result[0]).length === 2).to.be.true;
});
it(`.select({table, fields, where}) // fields = "username", where = "username like 'th%'"`, async () => {
const fields = "username";
const where = "username like 'th%'";
const result = await my.select({table, fields, where});
expect(Object.keys(result[0]).length === 1 && result.length === 2).to.be.true;
});
it(`.select({table, fields, where}) // fields = "id, username", where = "username like 'th%'"`, async () => {
const fields = "id, username";
const where = "username like 'th%'";
const result = await my.select({table, fields, where});
expect(Object.keys(result[0]).length === 2 && result.length === 2).to.be.true;
});
it(`.select({table, fields, where}) // fields = ["id", "username"], where = "username like 'th%'"`, async () => {
const fields = ["id", "username"];
const where = "username like 'th%'";
const result = await my.select({table, fields, where});
expect(Object.keys(result[0]).length === 2 && result.length === 2).to.be.true;
});
_________________();
it(`.select({table, fields, isGroup}) // fields = 'username', isGroup = true`, async () => {
const fields = 'username';
const result = await my.select({table, fields, isGroup: true});
expect(result.length === 7).to.be.true;
});
it(`.select({table, fields, group}) // fields = 'username', group = 'username'`, async () => {
const fields = 'username';
const result = await my.select({table, fields, isGroup: true});
expect(result.length === 7).to.be.true;
});
_________________();
it(`.select({table, order}) // order = 'username'`, async () => {
const order = 'username';
const result = await my.select({table, order});
expect(result[0].username === 'anthonyStark').to.be.true;
});
it(`.select({table, order, limit}) // order = 'username', limit = 3`, async () => {
const order = 'username';
const limit = 3;
const result = await my.select({table, order, limit});
expect(result.length === 3 && result[0].username === 'anthonyStark').to.be.true;
});
it(`.select({table, fields, limit}) // fields = 'username', limit = 3`, async () => {
const fields = 'username';
const limit = 3;
const result = await my.select({table, fields, limit});
expect(result.length === 3 && result[0].username === 'anthonyStark').to.be.true;
});
it(`.select({table, order, limit, offset}) // order = 'username', limit = 3, offset = 2`, async () => {
const order = 'username';
const limit = 3;
const offset = 2;
const result = await my.select({table, order, limit, offset});
expect(result.length === 3 && result[0].username === 'natasha').to.be.true;
});
it(`.select({table, order, offset}) // order = 'username', offset = 2 (limit = 1)`, async () => {
const order = 'username';
const offset = 2;
const result = await my.select({table, order, offset});
expect(result.length === 1 && result[0].username === 'natasha').to.be.true;
});
});