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

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 (47) hide show
  1. package/.github/workflows/melinda-node-tests.yml +1 -1
  2. package/dist/amqp.js +2 -2
  3. package/dist/amqp.js.map +1 -1
  4. package/dist/index.js +1 -1
  5. package/dist/index.js.map +1 -1
  6. package/dist/mongo.js +158 -80
  7. package/dist/mongo.js.map +1 -1
  8. package/dist/mongoLog.js +1 -4
  9. package/dist/mongoLog.js.map +1 -1
  10. package/package.json +26 -24
  11. package/{dist → tests}/amqp.health.spec.js +17 -10
  12. package/tests/amqp.tests.spec.js +301 -0
  13. package/tests/helpers/conversion.spec.js +95 -0
  14. package/tests/helpers/fix.spec.js +41 -0
  15. package/tests/helpers/validation.spec.js +58 -0
  16. package/tests/mongo-add-blob-size.spec.js +100 -0
  17. package/tests/mongo-check-and-check-state.spec.js +112 -0
  18. package/tests/mongo-check-and-set-import-job-state.spec.js +112 -0
  19. package/tests/mongo-check-timeout.spec.js +111 -0
  20. package/tests/mongo-create-bulk.spec.js +100 -0
  21. package/tests/mongo-create-prio.spec.js +98 -0
  22. package/tests/mongo-get-one.spec.js +103 -0
  23. package/tests/mongo-get-stream.spec.js +120 -0
  24. package/tests/mongo-push-ids.spec.js +101 -0
  25. package/tests/mongo-push-messages.spec.js +99 -0
  26. package/tests/mongo-query-by-id.spec.js +112 -0
  27. package/tests/mongo-query.spec.js +108 -0
  28. package/tests/mongo-read-content.spec.js +123 -0
  29. package/tests/mongo-remove.spec.js +115 -0
  30. package/tests/mongo-set-blob-size.spec.js +99 -0
  31. package/tests/mongo-set-import-job-state.spec.js +102 -0
  32. package/tests/mongo-set-operation.spec.js +102 -0
  33. package/tests/mongo-set-operations.spec.js +101 -0
  34. package/tests/mongo-set-state.spec.js +102 -0
  35. package/{dist → tests}/mongoLog.spec.js +22 -22
  36. package/tests/testUtils.js +94 -0
  37. package/dist/amqp.health.spec.js.map +0 -1
  38. package/dist/amqp.tests.spec.js +0 -310
  39. package/dist/amqp.tests.spec.js.map +0 -1
  40. package/dist/helpers/conversion.spec.js +0 -88
  41. package/dist/helpers/conversion.spec.js.map +0 -1
  42. package/dist/helpers/fix.spec.js +0 -46
  43. package/dist/helpers/fix.spec.js.map +0 -1
  44. package/dist/helpers/validation.spec.js +0 -47
  45. package/dist/helpers/validation.spec.js.map +0 -1
  46. package/dist/mongo.spec.js.draft +0 -54
  47. package/dist/mongoLog.spec.js.map +0 -1
