Skip to content

Commit

Permalink
Merge pull request #1128 from alfarj83/Add-min/max-test-function
Browse files Browse the repository at this point in the history
Max/min parsing function
  • Loading branch information
huss authored Feb 23, 2024
2 parents c0ab372 + 6961823 commit eeb4e0a
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
min,reading,max,start time,end time
10.6426891004995,202.88180026332,397.189849395253,2022-08-25 00:00:00,2022-08-26 00:00:00
16.5997962068877,179.691084704245,395.109107889164,2022-08-26 00:00:00,2022-08-27 00:00:00
4.5823651802204,199.183807536,398.411679942704,2022-08-27 00:00:00,2022-08-28 00:00:00
15.3642573360935,192.74995508249,394.643152087121,2022-08-28 00:00:00,2022-08-29 00:00:00
6.29056060065448,185.412501885932,385.856740151065,2022-08-29 00:00:00,2022-08-30 00:00:00
6.86420796677648,198.811074839024,395.563823102245,2022-08-30 00:00:00,2022-08-31 00:00:00
7.43438994317359,193.504608720336,387.965878057152,2022-08-31 00:00:00,2022-09-01 00:00:00
11.8212615738301,192.030328377401,399.520437427381,2022-09-01 00:00:00,2022-09-02 00:00:00
8.33718067987179,212.075434543447,398.567045473078,2022-09-02 00:00:00,2022-09-03 00:00:00
11.4111445542779,202.971610292043,393.389074200625,2022-09-03 00:00:00,2022-09-04 00:00:00
15.6820576786335,196.141097903804,388.815491970447,2022-09-04 00:00:00,2022-09-05 00:00:00
6.38333585089936,187.595473624637,397.052005292176,2022-09-05 00:00:00,2022-09-06 00:00:00
4.96432191677293,199.037216460864,397.035480858967,2022-09-06 00:00:00,2022-09-07 00:00:00
7.67557143008119,195.285318015471,381.204626945531,2022-09-07 00:00:00,2022-09-08 00:00:00
6.72960982731486,188.423597655062,394.035396458706,2022-09-08 00:00:00,2022-09-09 00:00:00
5.08026786370556,206.15342719543,398.133028735512,2022-09-09 00:00:00,2022-09-10 00:00:00
4.29407258042591,190.350749657986,397.995914276686,2022-09-10 00:00:00,2022-09-11 00:00:00
7.43874462146493,223.360284254564,399.60078117885,2022-09-11 00:00:00,2022-09-12 00:00:00
6.74162186785974,197.009006872453,395.97596159838,2022-09-12 00:00:00,2022-09-13 00:00:00
4.19776432857257,208.140752853939,398.720053843593,2022-09-13 00:00:00,2022-09-14 00:00:00
7.03930350560177,189.55589714252,398.056285836813,2022-09-14 00:00:00,2022-09-15 00:00:00
4.55696915516148,203.552587120228,399.432853809808,2022-09-15 00:00:00,2022-09-16 00:00:00
4.76122698910952,190.556043570215,386.592709772111,2022-09-16 00:00:00,2022-09-17 00:00:00
4.73481997409481,204.232783877117,398.469854942283,2022-09-17 00:00:00,2022-09-18 00:00:00
7.45500342512439,214.488485012056,396.534786377705,2022-09-18 00:00:00,2022-09-19 00:00:00
7.00053262408678,185.094172575785,397.119985055946,2022-09-19 00:00:00,2022-09-20 00:00:00
7.42313016152277,223.389218077435,398.265853881631,2022-09-20 00:00:00,2022-09-21 00:00:00
9.67235053788254,187.20328697284,398.374776981315,2022-09-21 00:00:00,2022-09-22 00:00:00
5.16339174456368,208.275189672324,399.186850998811,2022-09-22 00:00:00,2022-09-23 00:00:00
17.9386480149719,188.539265544258,395.141115538206,2022-09-23 00:00:00,2022-09-24 00:00:00
5.8614423320328,190.460396853295,399.516915252127,2022-09-24 00:00:00,2022-09-25 00:00:00
6.17122260696813,201.820856785749,399.282645580328,2022-09-25 00:00:00,2022-09-26 00:00:00
8.70731392274497,191.794497445458,394.801202250496,2022-09-26 00:00:00,2022-09-27 00:00:00
6.34851521671069,192.130376084482,389.992328627658,2022-09-27 00:00:00,2022-09-28 00:00:00
8.24508983064327,202.533545027477,390.345172000417,2022-09-28 00:00:00,2022-09-29 00:00:00
6.76368360147995,212.299757197661,391.989085803109,2022-09-29 00:00:00,2022-09-30 00:00:00
9.10871262767975,199.755451298223,398.972222072231,2022-09-30 00:00:00,2022-10-01 00:00:00
7.7582582413418,194.301395501046,391.792833080551,2022-10-01 00:00:00,2022-10-02 00:00:00
8.12940656028438,196.704308757642,392.052627840932,2022-10-02 00:00:00,2022-10-03 00:00:00
6.52102579118526,185.436973633105,395.797276038057,2022-10-03 00:00:00,2022-10-04 00:00:00
9.87881993328141,202.952811935456,395.1212023417,2022-10-04 00:00:00,2022-10-05 00:00:00
6.2135087268549,199.898044281286,393.89547811125,2022-10-05 00:00:00,2022-10-06 00:00:00
5.07981940271682,193.362702623083,391.978187648988,2022-10-06 00:00:00,2022-10-07 00:00:00
7.67023150518067,211.543177923329,396.694931535236,2022-10-07 00:00:00,2022-10-08 00:00:00
7.80832979853782,194.567632110949,390.851636573745,2022-10-08 00:00:00,2022-10-09 00:00:00
4.96334387737906,176.363865072343,398.367605692982,2022-10-09 00:00:00,2022-10-10 00:00:00
9.19451095623433,201.989201467621,397.732493942879,2022-10-10 00:00:00,2022-10-11 00:00:00
4.24986840379062,192.122400198517,396.549993967159,2022-10-11 00:00:00,2022-10-12 00:00:00
7.74352609726317,209.8374955238,399.270028335499,2022-10-12 00:00:00,2022-10-13 00:00:00
10.9035712482694,198.688044950915,398.86461625213,2022-10-13 00:00:00,2022-10-14 00:00:00
8.63879474383298,201.369452146518,389.390695109621,2022-10-14 00:00:00,2022-10-15 00:00:00
6.65847376258746,192.536837932488,396.806189953665,2022-10-15 00:00:00,2022-10-16 00:00:00
8.67954780394319,196.416640736523,398.534598443111,2022-10-16 00:00:00,2022-10-17 00:00:00
4.09507794602362,203.549183294885,395.940654417433,2022-10-17 00:00:00,2022-10-18 00:00:00
8.97579673030268,193.75716451131,398.955275738124,2022-10-18 00:00:00,2022-10-19 00:00:00
11.6088771786759,188.061448544929,398.856149589119,2022-10-19 00:00:00,2022-10-20 00:00:00
6.03539364717974,193.724535703625,397.49600341666,2022-10-20 00:00:00,2022-10-21 00:00:00
4.31986049410596,207.268466539482,399.340673406538,2022-10-21 00:00:00,2022-10-22 00:00:00
22.9053626180102,197.886677586795,396.812861365527,2022-10-22 00:00:00,2022-10-23 00:00:00
4.20468190549354,200.624025945017,392.839138915141,2022-10-23 00:00:00,2022-10-24 00:00:00
27.7472830952505,203.937945859504,396.731604745749,2022-10-24 00:00:00,2022-10-25 00:00:00
41 changes: 41 additions & 0 deletions src/server/test/web/readingsLineMinMaxMeterQuantity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
This file tests the readings retrieval API for line chart quantity meters (with added min/max values).
See: https://github.com/OpenEnergyDashboard/DesignDocs/blob/main/testing/testing.md for information.
*/

