From 7d7a85e844a3383d620bec1ed214650167f0b9fa Mon Sep 17 00:00:00 2001 From: Steven Huss-Lederman Date: Mon, 10 Feb 2025 18:21:20 -0600 Subject: [PATCH] add range tests - Replace older maxmin test code with range. This only tests min/max and not the reading. - L23 was incorrectly in raw and is now in quantity. - Misc minor edits. --- ...t_2022-08-25000000_et_2022-10-25000000.csv | 62 --------------- ...nge_ri_15_mu_kWh_gu_kWh_st_-inf_et_inf.csv | 76 +++++++++++++++++++ .../test/web/readingsLineMeterQuantity.js | 55 +++++++++++++- .../test/web/readingsLineMeterRangeFlow.js | 32 ++++++++ .../web/readingsLineMeterRangeQuantity.js | 74 ++++++++++++++++++ .../test/web/readingsLineMeterRangeRaw.js | 44 +++++++++++ src/server/test/web/readingsLineMeterRaw.js | 51 ------------- .../web/readingsLineMinMaxMeterQuantity.js | 41 ---------- src/server/util/readingsUtils.js | 22 +++--- 9 files changed, 289 insertions(+), 168 deletions(-) delete mode 100644 src/server/test/web/readingsData/expected_line_minmax__ri_15_mu_kWh_gu_kWh_st_2022-08-25000000_et_2022-10-25000000.csv create mode 100644 src/server/test/web/readingsData/expected_line_range_ri_15_mu_kWh_gu_kWh_st_-inf_et_inf.csv create mode 100644 src/server/test/web/readingsLineMeterRangeFlow.js create mode 100644 src/server/test/web/readingsLineMeterRangeQuantity.js create mode 100644 src/server/test/web/readingsLineMeterRangeRaw.js delete mode 100644 src/server/test/web/readingsLineMinMaxMeterQuantity.js diff --git a/src/server/test/web/readingsData/expected_line_minmax__ri_15_mu_kWh_gu_kWh_st_2022-08-25000000_et_2022-10-25000000.csv b/src/server/test/web/readingsData/expected_line_minmax__ri_15_mu_kWh_gu_kWh_st_2022-08-25000000_et_2022-10-25000000.csv deleted file mode 100644 index f034cb9ef..000000000 --- a/src/server/test/web/readingsData/expected_line_minmax__ri_15_mu_kWh_gu_kWh_st_2022-08-25000000_et_2022-10-25000000.csv +++ /dev/null @@ -1,62 +0,0 @@ -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 diff --git a/src/server/test/web/readingsData/expected_line_range_ri_15_mu_kWh_gu_kWh_st_-inf_et_inf.csv b/src/server/test/web/readingsData/expected_line_range_ri_15_mu_kWh_gu_kWh_st_-inf_et_inf.csv new file mode 100644 index 000000000..a1be99da0 --- /dev/null +++ b/src/server/test/web/readingsData/expected_line_range_ri_15_mu_kWh_gu_kWh_st_-inf_et_inf.csv @@ -0,0 +1,76 @@ +min,max,start time,end time +6.7259272013388,396.756437434564,2022-08-18 00:00:00,2022-08-19 00:00:00 +4.47751407346722,395.768246876483,2022-08-19 00:00:00,2022-08-20 00:00:00 +4.04110145750053,395.564911597572,2022-08-20 00:00:00,2022-08-21 00:00:00 +8.46964265531555,399.401060519104,2022-08-21 00:00:00,2022-08-22 00:00:00 +5.536027248112,395.703751686654,2022-08-22 00:00:00,2022-08-23 00:00:00 +12.3370481373449,391.673072369915,2022-08-23 00:00:00,2022-08-24 00:00:00 +9.30301762676087,396.299057260549,2022-08-24 00:00:00,2022-08-25 00:00:00 +10.6426891004995,397.189849395253,2022-08-25 00:00:00,2022-08-26 00:00:00 +16.5997962068877,395.109107889164,2022-08-26 00:00:00,2022-08-27 00:00:00 +4.5823651802204,398.411679942704,2022-08-27 00:00:00,2022-08-28 00:00:00 +15.3642573360935,394.643152087121,2022-08-28 00:00:00,2022-08-29 00:00:00 +6.29056060065448,385.856740151065,2022-08-29 00:00:00,2022-08-30 00:00:00 +6.86420796677648,395.563823102245,2022-08-30 00:00:00,2022-08-31 00:00:00 +7.43438994317359,387.965878057152,2022-08-31 00:00:00,2022-09-01 00:00:00 +11.8212615738301,399.520437427381,2022-09-01 00:00:00,2022-09-02 00:00:00 +8.33718067987179,398.567045473078,2022-09-02 00:00:00,2022-09-03 00:00:00 +11.4111445542779,393.389074200625,2022-09-03 00:00:00,2022-09-04 00:00:00 +15.6820576786335,388.815491970447,2022-09-04 00:00:00,2022-09-05 00:00:00 +6.38333585089936,397.052005292176,2022-09-05 00:00:00,2022-09-06 00:00:00 +4.96432191677293,397.035480858967,2022-09-06 00:00:00,2022-09-07 00:00:00 +7.67557143008119,381.204626945531,2022-09-07 00:00:00,2022-09-08 00:00:00 +6.72960982731486,394.035396458706,2022-09-08 00:00:00,2022-09-09 00:00:00 +5.08026786370556,398.133028735512,2022-09-09 00:00:00,2022-09-10 00:00:00 +4.29407258042591,397.995914276686,2022-09-10 00:00:00,2022-09-11 00:00:00 +7.43874462146493,399.60078117885,2022-09-11 00:00:00,2022-09-12 00:00:00 +6.74162186785974,395.97596159838,2022-09-12 00:00:00,2022-09-13 00:00:00 +4.19776432857257,398.720053843593,2022-09-13 00:00:00,2022-09-14 00:00:00 +7.03930350560177,398.056285836813,2022-09-14 00:00:00,2022-09-15 00:00:00 +4.55696915516148,399.432853809808,2022-09-15 00:00:00,2022-09-16 00:00:00 +4.76122698910952,386.592709772111,2022-09-16 00:00:00,2022-09-17 00:00:00 +4.73481997409481,398.469854942283,2022-09-17 00:00:00,2022-09-18 00:00:00 +7.45500342512439,396.534786377705,2022-09-18 00:00:00,2022-09-19 00:00:00 +7.00053262408678,397.119985055946,2022-09-19 00:00:00,2022-09-20 00:00:00 +7.42313016152277,398.265853881631,2022-09-20 00:00:00,2022-09-21 00:00:00 +9.67235053788254,398.374776981315,2022-09-21 00:00:00,2022-09-22 00:00:00 +5.16339174456368,399.186850998811,2022-09-22 00:00:00,2022-09-23 00:00:00 +17.9386480149719,395.141115538206,2022-09-23 00:00:00,2022-09-24 00:00:00 +5.8614423320328,399.516915252127,2022-09-24 00:00:00,2022-09-25 00:00:00 +6.17122260696813,399.282645580328,2022-09-25 00:00:00,2022-09-26 00:00:00 +8.70731392274497,394.801202250496,2022-09-26 00:00:00,2022-09-27 00:00:00 +6.34851521671069,389.992328627658,2022-09-27 00:00:00,2022-09-28 00:00:00 +8.24508983064327,390.345172000417,2022-09-28 00:00:00,2022-09-29 00:00:00 +6.76368360147995,391.989085803109,2022-09-29 00:00:00,2022-09-30 00:00:00 +9.10871262767975,398.972222072231,2022-09-30 00:00:00,2022-10-01 00:00:00 +7.7582582413418,391.792833080551,2022-10-01 00:00:00,2022-10-02 00:00:00 +8.12940656028438,392.052627840932,2022-10-02 00:00:00,2022-10-03 00:00:00 +6.52102579118526,395.797276038057,2022-10-03 00:00:00,2022-10-04 00:00:00 +9.87881993328141,395.1212023417,2022-10-04 00:00:00,2022-10-05 00:00:00 +6.2135087268549,393.89547811125,2022-10-05 00:00:00,2022-10-06 00:00:00 +5.07981940271682,391.978187648988,2022-10-06 00:00:00,2022-10-07 00:00:00 +7.67023150518067,396.694931535236,2022-10-07 00:00:00,2022-10-08 00:00:00 +7.80832979853782,390.851636573745,2022-10-08 00:00:00,2022-10-09 00:00:00 +4.96334387737906,398.367605692982,2022-10-09 00:00:00,2022-10-10 00:00:00 +9.19451095623433,397.732493942879,2022-10-10 00:00:00,2022-10-11 00:00:00 +4.24986840379062,396.549993967159,2022-10-11 00:00:00,2022-10-12 00:00:00 +7.74352609726317,399.270028335499,2022-10-12 00:00:00,2022-10-13 00:00:00 +10.9035712482694,398.86461625213,2022-10-13 00:00:00,2022-10-14 00:00:00 +8.63879474383298,389.390695109621,2022-10-14 00:00:00,2022-10-15 00:00:00 +6.65847376258746,396.806189953665,2022-10-15 00:00:00,2022-10-16 00:00:00 +8.67954780394319,398.534598443111,2022-10-16 00:00:00,2022-10-17 00:00:00 +4.09507794602362,395.940654417433,2022-10-17 00:00:00,2022-10-18 00:00:00 +8.97579673030268,398.955275738124,2022-10-18 00:00:00,2022-10-19 00:00:00 +11.6088771786759,398.856149589119,2022-10-19 00:00:00,2022-10-20 00:00:00 +6.03539364717974,397.49600341666,2022-10-20 00:00:00,2022-10-21 00:00:00 +4.31986049410596,399.340673406538,2022-10-21 00:00:00,2022-10-22 00:00:00 +22.9053626180102,396.812861365527,2022-10-22 00:00:00,2022-10-23 00:00:00 +4.20468190549354,392.839138915141,2022-10-23 00:00:00,2022-10-24 00:00:00 +27.7472830952505,396.731604745749,2022-10-24 00:00:00,2022-10-25 00:00:00 +4.47778260991617,399.558616555955,2022-10-25 00:00:00,2022-10-26 00:00:00 +4.0462991348737,398.412310485797,2022-10-26 00:00:00,2022-10-27 00:00:00 +8.53981265314293,396.940512915872,2022-10-27 00:00:00,2022-10-28 00:00:00 +5.39809958171692,393.535108782051,2022-10-28 00:00:00,2022-10-29 00:00:00 +18.2196506846333,396.819564194905,2022-10-29 00:00:00,2022-10-30 00:00:00 +8.72795862355026,398.153898839684,2022-10-30 00:00:00,2022-10-31 00:00:00 +4.89617899878394,399.628936218779,2022-10-31 00:00:00,2022-11-01 00:00:00 diff --git a/src/server/test/web/readingsLineMeterQuantity.js b/src/server/test/web/readingsLineMeterQuantity.js index 88a6f966f..575c036aa 100644 --- a/src/server/test/web/readingsLineMeterQuantity.js +++ b/src/server/test/web/readingsLineMeterQuantity.js @@ -413,7 +413,7 @@ mocha.describe('readings API', () => { intercept: 0, note: 'CO₂ → kg' }, - + ]; const meterData = [ { @@ -651,7 +651,7 @@ mocha.describe('readings API', () => { id: METER_ID } ]; - + await prepareTest(unitData, conversionData, meterData); // Get the unit ID since the DB could use any value. const unitId = await getUnitId('pound of CO₂'); @@ -714,6 +714,57 @@ mocha.describe('readings API', () => { .query({ timeInterval: createTimeString('2022-08-25', '00:00:00', '2022-10-24', '00:00:00'), graphicUnitId: unitId }); expectReadingToEqualExpected(res, expected) }); + mocha.it('L23: should have raw points for middle readings of 15 minute for a 14 day period and quantity units & kWh as MJ', async () => { + const unitData = unitDatakWh.concat([ + { + // u3 + name: 'MJ', + identifier: 'megaJoules', + unitRepresent: Unit.unitRepresentType.QUANTITY, + secInRate: 3600, + typeOfUnit: Unit.unitType.UNIT, + suffix: '', + displayable: Unit.displayableType.ALL, + preferredDisplay: false, + note: 'MJ' + } + ]); + const conversionData = conversionDatakWh.concat([ + { + // c2 + sourceName: 'kWh', + destinationName: 'MJ', + bidirectional: true, + slope: 3.6, + intercept: 0, + note: 'kWh → MJ' + } + ]); + const meterData = [ + { + name: 'Electric_Utility MJ', + unit: 'Electric_Utility', + defaultGraphicUnit: 'MJ', + displayable: true, + gps: undefined, + note: 'special meter', + file: 'test/web/readingsData/readings_ri_15_days_75.csv', + deleteFile: false, + readingFrequency: '15 minutes', + id: METER_ID + } + ]; + + await prepareTest(unitData, conversionData, meterData); + // Get the unit ID since the DB could use any value. + const unitId = await getUnitId('MJ'); + // Reuse same file as flow since value should be the same values. + const expected = await parseExpectedCsv('src/server/test/web/readingsData/expected_line_ri_15_mu_kWh_gu_MJ_st_2022-09-21%00#00#00_et_2022-10-05%00#00#00.csv'); + + const res = await chai.request(app).get(`/api/unitReadings/line/meters/${METER_ID}`) + .query({ timeInterval: createTimeString('2022-09-21', '00:00:00', '2022-10-05', '00:00:00'), graphicUnitId: unitId }); + expectReadingToEqualExpected(res, expected) + }); }); }); }); diff --git a/src/server/test/web/readingsLineMeterRangeFlow.js b/src/server/test/web/readingsLineMeterRangeFlow.js new file mode 100644 index 000000000..f3a216300 --- /dev/null +++ b/src/server/test/web/readingsLineMeterRangeFlow.js @@ -0,0 +1,32 @@ +/* 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 line chart flow meters. + 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, + expectReadingToEqualExpected, + getUnitId, + ETERNITY, + METER_ID } = 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', () => { + mocha.describe('for flow meters', () => { + + // Add LR8 here + + // Add LR25 here + + // Add LR26 here + }); + }); + }); +}); diff --git a/src/server/test/web/readingsLineMeterRangeQuantity.js b/src/server/test/web/readingsLineMeterRangeQuantity.js new file mode 100644 index 000000000..a4776d820 --- /dev/null +++ b/src/server/test/web/readingsLineMeterRangeQuantity.js @@ -0,0 +1,74 @@ +/* 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. + 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, + expectRangeToEqualExpected, + 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', () => { + mocha.describe('for range (min/max)', () => { + mocha.describe('for quantity meters', () => { + // Test using a date range of infinity, which should return as days + mocha.it('LR1: range should have daily points for 15 minute reading intervals and quantity units with +-inf start/end time & kWh as kWh', async () => { + // Load the data into the database + await prepareTest(unitDatakWh, conversionDatakWh, meterDatakWh); + // Get the unit ID since the DB could use any value. + const unitId = await getUnitId('kWh'); + // Load the expected response data from the corresponding csv file + const expected = await parseExpectedCsv('src/server/test/web/readingsData/expected_line_range_ri_15_mu_kWh_gu_kWh_st_-inf_et_inf.csv'); + // Create a request to the API for unbounded reading times and save the response + const res = await chai.request(app).get(`/api/unitReadings/line/meters/${METER_ID}`) + .query({ timeInterval: ETERNITY.toString(), graphicUnitId: unitId }); + // Check that the API reading is equal to what it is expected to equal + expectRangeToEqualExpected(res, expected); + }); + + // Add LR2 here + + // Add LR3 here + + // Add LR4 here + + // Add LR5 here + + // Add LR6 here + + // Add LR7 here + + // Add LR10 here + + // Add LR11 here + + // Add LR12 here + + // Add LR13 here + + // Add LR18 here + + // Add LR19 here + + // Add LR20 here + + // Add LR21 here + }); + }); + }); + }); +}); diff --git a/src/server/test/web/readingsLineMeterRangeRaw.js b/src/server/test/web/readingsLineMeterRangeRaw.js new file mode 100644 index 000000000..b26df7792 --- /dev/null +++ b/src/server/test/web/readingsLineMeterRangeRaw.js @@ -0,0 +1,44 @@ +/* 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 raw meters. + 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, + expectReadingToEqualExpected, + 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', () => { + mocha.describe('for range (min/max)', () => { + mocha.describe('for raw meters', () => { + + // Add LR9 here + + // Add LR14 here + + // Add LR15 here + + // Add LR16 here + + // Add LR17 here + + // Add LR22 here + }); + }); + }); + }); +}); diff --git a/src/server/test/web/readingsLineMeterRaw.js b/src/server/test/web/readingsLineMeterRaw.js index 503eca22b..af0d0baab 100644 --- a/src/server/test/web/readingsLineMeterRaw.js +++ b/src/server/test/web/readingsLineMeterRaw.js @@ -550,57 +550,6 @@ mocha.describe('readings API', () => { .query({ timeInterval: createTimeString('2022-08-25', '00:00:00', '2022-10-24', '00:00:00'), graphicUnitId: unitId }); expectReadingToEqualExpected(res, expected) }); - mocha.it('L23: should have raw points for middle readings of 15 minute for a 14 day period and quantity units & kWh as MJ', async () => { - const unitData = unitDatakWh.concat([ - { - // u3 - name: 'MJ', - identifier: 'megaJoules', - unitRepresent: Unit.unitRepresentType.QUANTITY, - secInRate: 3600, - typeOfUnit: Unit.unitType.UNIT, - suffix: '', - displayable: Unit.displayableType.ALL, - preferredDisplay: false, - note: 'MJ' - } - ]); - const conversionData = conversionDatakWh.concat([ - { - // c2 - sourceName: 'kWh', - destinationName: 'MJ', - bidirectional: true, - slope: 3.6, - intercept: 0, - note: 'kWh → MJ' - } - ]); - const meterData = [ - { - name: 'Electric_Utility MJ', - unit: 'Electric_Utility', - defaultGraphicUnit: 'MJ', - displayable: true, - gps: undefined, - note: 'special meter', - file: 'test/web/readingsData/readings_ri_15_days_75.csv', - deleteFile: false, - readingFrequency: '15 minutes', - id: METER_ID - } - ]; - - await prepareTest(unitData, conversionData, meterData); - // Get the unit ID since the DB could use any value. - const unitId = await getUnitId('MJ'); - // Reuse same file as flow since value should be the same values. - const expected = await parseExpectedCsv('src/server/test/web/readingsData/expected_line_ri_15_mu_kWh_gu_MJ_st_2022-09-21%00#00#00_et_2022-10-05%00#00#00.csv'); - - const res = await chai.request(app).get(`/api/unitReadings/line/meters/${METER_ID}`) - .query({ timeInterval: createTimeString('2022-09-21', '00:00:00', '2022-10-05', '00:00:00'), graphicUnitId: unitId }); - expectReadingToEqualExpected(res, expected) - }); mocha.it('L24: should have raw points for middle readings of 15 minute for a 14 day period and raw units & C as F with intercept', async () => { const unitData = [ { diff --git a/src/server/test/web/readingsLineMinMaxMeterQuantity.js b/src/server/test/web/readingsLineMinMaxMeterQuantity.js deleted file mode 100644 index 171fea6da..000000000 --- a/src/server/test/web/readingsLineMinMaxMeterQuantity.js +++ /dev/null @@ -1,41 +0,0 @@ -/* 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); - }); - }); - }); - }); -}); diff --git a/src/server/util/readingsUtils.js b/src/server/util/readingsUtils.js index e1fb387e9..45917e40a 100644 --- a/src/server/util/readingsUtils.js +++ b/src/server/util/readingsUtils.js @@ -76,25 +76,23 @@ function expectReadingToEqualExpected(res, expected, id = METER_ID) { } /** - * Compares readings from api call against the expected readings csv (now with min/max) + * Compares readings from api call against the expected readings csv * @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) { +function expectRangeToEqualExpected(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])); + // Check that the reading's min/max is within the expected tolerance (DELTA). + 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][1]), 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][2])); + expect(res.body).to.have.property(`${id}`).to.have.property(`${i}`).to.have.property('endTimestamp').to.equal(Date.parse(expected[i][3])); } } @@ -281,7 +279,7 @@ module.exports = { prepareTest, parseExpectedCsv, expectReadingToEqualExpected, - expectMaxMinToEqualExpected, + expectRangeToEqualExpected, expectCompareToEqualExpected, expectThreeDReadingToEqualExpected, createTimeString, @@ -296,4 +294,4 @@ module.exports = { meterDatakWh, meterDatakWhGroups, groupDatakWh -}; \ No newline at end of file +};