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