@natlibfi/melinda-rest-api-commons 4.2.4-alpha.7 → 4.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/dist/mongo.js +15 -9
  2. package/dist/mongo.js.map +1 -1
  3. package/package.json +15 -14
  4. package/{dist → tests}/amqp.health.spec.js +17 -10
  5. package/tests/amqp.tests.spec.js +301 -0
  6. package/tests/helpers/conversion.spec.js +95 -0
  7. package/tests/helpers/fix.spec.js +41 -0
  8. package/tests/helpers/validation.spec.js +58 -0
  9. package/tests/mongo-add-blob-size.spec.js +92 -0
  10. package/tests/mongo-check-and-check-state.spec.js +105 -0
  11. package/tests/mongo-check-and-set-import-job-state.spec.js +104 -0
  12. package/tests/mongo-check-timeout.spec.js +104 -0
  13. package/tests/mongo-create-bulk.spec.js +93 -0
  14. package/tests/mongo-create-prio.spec.js +89 -0
  15. package/tests/mongo-get-one.spec.js +93 -0
  16. package/tests/mongo-get-stream.spec.js +112 -0
  17. package/tests/mongo-push-ids.spec.js +91 -0
  18. package/tests/mongo-push-messages.spec.js +89 -0
  19. package/tests/mongo-query-by-id.spec.js +106 -0
  20. package/tests/mongo-query.spec.js +98 -0
  21. package/tests/mongo-read-content.spec.js +115 -0
  22. package/tests/mongo-remove.spec.js +110 -0
  23. package/tests/mongo-set-blob-size.spec.js +89 -0
  24. package/tests/mongo-set-import-job-state.spec.js +92 -0
  25. package/tests/mongo-set-operation.spec.js +92 -0
  26. package/tests/mongo-set-operations.spec.js +91 -0
  27. package/tests/mongo-set-state.spec.js +92 -0
  28. package/{dist → tests}/mongoLog.spec.js +22 -22
  29. package/tests/testUtils.js +102 -0
  30. package/dist/amqp.health.spec.js.map +0 -1
  31. package/dist/amqp.tests.spec.js +0 -310
  32. package/dist/amqp.tests.spec.js.map +0 -1
  33. package/dist/helpers/conversion.spec.js +0 -88
  34. package/dist/helpers/conversion.spec.js.map +0 -1
  35. package/dist/helpers/fix.spec.js +0 -46
  36. package/dist/helpers/fix.spec.js.map +0 -1
  37. package/dist/helpers/validation.spec.js +0 -47
  38. package/dist/helpers/validation.spec.js.map +0 -1
  39. package/dist/mongo.spec.js +0 -768
  40. package/dist/mongo.spec.js.map +0 -1
  41. package/dist/mongoLog.spec.js.map +0 -1