const { chai, mocha, app } = require('../common');
const Unit = require('../../models/Unit');
const { prepareTest,
parseExpectedCsv,
expectMaxMinToEqualExpected,
createTimeString,
getUnitId,
//ETERNITY,
METER_ID,
unitDatakWh,
conversionDatakWh,
meterDatakWh } = require('../../util/readingsUtils');

mocha.describe('readings API', () => {
mocha.describe('readings test, test if data returned by API is as expected', () => {
mocha.describe('for line charts with min/max', () => {
mocha.describe('for quantity meters', () => {
// This date range is on the threshold of returning daily point readings, 61 days
mocha.it('LM3: should have daily points for middle readings of 15 minute for a 61 day period and quantity units with kWh as kWh', async () => {
// Load the data into the database (u1, u2, c1)
await prepareTest(unitDatakWh, conversionDatakWh, meterDatakWh);
// Get the unit ID since the DB could use any value.
const unitId = await getUnitId('kWh');
const expected = await parseExpectedCsv('src/server/test/web/readingsData/expected_line_minmax__ri_15_mu_kWh_gu_kWh_st_2022-08-25000000_et_2022-10-25000000.csv');
const res = await chai.request(app).get(`/api/unitReadings/line/meters/${METER_ID}`)
.query({ timeInterval: createTimeString('2022-08-25', '00:00:00', '2022-10-25', '00:00:00'), graphicUnitId: unitId });
expectMaxMinToEqualExpected(res, expected);
});
});
});
});
});
24 changes: 24 additions & 0 deletions src/server/util/readingsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,29 @@ function expectReadingToEqualExpected(res, expected, id = METER_ID) {
}
}

