-
Notifications
You must be signed in to change notification settings - Fork 0
/
count.js
248 lines (221 loc) · 7.14 KB
/
count.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
// During the test the env variable is set to test
process.env.NODE_ENV = 'test';
// Require the dev-dependencies
const { assert } = require('chai');
const knex = require('../../../../knex');
const Coffee = require('../../../../models/coffee-example');
// Our parent block
describe('TABLE GATEWAY: count', () => { // eslint-disable-line
before(async () => { // eslint-disable-line
await knex.seed.run();
});
it('Empty query', async () => { // eslint-disable-line
const results = await Coffee.count({});
assert.equal(results, 4);
});
it('With query', async () => { // eslint-disable-line
const results = await Coffee.count({
price: 100,
});
assert.equal(results, 2);
});
});
describe('Malicious happy path', () => { // eslint-disable-line
before(async () => { // eslint-disable-line
await knex.seed.run();
});
it('Query is undefined', async () => { // eslint-disable-line
const results = await Coffee.count();
assert.equal(results, 4);
});
it('Query is not a valid json', async () => { // eslint-disable-line
const results = await Coffee.count('something wierd');
assert.equal(results, 4);
});
it('Query with invalid attr', (done) => { // eslint-disable-line
Coffee.count({
invalid: 'ues',
}).then(() => {
done('SHOULD NOT GET HERE');
}).catch((err) => {
assert.equal(err.code, 400);
assert.equal(err.fullMessage.routine, 'errorMissingColumn');
done();
});
});
});
describe('with advance settings: group by', () => { // eslint-disable-line
before(async () => { // eslint-disable-line
await knex.seed.run();
});
it('Give group by', async () => { // eslint-disable-line
const coffee = await Coffee.count({}, {
groupBy: 'name',
});
assert.equal(coffee.length, 3);
const counts = {
'this is the name': 2,
other: 1,
expensive: 1,
};
for (let i = 0; i < coffee.length; i++) {
const keys = Object.keys(coffee[i]);
assert.isTrue(keys.indexOf('name') > -1);
assert.isTrue(keys.indexOf('count') > -1);
const c = counts[coffee[i].name];
assert.equal(coffee[i].count, c);
}
});
it('Give group by: array', async () => { // eslint-disable-line
const coffee = await Coffee.count({}, {
groupBy: ['price', 'id'],
});
assert.equal(coffee.length, 4);
for (let i = 0; i < coffee.length; i++) {
const keys = Object.keys(coffee[i]);
assert.isTrue(keys.indexOf('price') > -1);
assert.isTrue(keys.indexOf('id') > -1);
assert.equal(coffee[i].count, 1);
}
});
it('Give group by: array and complex raw select', async () => { // eslint-disable-line
const options = {};
options.rawSelect = 'created_at::DATE as date, EXTRACT (hour from (created_at)) as hour';
options.groupBy = ['date', 'hour'];
const coffee = await Coffee.count({}, options);
assert.equal(coffee.length, 2);
for (let i = 0; i < coffee.length; i++) {
const keys = Object.keys(coffee[i]);
assert.isTrue(keys.indexOf('date') > -1);
assert.isTrue(keys.indexOf('hour') > -1);
assert.equal(coffee[i].count, 2);
}
});
it('invalid group by', (done) => { // eslint-disable-line
Coffee.count({}, {
groupBy: 'unexistant',
}).then(() => {
done('SHOULD NOT GET HERE');
}).catch((err) => {
assert.equal(err.code, 400);
assert.equal(err.fullMessage.routine, 'errorMissingColumn');
done();
});
});
// cannot do a group by
it('Give a complex group by', async () => { // eslint-disable-line
const coffee = await Coffee.count({}, {
rawSelect: '(created_at)::DATE as d',
groupBy: 'd',
});
assert.equal(coffee.length, 1);
for (let i = 0; i < coffee.length; i++) {
const keys = Object.keys(coffee[i]);
assert.isTrue(keys.indexOf('d') > -1);
assert.isTrue(keys.indexOf('count') > -1);
assert.equal(coffee[i].count, 4);
}
});
});
describe('with advance settings: order by', () => { // eslint-disable-line
before(async () => { // eslint-disable-line
await knex.seed.run();
});
it('With order by', async () => { // eslint-disable-line
const results = await Coffee.count({}, {
groupBy: 'name',
orderBy: ['count', 'desc'],
});
assert.equal(results.length, 3);
for (let i = 1; i < results.length; i++) {
assert.isTrue(results[i - 1].count >= results[i].count);
}
});
it('With invalid order by', (done) => { // eslint-disable-line
Coffee.count({}, {
orderBy: ['price', 'asc'],
}).then(() => {
done('SHOULD NOT GET HERE');
}).catch((err) => {
assert.equal(err.code, 400);
assert.equal(err.fullMessage.routine, 'check_ungrouped_columns_walker');
done();
});
});
});
describe('with advance settings: start_date and end_date', () => { // eslint-disable-line
before(async () => { // eslint-disable-line
await knex.seed.run();
});
it('With start date', async () => { // eslint-disable-line
const date = new Date('2018-11-21T12:00:00.000Z');
const results = await Coffee.count({}, {
startDate: date,
});
assert.equal(results, 2);
});
it('With end date', async () => { // eslint-disable-line
const date = new Date('2018-11-21T12:06:05.000Z');
const results = await Coffee.count({}, {
endDate: date,
});
assert.equal(results, 3);
});
it('With start day and end date', async () => { // eslint-disable-line
const startDate = new Date('2018-11-21T12:00:00.000Z');
const endDate = new Date('2018-11-21T12:06:05.000Z');
const results = await Coffee.count({}, {
endDate,
startDate,
});
assert.equal(results.length, 1);
});
it('invalid start date', (done) => { // eslint-disable-line
const date = 'this is not a date';
Coffee.count({}, {
startDate: date,
}).then(() => {
done('SHOULD NOT GET HERE');
}).catch((err) => {
assert.equal(err.code, 400);
assert.equal(err.fullMessage.routine, 'DateTimeParseError');
done();
});
});
});
describe('with advance settings: countDistinct', () => { // eslint-disable-line
before(async () => { // eslint-disable-line
await knex.seed.run();
});
it('With countDistinct', async () => { // eslint-disable-line
const results = await Coffee.count({}, {
countDistinct: 'name',
});
assert.equal(results, 3);
});
it('With invalid countDistinct', (done) => { // eslint-disable-line
Coffee.count({}, {
countDistinct: 'not a valid column',
}).then(() => {
done('SHOULD NOT GET HERE');
}).catch((err) => {
assert.equal(err.code, 400);
assert.equal(err.fullMessage.routine, 'errorMissingColumn');
done();
});
});
});
describe('with advance settings: rawSelect', () => { // eslint-disable-line
before(async () => { // eslint-disable-line
await knex.seed.run();
});
it('With max(created_at)', async () => { // eslint-disable-line
const results = await Coffee.count({}, {
rawSelect: 'max(created_at)',
});
const keys = Object.keys(results);
assert.equal(keys.length, 2);
assert.isTrue(keys.indexOf('count') > -1);
assert.isTrue(keys.indexOf('max') > -1);
});
});