Skip to content

Commit

Permalink
Add test etc. (#139)
Browse files Browse the repository at this point in the history
* add tests
* improve old tests
* add test to bad date
  • Loading branch information
natlibfi-jonollil authored Oct 24, 2023
1 parent f4ed42b commit f98f5fa
Show file tree
Hide file tree
Showing 21 changed files with 2,177 additions and 370 deletions.
728 changes: 380 additions & 348 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions src/mongoLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default async function (MONGO_URI, dbName = 'rest-api') {
const client = await MongoClient.connect(MONGO_URI, {useNewUrlParser: true, useUnifiedTopology: true});
const db = client.db(dbName);
const collection = 'logs';
return {addLogItem, query, queryById, getListOfLogs, getCatalogersListOfLogs, getExpandedListOfLogs, protect, remove, removeBySequences};
return {addLogItem, query, queryById, getListOfLogs, getListOfCatalogers, getExpandedListOfLogs, protect, remove, removeBySequences};

async function addLogItem(logItem) {
const time = moment().toDate();
Expand All @@ -52,14 +52,14 @@ export default async function (MONGO_URI, dbName = 'rest-api') {
protected: false
};
try {
console.log(newLogItem); // eslint-disable-line
// console.log(newLogItem); // eslint-disable-line
checkLogItemType(logItem.logItemType, false);
const result = await db.collection(collection).insertOne(newLogItem);
if (result.acknowledged) {
const {blobSequence, blobSequenceStart, blobSequenceEnd} = logItem;
const itemString = blobSequenceStart && blobSequenceEnd ? `${blobSequenceStart} - ${blobSequenceEnd}` : `${blobSequence}`;
logger.info(`*** New ${logItem.logItemType} added for ${logItem.correlationId}, blobSequence(s): ${itemString}. ***`);
console.log('addLogItem done', result); // eslint-disable-line
// console.log('addLogItem done', result); // eslint-disable-line
return;
}
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR);
Expand Down Expand Up @@ -109,8 +109,8 @@ export default async function (MONGO_URI, dbName = 'rest-api') {
return {status: result.length > 0 ? httpStatus.OK : httpStatus.NOT_FOUND, payload: result.length > 0 ? result : 'No logs found'};
}

async function getCatalogersListOfLogs() {
logger.debug(`Getting expanded list of logs`);
async function getListOfCatalogers() {
logger.debug(`Getting list of Catalogers`);

const result = await db.collection(collection) // eslint-disable-line functional/immutable-data
.distinct('cataloger');
Expand Down Expand Up @@ -163,13 +163,15 @@ export default async function (MONGO_URI, dbName = 'rest-api') {
return {status: fixedResult.length > 0 ? httpStatus.OK : httpStatus.NOT_FOUND, payload: fixedResult.length > 0 ? fixedResult : 'No logs found'};

function generateMatchObject(logItemTypes, catalogers, dateBefore, dateAfter) {
const dateBeforeIso = new Date(dateBefore).toISOString();
const dateAfterIso = new Date(dateAfter).toISOString();
const matchOptions = {
'$match': {
'logItemType': logItemTypes.length > 0 ? {'$in': logItemTypes} : /.*/ui,
'cataloger': catalogers.length > 0 ? {'$in': catalogers} : /.*/ui,
'creationTime': {
'$gte': new Date(dateAfter).toISOString(),
'$lte': new Date(dateBefore).toISOString()
'$gte': dateAfterIso,
'$lte': dateBeforeIso
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/mongoLog.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ async function callback({

if (functionName === 'getCatalogersListOfLogs') {
const result = await mongoLogOperator.getCatalogersListOfLogs();
console.log(result); // eslint-disable-line
// console.log(result); // eslint-disable-line

return expect(result).to.eql(expectedResult);
}

if (functionName === 'getExpandedListOfLogs') {
const result = await mongoLogOperator.getExpandedListOfLogs(params);
console.log(result); // eslint-disable-line
// console.log(result); // eslint-disable-line

return expect(result).to.eql(expectedResult);
}
Expand Down
4 changes: 2 additions & 2 deletions test-fixtures/mongoLog/02/metadata.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"description": "Function: getCatalogersListOfLogs, gets 3 uniq catalogers from 3 logs (MATCH_LOG and MERGE_LOG)",
"description": "Function: getListOfCatalogers, gets 3 uniq catalogers from 3 logs (MATCH_LOG and MERGE_LOG)",
"only": false,
"skip": false,
"functionName": "getCatalogersListOfLogs",
"functionName": "getListOfCatalogers",
"preFillDb": true,
"params": []
}
7 changes: 7 additions & 0 deletions test-fixtures/mongoLog/04/expectedResult.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
"creationTime": "2023-08-08T01:25:59.348Z",
"logCount": 1,
"logItemType": "MERGE_LOG"
},
{
"cataloger": "IMP_TEST3",
"correlationId": "7207c1f2-cbc4-4a0c-bd64-344a746ee605",
"creationTime": "2023-09-21T01:07:26.693Z",
"logCount": 1,
"logItemType": "MATCH_LOG"
}
],
"status": 200
Expand Down
4 changes: 2 additions & 2 deletions test-fixtures/mongoLog/04/metadata.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"description": "Function: getExpandedListOfLogs - Empty params get all 3",
"description": "Function: getExpandedListOfLogs - params {logItemTypes: [MERGE_LOG, MATCH_LOG]} get all 3",
"only": false,
"skip": false,
"functionName": "getExpandedListOfLogs",
"preFillDb": true,
"params": {"logItemTypes": ["MERGE_LOG"]}
"params": {"logItemTypes": ["MERGE_LOG", "MATCH_LOG"]}
}
6 changes: 3 additions & 3 deletions test-fixtures/mongoLog/05/metadata.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"description": "Function: getExpandedListOfLogs - logItemType and default dates - get 2/3",
"only": true,
"description": "Function: getExpandedListOfLogs - params {logItemTypes: [MERGE_LOG]} get 2",
"only": false,
"skip": false,
"functionName": "getExpandedListOfLogs",
"preFillDb": true,
"params": {"logItemTypes": ["MERGE_LOG"], "dateAfter": "Sat Jan 01 2000 02:00:00 GMT+0200 (Eastern European Standard Time)", "dateBefore": "Fri Oct 20 2023 16:37:00 GMT+0300 (Eastern European Summer Time)"}
"params": {"logItemTypes": ["MERGE_LOG"]}
}
6 changes: 3 additions & 3 deletions test-fixtures/mongoLog/06/expectedResult.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"payload": [
{
"cataloger": "IMP_TEST2",
"correlationId": "f63bad0b-8820-428a-b570-c172f209ee94",
"creationTime": "2023-08-08T01:25:59.348Z",
"cataloger": "IMP_TEST1",
"correlationId": "bdce85eb-0ca2-4525-b00e-ae67d43b68ec",
"creationTime": "2023-08-07T01:21:38.392Z",
"logCount": 1,
"logItemType": "MERGE_LOG"
}
Expand Down
6 changes: 3 additions & 3 deletions test-fixtures/mongoLog/06/metadata.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"description": "Function: getExpandedListOfLogs - logItemType and default dates - get 2/3",
"only": true,
"description": "Function: getExpandedListOfLogs - params {catalogers: [IMP_TEST1]} get 1",
"only": false,
"skip": false,
"functionName": "getExpandedListOfLogs",
"preFillDb": true,
"params": {"logItemTypes": ["MERGE_LOG"], "dateAfter": "2023-08-07T01:24:00", "dateBefore": "Fri Oct 20 2023 16:37:00 GMT+0300 (Eastern European Summer Time)"}
"params": {"catalogers": ["IMP_TEST1"]}
}
Loading

0 comments on commit f98f5fa

Please sign in to comment.