/**
* Compares readings from api call against the expected readings csv (now with min/max)
* @param {request.Response} res the response to the HTTP GET request from Chai
* @param {array} expected the returned array from parseExpectedCsv
*/
function expectMaxMinToEqualExpected(res, expected, id = METER_ID) {
expect(res).to.be.json;
expect(res).to.have.status(HTTP_CODE.OK);
// Did the response have the correct number of readings.
expect(res.body).to.have.property(`${id}`).to.have.lengthOf(expected.length);
// Loop over each reading
for (let i = 0; i < expected.length; i++) {
// Check that the reading's value is within the expected tolerance (DELTA).
expect(res.body).to.have.property(`${id}`).to.have.property(`${i}`).to.have.property('reading').to.be.closeTo(Number(expected[i][1]), DELTA);
// Reading also has correct max/min
expect(res.body).to.have.property(`${id}`).to.have.property(`${i}`).to.have.property('min').to.be.closeTo(Number(expected[i][0]), DELTA);
expect(res.body).to.have.property(`${id}`).to.have.property(`${i}`).to.have.property('max').to.be.closeTo(Number(expected[i][2]), DELTA);
// Reading has correct start/end date and time
expect(res.body).to.have.property(`${id}`).to.have.property(`${i}`).to.have.property('startTimestamp').to.equal(Date.parse(expected[i][3]));
expect(res.body).to.have.property(`${id}`).to.have.property(`${i}`).to.have.property('endTimestamp').to.equal(Date.parse(expected[i][4]));
}
}

/**
* Compares readings from api call against the expected readings csv
* @param {request.Response} res the response to the HTTP GET request from Chai
Expand Down Expand Up @@ -243,6 +266,7 @@ module.exports = {
prepareTest,
parseExpectedCsv,
expectReadingToEqualExpected,
expectMaxMinToEqualExpected,
expectThreeDReadingToEqualExpected,
createTimeString,
getUnitId,
Expand Down

0 comments on commit eeb4e0a

Please sign in to comment.