@@ -0,0 +1,120 @@
1
+ import {expect} from 'chai';
2
+ import {READERS} from '@natlibfi/fixura';
3
+ import mongoFixturesFactory from '@natlibfi/fixura-mongo';
4
+ import generateTests from '@natlibfi/fixugen';
5
+ import createMongoOperator from '../src/mongo';
6
+ import createDebugLogger from 'debug';
7
+ //import {handleError, compareToFirstDbEntry, compareToDbEntry, formatQueueItem, streamToString} from './testUtils';
8
+ import {handleError, streamToString} from './testUtils';
9
+
10
+ let mongoFixtures; // eslint-disable-line functional/no-let
11
+ const debug = createDebugLogger('@natlibfi/melinda-rest-api-commons/mongo:get-stream:test');
12
+
13
+ generateTests({
14
+ callback,
15
+ path: [__dirname, '..', 'test-fixtures', 'mongo', 'get-stream'],
16
+ recurse: false,
17
+ useMetadataFile: true,
18
+ fixura: {
19
+ failWhenNotFound: true,
20
+ reader: READERS.JSON
21
+ },
22
+ mocha: {
23
+ before: async () => {
24
+ //debug(`<< Before`);
25
+ await initMongofixtures();
26
+ },
27
+ beforeEach: async () => {
28
+ //debug(`<< BeforeEach`);
29
+ await mongoFixtures.clear();
30
+ },
31
+ afterEach: async () => {
32
+ //debug(`<< AfterEach`);
33
+ await mongoFixtures.clear();
34
+ },
35
+ after: async () => {
36
+ //debug(`<< After`);
37
+ await mongoFixtures.close();
38
+ }
39
+ }
40
+ });
41
+
42
+ async function initMongofixtures() {
43
+ mongoFixtures = await mongoFixturesFactory({
44
+ recurse: false,
45
+ rootPath: [__dirname, '..', 'test-fixtures', 'mongo', 'get-stream'],
46
+ gridFS: {bucketName: 'foobar'},
47
+ useObjectId: true
48
+ });
49
+ }
50
+
51
+ // eslint-disable-next-line max-statements, complexity
52
+ async function callback({
53
+ getFixture,
54
+ functionName,
55
+ params,
56
+ //expectModificationTime = false,
57
+ preFillDb = false,
58
+ expectedToThrow = false,
59
+ expectedErrorMessage = '',
60
+ expectedErrorStatus = '',
61
+ contentStream = false,
62
+ //expectedOpResult = undefined,
63
+ //updateStateBeforeTest = undefined,
64
+ //resultIndex = undefined,
65
+ createBulkParams = undefined
66
+ //expectedFileCount = undefined,
67
+ //expectUndefined = undefined
68
+ }) {
69
+
70
+ const mongoUri = await mongoFixtures.getUri();
71
+ const mongoOperator = await createMongoOperator(mongoUri, 'foobar', '');
72
+ //const expectedResult = await getFixture('expectedResult.json');
73
+
74
+ await doPreFillDb(preFillDb);
75
+
76
+ async function doPreFillDb(preFillDb) {
77
+ if (preFillDb) {
78
+ await mongoFixtures.populate(getFixture('dbContents.json'));
79
+ return;
80
+ }
81
+ return;
82
+ }
83
+
84
+
85
+ if (functionName === 'getStream') {
86
+ try {
87
+ debug(`getStream`);
88
+ debug(JSON.stringify(params));
89
+ //correlationId
90
+
91
+ // eslint-disable-next-line functional/no-conditional-statements
92
+ if (createBulkParams) {
93
+ const stream = contentStream ? await getFixture({components: ['contentStream'], reader: READERS.STREAM}) : createBulkParams.stream;
94
+ //debug(stream);
95
+ const params2 = {...createBulkParams, stream};
96
+ const createBulkResult = await mongoOperator.createBulk(params2);
97
+ debug(`createBulkResult: ${JSON.stringify(createBulkResult)}`);
98
+ }
99
+ //const {correlationId} = params;
100
+
101
+ const opResult = mongoOperator.getStream(params);
102
+ const opResultString = await streamToString(opResult);
103
+
104
+ if (contentStream) {
105
+ const contentStreamString = await getFixture({components: ['contentStream'], reader: READERS.TEXT});
106
+ //debug(contentStreamString);
107
+ expect(opResultString).to.eql(contentStreamString);
108
+ return;
109
+ }
110
+
111
+
112
+ } catch (error) {
113
+ handleError({error, expectedToThrow, expectedErrorMessage, expectedErrorStatus});
114
+ return;
115
+ }
116
+ return;
117
+ }
118
+ throw new Error(`Unknown functionName: ${functionName}`);
119
+ }
120
+
@@ -0,0 +1,101 @@
1
+ import {READERS} from '@natlibfi/fixura';
2
+ import mongoFixturesFactory from '@natlibfi/fixura-mongo';
3
+ import generateTests from '@natlibfi/fixugen';
4
+ import createMongoOperator from '../src/mongo';
5
+ import createDebugLogger from 'debug';
6
+ //import {handleError, compareToFirstDbEntry, compareToDbEntry, formatQueueItem, streamToString} from './testUtils';
7
+ import {handleError, compareToFirstDbEntry} from './testUtils';
8
+
9
+
10
+ let mongoFixtures; // eslint-disable-line functional/no-let
11
+ const debug = createDebugLogger('@natlibfi/melinda-rest-api-commons/mongo:push-ids:test');
12
+
13
+ generateTests({
14
+ callback,
15
+ path: [__dirname, '..', 'test-fixtures', 'mongo', 'push-ids'],
16
+ recurse: false,
17
+ useMetadataFile: true,
18
+ fixura: {
19
+ failWhenNotFound: true,
20
+ reader: READERS.JSON
21
+ },
22
+ mocha: {
23
+ before: async () => {
24
+ //debug(`<< Before`);
25
+ await initMongofixtures();
26
+ },
27
+ beforeEach: async () => {
28
+ //debug(`<< BeforeEach`);
29
+ await mongoFixtures.clear();
30
+ },
31
+ afterEach: async () => {
32
+ //debug(`<< AfterEach`);
33
+ await mongoFixtures.clear();
34
+ },
35
+ after: async () => {
36
+ //debug(`<< After`);
37
+ await mongoFixtures.close();
38
+ }
39
+ }
40
+ });
41
+
42
+ async function initMongofixtures() {
43
+ mongoFixtures = await mongoFixturesFactory({
44
+ recurse: false,
45
+ rootPath: [__dirname, '..', 'test-fixtures', 'mongo', 'push-ids'],
46
+ gridFS: {bucketName: 'foobar'},
47
+ useObjectId: true
48
+ });
49
+ }
50
+
51
+ // eslint-disable-next-line max-statements, complexity
52
+ async function callback({
53
+ getFixture,
54
+ functionName,
55
+ params,
56
+ expectModificationTime = false,
57
+ preFillDb = false,
58
+ expectedToThrow = false,
59
+ expectedErrorMessage = '',
60
+ expectedErrorStatus = ''
61
+ //contentStream = false,
62
+ //expectedOpResult = undefined,
63
+ //updateStateBeforeTest = undefined,
64
+ //resultIndex = undefined,
65
+ //createBulkParams = undefined,
66
+ //expectedFileCount = undefined,
67
+ //expectUndefined = undefined
68
+ }) {
69
+
70
+ const mongoUri = await mongoFixtures.getUri();
71
+ const mongoOperator = await createMongoOperator(mongoUri, 'foobar', '');
72
+ const expectedResult = await getFixture('expectedResult.json');
73
+
74
+ await doPreFillDb(preFillDb);
75
+
76
+ async function doPreFillDb(preFillDb) {
77
+ if (preFillDb) {
78
+ await mongoFixtures.populate(getFixture('dbContents.json'));
79
+ return;
80
+ }
81
+ return;
82
+ }
83
+
84
+
85
+ if (functionName === 'pushIds') {
86
+ try {
87
+ debug(`pushIds`);
88
+ debug(JSON.stringify(params));
89
+ //{correlationId, handledIds, rejectedIds}
90
+ const opResult = await mongoOperator.pushIds(params);
91
+ debug(`pushIds result: ${JSON.stringify(opResult)}`);
92
+ await compareToFirstDbEntry({mongoFixtures, expectedResult, expectModificationTime, formatDates: true});
93
+
94
+ } catch (error) {
95
+ handleError({error, expectedToThrow, expectedErrorMessage, expectedErrorStatus});
96
+ return;
97
+ }
98
+ return;
99
+ }
100
+ throw new Error(`Unknown functionName: ${functionName}`);
101
+ }
@@ -0,0 +1,99 @@
1
+ //import {expect} from 'chai';
2
+ import {READERS} from '@natlibfi/fixura';
3
+ import mongoFixturesFactory from '@natlibfi/fixura-mongo';
4
+ import generateTests from '@natlibfi/fixugen';
5
+ import createMongoOperator from '../src/mongo';
6
+ import createDebugLogger from 'debug';
7
+ //import {handleError, compareToFirstDbEntry, compareToDbEntry, formatQueueItem, streamToString} from './testUtils';
8
+ import {handleError, compareToFirstDbEntry} from './testUtils';
9
+
10
+ let mongoFixtures; // eslint-disable-line functional/no-let
11
+ const debug = createDebugLogger('@natlibfi/melinda-rest-api-commons/mongo:push-messages:test');
12
+
13
+ generateTests({
14
+ callback,
15
+ path: [__dirname, '..', 'test-fixtures', 'mongo', 'push-messages'],
16
+ recurse: false,
17
+ useMetadataFile: true,
18
+ fixura: {
19
+ failWhenNotFound: true,
20
+ reader: READERS.JSON
21
+ },
22
+ mocha: {
23
+ before: async () => {
24
+ //debug(`<< Before`);
25
+ await initMongofixtures();
26
+ },
27
+ beforeEach: async () => {
28
+ //debug(`<< BeforeEach`);
29
+ await mongoFixtures.clear();
30
+ },
31
+ afterEach: async () => {
32
+ //debug(`<< AfterEach`);
33
+ await mongoFixtures.clear();
34
+ },
35
+ after: async () => {
36
+ //debug(`<< After`);
37
+ await mongoFixtures.close();
38
+ }
39
+ }
40
+ });
41
+
42
+ async function initMongofixtures() {
43
+ mongoFixtures = await mongoFixturesFactory({
44
+ recurse: false,
45
+ rootPath: [__dirname, '..', 'test-fixtures', 'mongo', 'push-messages'],
46
+ gridFS: {bucketName: 'foobar'},
47
+ useObjectId: true
48
+ });
49
+ }
50
+
51
+ // eslint-disable-next-line max-statements, complexity
52
+ async function callback({
53
+ getFixture,
54
+ functionName,
55
+ params,
56
+ expectModificationTime = false,
57
+ preFillDb = false,
58
+ expectedToThrow = false,
59
+ expectedErrorMessage = '',
60
+ expectedErrorStatus = ''
61
+ //contentStream = false,
62
+ // expectedOpResult = undefined,
63
+ // updateStateBeforeTest = undefined,
64
+ // resultIndex = undefined,
65
+ // createBulkParams = undefined,
66
+ // expectedFileCount = undefined,
67
+ // expectUndefined = undefined
68
+ }) {
69
+
70
+ const mongoUri = await mongoFixtures.getUri();
71
+ const mongoOperator = await createMongoOperator(mongoUri, 'foobar', '');
72
+ const expectedResult = await getFixture('expectedResult.json');
73
+
74
+ await doPreFillDb(preFillDb);
75
+
76
+ async function doPreFillDb(preFillDb) {
77
+ if (preFillDb) {
78
+ await mongoFixtures.populate(getFixture('dbContents.json'));
79
+ return;
80
+ }
81
+ return;
82
+ }
83
+
84
+ if (functionName === 'pushMessages') {
85
+ try {
86
+ debug(`pushMessages`);
87
+ debug(JSON.stringify(params));
88
+ //{correlationId, messages, messageField = 'messages'}
89
+ const opResult = await mongoOperator.pushMessages(params);
90
+ debug(`pushMessages result: ${JSON.stringify(opResult)}`);
91
+ await compareToFirstDbEntry({mongoFixtures, expectedResult, expectModificationTime, formatDates: true});
92
+ } catch (error) {
93
+ handleError({error, expectedToThrow, expectedErrorMessage, expectedErrorStatus});
94
+ return;
95
+ }
96
+ return;
97
+ }
98
+ throw new Error(`Unknown functionName: ${functionName}`);
99
+ }
@@ -0,0 +1,112 @@
1
+ import {expect} from 'chai';
2
+ import {READERS} from '@natlibfi/fixura';
3
+ import mongoFixturesFactory from '@natlibfi/fixura-mongo';
4
+ import generateTests from '@natlibfi/fixugen';
5
+ import createMongoOperator from '../src/mongo';
6
+ import createDebugLogger from 'debug';
7
+ //import {handleError, compareToFirstDbEntry, compareToDbEntry, formatQueueItem, streamToString} from './testUtils';
8
+ import {handleError, compareToDbEntry, formatQueueItem} from './testUtils';
9
+
10
+
11
+ let mongoFixtures; // eslint-disable-line functional/no-let
12
+ const debug = createDebugLogger('@natlibfi/melinda-rest-api-commons/mongo:query-by-id:test');
13
+
14
+ generateTests({
15
+ callback,
16
+ path: [__dirname, '..', 'test-fixtures', 'mongo', 'query-by-id'],
17
+ recurse: false,
18
+ useMetadataFile: true,
19
+ fixura: {
20
+ failWhenNotFound: true,
21
+ reader: READERS.JSON
22
+ },
23
+ mocha: {
24
+ before: async () => {
25
+ //debug(`<< Before`);
26
+ await initMongofixtures();
27
+ },
28
+ beforeEach: async () => {
29
+ //debug(`<< BeforeEach`);
30
+ await mongoFixtures.clear();
31
+ },
32
+ afterEach: async () => {
33
+ //debug(`<< AfterEach`);
34
+ await mongoFixtures.clear();
35
+ },
36
+ after: async () => {
37
+ //debug(`<< After`);
38
+ await mongoFixtures.close();
39
+ }
40
+ }
41
+ });
42
+
43
+ async function initMongofixtures() {
44
+ mongoFixtures = await mongoFixturesFactory({
45
+ recurse: false,
46
+ rootPath: [__dirname, '..', 'test-fixtures', 'mongo', 'query-by-id'],
47
+ gridFS: {bucketName: 'foobar'},
48
+ useObjectId: true
49
+ });
50
+ }
51
+
52
+ // eslint-disable-next-line max-statements, complexity
53
+ async function callback({
54
+ getFixture,
55
+ functionName,
56
+ params,
57
+ expectModificationTime = false,
58
+ preFillDb = false,
59
+ expectedToThrow = false,
60
+ expectedErrorMessage = '',
61
+ expectedErrorStatus = '',
62
+ //contentStream = false,
63
+ expectedOpResult = undefined,
64
+ updateStateBeforeTest = undefined,
65
+ resultIndex = undefined
66
+ //createBulkParams = undefined,
67
+ //expectedFileCount = undefined,
68
+ //expectUndefined = undefined
69
+ }) {
70
+
71
+ const mongoUri = await mongoFixtures.getUri();
72
+ const mongoOperator = await createMongoOperator(mongoUri, 'foobar', '');
73
+ const expectedResult = await getFixture('expectedResult.json');
74
+
75
+ await doPreFillDb(preFillDb);
76
+
77
+ async function doPreFillDb(preFillDb) {
78
+ if (preFillDb) {
79
+ await mongoFixtures.populate(getFixture('dbContents.json'));
80
+ return;
81
+ }
82
+ return;
83
+ }
84
+
85
+
86
+ if (functionName === 'queryById') {
87
+ try {
88
+ debug(`queryById`);
89
+ debug(JSON.stringify(params));
90
+ //{correlationId, checkModTime = false}
91
+ // eslint-disable-next-line functional/no-conditional-statements
92
+ if (updateStateBeforeTest && params.correlationId) {
93
+ debug(`setState to reset modificationTime`);
94
+ await mongoOperator.setState({correlationId: params.correlationId, state: updateStateBeforeTest});
95
+ }
96
+ const opResult = await mongoOperator.queryById(params);
97
+ debug(`queryById result: ${JSON.stringify(opResult)} (it should be: ${JSON.stringify(expectedOpResult)})}`);
98
+ // eslint-disable-next-line functional/no-conditional-statements
99
+ if (expectedOpResult !== undefined) {
100
+ expect(formatQueueItem(opResult)).to.eql(formatQueueItem(expectedOpResult));
101
+ }
102
+ const compareResultIndex = resultIndex ? resultIndex : 0;
103
+ await compareToDbEntry({mongoFixtures, expectedResult, resultIndex: compareResultIndex, expectModificationTime, formatDates: true});
104
+ } catch (error) {
105
+ handleError({error, expectedToThrow, expectedErrorMessage, expectedErrorStatus});
106
+ return;
107
+ }
108
+ return;
109
+ }
110
+
111
+ throw new Error(`Unknown functionName: ${functionName}`);
112
+ }
@@ -0,0 +1,108 @@
1
+ import {expect} from 'chai';
2
+ import {READERS} from '@natlibfi/fixura';
3
+ import mongoFixturesFactory from '@natlibfi/fixura-mongo';
4
+ import generateTests from '@natlibfi/fixugen';
5
+ import createMongoOperator from '../src/mongo';
6
+ import createDebugLogger from 'debug';
7
+ //import {handleError, compareToFirstDbEntry, compareToDbEntry, formatQueueItem, streamToString} from './testUtils';
8
+ import {handleError} from './testUtils';
9
+
10
+ let mongoFixtures; // eslint-disable-line functional/no-let
11
+ const debug = createDebugLogger('@natlibfi/melinda-rest-api-commons/mongo:test');
12
+
13
+ generateTests({
14
+ callback,
15
+ path: [__dirname, '..', 'test-fixtures', 'mongo', 'query'],
16
+ recurse: false,
17
+ useMetadataFile: true,
18
+ fixura: {
19
+ failWhenNotFound: true,
20
+ reader: READERS.JSON
21
+ },
22
+ mocha: {
23
+ before: async () => {
24
+ //debug(`<< Before`);
25
+ await initMongofixtures();
26
+ },
27
+ beforeEach: async () => {
28
+ //debug(`<< BeforeEach`);
29
+ await mongoFixtures.clear();
30
+ },
31
+ afterEach: async () => {
32
+ //debug(`<< AfterEach`);
33
+ await mongoFixtures.clear();
34
+ },
35
+ after: async () => {
36
+ //debug(`<< After`);
37
+ await mongoFixtures.close();
38
+ }
39
+ }
40
+ });
41
+
42
+ async function initMongofixtures() {
43
+ mongoFixtures = await mongoFixturesFactory({
44
+ recurse: false,
45
+ rootPath: [__dirname, '..', 'test-fixtures', 'mongo', 'query'],
46
+ gridFS: {bucketName: 'foobar'},
47
+ useObjectId: true
48
+ });
49
+ }
50
+
51
+ // eslint-disable-next-line max-statements, complexity
52
+ async function callback({
53
+ getFixture,
54
+ functionName,
55
+ params,
56
+ //expectModificationTime = false,
57
+ preFillDb = false,
58
+ expectedToThrow = false,
59
+ expectedErrorMessage = '',
60
+ expectedErrorStatus = '',
61
+ //contentStream = false,
62
+ expectedOpResult = undefined
63
+ //updateStateBeforeTest = undefined,
64
+ //resultIndex = undefined,
65
+ //createBulkParams = undefined,
66
+ //expectedFileCount = undefined,
67
+ //expectUndefined = undefined
68
+ }) {
69
+
70
+ const mongoUri = await mongoFixtures.getUri();
71
+ const mongoOperator = await createMongoOperator(mongoUri, 'foobar', '');
72
+ //const expectedResult = await getFixture('expectedResult.json');
73
+
74
+ await doPreFillDb(preFillDb);
75
+
76
+ async function doPreFillDb(preFillDb) {
77
+ if (preFillDb) {
78
+ await mongoFixtures.populate(getFixture('dbContents.json'));
79
+ return;
80
+ }
81
+ return;
82
+ }
83
+
84
+
85
+ if (functionName === 'query') {
86
+ try {
87
+ debug(`query`);
88
+ debug(JSON.stringify(params));
89
+ //params, showParams = {}
90
+ //params: skip, limit, rest (rest: actual mongo queryParams)
91
+ //showParams: {showAll = 0, showOperations = 0, showOperationSettings = 0, showRecordLoadParams = 0, showImportJobState = 0} = showParams;
92
+ debug(`${JSON.stringify(params.params)}`);
93
+ debug(`${JSON.stringify(params.showParams)}`);
94
+ const opResult = await mongoOperator.query(params.params, params.showParams);
95
+ debug(`query result: ${JSON.stringify(opResult)} (it should be: ${JSON.stringify(expectedOpResult)})}`);
96
+ // eslint-disable-next-line functional/no-conditional-statements
97
+ if (expectedOpResult !== undefined) {
98
+ expect(opResult).to.eql(expectedOpResult);
99
+ }
100
+ } catch (error) {
101
+ handleError({error, expectedToThrow, expectedErrorMessage, expectedErrorStatus});
102
+ return;
103
+ }
104
+ return;
105
+ }
106
+
107
+ throw new Error(`Unknown functionName: ${functionName}`);
108
+ }
@@ -0,0 +1,123 @@
1
+ import {expect} from 'chai';
2
+ import {READERS} from '@natlibfi/fixura';
3
+ import mongoFixturesFactory from '@natlibfi/fixura-mongo';
4
+ import generateTests from '@natlibfi/fixugen';
5
+ import createMongoOperator from '../src/mongo';
6
+ import createDebugLogger from 'debug';
7
+ //import {handleError, compareToFirstDbEntry, compareToDbEntry, formatQueueItem, streamToString} from './testUtils';
8
+ import {handleError, streamToString} from './testUtils';
9
+
10
+
11
+ let mongoFixtures; // eslint-disable-line functional/no-let
12
+ const debug = createDebugLogger('@natlibfi/melinda-rest-api-commons/mongo:read-content:test');
13
+
14
+ generateTests({
15
+ callback,
16
+ path: [__dirname, '..', 'test-fixtures', 'mongo', 'read-content'],
17
+ recurse: false,
18
+ useMetadataFile: true,
19
+ fixura: {
20
+ failWhenNotFound: true,
21
+ reader: READERS.JSON
22
+ },
23
+ mocha: {
24
+ before: async () => {
25
+ //debug(`<< Before`);
26
+ await initMongofixtures();
27
+ },
28
+ beforeEach: async () => {
29
+ //debug(`<< BeforeEach`);
30
+ await mongoFixtures.clear();
31
+ },
32
+ afterEach: async () => {
33
+ //debug(`<< AfterEach`);
34
+ await mongoFixtures.clear();
35
+ },
36
+ after: async () => {
37
+ //debug(`<< After`);
38
+ await mongoFixtures.close();
39
+ }
40
+ }
41
+ });
42
+
43
+ async function initMongofixtures() {
44
+ mongoFixtures = await mongoFixturesFactory({
45
+ recurse: false,
46
+ rootPath: [__dirname, '..', 'test-fixtures', 'mongo', 'read-content'],
47
+ gridFS: {bucketName: 'foobar'},
48
+ useObjectId: true
49
+ });
50
+ }
51
+
52
+ // eslint-disable-next-line max-statements, complexity
53
+ async function callback({
54
+ getFixture,
55
+ functionName,
56
+ params,
57
+ //expectModificationTime = false,
58
+ preFillDb = false,
59
+ expectedToThrow = false,
60
+ expectedErrorMessage = '',
61
+ expectedErrorStatus = '',
62
+ contentStream = false,
63
+ //expectedOpResult = undefined,
64
+ //updateStateBeforeTest = undefined,
65
+ //resultIndex = undefined,
66
+ createBulkParams = undefined
67
+ //expectedFileCount = undefined,
68
+ //expectUndefined = undefined
69
+ }) {
70
+
71
+ const mongoUri = await mongoFixtures.getUri();
72
+ const mongoOperator = await createMongoOperator(mongoUri, 'foobar', '');
73
+ //const expectedResult = await getFixture('expectedResult.json');
74
+
75
+ await doPreFillDb(preFillDb);
76
+
77
+ async function doPreFillDb(preFillDb) {
78
+ if (preFillDb) {
79
+ await mongoFixtures.populate(getFixture('dbContents.json'));
80
+ return;
81
+ }
82
+ return;
83
+ }
84
+
85
+
86
+ if (functionName === 'readContent') {
87
+ try {
88
+ debug(`readContent`);
89
+ debug(JSON.stringify(params));
90
+ //correlationId
91
+
92
+ // eslint-disable-next-line functional/no-conditional-statements
93
+ if (createBulkParams) {
94
+ const stream = contentStream ? await getFixture({components: ['contentStream'], reader: READERS.STREAM}) : createBulkParams.stream;
95
+ //debug(stream);
96
+ const params2 = {...createBulkParams, stream};
97
+ const createBulkResult = await mongoOperator.createBulk(params2);
98
+ debug(`createBulkResult: ${JSON.stringify(createBulkResult)}`);
99
+ }
100
+
101
+ const opResult = await mongoOperator.readContent(params);
102
+ const opResultString = await streamToString(opResult.readStream);
103
+
104
+ // eslint-disable-next-line functional/no-conditional-statements
105
+ if (createBulkParams && createBulkParams.contentType) {
106
+ expect(opResult.contentType).to.eql(createBulkParams.contentType);
107
+ }
108
+
109
+ if (contentStream) {
110
+ const contentStreamString = await getFixture({components: ['contentStream'], reader: READERS.TEXT});
111
+ //debug(contentStreamString);
112
+ expect(opResultString).to.eql(contentStreamString);
113
+ return;
114
+ }
115
+
116
+ } catch (error) {
117
+ handleError({error, expectedToThrow, expectedErrorMessage, expectedErrorStatus});
118
+ return;
119
+ }
120
+ return;
121
+ }
122
+ throw new Error(`Unknown functionName: ${functionName}`);
123
+ }