-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
test-foo-bar.controller.spec.current-dir.ts
430 lines (333 loc) 路 12.6 KB
/
test-foo-bar.controller.spec.current-dir.ts
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// std
import { notStrictEqual, ok, strictEqual } from 'assert';
// 3p
import {
Context, createController, getHttpMethod, getPath,
isHttpResponseCreated, isHttpResponseMethodNotAllowed, isHttpResponseNoContent,
isHttpResponseNotFound, isHttpResponseOK
} from '@foal/core';
import { createConnection, getConnection, getConnectionOptions, getRepository } from 'typeorm';
// App
import { TestFooBarController } from './test-foo-bar.controller';
import { TestFooBar } from './test-foo-bar.entity';
describe('TestFooBarController', () => {
let controller: TestFooBarController;
let testFooBar1: TestFooBar;
let testFooBar2: TestFooBar;
before(async () => {
const connectionOptions = await getConnectionOptions();
Object.assign(connectionOptions, {
database: './test_db.sqlite3',
dropSchema: true,
synchronize: true,
type: 'sqlite',
});
await createConnection(connectionOptions);
});
after(() => getConnection().close());
beforeEach(async () => {
controller = createController(TestFooBarController);
const repository = getRepository(TestFooBar);
await repository.clear();
[ testFooBar1, testFooBar2 ] = await repository.save([
{
text: 'TestFooBar 1'
},
{
text: 'TestFooBar 2'
},
]);
});
describe('has a "get" method that', () => {
it('should handle requests at GET /.', () => {
strictEqual(getHttpMethod(TestFooBarController, 'get'), 'GET');
strictEqual(getPath(TestFooBarController, 'get'), '/');
});
it('should return an HttpResponseOK object with the testFooBar list.', async () => {
const ctx = new Context({ query: {} });
const response = await controller.get(ctx);
if (!isHttpResponseOK(response)) {
throw new Error('The returned value should be an HttpResponseOK object.');
}
if (!Array.isArray(response.body)) {
throw new Error('The response body should be an array of testFooBars.');
}
strictEqual(response.body.length, 2);
ok(response.body.find(testFooBar => testFooBar.text === testFooBar1.text));
ok(response.body.find(testFooBar => testFooBar.text === testFooBar2.text));
});
it('should support pagination', async () => {
const testFooBar3 = await getRepository(TestFooBar).save({
text: 'TestFooBar 3',
});
let ctx = new Context({
query: {
take: 2
}
});
let response = await controller.get(ctx);
strictEqual(response.body.length, 2);
ok(response.body.find(testFooBar => testFooBar.id === testFooBar1.id));
ok(response.body.find(testFooBar => testFooBar.id === testFooBar2.id));
ok(!response.body.find(testFooBar => testFooBar.id === testFooBar3.id));
ctx = new Context({
query: {
skip: 1
}
});
response = await controller.get(ctx);
strictEqual(response.body.length, 2);
ok(!response.body.find(testFooBar => testFooBar.id === testFooBar1.id));
ok(response.body.find(testFooBar => testFooBar.id === testFooBar2.id));
ok(response.body.find(testFooBar => testFooBar.id === testFooBar3.id));
});
});
describe('has a "getById" method that', () => {
it('should handle requests at GET /:testFooBarId.', () => {
strictEqual(getHttpMethod(TestFooBarController, 'getById'), 'GET');
strictEqual(getPath(TestFooBarController, 'getById'), '/:testFooBarId');
});
it('should return an HttpResponseOK object if the testFooBar was found.', async () => {
const ctx = new Context({
params: {
testFooBarId: testFooBar2.id
}
});
const response = await controller.getById(ctx);
if (!isHttpResponseOK(response)) {
throw new Error('The returned value should be an HttpResponseOK object.');
}
strictEqual(response.body.id, testFooBar2.id);
strictEqual(response.body.text, testFooBar2.text);
});
it('should return an HttpResponseNotFound object if the testFooBar was not found.', async () => {
const ctx = new Context({
params: {
testFooBarId: -1
}
});
const response = await controller.getById(ctx);
if (!isHttpResponseNotFound(response)) {
throw new Error('The returned value should be an HttpResponseNotFound object.');
}
});
});
describe('has a "post" method that', () => {
it('should handle requests at POST /.', () => {
strictEqual(getHttpMethod(TestFooBarController, 'post'), 'POST');
strictEqual(getPath(TestFooBarController, 'post'), '/');
});
it('should create the testFooBar in the database and return it through '
+ 'an HttpResponseCreated object.', async () => {
const ctx = new Context({
body: {
text: 'TestFooBar 3',
}
});
const response = await controller.post(ctx);
if (!isHttpResponseCreated(response)) {
throw new Error('The returned value should be an HttpResponseCreated object.');
}
const testFooBar = await getRepository(TestFooBar).findOne({ text: 'TestFooBar 3' });
if (!testFooBar) {
throw new Error('No testFooBar 3 was found in the database.');
}
strictEqual(testFooBar.text, 'TestFooBar 3');
strictEqual(response.body.id, testFooBar.id);
strictEqual(response.body.text, testFooBar.text);
});
});
describe('has a "postById" method that', () => {
it('should handle requests at POST /:testFooBarId.', () => {
strictEqual(getHttpMethod(TestFooBarController, 'postById'), 'POST');
strictEqual(getPath(TestFooBarController, 'postById'), '/:testFooBarId');
});
it('should return a HttpResponseMethodNotAllowed.', () => {
ok(isHttpResponseMethodNotAllowed(controller.postById()));
});
});
describe('has a "patch" method that', () => {
it('should handle requests at PATCH /.', () => {
strictEqual(getHttpMethod(TestFooBarController, 'patch'), 'PATCH');
strictEqual(getPath(TestFooBarController, 'patch'), '/');
});
it('should return a HttpResponseMethodNotAllowed.', () => {
ok(isHttpResponseMethodNotAllowed(controller.patch()));
});
});
describe('has a "patchById" method that', () => {
it('should handle requests at PATCH /:testFooBarId.', () => {
strictEqual(getHttpMethod(TestFooBarController, 'patchById'), 'PATCH');
strictEqual(getPath(TestFooBarController, 'patchById'), '/:testFooBarId');
});
it('should update the testFooBar in the database and return it through an HttpResponseOK object.', async () => {
const ctx = new Context({
body: {
text: 'TestFooBar 2 (version 2)',
},
params: {
testFooBarId: testFooBar2.id
}
});
const response = await controller.patchById(ctx);
if (!isHttpResponseOK(response)) {
throw new Error('The returned value should be an HttpResponseOK object.');
}
const testFooBar = await getRepository(TestFooBar).findOne(testFooBar2.id);
if (!testFooBar) {
throw new Error();
}
strictEqual(testFooBar.text, 'TestFooBar 2 (version 2)');
strictEqual(response.body.id, testFooBar.id);
strictEqual(response.body.text, testFooBar.text);
});
it('should not update the other testFooBars.', async () => {
const ctx = new Context({
body: {
text: 'TestFooBar 2 (version 2)',
},
params: {
testFooBarId: testFooBar2.id
}
});
await controller.patchById(ctx);
const testFooBar = await getRepository(TestFooBar).findOne(testFooBar1.id);
if (!testFooBar) {
throw new Error();
}
notStrictEqual(testFooBar.text, 'TestFooBar 2 (version 2)');
});
it('should return an HttpResponseNotFound if the object does not exist.', async () => {
const ctx = new Context({
body: {
text: '',
},
params: {
testFooBarId: -1
}
});
const response = await controller.patchById(ctx);
if (!isHttpResponseNotFound(response)) {
throw new Error('The returned value should be an HttpResponseNotFound object.');
}
});
});
describe('has a "put" method that', () => {
it('should handle requests at PUT /.', () => {
strictEqual(getHttpMethod(TestFooBarController, 'put'), 'PUT');
strictEqual(getPath(TestFooBarController, 'put'), '/');
});
it('should return a HttpResponseMethodNotAllowed.', () => {
ok(isHttpResponseMethodNotAllowed(controller.put()));
});
});
describe('has a "putById" method that', () => {
it('should handle requests at PUT /:testFooBarId.', () => {
strictEqual(getHttpMethod(TestFooBarController, 'putById'), 'PUT');
strictEqual(getPath(TestFooBarController, 'putById'), '/:testFooBarId');
});
it('should update the testFooBar in the database and return it through an HttpResponseOK object.', async () => {
const ctx = new Context({
body: {
text: 'TestFooBar 2 (version 2)',
},
params: {
testFooBarId: testFooBar2.id
}
});
const response = await controller.putById(ctx);
if (!isHttpResponseOK(response)) {
throw new Error('The returned value should be an HttpResponseOK object.');
}
const testFooBar = await getRepository(TestFooBar).findOne(testFooBar2.id);
if (!testFooBar) {
throw new Error();
}
strictEqual(testFooBar.text, 'TestFooBar 2 (version 2)');
strictEqual(response.body.id, testFooBar.id);
strictEqual(response.body.text, testFooBar.text);
});
it('should not update the other testFooBars.', async () => {
const ctx = new Context({
body: {
text: 'TestFooBar 2 (version 2)',
},
params: {
testFooBarId: testFooBar2.id
}
});
await controller.putById(ctx);
const testFooBar = await getRepository(TestFooBar).findOne(testFooBar1.id);
if (!testFooBar) {
throw new Error();
}
notStrictEqual(testFooBar.text, 'TestFooBar 2 (version 2)');
});
it('should return an HttpResponseNotFound if the object does not exist.', async () => {
const ctx = new Context({
body: {
text: '',
},
params: {
testFooBarId: -1
}
});
const response = await controller.putById(ctx);
if (!isHttpResponseNotFound(response)) {
throw new Error('The returned value should be an HttpResponseNotFound object.');
}
});
});
describe('has a "delete" method that', () => {
it('should handle requests at DELETE /.', () => {
strictEqual(getHttpMethod(TestFooBarController, 'delete'), 'DELETE');
strictEqual(getPath(TestFooBarController, 'delete'), '/');
});
it('should return a HttpResponseMethodNotAllowed.', () => {
ok(isHttpResponseMethodNotAllowed(controller.delete()));
});
});
describe('has a "deleteById" method that', () => {
it('should handle requests at DELETE /:testFooBarId.', () => {
strictEqual(getHttpMethod(TestFooBarController, 'deleteById'), 'DELETE');
strictEqual(getPath(TestFooBarController, 'deleteById'), '/:testFooBarId');
});
it('should delete the testFooBar and return an HttpResponseNoContent object.', async () => {
const ctx = new Context({
params: {
testFooBarId: testFooBar2.id
}
});
const response = await controller.deleteById(ctx);
if (!isHttpResponseNoContent(response)) {
throw new Error('The returned value should be an HttpResponseNoContent object.');
}
const testFooBar = await getRepository(TestFooBar).findOne(testFooBar2.id);
strictEqual(testFooBar, undefined);
});
it('should not delete the other testFooBars.', async () => {
const ctx = new Context({
params: {
testFooBarId: testFooBar2.id
}
});
const response = await controller.deleteById(ctx);
if (!isHttpResponseNoContent(response)) {
throw new Error('The returned value should be an HttpResponseNoContent object.');
}
const testFooBar = await getRepository(TestFooBar).findOne(testFooBar1.id);
notStrictEqual(testFooBar, undefined);
});
it('should return an HttpResponseNotFound if the testFooBar was not fond.', async () => {
const ctx = new Context({
params: {
testFooBarId: -1
}
});
const response = await controller.deleteById(ctx);
if (!isHttpResponseNotFound(response)) {
throw new Error('The returned value should be an HttpResponseNotFound object.');
}
});
});
});