@@ -1,768 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
- var _chai = require("chai");
5
- var _fixura = require("@natlibfi/fixura");
6
- var _fixuraMongo = _interopRequireDefault(require("@natlibfi/fixura-mongo"));
7
- var _fixugen = _interopRequireDefault(require("@natlibfi/fixugen"));
8
- var _mongo = _interopRequireDefault(require("./mongo"));
9
- var _debug = _interopRequireDefault(require("debug"));
10
- /* eslint-disable max-lines */
11
- /* eslint-disable no-console */
12
-
13
- let mongoFixtures; // eslint-disable-line functional/no-let
14
- const debug = (0, _debug.default)('@natlibfi/melinda-rest-api-commons/mongo:test');
15
- (0, _fixugen.default)({
16
- callback,
17
- path: [__dirname, '..', 'test-fixtures', 'mongo'],
18
- recurse: false,
19
- useMetadataFile: true,
20
- fixura: {
21
- failWhenNotFound: true,
22
- reader: _fixura.READERS.JSON
23
- },
24
- mocha: {
25
- before: async () => {
26
- //debug(`<< Before`);
27
- await initMongofixtures();
28
- },
29
- beforeEach: async () => {
30
- //debug(`<< BeforeEach`);
31
- await mongoFixtures.clear();
32
- },
33
- afterEach: async () => {
34
- //debug(`<< AfterEach`);
35
- await mongoFixtures.clear();
36
- },
37
- after: async () => {
38
- //debug(`<< After`);
39
- await mongoFixtures.close();
40
- }
41
- }
42
- });
43
- async function initMongofixtures() {
44
- mongoFixtures = await (0, _fixuraMongo.default)({
45
- recurse: false,
46
- rootPath: [__dirname, '..', 'test-fixtures', 'mongo'],
47
- gridFS: {
48
- bucketName: 'foobar'
49
- },
50
- useObjectId: true
51
- });
52
- }
53
-
54
- // eslint-disable-next-line max-statements, complexity
55
- async function callback({
56
- getFixture,
57
- functionName,
58
- params,
59
- expectModificationTime = false,
60
- preFillDb = false,
61
- expectedToThrow = false,
62
- expectedErrorMessage = '',
63
- expectedErrorStatus = '',
64
- contentStream = false,
65
- expectedOpResult = undefined,
66
- updateStateBeforeTest = undefined,
67
- resultIndex = undefined,
68
- createBulkParams = undefined
69
- }) {
70
- const mongoUri = await mongoFixtures.getUri();
71
- const mongoOperator = await (0, _mongo.default)(mongoUri, 'foobar', '');
72
- const expectedResult = await getFixture('expectedResult.json');
73
- // DEVELOP: we could use sama function for most of tests?
74
- // debug(typeof mongoUri); // eslint-disable-line
75
-
76
- if (preFillDb) {
77
- // eslint-disable-line functional/no-conditional-statements
78
- await mongoFixtures.populate(getFixture('dbContents.json'));
79
- }
80
-
81
- //return {createPrio, createBulk, checkAndSetState, checkAndSetImportJobState, query, queryById, remove, readContent, removeContent, getOne, getStream, setState, setImportJobState, pushIds, pushMessages, setOperation, setOperations, addBlobSize, setBlobSize};
82
-
83
- if (functionName === 'createPrio') {
84
- try {
85
- debug(`CreatePrio`);
86
- debug(JSON.stringify(params));
87
- const opResult = await mongoOperator.createPrio(params);
88
- debug(`createPrio result: ${JSON.stringify(opResult)}`);
89
- await compareToFirstDbEntry({
90
- expectedResult,
91
- formatDates: true
92
- });
93
- } catch (error) {
94
- handleError({
95
- error,
96
- expectedToThrow,
97
- expectedErrorMessage,
98
- expectedErrorStatus
99
- });
100
- return;
101
- }
102
- return;
103
- }
104
- if (functionName === 'createBulk') {
105
- try {
106
- debug(`CreateBulk`);
107
- debug(JSON.stringify(params));
108
- //{correlationId, cataloger, oCatalogerIn, operation, contentType, recordLoadParams, stream, operationSettings}
109
- const stream = contentStream ? await getFixture({
110
- components: ['contentStream'],
111
- reader: _fixura.READERS.STREAM
112
- }) : params.stream;
113
- const params2 = {
114
- ...params,
115
- stream
116
- };
117
- const opResult = await mongoOperator.createBulk(params2);
118
- debug(`createBulkResult: ${JSON.stringify(opResult)}`);
119
- await compareToFirstDbEntry({
120
- expectedResult,
121
- formatDates: true
122
- });
123
- } catch (error) {
124
- handleError({
125
- error,
126
- expectedToThrow,
127
- expectedErrorMessage,
128
- expectedErrorStatus
129
- });
130
- return;
131
- }
132
- return;
133
- }
134
- if (functionName === 'checkAndSetState') {
135
- try {
136
- debug(`checkAndSetState`);
137
- debug(JSON.stringify(params));
138
- //{correlationId, state, errorMessage = undefined, errorStatus = undefined})
139
- // timeout
140
-
141
- // eslint-disable-next-line functional/no-conditional-statements
142
- if (updateStateBeforeTest && params.correlationId) {
143
- debug(`setState to reset modificationTime`);
144
- await mongoOperator.setState({
145
- correlationId: params.correlationId,
146
- state: updateStateBeforeTest
147
- });
148
- }
149
- const opResult = await mongoOperator.checkAndSetState(params);
150
- debug(`checkAndSetState result: ${JSON.stringify(opResult)} (it should be: ${JSON.stringify(expectedOpResult)})}`);
151
-
152
- // eslint-disable-next-line functional/no-conditional-statements
153
- if (expectedOpResult !== undefined) {
154
- (0, _chai.expect)(opResult).to.eql(expectedOpResult);
155
- }
156
- await compareToFirstDbEntry({
157
- expectedResult,
158
- expectModificationTime,
159
- formatDates: true
160
- });
161
- } catch (error) {
162
- handleError({
163
- error,
164
- expectedToThrow,
165
- expectedErrorMessage,
166
- expectedErrorStatus
167
- });
168
- }
169
- return;
170
- }
171
- if (functionName === 'checkAndSetImportJobState') {
172
- try {
173
- debug(`checkAndSetImportJobState`);
174
- debug(JSON.stringify(params));
175
- //{correlationId, operation, importJobState, errorMessage = '', errorStatus = ''}
176
- // timeout
177
- // eslint-disable-next-line functional/no-conditional-statements
178
- if (updateStateBeforeTest && params.correlationId) {
179
- debug(`setState to reset modificationTime`);
180
- await mongoOperator.setState({
181
- correlationId: params.correlationId,
182
- state: updateStateBeforeTest
183
- });
184
- }
185
- const opResult = await mongoOperator.checkAndSetImportJobState(params);
186
- debug(`checkAndSetImportJobState result: ${JSON.stringify(opResult)} (it should be: ${JSON.stringify(expectedOpResult)})}`);
187
-
188
- // eslint-disable-next-line functional/no-conditional-statements
189
- if (expectedOpResult !== undefined) {
190
- (0, _chai.expect)(opResult).to.eql(expectedOpResult);
191
- }
192
- await compareToFirstDbEntry({
193
- expectedResult,
194
- expectModificationTime,
195
- formatDates: true
196
- });
197
- } catch (error) {
198
- handleError({
199
- error,
200
- expectedToThrow,
201
- expectedErrorMessage,
202
- expectedErrorStatus
203
- });
204
- return;
205
- }
206
- return;
207
- }
208
- if (functionName === 'query') {
209
- try {
210
- debug(`query`);
211
- debug(JSON.stringify(params));
212
- //params, showParams = {}
213
- //params: skip, limit, rest (rest: actual mongo queryParams)
214
- //showParams: {showAll = 0, showOperations = 0, showOperationSettings = 0, showRecordLoadParams = 0, showImportJobState = 0} = showParams;
215
- debug(`${JSON.stringify(params.params)}`);
216
- debug(`${JSON.stringify(params.showParams)}`);
217
- const opResult = await mongoOperator.query(params.params, params.showParams);
218
- debug(`query result: ${JSON.stringify(opResult)} (it should be: ${JSON.stringify(expectedOpResult)})}`);
219
- // eslint-disable-next-line functional/no-conditional-statements
220
- if (expectedOpResult !== undefined) {
221
- (0, _chai.expect)(opResult).to.eql(expectedOpResult);
222
- }
223
- } catch (error) {
224
- handleError({
225
- error,
226
- expectedToThrow,
227
- expectedErrorMessage,
228
- expectedErrorStatus
229
- });
230
- return;
231
- }
232
- return;
233
- }
234
-
235
- /*
236
- // NOT EXPORTED
237
- if (functionName === 'createProjection') {
238
- try {
239
- debug(`createProjection`);
240
- debug(JSON.stringify(params));
241
- //showParams = {}
242
- //no db
243
- } catch (error) {
244
- handleError({error, expectedToThrow, expectedErrorMessage, expectedErrorStatus});
245
- return;
246
- }
247
- return;
248
- }
249
- */
250
-
251
- if (functionName === 'queryById') {
252
- try {
253
- debug(`queryById`);
254
- debug(JSON.stringify(params));
255
- //{correlationId, checkModTime = false}
256
- // eslint-disable-next-line functional/no-conditional-statements
257
- if (updateStateBeforeTest && params.correlationId) {
258
- debug(`setState to reset modificationTime`);
259
- await mongoOperator.setState({
260
- correlationId: params.correlationId,
261
- state: updateStateBeforeTest
262
- });
263
- }
264
- const opResult = await mongoOperator.queryById(params);
265
- debug(`queryById result: ${JSON.stringify(opResult)} (it should be: ${JSON.stringify(expectedOpResult)})}`);
266
- // eslint-disable-next-line functional/no-conditional-statements
267
- if (expectedOpResult !== undefined) {
268
- (0, _chai.expect)(formatQueueItem(opResult)).to.eql(formatQueueItem(expectedOpResult));
269
- }
270
- const compareResultIndex = resultIndex ? resultIndex : 0;
271
- await compareToDbEntry({
272
- expectedResult,
273
- resultIndex: compareResultIndex,
274
- expectModificationTime,
275
- formatDates: true
276
- });
277
- } catch (error) {
278
- handleError({
279
- error,
280
- expectedToThrow,
281
- expectedErrorMessage,
282
- expectedErrorStatus
283
- });
284
- return;
285
- }
286
- return;
287
- }
288
-
289
- // only returned for test
290
- if (functionName === 'checkTimeOut') {
291
- try {
292
- debug(`checkTimeOut`);
293
- debug(JSON.stringify(params));
294
- //{correlationId}
295
- // timeout
296
- // eslint-disable-next-line functional/no-conditional-statements
297
- if (updateStateBeforeTest && params.correlationId) {
298
- debug(`setState to reset modificationTime`);
299
- await mongoOperator.setState({
300
- correlationId: params.correlationId,
301
- state: updateStateBeforeTest
302
- });
303
- }
304
- const opResult = await mongoOperator.checkTimeOut(params);
305
- debug(`checkTimeOut result: ${JSON.stringify(opResult)} (it should be: ${JSON.stringify(expectedOpResult)})}`);
306
-
307
- // eslint-disable-next-line functional/no-conditional-statements
308
- if (expectedOpResult !== undefined) {
309
- (0, _chai.expect)(opResult).to.eql(expectedOpResult);
310
- }
311
- await compareToFirstDbEntry({
312
- expectedResult,
313
- expectModificationTime,
314
- formatDates: true
315
- });
316
- } catch (error) {
317
- handleError({
318
- error,
319
- expectedToThrow,
320
- expectedErrorMessage,
321
- expectedErrorStatus
322
- });
323
- return;
324
- }
325
- return;
326
- }
327
- if (functionName === 'remove') {
328
- try {
329
- debug(`remove`);
330
- debug(JSON.stringify(params));
331
- //params: {correlationId}
332
-
333
- // eslint-disable-next-line functional/no-conditional-statements
334
- if (createBulkParams) {
335
- const stream = contentStream ? await getFixture({
336
- components: ['contentStream'],
337
- reader: _fixura.READERS.STREAM
338
- }) : createBulkParams.stream;
339
- //debug(stream);
340
- const params2 = {
341
- ...createBulkParams,
342
- stream
343
- };
344
- const createBulkResult = await mongoOperator.createBulk(params2);
345
- debug(`createBulkResult: ${JSON.stringify(createBulkResult)}`);
346
- }
347
- const opResult = await mongoOperator.remove(params);
348
- debug(`remove result: ${JSON.stringify(opResult)}`);
349
- await compareToFirstDbEntry({
350
- expectedResult: undefined,
351
- expectModificationTime,
352
- formatDates: false
353
- });
354
- } catch (error) {
355
- handleError({
356
- error,
357
- expectedToThrow,
358
- expectedErrorMessage,
359
- expectedErrorStatus
360
- });
361
- return;
362
- }
363
- return;
364
- }
365
- if (functionName === 'readContent') {
366
- try {
367
- debug(`readContent`);
368
- debug(JSON.stringify(params));
369
- //correlationId
370
-
371
- // eslint-disable-next-line functional/no-conditional-statements
372
- if (createBulkParams) {
373
- const stream = contentStream ? await getFixture({
374
- components: ['contentStream'],
375
- reader: _fixura.READERS.STREAM
376
- }) : createBulkParams.stream;
377
- //debug(stream);
378
- const params2 = {
379
- ...createBulkParams,
380
- stream
381
- };
382
- const createBulkResult = await mongoOperator.createBulk(params2);
383
- debug(`createBulkResult: ${JSON.stringify(createBulkResult)}`);
384
- }
385
- const opResult = await mongoOperator.readContent(params);
386
- const opResultString = await streamToString(opResult.readStream);
387
-
388
- // eslint-disable-next-line functional/no-conditional-statements
389
- if (createBulkParams && createBulkParams.contentType) {
390
- (0, _chai.expect)(opResult.contentType).to.eql(createBulkParams.contentType);
391
- }
392
- if (contentStream) {
393
- const contentStreamString = await getFixture({
394
- components: ['contentStream'],
395
- reader: _fixura.READERS.TEXT
396
- });
397
- //debug(contentStreamString);
398
- (0, _chai.expect)(opResultString).to.eql(contentStreamString);
399
- return;
400
- }
401
- } catch (error) {
402
- handleError({
403
- error,
404
- expectedToThrow,
405
- expectedErrorMessage,
406
- expectedErrorStatus
407
- });
408
- return;
409
- }
410
- return;
411
- }
412
-
413
- /*
414
- if (functionName === 'removeContent') {
415
- try {
416
- debug(`removeContent`);
417
- debug(JSON.stringify(params));
418
- //correlationId
419
- } catch (error) {
420
- handleError({error, expectedToThrow, expectedErrorMessage, expectedErrorStatus});
421
- return;
422
- }
423
- return;
424
- }
425
- */
426
-
427
- if (functionName === 'getOne') {
428
- try {
429
- debug(`getOne`);
430
- debug(JSON.stringify(params));
431
- //{queueItemState, importJobState = undefined}
432
- const opResult = await mongoOperator.getOne(params);
433
- debug(`getOne result: ${JSON.stringify(opResult)} (it should be: ${JSON.stringify(expectedOpResult)})}`);
434
- (0, _chai.expect)(opResult).to.eql(expectedOpResult);
435
- } catch (error) {
436
- handleError({
437
- error,
438
- expectedToThrow,
439
- expectedErrorMessage,
440
- expectedErrorStatus
441
- });
442
- return;
443
- }
444
- return;
445
- }
446
- if (functionName === 'getStream') {
447
- try {
448
- debug(`getStream`);
449
- debug(JSON.stringify(params));
450
- //correlationId
451
-
452
- // eslint-disable-next-line functional/no-conditional-statements
453
- if (createBulkParams) {
454
- const stream = contentStream ? await getFixture({
455
- components: ['contentStream'],
456
- reader: _fixura.READERS.STREAM
457
- }) : createBulkParams.stream;
458
- //debug(stream);
459
- const params2 = {
460
- ...createBulkParams,
461
- stream
462
- };
463
- const createBulkResult = await mongoOperator.createBulk(params2);
464
- debug(`createBulkResult: ${JSON.stringify(createBulkResult)}`);
465
- }
466
- //const {correlationId} = params;
467
-
468
- const opResult = mongoOperator.getStream(params);
469
- const opResultString = await streamToString(opResult);
470
- if (contentStream) {
471
- const contentStreamString = await getFixture({
472
- components: ['contentStream'],
473
- reader: _fixura.READERS.TEXT
474
- });
475
- //debug(contentStreamString);
476
- (0, _chai.expect)(opResultString).to.eql(contentStreamString);
477
- return;
478
- }
479
- } catch (error) {
480
- handleError({
481
- error,
482
- expectedToThrow,
483
- expectedErrorMessage,
484
- expectedErrorStatus
485
- });
486
- return;
487
- }
488
- return;
489
- }
490
- if (functionName === 'pushIds') {
491
- try {
492
- debug(`pushIds`);
493
- debug(JSON.stringify(params));
494
- //{correlationId, handledIds, rejectedIds}
495
- const opResult = await mongoOperator.pushIds(params);
496
- debug(`pushIds result: ${JSON.stringify(opResult)}`);
497
- await compareToFirstDbEntry({
498
- expectedResult,
499
- expectModificationTime,
500
- formatDates: true
501
- });
502
- } catch (error) {
503
- handleError({
504
- error,
505
- expectedToThrow,
506
- expectedErrorMessage,
507
- expectedErrorStatus
508
- });
509
- return;
510
- }
511
- return;
512
- }
513
- if (functionName === 'pushMessages') {
514
- try {
515
- debug(`pushMessages`);
516
- debug(JSON.stringify(params));
517
- //{correlationId, messages, messageField = 'messages'}
518
- const opResult = await mongoOperator.pushMessages(params);
519
- debug(`pushMessages result: ${JSON.stringify(opResult)}`);
520
- await compareToFirstDbEntry({
521
- expectedResult,
522
- expectModificationTime,
523
- formatDates: true
524
- });
525
- } catch (error) {
526
- handleError({
527
- error,
528
- expectedToThrow,
529
- expectedErrorMessage,
530
- expectedErrorStatus
531
- });
532
- return;
533
- }
534
- return;
535
- }
536
- if (functionName === 'setState') {
537
- try {
538
- debug(`setState`);
539
- debug(JSON.stringify(params));
540
- //{correlationId, state, errorMessage = undefined, errorStatus = undefined}
541
- const opResult = await mongoOperator.setState(params);
542
- debug(`setState result: ${JSON.stringify(opResult)}`);
543
- await compareToFirstDbEntry({
544
- expectedResult,
545
- expectModificationTime,
546
- formatDates: true
547
- });
548
- } catch (error) {
549
- handleError({
550
- error,
551
- expectedToThrow,
552
- expectedErrorMessage,
553
- expectedErrorStatus
554
- });
555
- return;
556
- }
557
- return;
558
- }
559
- if (functionName === 'setImportJobState') {
560
- try {
561
- debug(`setImportJobState`);
562
- debug(JSON.stringify(params));
563
- //{correlationId, operation, importJobState}
564
- const opResult = await mongoOperator.setImportJobState(params);
565
- debug(`setImportJobState result: ${JSON.stringify(opResult)}`);
566
- await compareToFirstDbEntry({
567
- expectedResult,
568
- expectModificationTime,
569
- formatDates: true
570
- });
571
- } catch (error) {
572
- handleError({
573
- error,
574
- expectedToThrow,
575
- expectedErrorMessage,
576
- expectedErrorStatus
577
- });
578
- return;
579
- }
580
- return;
581
- }
582
- if (functionName === 'setOperation') {
583
- try {
584
- debug(`setOperation`);
585
- debug(JSON.stringify(params));
586
- //{correlationId, operation}
587
- const opResult = await mongoOperator.setOperation(params);
588
- debug(`setOperation result: ${JSON.stringify(opResult)}`);
589
- await compareToFirstDbEntry({
590
- expectedResult,
591
- expectModificationTime,
592
- formatDates: true
593
- });
594
- } catch (error) {
595
- handleError({
596
- error,
597
- expectedToThrow,
598
- expectedErrorMessage,
599
- expectedErrorStatus
600
- });
601
- return;
602
- }
603
- return;
604
- }
605
- if (functionName === 'setOperations') {
606
- try {
607
- debug(`setOperations`);
608
- debug(JSON.stringify(params));
609
- //{correlationId, addOperation, removeOperation = undefined}
610
- const opResult = await mongoOperator.setOperations(params);
611
- debug(`setOperations result: ${JSON.stringify(opResult)}`);
612
- await compareToFirstDbEntry({
613
- expectedResult,
614
- expectModificationTime,
615
- formatDates: true
616
- });
617
- } catch (error) {
618
- handleError({
619
- error,
620
- expectedToThrow,
621
- expectedErrorMessage,
622
- expectedErrorStatus
623
- });
624
- return;
625
- }
626
- return;
627
- }
628
- if (functionName === 'addBlobSize') {
629
- try {
630
- debug(`addBlobSize`);
631
- debug(JSON.stringify(params));
632
- //{correlationId}
633
- const opResult = await mongoOperator.addBlobSize(params);
634
- debug(`addBlobSize result: ${JSON.stringify(opResult)}`);
635
- await compareToFirstDbEntry({
636
- expectedResult,
637
- expectModificationTime,
638
- formatDates: true
639
- });
640
- } catch (error) {
641
- handleError({
642
- error,
643
- expectedToThrow,
644
- expectedErrorMessage,
645
- expectedErrorStatus
646
- });
647
- return;
648
- }
649
- return;
650
- }
651
- if (functionName === 'setBlobSize') {
652
- try {
653
- debug(`setBlobSize`);
654
- debug(JSON.stringify(params));
655
- //{correlationId, blobSize}
656
- const opResult = await mongoOperator.setBlobSize(params);
657
- debug(`setBlobSize result: ${JSON.stringify(opResult)}`);
658
- await compareToFirstDbEntry({
659
- expectedResult,
660
- expectModificationTime,
661
- formatDates: true
662
- });
663
- } catch (error) {
664
- handleError({
665
- error,
666
- expectedToThrow,
667
- expectedErrorMessage,
668
- expectedErrorStatus
669
- });
670
- return;
671
- }
672
- return;
673
- }
674
- throw new Error(`Unknown functionName: ${functionName}`);
675
- }
676
- async function compareToFirstDbEntry({
677
- expectedResult,
678
- expectModificationTime = false,
679
- formatDates = true
680
- }) {
681
- const result = await compareToDbEntry({
682
- expectedResult,
683
- resultIndex: 0,
684
- expectModificationTime,
685
- formatDates
686
- });
687
- return result;
688
- }
689
- async function compareToDbEntry({
690
- expectedResult,
691
- resultIndex = 0,
692
- expectModificationTime = false,
693
- formatDates = true
694
- }) {
695
- const dump = await mongoFixtures.dump();
696
- debug(`--- We have ${dump.foobar.length} documents in db`);
697
- debug(dump.foobar);
698
- const result = await dump.foobar[resultIndex];
699
- debug(`db result (document ${resultIndex}): ${JSON.stringify(result)}`);
700
- const dump2 = await mongoFixtures.dump();
701
- debug(`--- We have ${dump2.foobar.length} documents in db now`);
702
- checkModificationTime({
703
- result,
704
- expectModificationTime
705
- });
706
- if (formatDates) {
707
- const formattedResult = formatQueueItem(result);
708
- (0, _chai.expect)(formattedResult).to.eql(expectedResult);
709
- return;
710
- }
711
- (0, _chai.expect)(result).to.eql(expectedResult);
712
- return;
713
- }
714
- function checkModificationTime({
715
- result,
716
- expectModificationTime
717
- }) {
718
- if (expectModificationTime) {
719
- debug('Check if modificationTime was edited');
720
- (0, _chai.expect)(result.modificationTime).to.not.eql('');
721
- debug(`OK. We have modified modificationTime`);
722
- return;
723
- }
724
- return;
725
- }
726
- function formatQueueItem(queueItem) {
727
- const filteredQueueItem = {
728
- ...queueItem,
729
- creationTime: '',
730
- modificationTime: ''
731
- };
732
- debug(`Formatted result: ${JSON.stringify(filteredQueueItem)}`);
733
- return filteredQueueItem;
734
- }
735
- function handleError({
736
- error,
737
- expectedToThrow,
738
- expectedErrorMessage,
739
- expectedErrorStatus
740
- }) {
741
- debug(error);
742
- if (expectedToThrow) {
743
- debug('OK. Expected to throw');
744
- debug(JSON.stringify(error));
745
- // eslint-disable-next-line max-lines
746
- if (expectedErrorMessage !== '' || expectedErrorStatus !== '') {
747
- const errorMessage = error.message || error.payload.message || error.payload || '';
748
- debug(errorMessage);
749
- (0, _chai.expect)(errorMessage).to.eql(expectedErrorMessage);
750
- const errorStatus = error.status || '';
751
- (0, _chai.expect)(errorStatus).to.eql(expectedErrorStatus);
752
- return;
753
- }
754
- return;
755
- }
756
- debug('NOT OK. Not expexted to throw');
757
- throw error;
758
- }
759
- function streamToString(stream) {
760
- const chunks = [];
761
- return new Promise((resolve, reject) => {
762
- // eslint-disable-next-line functional/immutable-data
763
- stream.on('data', chunk => chunks.push(Buffer.from(chunk)));
764
- stream.on('error', err => reject(err));
765
- stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
766
- });
767
- }
768
- //# sourceMappingURL=mongo.spec.js.map