Skip to content

Commit

Permalink
worked on tests to corroborate the updates made with the default disa…
Browse files Browse the repository at this point in the history
…ble checks
  • Loading branch information
nneni committed Oct 20, 2024
1 parent 674de8a commit a9e174a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
34 changes: 25 additions & 9 deletions src/server/services/pipeline-in-progress/validateReadings.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ const { log } = require('../../log');
* @param {Reading[]} arrayToValidate
* @param {dict} conditionSet used to validate readings (minVal, maxVal, minDate, maxDate, threshold, maxError)
* @param {string} meterIdentifier identifier of meter being checked
* @returns {object} { validReadings, invalidReadings, errMsg }
*/
function validateReadings(arrayToValidate, conditionSet, meterIdentifier = undefined) {
/* tslint:disable:no-string-literal */
const { validDates, errMsg: errMsgDate } = checkDate(arrayToValidate, conditionSet['minDate'], conditionSet['maxDate'], conditionSet['maxError'] / 2, meterIdentifier);
const { validValues, errMsg: errMsgValue } = checkValue(arrayToValidate, conditionSet['minVal'], conditionSet['maxVal'], conditionSet['maxError'] / 2, meterIdentifier);
/* tslint:enable:no-string-literal */
const validReadings = validDates.filter(reading => validValues.includes(reading));
const invalidReadings = [...invalidDates, ...invalidValues];
const errMsg = errMsgDate + errMsgValue;

return {
validReadings: validDates && validValues,
errMsg,
Expand All @@ -32,14 +36,18 @@ function validateReadings(arrayToValidate, conditionSet, meterIdentifier = undef
* @param {Moment} maxDate inclusive latest acceptable date (won't be rejected)
* @param {number} maxError maximum number of errors to be reported, ignore the rest
* @param {string} meterIdentifier identifier of meter being checked.
* @returns {object} { validDates, invalidDates, errMsg }
*/
function checkDate(arrayToValidate, minDate, maxDate, maxError, meterIdentifier) {
let validDates = true;
let validDates = [];
let invalidDates = [];
let errMsg = '';
let readingNumber = 0;

if (minDate === null && maxDate === null) {
return { validDates, errMsg };
return { validDates: arrayToValidate, invalidDates: [], errMsg };
}
let readingNumber = 0;

for (const reading of arrayToValidate) {
readingNumber++;
if (maxError <= 0) {
Expand All @@ -53,18 +61,20 @@ function checkDate(arrayToValidate, minDate, maxDate, maxError, meterIdentifier)
errMsg += '<br>' + newErrMsg + '<br>';
--maxError;
validDates = false;
}
if (reading.endTimestamp > maxDate) {
} else if (reading.endTimestamp > maxDate) {
const newErrMsg = `error when checking reading time for #${readingNumber} on meter ${meterIdentifier}: ` +
`time ${reading.endTimestamp} is later than upper bound ${maxDate} ` +
`with reading ${reading.reading} and startTimestamp ${reading.startTimestamp}`;
log.error(newErrMsg);
errMsg += '<br>' + newErrMsg + '<br>';
invalidDates.push(reading);
--maxError;
validDates = false;
} else {
validDates.push(reading);
}
}
return { validDates, errMsg };

return { validDates, invalidDates, errMsg };
}

/**
Expand All @@ -74,11 +84,14 @@ function checkDate(arrayToValidate, minDate, maxDate, maxError, meterIdentifier)
* @param {number} maxVal inclusive maximum acceptable reading value (won't be rejected)
* @param {number} maxError maximum number of errors to be reported, ignore the rest
* @param {string} meterIdentifier identifier of meter being checked.
* @returns {object} { validValues, invalidValues, errMsg }
*/
function checkValue(arrayToValidate, minVal, maxVal, maxError, meterIdentifier) {
let validValues = true;
let invalidValues = [];
let errMsg = '';
let readingNumber = 0;

for (const reading of arrayToValidate) {
readingNumber++;
if (maxError <= 0) {
Expand All @@ -98,11 +111,14 @@ function checkValue(arrayToValidate, minVal, maxVal, maxError, meterIdentifier)
`with startTimestamp ${reading.startTimestamp} and endTimestamp ${reading.endTimestamp}`;
log.error(newErrMsg);
errMsg += '<br>' + newErrMsg + '<br>';
invalidValues.push(reading);
--maxError;
validValues = false;
} else {
validValues.push(reading);
}
}
return { validValues, errMsg };

return { validValues, invalidValues, errMsg };
}

/**
Expand Down
29 changes: 25 additions & 4 deletions src/server/test/db/validateReadingsTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ mocha.describe('PIPELINE: Validate Readings', () => {
)).map(reading => checkDate([reading], minDate, maxDate, Number.MAX_VALUE));
for (let i = 0; i < 4; ++i) {
if (i % 2 === 0) {
expect(results[i].validDates).to.equal(true);
expect(results[i].validDates.length).to.be.greaterThan(0);
expect(results[i].invalidDates.length).to.equal(0);
} else {
expect(results[i].validDates).to.equal(false);
expect(results[i].validDates.length).to.equal(0);
expect(results[i].invalidDates.length).to.be.greaterThan(0);
}
}
});

mocha.it('detects out-of-bound data', async () => {
const minVal = 10;
const maxVal = 20;
Expand All @@ -46,9 +49,11 @@ mocha.describe('PIPELINE: Validate Readings', () => {
)).map(reading => checkValue([reading], minVal, maxVal, Number.MAX_VALUE));
for (let i = 0; i < 7; ++i) {
if (i % 2 === 0) {
expect(results[i].validValues).to.equal(false);
expect(results[i].validValues.length).to.equal(0);
expect(results[i].invalidValues.length).to.be.greaterThan(0);
} else {
expect(results[i].validValues).to.equal(true);
expect(results[i].validValues.length).to.be.greaterThan(0);
expect(results[i].invalidValues.length).to.equal(0);
}
}
});
Expand Down Expand Up @@ -92,10 +97,26 @@ mocha.describe('PIPELINE: Validate Readings', () => {
new Reading(undefined, 20, moment('1970-01-01 00:01:00'), moment('1970-01-01 00:01:01')),
new Reading(undefined, 0, moment('1970-01-01 00:01:30'), moment('1970-01-01 00:02:01'))];

let mixedData = [
new Reading(undefined, 0, moment('1970-01-01 00:00:00'), moment('1970-01-01 00:01:00')), // valid
new Reading(undefined, 30, moment('1970-01-01 00:01:00'), moment('1970-01-01 00:02:00')), // invalid (value too high)
new Reading(undefined, 0, moment('1969-01-01 00:00:00'), moment('1969-01-01 00:01:00')), // invalid (date too early)
new Reading(undefined, 10, moment('1980-01-01 00:00:00'), moment('1980-01-01 01:00:00')) // valid
];

const result = validateReadings(mixedData, conditionSet);

expect(checkIntervals(badIntervals, conditionSet['threshold']).validIntervals).to.equal(false);
expect(validateReadings(badDate, conditionSet).validReadings).to.equal(false);
expect(validateReadings(badValue, conditionSet).validReadings).to.equal(false);
expect(validateReadings(goodData, conditionSet).validReadings).to.equal(true);

expect(result.validReadings.length).to.equal(2);
expect(result.validReadings[0].reading).to.equal(0);
expect(result.validReadings[1].reading).to.equal(10);

expect(result.invalidReadings.length).to.equal(2);
expect(result.invalidReadings[0].reading).to.equal(30);
expect(result.invalidReadings[1].startTimestamp.isBefore('1970-01-01 00:00:00')).to.equal(true);
});
});

0 comments on commit a9e174a

Please sign in to comment.