@natlibfi/melinda-rest-api-commons 4.2.4-alpha.8 → 4.2.5-alpha.1

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