@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.
- package/dist/mongo.js +15 -9
- package/dist/mongo.js.map +1 -1
- package/package.json +15 -14
- 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 +92 -0
- package/tests/mongo-check-and-check-state.spec.js +105 -0
- package/tests/mongo-check-and-set-import-job-state.spec.js +104 -0
- package/tests/mongo-check-timeout.spec.js +104 -0
- package/tests/mongo-create-bulk.spec.js +93 -0
- package/tests/mongo-create-prio.spec.js +89 -0
- package/tests/mongo-get-one.spec.js +93 -0
- package/tests/mongo-get-stream.spec.js +112 -0
- package/tests/mongo-push-ids.spec.js +91 -0
- package/tests/mongo-push-messages.spec.js +89 -0
- package/tests/mongo-query-by-id.spec.js +106 -0
- package/tests/mongo-query.spec.js +98 -0
- package/tests/mongo-read-content.spec.js +115 -0
- package/tests/mongo-remove.spec.js +110 -0
- package/tests/mongo-set-blob-size.spec.js +89 -0
- package/tests/mongo-set-import-job-state.spec.js +92 -0
- package/tests/mongo-set-operation.spec.js +92 -0
- package/tests/mongo-set-operations.spec.js +91 -0
- package/tests/mongo-set-state.spec.js +92 -0
- package/{dist → tests}/mongoLog.spec.js +22 -22
- package/tests/testUtils.js +102 -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 +0 -768
- package/dist/mongo.spec.js.map +0 -1
- package/dist/mongoLog.spec.js.map +0 -1
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import {expect} from 'chai';
|
|
2
|
+
import {READERS} from '@natlibfi/fixura';
|
|
3
|
+
import createDebugLogger from 'debug';
|
|
4
|
+
import generateTests from '@natlibfi/fixugen';
|
|
5
|
+
import {amqpFactory} from '../src/index';
|
|
6
|
+
import {Error as ApiError} from '@natlibfi/melinda-commons';
|
|
7
|
+
import {promisify} from 'util';
|
|
8
|
+
|
|
9
|
+
const setTimeoutPromise = promisify(setTimeout);
|
|
10
|
+
const debug = createDebugLogger('@natlibfi/melinda-rest-api-commons:amqp:test');
|
|
11
|
+
//const debugData = debug.extend('data');
|
|
12
|
+
|
|
13
|
+
const testData = {
|
|
14
|
+
queue: 'TESTQUEUE',
|
|
15
|
+
correlationId: 'aaaaaaaa-0000-aaaa-0000-000000000000',
|
|
16
|
+
data: {'leader': '02518cam a2200745zi 4500', 'fields': [{'tag': '001', 'value': '000019640'}]}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
debug(`Testing amqp against a local AMQP instance - we'll need some kind of mockup for this`);
|
|
20
|
+
const amqpUrl = 'amqp://127.0.0.1:5672/';
|
|
21
|
+
// eslint-disable-next-line functional/no-let
|
|
22
|
+
let amqpOperator;
|
|
23
|
+
|
|
24
|
+
generateTests({
|
|
25
|
+
callback,
|
|
26
|
+
path: [__dirname, '..', 'test-fixtures', 'amqp'],
|
|
27
|
+
recurse: false,
|
|
28
|
+
useMetadataFile: true,
|
|
29
|
+
fixura: {
|
|
30
|
+
failWhenNotFound: true,
|
|
31
|
+
reader: READERS.JSON
|
|
32
|
+
},
|
|
33
|
+
mocha: {
|
|
34
|
+
before: async () => {
|
|
35
|
+
debug(`Connecting to ${amqpUrl}, with healthCheck running`);
|
|
36
|
+
amqpOperator = await amqpFactory(amqpUrl, true);
|
|
37
|
+
},
|
|
38
|
+
beforeEach: async () => {
|
|
39
|
+
await amqpOperator.checkQueue({queue: testData.queue, toRecord: false, purge: true});
|
|
40
|
+
},
|
|
41
|
+
afterEach: async () => {
|
|
42
|
+
await amqpOperator.removeQueue(testData.queue);
|
|
43
|
+
},
|
|
44
|
+
after: async () => {
|
|
45
|
+
await amqpOperator.closeConnection();
|
|
46
|
+
//await testHeathCheck();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// eslint-disable-next-line max-statements
|
|
52
|
+
async function callback({
|
|
53
|
+
getFixture,
|
|
54
|
+
expectToFail = false,
|
|
55
|
+
expectedErrorStatus = '',
|
|
56
|
+
ackMessages = false,
|
|
57
|
+
nackMessages = false,
|
|
58
|
+
checkNextMessage = false,
|
|
59
|
+
checkQueueStyle = false,
|
|
60
|
+
queue = testData.queue,
|
|
61
|
+
sentToQueue,
|
|
62
|
+
expectedMessageCount,
|
|
63
|
+
removeQueue = false,
|
|
64
|
+
expectedErrorMessage = undefined,
|
|
65
|
+
expectApiError = false,
|
|
66
|
+
expectFalseCheckResult = false
|
|
67
|
+
}) {
|
|
68
|
+
try {
|
|
69
|
+
await sendMessagesToQueue();
|
|
70
|
+
|
|
71
|
+
await ackOrNackMessages();
|
|
72
|
+
|
|
73
|
+
await checkQueue();
|
|
74
|
+
|
|
75
|
+
await queueRemoval();
|
|
76
|
+
|
|
77
|
+
const messageCount = await amqpOperator.checkQueue({queue, style: 'messages'});
|
|
78
|
+
debug(`MessageCount: ${messageCount}`);
|
|
79
|
+
expect(messageCount).to.eql(expectedMessageCount);
|
|
80
|
+
|
|
81
|
+
if (checkNextMessage) {
|
|
82
|
+
const {headers, records} = await queueCheckNextMessage();
|
|
83
|
+
const nextMessage = {headers, records};
|
|
84
|
+
const expectedNextMessage = getFixture('expectedMessage.json');
|
|
85
|
+
expect(nextMessage).to.eql(expectedNextMessage);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
} catch (error) {
|
|
90
|
+
if (!expectToFail) {
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
expect(expectToFail, 'This is expected to fail').to.equal(true);
|
|
94
|
+
testErrors(error);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function testErrors(error) {
|
|
98
|
+
|
|
99
|
+
if (expectApiError) {
|
|
100
|
+
expect(error).to.be.instanceOf(ApiError);
|
|
101
|
+
debug(`We expected and got an ApiError`);
|
|
102
|
+
expect(error.status).to.equal(expectedErrorStatus);
|
|
103
|
+
|
|
104
|
+
if (expectedErrorMessage) {
|
|
105
|
+
expect(error.payload).to.eql(expectedErrorMessage);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
expect(error).not.to.be.instanceOf(ApiError);
|
|
112
|
+
debug(`We expected and got an non-ApiError`);
|
|
113
|
+
debug(`Error is a ${error.constructor.name}`);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function queueRemoval() {
|
|
117
|
+
if (removeQueue) {
|
|
118
|
+
return amqpOperator.removeQueue(removeQueue);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function queueCheckNextMessage() {
|
|
125
|
+
if (checkNextMessage) {
|
|
126
|
+
return amqpOperator.consumeOne(testData.queue, true);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// eslint-disable-next-line max-statements
|
|
133
|
+
async function checkQueue() {
|
|
134
|
+
debug(`checkQueueStyle: ${checkQueueStyle}`);
|
|
135
|
+
|
|
136
|
+
if (checkQueueStyle === 'oneToRecord') {
|
|
137
|
+
const result = await amqpOperator.checkQueue({queue, style: 'one', toRecord: true, purge: false});
|
|
138
|
+
|
|
139
|
+
if (expectFalseCheckResult) {
|
|
140
|
+
expect(result).to.eql(false);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
expect(result).to.have.property('headers');
|
|
145
|
+
expect(result).to.have.property('records');
|
|
146
|
+
expect(result).to.have.property('messages');
|
|
147
|
+
expect(result.records.length).to.eql(1);
|
|
148
|
+
expect(result.messages.length).to.eql(1);
|
|
149
|
+
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (checkQueueStyle === 'oneNotRecord') {
|
|
154
|
+
const result = await amqpOperator.checkQueue({queue, style: 'one', toRecord: false, purge: false});
|
|
155
|
+
const {fields} = result;
|
|
156
|
+
|
|
157
|
+
if (expectFalseCheckResult) {
|
|
158
|
+
expect(result).to.eql(false);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
expect(fields).to.have.property('deliveryTag');
|
|
163
|
+
expect(fields).to.have.property('exchange');
|
|
164
|
+
expect(fields).to.have.property('messageCount');
|
|
165
|
+
expect(fields).to.have.property('redelivered');
|
|
166
|
+
expect(fields).to.have.property('routingKey');
|
|
167
|
+
expect(typeof fields.deliveryTag).to.be.eql('number');
|
|
168
|
+
expect(typeof fields.messageCount).to.be.eql('number');
|
|
169
|
+
expect(typeof fields.redelivered).to.be.eql('boolean');
|
|
170
|
+
expect(typeof fields.routingKey).to.be.eql('string');
|
|
171
|
+
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (checkQueueStyle === 'basicToRecord') {
|
|
176
|
+
const result = await amqpOperator.checkQueue({queue, style: 'basic', toRecord: true, purge: false});
|
|
177
|
+
|
|
178
|
+
if (expectFalseCheckResult) {
|
|
179
|
+
expect(result).to.eql(false);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
expect(result).to.have.property('headers');
|
|
184
|
+
expect(result).to.have.property('records');
|
|
185
|
+
expect(result).to.have.property('messages');
|
|
186
|
+
expect(result.records.length).to.eql(3);
|
|
187
|
+
expect(result.messages.length).to.eql(3);
|
|
188
|
+
const expectedResult = getFixture('expectedResult.json');
|
|
189
|
+
expect(result.records).to.eql(expectedResult);
|
|
190
|
+
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (checkQueueStyle === 'basicNotRecord') {
|
|
195
|
+
const result = await amqpOperator.checkQueue({queue, style: 'basic', toRecord: false, purge: false});
|
|
196
|
+
|
|
197
|
+
if (expectFalseCheckResult) {
|
|
198
|
+
expect(result).to.eql(false);
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
expect(result).to.have.property('headers');
|
|
203
|
+
expect(result).to.have.property('messages');
|
|
204
|
+
expect(result.messages.length).to.eql(3);
|
|
205
|
+
|
|
206
|
+
const [{fields}] = result.messages;
|
|
207
|
+
|
|
208
|
+
expect(fields).to.have.property('deliveryTag');
|
|
209
|
+
expect(fields).to.have.property('exchange');
|
|
210
|
+
expect(fields).to.have.property('messageCount');
|
|
211
|
+
expect(fields).to.have.property('redelivered');
|
|
212
|
+
expect(fields).to.have.property('routingKey');
|
|
213
|
+
|
|
214
|
+
expect(typeof fields.deliveryTag).to.be.eql('number');
|
|
215
|
+
expect(typeof fields.messageCount).to.be.eql('number');
|
|
216
|
+
expect(typeof fields.redelivered).to.be.eql('boolean');
|
|
217
|
+
expect(typeof fields.routingKey).to.be.eql('string');
|
|
218
|
+
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (checkQueueStyle === 'purge') {
|
|
223
|
+
return amqpOperator.checkQueue({queue, style: 'messages', purge: true});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (checkQueueStyle === 'foobar') {
|
|
227
|
+
return amqpOperator.checkQueue({queue, style: 'foobar', toRecord: false, purge: false});
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
async function ackOrNackMessages() {
|
|
234
|
+
if (ackMessages) {
|
|
235
|
+
const message = await amqpOperator.consumeOne(testData.queue, false);
|
|
236
|
+
await amqpOperator.ackMessages([message]);
|
|
237
|
+
|
|
238
|
+
return setTimeoutPromise(10);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (nackMessages) {
|
|
242
|
+
const message = await amqpOperator.consumeOne(testData.queue, false);
|
|
243
|
+
await amqpOperator.nackMessages([message]);
|
|
244
|
+
|
|
245
|
+
return setTimeoutPromise(10);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
async function sendMessagesToQueue() {
|
|
252
|
+
if (sentToQueue) {
|
|
253
|
+
const messages = getFixture('input.json');
|
|
254
|
+
// {queue, correlationId, headers, data}
|
|
255
|
+
|
|
256
|
+
const promises = messages.map(message => amqpOperator.sendToQueue(message));
|
|
257
|
+
await Promise.all(promises);
|
|
258
|
+
|
|
259
|
+
return setTimeoutPromise(10);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/*
|
|
267
|
+
function testHeathCheck() {
|
|
268
|
+
return it('expects healthCheck', async () => {
|
|
269
|
+
|
|
270
|
+
const amqpOperator2 = await amqpFactory(amqpUrl, true);
|
|
271
|
+
try {
|
|
272
|
+
|
|
273
|
+
debug(`**********`);
|
|
274
|
+
await testHealthCheckLoopWorking(amqpOperator2);
|
|
275
|
+
|
|
276
|
+
debug(`**********`);
|
|
277
|
+
await testHealthCheckLoopErroring(amqpOperator2);
|
|
278
|
+
} catch (error) {
|
|
279
|
+
debug(error);
|
|
280
|
+
expect(error).to.eql('');
|
|
281
|
+
} finally {
|
|
282
|
+
amqpOperator2.closeConnection();
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
async function testHealthCheckLoopWorking(amqpOperator) {
|
|
286
|
+
debug(`Testing healthCheckLoop for ${amqpOperator}`);
|
|
287
|
+
debug(`Waiting 0.5s and expecting healthCheckLoop to work`);
|
|
288
|
+
await setTimeoutPromise('500');
|
|
289
|
+
debug(`Amqp Operator should have been alive.`);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
async function testHealthCheckLoopErroring(amqpOperator) {
|
|
293
|
+
debug(`Testing healthCheckLoop with closed channel`);
|
|
294
|
+
amqpOperator.closeChannel();
|
|
295
|
+
debug(`Waiting 0.5s and expecting healthCheckLoop to throw error`);
|
|
296
|
+
await setTimeoutPromise('500');
|
|
297
|
+
debug(`amqpOperator should have errored`);
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
*/
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import {expect} from 'chai';
|
|
4
|
+
import fixtureFactory from '@natlibfi/fixura';
|
|
5
|
+
import {MarcRecord} from '@natlibfi/marc-record';
|
|
6
|
+
import {Error as ConversionError} from '@natlibfi/melinda-commons';
|
|
7
|
+
import createConversionService from '../../src/helpers/conversion';
|
|
8
|
+
import {CONVERSION_FORMATS} from '../../src/constants';
|
|
9
|
+
|
|
10
|
+
describe('services/conversion', () => {
|
|
11
|
+
const {getFixture} = fixtureFactory({root: [
|
|
12
|
+
__dirname,
|
|
13
|
+
'..',
|
|
14
|
+
'..',
|
|
15
|
+
'test-fixtures',
|
|
16
|
+
'conversion'
|
|
17
|
+
]});
|
|
18
|
+
const conversionService = createConversionService();
|
|
19
|
+
|
|
20
|
+
const marcRecord = new MarcRecord(JSON.parse(getFixture({components: ['json1']})), {subfieldValues: false});
|
|
21
|
+
const marcXml = getFixture({components: ['marcxml1']});
|
|
22
|
+
const iso2709 = getFixture({components: ['iso2709_1']});
|
|
23
|
+
const json = getFixture({components: ['json1']});
|
|
24
|
+
const alephSeq = getFixture({components: ['aleph_sequential']});
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
describe('factory', () => {
|
|
28
|
+
it('Should create the expected object', () => {
|
|
29
|
+
const service = createConversionService();
|
|
30
|
+
expect(service).to.be.an('object').and
|
|
31
|
+
.respondTo('serialize')
|
|
32
|
+
.respondTo('unserialize');
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
describe('#serialize', () => {
|
|
37
|
+
it('Should throw because of unsupported format', () => {
|
|
38
|
+
expect(conversionService.serialize).to.throw();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('Should serialize to MARCXML', () => {
|
|
42
|
+
const data = conversionService.serialize(marcRecord, CONVERSION_FORMATS.MARCXML);
|
|
43
|
+
expect(data).to.equal(marcXml);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('Should serialize to ISO2709', () => {
|
|
47
|
+
const data = conversionService.serialize(marcRecord, CONVERSION_FORMATS.ISO2709);
|
|
48
|
+
expect(data).to.equal(iso2709);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('Should serialize to JSON', () => {
|
|
52
|
+
const data = conversionService.serialize(marcRecord, CONVERSION_FORMATS.JSON);
|
|
53
|
+
expect(data).to.equal(json);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('Should serialize to aleph sequential', () => {
|
|
57
|
+
const data = conversionService.serialize(marcRecord, CONVERSION_FORMATS.ALEPHSEQ);
|
|
58
|
+
expect(data).to.equal(alephSeq);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe('#unserialize', () => {
|
|
64
|
+
it('Should throw because of unsupported format', () => {
|
|
65
|
+
expect(conversionService.unserialize).to.throw();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('Should unserialize from MARCXML', async () => {
|
|
69
|
+
const record = await conversionService.unserialize(marcXml, CONVERSION_FORMATS.MARCXML);
|
|
70
|
+
expect(record.equalsTo(marcRecord)).to.equal(true);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('Should unserialize from ISO2709', () => {
|
|
74
|
+
const record = conversionService.unserialize(iso2709, CONVERSION_FORMATS.ISO2709);
|
|
75
|
+
expect(record.equalsTo(marcRecord)).to.equal(true);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('Should unserialize from JSON', () => {
|
|
79
|
+
const record = conversionService.unserialize(json, CONVERSION_FORMATS.JSON);
|
|
80
|
+
expect(record.equalsTo(marcRecord)).to.equal(true);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('Should unserialize from Aleph sequential', () => {
|
|
84
|
+
const record = conversionService.unserialize(alephSeq, CONVERSION_FORMATS.ALEPHSEQ);
|
|
85
|
+
expect(record.equalsTo(marcRecord)).to.equal(true);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
it('Should throw because the record could not be unserialized', () => {
|
|
90
|
+
expect(() => {
|
|
91
|
+
conversionService.unserialize('', CONVERSION_FORMATS.JSON);
|
|
92
|
+
}).to.throw(ConversionError);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {expect} from 'chai';
|
|
2
|
+
import {MarcRecord} from '@natlibfi/marc-record';
|
|
3
|
+
import {READERS} from '@natlibfi/fixura';
|
|
4
|
+
import generateTests from '@natlibfi/fixugen';
|
|
5
|
+
import {fixRecord} from '../../src/helpers/fix';
|
|
6
|
+
import * as fixSettings from '../../src/helpers/fix-constants';
|
|
7
|
+
import createDebugLogger from 'debug';
|
|
8
|
+
// import inspect from 'util';
|
|
9
|
+
|
|
10
|
+
const debug = createDebugLogger('@natlibfi/melinda-rest-api-commons:fix:test');
|
|
11
|
+
const debugData = debug.extend('data');
|
|
12
|
+
|
|
13
|
+
generateTests({
|
|
14
|
+
callback,
|
|
15
|
+
path: [__dirname, '..', '..', 'test-fixtures', 'fix'],
|
|
16
|
+
recurse: true,
|
|
17
|
+
useMetadataFile: true,
|
|
18
|
+
fixura: {
|
|
19
|
+
failWhenNotFound: false,
|
|
20
|
+
reader: READERS.JSON
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
function callback({getFixture, settings = undefined}) {
|
|
25
|
+
if (settings !== undefined && fixSettings[settings] === undefined) {
|
|
26
|
+
throw new Error(`There are no settings ${settings} defined!`);
|
|
27
|
+
}
|
|
28
|
+
const runSettings = fixSettings[settings] || settings;
|
|
29
|
+
|
|
30
|
+
const record = new MarcRecord(getFixture('record.json'), {subfieldValues: false});
|
|
31
|
+
const expectedRecord = getFixture('expectedRecord.json');
|
|
32
|
+
|
|
33
|
+
debug(`Running fixer with settings ${settings}.`);
|
|
34
|
+
debugData(JSON.stringify(runSettings, null, 2));
|
|
35
|
+
|
|
36
|
+
const resultRecord = fixRecord(record, runSettings);
|
|
37
|
+
debugData(`Result: ${JSON.stringify(resultRecord)}`);
|
|
38
|
+
debugData(`Expected: ${JSON.stringify(expectedRecord)}`);
|
|
39
|
+
expect(resultRecord).to.eql(expectedRecord);
|
|
40
|
+
|
|
41
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {expect} from 'chai';
|
|
2
|
+
import fixtureFactory, {READERS} from '@natlibfi/fixura';
|
|
3
|
+
import {MarcRecord} from '@natlibfi/marc-record';
|
|
4
|
+
import createValidator from '../../src/helpers/validation';
|
|
5
|
+
|
|
6
|
+
describe('services/validate', () => {
|
|
7
|
+
const FIXTURES_PATH = [
|
|
8
|
+
__dirname,
|
|
9
|
+
'..',
|
|
10
|
+
'..',
|
|
11
|
+
'test-fixtures',
|
|
12
|
+
'validation'
|
|
13
|
+
];
|
|
14
|
+
const {getFixture} = fixtureFactory({root: FIXTURES_PATH, reader: READERS.JSON});
|
|
15
|
+
|
|
16
|
+
describe('f003-fi-melinda', () => {
|
|
17
|
+
it('Should have failed: false', async () => {
|
|
18
|
+
const validator = await createValidator();
|
|
19
|
+
const record = new MarcRecord(getFixture({
|
|
20
|
+
components: [
|
|
21
|
+
'in',
|
|
22
|
+
'f003-fi-melinda.json'
|
|
23
|
+
]
|
|
24
|
+
}));
|
|
25
|
+
const result = await validator(record.toObject());
|
|
26
|
+
const expected = getFixture({
|
|
27
|
+
components: [
|
|
28
|
+
'out',
|
|
29
|
+
'f003-fi-melinda.json'
|
|
30
|
+
]
|
|
31
|
+
});
|
|
32
|
+
expect(result).to.eql(expected);
|
|
33
|
+
expect(result.failed).to.equal(false);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('f003-not-fi-melinda', () => {
|
|
38
|
+
it('Should have failed: true', async () => {
|
|
39
|
+
const validator = await createValidator();
|
|
40
|
+
const record = new MarcRecord(getFixture({
|
|
41
|
+
components: [
|
|
42
|
+
'in',
|
|
43
|
+
'f003-not-fi-melinda.json'
|
|
44
|
+
]
|
|
45
|
+
}));
|
|
46
|
+
const result = await validator(record.toObject());
|
|
47
|
+
const expected = getFixture({
|
|
48
|
+
components: [
|
|
49
|
+
'out',
|
|
50
|
+
'f003-not-fi-melinda.json'
|
|
51
|
+
]
|
|
52
|
+
});
|
|
53
|
+
expect(result).to.eql(expected);
|
|
54
|
+
expect(result.failed).to.equal(true);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
@@ -0,0 +1,92 @@
|
|
|
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 createDebugLogger from 'debug';
|
|
6
|
+
//import {handleError, compareToFirstDbEntry, compareToDbEntry, formatQueueItem, streamToString} from './testUtils';
|
|
7
|
+
import {getMongoOperator, 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:add-blob-size:test');
|
|
12
|
+
|
|
13
|
+
generateTests({
|
|
14
|
+
callback,
|
|
15
|
+
path: [__dirname, '..', 'test-fixtures', 'mongo', 'add-blob-size'],
|
|
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', 'add-blob-size'],
|
|
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
|
+
|
|
62
|
+
}) {
|
|
63
|
+
|
|
64
|
+
const mongoOperator = await getMongoOperator(mongoFixtures);
|
|
65
|
+
const expectedResult = await getFixture('expectedResult.json');
|
|
66
|
+
|
|
67
|
+
await doPreFillDb(preFillDb);
|
|
68
|
+
|
|
69
|
+
async function doPreFillDb(preFillDb) {
|
|
70
|
+
if (preFillDb) {
|
|
71
|
+
await mongoFixtures.populate(getFixture('dbContents.json'));
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (functionName === 'addBlobSize') {
|
|
78
|
+
try {
|
|
79
|
+
debug(`addBlobSize`);
|
|
80
|
+
debug(JSON.stringify(params));
|
|
81
|
+
//{correlationId}
|
|
82
|
+
const opResult = await mongoOperator.addBlobSize(params);
|
|
83
|
+
debug(`addBlobSize result: ${JSON.stringify(opResult)}`);
|
|
84
|
+
await compareToFirstDbEntry({mongoFixtures, expectedResult, expectModificationTime, formatDates: true});
|
|
85
|
+
} catch (error) {
|
|
86
|
+
handleError({error, expectedToThrow, expectedErrorMessage, expectedErrorStatus});
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
throw new Error(`Unknown functionName: ${functionName}`);
|
|
92
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
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 createDebugLogger from 'debug';
|
|
6
|
+
//import {handleError, compareToFirstDbEntry, compareToDbEntry, formatQueueItem, streamToString} from './testUtils';
|
|
7
|
+
import {getMongoOperator, 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:test');
|
|
12
|
+
|
|
13
|
+
generateTests({
|
|
14
|
+
callback,
|
|
15
|
+
path: [__dirname, '..', 'test-fixtures', 'mongo', 'check-and-set-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-state'],
|
|
46
|
+
gridFS: {bucketName: 'foobar'},
|
|
47
|
+
useObjectId: true
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function callback({
|
|
52
|
+
getFixture,
|
|
53
|
+
functionName,
|
|
54
|
+
params,
|
|
55
|
+
expectModificationTime = false,
|
|
56
|
+
preFillDb = false,
|
|
57
|
+
expectedToThrow = false,
|
|
58
|
+
expectedErrorMessage = '',
|
|
59
|
+
expectedErrorStatus = '',
|
|
60
|
+
expectedOpResult = undefined,
|
|
61
|
+
updateStateBeforeTest = undefined
|
|
62
|
+
}) {
|
|
63
|
+
|
|
64
|
+
const mongoOperator = await getMongoOperator(mongoFixtures);
|
|
65
|
+
const expectedResult = await getFixture('expectedResult.json');
|
|
66
|
+
|
|
67
|
+
await doPreFillDb(preFillDb);
|
|
68
|
+
|
|
69
|
+
async function doPreFillDb(preFillDb) {
|
|
70
|
+
if (preFillDb) {
|
|
71
|
+
await mongoFixtures.populate(getFixture('dbContents.json'));
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (functionName === 'checkAndSetState') {
|
|
78
|
+
try {
|
|
79
|
+
debug(`checkAndSetState`);
|
|
80
|
+
debug(JSON.stringify(params));
|
|
81
|
+
//{correlationId, state, errorMessage = undefined, errorStatus = undefined})
|
|
82
|
+
// timeout
|
|
83
|
+
|
|
84
|
+
// eslint-disable-next-line functional/no-conditional-statements
|
|
85
|
+
if (updateStateBeforeTest && params.correlationId) {
|
|
86
|
+
debug(`setState to reset modificationTime`);
|
|
87
|
+
await mongoOperator.setState({correlationId: params.correlationId, state: updateStateBeforeTest});
|
|
88
|
+
}
|
|
89
|
+
const opResult = await mongoOperator.checkAndSetState(params);
|
|
90
|
+
debug(`checkAndSetState result: ${JSON.stringify(opResult)} (it should be: ${JSON.stringify(expectedOpResult)})}`);
|
|
91
|
+
|
|
92
|
+
// eslint-disable-next-line functional/no-conditional-statements
|
|
93
|
+
if (expectedOpResult !== undefined) {
|
|
94
|
+
expect(opResult).to.eql(expectedOpResult);
|
|
95
|
+
}
|
|
96
|
+
await compareToFirstDbEntry({mongoFixtures, expectedResult, expectModificationTime, formatDates: true});
|
|
97
|
+
|
|
98
|
+
} catch (error) {
|
|
99
|
+
handleError({error, expectedToThrow, expectedErrorMessage, expectedErrorStatus});
|
|
100
|
+
}
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
throw new Error(`Unknown functionName: ${functionName}`);
|
|
105
|
+
}
|