-
Notifications
You must be signed in to change notification settings - Fork 7
/
complex_where.test.js
173 lines (140 loc) · 4.44 KB
/
complex_where.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
'use strict';
const t = require('tap');
const { test } = t;
const mongoose = require('mongoose');
const BackwardWrapper = require('./BackwardWrapper.js');
const bw = new BackwardWrapper(t);
test('mongoose db initialization', async () => {
await bw.createConnection();
});
test('schema initialization', async t => {
const schema = mongoose.Schema({
name: String,
appleCount: Number,
bananaCount: Number
});
bw.conn.model('WhereTest', schema);
t.ok(bw.conn.models.WhereTest);
});
test('clean up test collections', async () => {
await bw.conn.models.WhereTest.deleteMany({}).exec();
});
test('initialization of API server', async t => {
await bw.createServer({
models: bw.conn.models
});
t.equal(
bw.fastify.mongooseAPI.apiRouters.WhereTest.collectionName,
'wheretests',
'Collection name used in API path'
);
});
const bob = { name: 'Bob', appleCount: 1, bananaCount: 2 };
const rob = { name: 'Rob', appleCount: 2, bananaCount: 3 };
const alice = { name: 'Alice', appleCount: 50, bananaCount: 90 };
test('POST item test', async t => {
let response = null;
response = await bw.inject(t, {
method: 'POST',
url: '/api/wheretests',
payload: bob
});
t.match(response.json(), bob, 'POST api ok');
response = await bw.inject(t, {
method: 'POST',
url: '/api/wheretests',
payload: rob
});
t.match(response.json(), rob, 'POST api ok');
response = await bw.inject(t, {
method: 'POST',
url: '/api/wheretests',
payload: alice
});
t.match(response.json(), alice, 'POST api ok');
response = await bw.inject(t, {
method: 'GET',
url: '/api/wheretests'
});
t.equal(response.json().total, 3, 'There re 3 banana holders');
});
test('GET collection complex where', async t => {
let response = null;
response = await bw.inject(t, {
method: 'GET',
url: '/api/wheretests',
query: { where: '{"bananaCount": 2}' }
});
t.equal(response.json().total, 1, 'API returns 1 filtered');
t.equal(response.json().items.length, 1, 'API returns 1 filtered');
t.match(response.json().items[0], bob, 'Filtered');
response = await bw.inject(t, {
method: 'GET',
url: '/api/wheretests',
query: { where: JSON.stringify({ appleCount: { $gt: 10 } }) }
});
t.equal(response.json().total, 1, 'API returns 1 filtered');
t.equal(response.json().items.length, 1, 'API returns 1 filtered');
t.match(response.json().items[0], alice, 'Filtered');
response = await bw.inject(t, {
method: 'GET',
url: '/api/wheretests',
query: {
where: JSON.stringify({
$and: [{ appleCount: { $gt: 1 } }, { bananaCount: { $lt: 5 } }]
})
}
});
t.equal(response.json().total, 1, 'API returns 1 filtered');
t.equal(response.json().items.length, 1, 'API returns 1 filtered');
t.match(response.json().items[0], rob, 'Filtered');
// invalid where
response = await bw.inject(
t,
{
method: 'GET',
url: '/api/wheretests',
query: {
where: JSON.stringify({
name: { $nonvalid: false }
})
}
},
500
);
// $regex
response = await bw.inject(t, {
method: 'GET',
url: '/api/wheretests',
query: {
where: JSON.stringify({
name: { $regex: '^A' }
})
}
});
t.equal(response.json().total, 1, 'API returns 1 filtered');
t.equal(response.json().items.length, 1, 'API returns 1 filtered');
t.match(response.json().items[0], alice, 'Filtered');
response = await bw.inject(t, {
method: 'GET',
url: '/api/wheretests',
query: {
where: JSON.stringify({
name: { $regex: '^a' }
})
}
});
t.equal(response.json().total, 0, 'API returns 0 filtered');
response = await bw.inject(t, {
method: 'GET',
url: '/api/wheretests',
query: {
where: JSON.stringify({
name: { $regex: '^a', $options: 'i' }
})
}
});
t.equal(response.json().total, 1, 'API returns 1 filtered');
t.equal(response.json().items.length, 1, 'API returns 1 filtered');
t.match(response.json().items[0], alice, 'Filtered');
});