-
Notifications
You must be signed in to change notification settings - Fork 7
/
boolean_fields.test.js
181 lines (142 loc) · 4.39 KB
/
boolean_fields.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
'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,
aGoodMan: Boolean
});
bw.conn.model('BooleanTest', schema);
t.ok(bw.conn.models.BooleanTest);
});
test('clean up test collections', async () => {
await bw.conn.models.BooleanTest.deleteMany({}).exec();
});
test('initialization of API server', async t => {
await bw.createServer({
models: bw.conn.models
});
t.equal(
bw.fastify.mongooseAPI.apiRouters.BooleanTest.collectionName,
'booleantests',
'Collection name used in API path'
);
});
test('POST item test', async t => {
let response = null;
response = await bw.inject(t, {
method: 'POST',
url: '/api/booleantests',
payload: { name: 'Good', aGoodMan: true }
});
t.match(response.json(), { name: 'Good', aGoodMan: true }, 'POST api ok');
response = await bw.inject(t, {
method: 'GET',
url: '/api/booleantests'
});
t.equal(response.json().total, 1, 'There is one good man');
});
test('POST item false test', async t => {
let response = null;
response = await bw.inject(t, {
method: 'POST',
url: '/api/booleantests',
body: { name: 'Bad', aGoodMan: false }
});
t.match(response.json(), { name: 'Bad', aGoodMan: false }, 'POST api ok');
response = await bw.inject(t, {
method: 'GET',
url: '/api/booleantests'
});
t.equal(response.json().total, 2, 'There are 2 men');
});
test('Update to false test', async t => {
let response = null;
response = await bw.inject(t, {
method: 'GET',
url: '/api/booleantests'
});
t.equal(response.json().total, 2, 'There re 2 men');
let goodId = null;
let foundGood = false;
for (let item of response.json().items) {
if (item.aGoodMan === true) {
goodId = item._id;
foundGood = true;
}
}
t.ok(foundGood);
response = await bw.inject(t, {
method: 'PUT',
url: '/api/booleantests/' + goodId,
payload: { aGoodMan: false }
});
t.match(response.json(), { name: 'Good', aGoodMan: false }, 'PUT api ok');
});
test('GET collection filtering', async t => {
const response = await bw.inject(t, {
method: 'GET',
url: '/api/booleantests',
query: { filter: 'aGoodMan=0' }
});
t.equal(response.json().total, 2, 'API returns 2 filtered men');
t.equal(response.json().items.length, 2, 'API returns 2 filtered men');
});
test('And back to true', async t => {
let response = null;
response = await bw.inject(t, {
method: 'GET',
url: '/api/booleantests'
});
t.equal(response.json().total, 2, 'There re 2 men');
let goodId = null;
let foundGood = false;
for (let item of response.json().items) {
if (item.name === 'Good') {
goodId = item._id;
foundGood = true;
}
}
t.ok(foundGood);
response = await bw.inject(t, {
method: 'PUT',
url: '/api/booleantests/' + goodId,
payload: { aGoodMan: true }
});
t.match(response.json(), { name: 'Good', aGoodMan: true }, 'PUT api ok');
});
test('GET collection filtering', async t => {
let response = null;
response = await bw.inject(t, {
method: 'GET',
url: '/api/booleantests',
query: { filter: 'aGoodMan=0' }
});
t.equal(response.json().total, 1, 'API returns 1 filtered man');
t.equal(response.json().items.length, 1, 'API returns 1 filtered man');
t.match(
response.json().items[0],
{ name: 'Bad', aGoodMan: false },
'Filtered author'
);
});
test('GET collection filtering', async t => {
const response = await bw.inject(t, {
method: 'GET',
url: '/api/booleantests',
query: { filter: 'aGoodMan=1' }
});
t.equal(response.json().total, 1, 'API returns 1 filtered man');
t.equal(response.json().items.length, 1, 'API returns 1 filtered man');
t.match(
response.json().items[0],
{ name: 'Good', aGoodMan: true },
'Filtered author'
);
});