diff --git a/src/server/models/Meter.js b/src/server/models/Meter.js index 739283e102..6475938be9 100644 --- a/src/server/models/Meter.js +++ b/src/server/models/Meter.js @@ -9,7 +9,7 @@ const Unit = require('./Unit'); const { log } = require('../log'); const sqlFile = database.sqlFile; -class Meter { +class Meter { /** * @param id This meter's ID. Should be undefined if the meter is being newly created * @param name This meter's name diff --git a/src/server/test/db/meterTests.js b/src/server/test/db/meterTests.js deleted file mode 100644 index e57aff6a09..0000000000 --- a/src/server/test/db/meterTests.js +++ /dev/null @@ -1,182 +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/. */ - -/** - * Tests if the expected properties are the actual results from meters. - */ - -const { mocha, expect, testDB } = require('../common'); -const Meter = require('../../models/Meter'); -const Point = require('../../models/Point'); -const moment = require('moment-timezone'); -const Unit = require('../../models/Unit'); -const gps = new Point(90, 45); - -// TODO add 2 new unit parameters for meters. - -/** - * Checks if the expected meter properties have the actual properties. - * @param expected expected meter properties - * @param actual actual meter properties - */ -function expectMetersToBeEquivalent(expected, actual) { - expect(actual).to.have.property('id', expected.id); - expect(actual).to.have.property('name', expected.name); - expect(actual).to.have.property('enabled', expected.enabled); - expect(actual).to.have.property('type', expected.type); - expect(actual).to.have.property('gps'); - expect(actual.gps).to.have.property('latitude', expected.gps.latitude); - expect(actual.gps).to.have.property('longitude', expected.gps.longitude); - expect(actual).to.have.property('meterTimezone', expected.meterTimezone); - expect(actual).to.have.property('identifier', expected.identifier); - expect(actual).to.have.property('note', expected.note); - expect(actual).to.have.property('area', expected.area); - expect(actual).to.have.property('cumulative', expected.cumulative); - expect(actual).to.have.property('cumulativeReset', expected.cumulativeReset); - expect(actual).to.have.property('cumulativeResetStart', expected.cumulativeResetStart); - expect(actual).to.have.property('cumulativeResetEnd', expected.cumulativeResetEnd); - expect(actual).to.have.property('readingGap', expected.readingGap); - expect(actual).to.have.property('readingVariation', expected.readingVariation); - expect(actual).to.have.property('readingDuplication', expected.readingDuplication); - expect(actual).to.have.property('timeSort', expected.timeSort); - expect(actual).to.have.property('endOnlyTime', expected.endOnlyTime); - expect(actual).to.have.property('reading', expected.reading); - expect(actual).to.have.property('startTimestamp', expected.startTimestamp); - expect(actual).to.have.property('endTimestamp', expected.endTimestamp); - // TODO save until sure not needed. - // expect(actual.startTimestamp.isSame(moment.utc(expected.startTimestamp))).to.equal(true); - // expect(actual.endTimestamp.isSame(moment.utc(expected.endTimestamp))).to.equal(true); - // Need to work in UTC time since that is what the database returns and comparing - // to database values. Done in all moment objects in this test. - // This gets the date/time to be the same but put into UTC. - expect(actual.previousEnd.isSame(moment.parseZone(expected.previousEnd, true).tz('UTC', true))).to.equal(true); - expect(actual).to.have.property('unitId', expected.unitId); - expect(actual).to.have.property('defaultGraphicUnit', expected.defaultGraphicUnit); - expect(actual).to.have.property('areaUnit', expected.areaUnit); - expect(actual).to.have.property('readingFrequency'); - expect(actual.readingFrequency.asMilliseconds()).to.equal(expected.readingFrequency.asMilliseconds()); -} - -mocha.describe('Meters', () => { - let unitA, unitB; - mocha.beforeEach(async () => { - unitA = new Unit(undefined, 'Unit A', 'Unit A Id', Unit.unitRepresentType.QUANTITY, 1000, - Unit.unitType.UNIT, 'Unit A Suffix', Unit.displayableType.ALL, true, 'Unit A Note'); - unitB = new Unit(undefined, 'Unit B', 'Unit B Id', Unit.unitRepresentType.QUANTITY, 2000, - Unit.unitType.UNIT, 'Unit B Suffix', Unit.displayableType.ALL, true, 'Unit B Note'); - const unitC = new Unit(undefined, 'Unit C', 'Unit C Id', Unit.unitRepresentType.QUANTITY, 3000, - Unit.unitType.UNIT, 'Unit C Suffix', Unit.displayableType.ALL, true, 'Unit C Note'); - await Promise.all([unitA, unitB, unitC].map(unit => unit.insert(conn))); - }); - - mocha.it('can be saved and retrieved', async () => { - const conn = testDB.getConnection(); - const meterPreInsert = new Meter(undefined, 'Meter', null, false, true, Meter.type.MAMAC, 'UTC', - gps, 'Identified', 'notes', 33.5, true, true, '05:05:09', '09:00:01', 0, 0, 1, 'increasing', false, - 25.5, '0001-01-01 23:59:59-05:00', '2020-07-02 01:00:10-06:00', '2020-03-05 02:12:00-06:00', unitA.id, - unitA.id, Unit.areaUnitType.METERS, '12:34:56'); - await meterPreInsert.insert(conn); - const meterPostInsertByName = await Meter.getByName(meterPreInsert.name, conn); - expectMetersToBeEquivalent(meterPreInsert, meterPostInsertByName); - const meterPostInsertByID = await Meter.getByID(meterPreInsert.id, conn); - expectMetersToBeEquivalent(meterPreInsert, meterPostInsertByID); - }); - - mocha.it('can be saved and retrieved with no graphic units', async () => { - const conn = testDB.getConnection(); - const meterPreInsert = new Meter(undefined, 'Meter', null, false, true, Meter.type.MAMAC, 'UTC', - gps, 'Identified', 'notes', 33.5, true, true, '05:05:09', '09:00:01', 0, 0, 1, 'increasing', false, - 25.5, '0001-01-01 23:59:59', '2020-07-02 01:00:10', '2020-03-05 02:12:00', -99, -99, - Unit.areaUnitType.FEET, undefined); - await meterPreInsert.insert(conn); - const meterPostInsertByName = await Meter.getByName(meterPreInsert.name, conn); - expectMetersToBeEquivalent(meterPreInsert, meterPostInsertByName); - const meterPostInsertByID = await Meter.getByID(meterPreInsert.id, conn); - expectMetersToBeEquivalent(meterPreInsert, meterPostInsertByID); - }); - - mocha.it('can be saved, edited, and retrieved', async () => { - const conn = testDB.getConnection(); - const meterPreInsert = new Meter(undefined, 'Meter', null, false, true, Meter.type.MAMAC, 'UTC', gps, - 'Identified', 'notes', 35.0, true, true, '01:01:25', '00:00:00', 5, 0, 1, 'increasing', false, - 1.5, '0001-01-01 23:59:59+00:00', '2020-07-02 01:00:10+00:00', '2020-03-05 02:12:00+00:00', unitA.id, unitA.id, - Unit.areaUnitType.METERS, '1 day 3 hours 12 minutes'); - await meterPreInsert.insert(conn); - const meterPostInsertByID = await Meter.getByID(meterPreInsert.id, conn); - expectMetersToBeEquivalent(meterPreInsert, meterPostInsertByID); - - meterPreInsert.name = 'Something Else'; - meterPreInsert.enabled = true; - meterPreInsert.meterTimezone = 'GMT'; - meterPreInsert.unitId = 3; - await meterPreInsert.update(conn); - const meterPostUpdate = await Meter.getByID(meterPreInsert.id, conn); - expectMetersToBeEquivalent(meterPreInsert, meterPostUpdate); - }); - - mocha.it('can get only enabled meters', async () => { - const conn = testDB.getConnection(); - // Don't set timestamp values to see if defaults work. - const enabledMeter = new Meter(undefined, 'EnabledMeter', null, true, true, Meter.type.MAMAC, null, gps, - 'Identified', 'notes', 35.0, true, true, '01:01:25', '00:00:00', 7, 11, 1, 'increasing', false, - 1.5, '0001-01-01 23:59:59+00:00', '2020-07-02 01:00:10+00:00', '2020-03-05 02:12:00+00:00', unitA.id, unitA.id, - Unit.areaUnitType.METERS, undefined); - const disabledMeter = new Meter(undefined, 'DisabledMeter', null, false, true, Meter.type.MAMAC, null, gps, - 'Identified 1', 'Notes 1', 35.0, true, true, '01:01:25', '00:00:00', 5, 0, 1, 'increasing', false, - 1.5, '0002-01-01 23:59:59+00:00', '2020-07-02 01:00:10+00:00', '2020-03-05 02:12:00+00:00', unitA.id, unitA.id, - Unit.areaUnitType.METERS, undefined); - await enabledMeter.insert(conn); - await disabledMeter.insert(conn); - // set default timestamps for testing. - disabledMeter.startTimestamp = '1970-01-01 00:00:00+00:00'; - disabledMeter.endTimestamp = '1970-01-01 00:00:00+00:00'; - disabledMeter.previousEnd = '1970-01-01 00:00:00+00:00'; - - const enabledMeters = await Meter.getEnabled(conn); - expect(enabledMeters).to.have.lengthOf(1); - expectMetersToBeEquivalent(enabledMeter, enabledMeters[0]); - }); - - mocha.it('can get only visible meters', async () => { - const conn = testDB.getConnection(); - const visibleMeter = new Meter(undefined, 'VisibleMeter', null, true, true, Meter.type.MAMAC, null, gps, - 'Identified 1', 'notes 1', 35.0, true, true, '01:01:25', '00:00:00', 5, 0, 1, 'increasing', false, - 1.5, '0001-01-01 23:59:59', '2020-07-02 01:00:10', '2020-03-05 02:12:00', unitA.id, unitA.id, - Unit.areaUnitType.METERS, undefined); - const invisibleMeter = new Meter(undefined, 'InvisibleMeter', null, true, false, Meter.type.MAMAC, null, gps, - 'Identified 2', 'Notes 2', 35.0, true, true, '01:01:25', '00:00:00', 5, 0, 1, 'increasing', false, - 1.5, '0002-01-01 23:59:59', '2020-07-02 01:00:10', '2020-03-05 02:12:00', unitA.id, unitA.id, - Unit.areaUnitType.METERS, undefined); - - await visibleMeter.insert(conn); - await invisibleMeter.insert(conn); - - const visibleMeters = await Meter.getDisplayable(conn); - expect(visibleMeters).to.have.lengthOf(1); - expectMetersToBeEquivalent(visibleMeter, visibleMeters[0]); - }); - - mocha.it('can get all meter where unitId is not null', async () => { - const meterA = new Meter(undefined, 'MeterA', null, true, true, Meter.type.MAMAC, null, gps, - 'MeterA', 'notes 1', 35.0, true, true, '01:01:25', '00:00:00', 5, 0, 1, 'increasing', false, - 1.5, '0001-01-01 23:59:59', '2020-07-02 01:00:10', '2020-03-05 02:12:00', unitA.id, unitA.id, - Unit.areaUnitType.METERS, undefined); - const meterB = new Meter(undefined, 'MeterB', null, true, true, Meter.type.MAMAC, null, gps, - 'MeterB', 'notes 2', 35.0, true, true, '01:01:25', '00:00:00', 5, 0, 1, 'increasing', false, - 1.5, '0001-01-01 23:59:59', '2020-07-02 01:00:10', '2020-03-05 02:12:00', unitB.id, unitB.id, - Unit.areaUnitType.METERS, undefined); - const meterC = new Meter(undefined, 'Meter C', null, true, true, Meter.type.MAMAC, null); - - await Promise.all([meterA, meterB, meterC].map(meter => meter.insert(conn))); - const expectedMeters = [meterA, meterB]; - const actualMeters = await Meter.getUnitNotNull(conn); - actualMeters.sort((a, b) => a.id - b.id); - expectedMeters.sort((a, b) => a.id - b.id); - - expect(expectedMeters.length).to.be.equal(actualMeters.length); - for (let i = 0; i < expectedMeters.length; ++i) { - expectMetersToBeEquivalent(expectedMeters[i], actualMeters[i]); - } - }); -}); diff --git a/src/server/test/web/csvPipelineTest.js b/src/server/test/web/csvPipelineTest.js index b31c4a5d30..f0077a8301 100644 --- a/src/server/test/web/csvPipelineTest.js +++ b/src/server/test/web/csvPipelineTest.js @@ -7,8 +7,9 @@ const Meter = require('../../models/Meter'); const Reading = require('../../models/Reading'); const Point = require('../../models/Point'); const Unit = require('../../models/Unit'); -const { insertStandardUnits, insertStandardConversions, insertUnits, insertConversions } = require('../../util/insertData') +const { insertStandardUnits, insertStandardConversions, insertUnits, insertConversions } = require('../../util/insertData'); const { redoCik } = require('../../services/graph/redoCik'); +const { expectMetersToBeEquivalent } = require('./meters'); const util = require('util'); const fs = require('fs'); const csv = require('csv'); @@ -896,50 +897,8 @@ for (let fileKey in testMeters) { // Get the database value for the meter. let meter = await Meter.getByName(expectMeter.name, conn); // Verify they are the same. - compareMeters(expectMeter, meter); + expectMetersToBeEquivalent(expectMeter, meter); } }); }); } - -// TODO It would be nice to make this use the code in src/server/test/db/meterTests.js and make -// all meter tests use one common function fo meter comparison. -/** - * Compares the two provided meters to make sure they are the same. - * @param {*} expectMeter A meter object that gives the values expected for the meter. - * @param {*} receivedMeter A meter object that has the actual values (normally from database). - */ -function compareMeters(expectMeter, receivedMeter) { - // Make sure it has all the expected properties with the values expected. - expect(receivedMeter).to.have.property('name', expectMeter.name); - expect(receivedMeter).to.have.property('url', expectMeter.url); - expect(receivedMeter).to.have.property('enabled', expectMeter.enabled); - expect(receivedMeter).to.have.property('displayable', expectMeter.displayable); - expect(receivedMeter).to.have.property('type', expectMeter.type); - expect(receivedMeter).to.have.property('meterTimezone', expectMeter.meterTimezone); - expect(receivedMeter).to.have.property('gps'); - // Only check GPS value if it exists. - if (receivedMeter.gps !== null) { - expect(receivedMeter.gps).to.have.property('latitude', expectMeter.gps.latitude); - expect(receivedMeter.gps).to.have.property('longitude', expectMeter.gps.longitude); - } - expect(receivedMeter).to.have.property('identifier', expectMeter.identifier); - expect(receivedMeter).to.have.property('note', expectMeter.note); - expect(receivedMeter).to.have.property('area', expectMeter.area); - expect(receivedMeter).to.have.property('cumulative', expectMeter.cumulative); - expect(receivedMeter).to.have.property('cumulativeReset', expectMeter.cumulativeReset); - expect(receivedMeter).to.have.property('cumulativeResetStart', expectMeter.cumulativeResetStart); - expect(receivedMeter).to.have.property('cumulativeResetEnd', expectMeter.cumulativeResetEnd); - expect(receivedMeter).to.have.property('readingGap', expectMeter.readingGap); - expect(receivedMeter).to.have.property('readingVariation', expectMeter.readingVariation); - expect(receivedMeter).to.have.property('readingDuplication', expectMeter.readingDuplication); - expect(receivedMeter).to.have.property('timeSort', expectMeter.timeSort); - expect(receivedMeter).to.have.property('endOnlyTime', expectMeter.endOnlyTime); - expect(receivedMeter).to.have.property('reading', expectMeter.reading); - expect(receivedMeter).to.have.property('startTimestamp', expectMeter.startTimestamp); - expect(receivedMeter).to.have.property('endTimestamp', expectMeter.endTimestamp); - expect(receivedMeter.previousEnd.isSame(moment.parseZone(expectMeter.previousEnd, true).tz('UTC', true))).to.equal(true); - expect(receivedMeter).to.have.property('unitId', expectMeter.unitId); - expect(receivedMeter).to.have.property('defaultGraphicUnit', expectMeter.defaultGraphicUnit); - expect(receivedMeter).to.have.property('areaUnit', expectMeter.areaUnit); -} diff --git a/src/server/test/web/meters.js b/src/server/test/web/meters.js index ea3f137ad7..bb801ec7e1 100644 --- a/src/server/test/web/meters.js +++ b/src/server/test/web/meters.js @@ -14,9 +14,6 @@ const moment = require('moment-timezone'); const gps = new Point(90, 45); const Unit = require('../../models/Unit'); -// TODO These tests are not as good as they should be now that information on -// meters is returned to all users. They should be updated. - /** * Verifies the values in the meter are the ones expected. * @param {*} meters If # meters > 1 then array of meters, else single meter @@ -71,6 +68,8 @@ function expectMetersToBeEquivalent(meters, length, isAdmin, unit) { expect(meter).to.have.property('startTimestamp', '0001-01-01 23:59:59'); expect(meter).to.have.property('endTimestamp', '2020-07-02 01:00:10'); expect(meter).to.have.property('previousEnd', '2020-03-05T13:15:13.000Z'); + expect(meter).to.have.property('areaUnit', Unit.areaUnitType.METERS); + expect(meter).to.have.property('readingFrequency', 'PT13H57M19S'); } else { expect(meter).to.have.property('name', null); expect(meter).to.have.property('url', null); @@ -90,6 +89,9 @@ function expectMetersToBeEquivalent(meters, length, isAdmin, unit) { expect(meter).to.have.property('startTimestamp', null); expect(meter).to.have.property('endTimestamp', null); expect(meter).to.have.property('previousEnd', null); + expect(meter).to.have.property('areaUnit', Unit.areaUnitType.METERS); + expect(meter).to.have.property('readingFrequency', null); + } } } @@ -261,3 +263,215 @@ mocha.describe('meters API', () => { expect(res).to.have.status(500); }); }); + +mocha.describe('Meter model', () => { + let unitId; + mocha.beforeEach(async () => { + conn = testDB.getConnection(); + const unit = new Unit(undefined, 'Unit', 'Unit', Unit.unitRepresentType.QUANTITY, 1000, Unit.unitType.UNIT, + 'Unit Suffix', Unit.displayableType.ALL, true, 'Unit Note'); + await unit.insert(conn); + unitId = unit.id; + }); + + mocha.it('returns -99 when convertUnitValue is passed with null', async () => { + const unit = Meter.convertUnitValue(null); + expect(unit).to.equal(-99); + }); + + mocha.it('returns -99 when unitID & defaultGraphicUnit is -99 and inserted into DB', async () => { + const conn = testDB.getConnection(); + const meterPreInsert = new Meter(undefined, 'Meter 1', '1.1.1.1', true, true, Meter.type.MAMAC, '+01', gps, + 'Identified 1', 'notes 1', 10.0, true, true, '01:01:25', '05:05:05', 5.1, 7.3, 1, 'increasing', false, + 1.0, '0001-01-01 23:59:59', '2020-07-02 01:00:10', '2020-03-05 13:15:13', -99, -99, + Unit.areaUnitType.METERS, '13:57:19'); + await meterPreInsert.insert(conn); + + const meterPostInsert = await Meter.getByID(meterPreInsert.id, conn); + expect(meterPostInsert.unitId).to.equal(-99); + expect(meterPostInsert.defaultGraphicUnit).to.equal(-99); + }); + + mocha.it('returns -99 when unitID & defaultGraphicUnit is updated to -99', async() => { + const conn = testDB.getConnection(); + const meterPreInsert = new Meter(undefined, 'Meter 1', '1.1.1.1', true, true, Meter.type.MAMAC, '+01', gps, + 'Identified 1', 'notes 1', 10.0, true, true, '01:01:25', '05:05:05', 5.1, 7.3, 1, 'increasing', false, + 1.0, '0001-01-01 23:59:59', '2020-07-02 01:00:10', '2020-03-05 13:15:13', unitId, unitId, + Unit.areaUnitType.METERS, '13:57:19'); + await meterPreInsert.insert(conn); + + meterPreInsert.unitId = -99; + meterPreInsert.defaultGraphicUnit = -99; + await meterPreInsert.update(conn); + + const meterPostUpdate = await Meter.getByID(meterPreInsert.id, conn); + expect(meterPostUpdate.unitId).to.equal(-99); + expect(meterPostUpdate.defaultGraphicUnit).to.equal(-99); + }); +}); + +mocha.describe('Meters', () => { + let unitA, unitB; + mocha.beforeEach(async () => { + unitA = new Unit(undefined, 'Unit A', 'Unit A Id', Unit.unitRepresentType.QUANTITY, 1000, + Unit.unitType.UNIT, 'Unit A Suffix', Unit.displayableType.ALL, true, 'Unit A Note'); + unitB = new Unit(undefined, 'Unit B', 'Unit B Id', Unit.unitRepresentType.QUANTITY, 2000, + Unit.unitType.UNIT, 'Unit B Suffix', Unit.displayableType.ALL, true, 'Unit B Note'); + const unitC = new Unit(undefined, 'Unit C', 'Unit C Id', Unit.unitRepresentType.QUANTITY, 3000, + Unit.unitType.UNIT, 'Unit C Suffix', Unit.displayableType.ALL, true, 'Unit C Note'); + await Promise.all([unitA, unitB, unitC].map(unit => unit.insert(conn))); + }); + + mocha.it('can be saved and retrieved', async () => { + const conn = testDB.getConnection(); + const meterPreInsert = new Meter(undefined, 'Meter', null, false, true, Meter.type.MAMAC, 'UTC', + gps, 'Identified', 'notes', 33.5, true, true, '05:05:09', '09:00:01', 0, 0, 1, 'increasing', false, + 25.5, '0001-01-01 23:59:59-05:00', '2020-07-02 01:00:10-06:00', '2020-03-05 02:12:00-06:00', unitA.id, + unitA.id, Unit.areaUnitType.METERS, '12:34:56'); + await meterPreInsert.insert(conn); + const meterPostInsertByName = await Meter.getByName(meterPreInsert.name, conn); + expectMetersToBeEquivalent(meterPreInsert, meterPostInsertByName); + const meterPostInsertByID = await Meter.getByID(meterPreInsert.id, conn); + expectMetersToBeEquivalent(meterPreInsert, meterPostInsertByID); + }); + + mocha.it('can be saved and retrieved with no graphic units', async () => { + const conn = testDB.getConnection(); + const meterPreInsert = new Meter(undefined, 'Meter', null, false, true, Meter.type.MAMAC, 'UTC', + gps, 'Identified', 'notes', 33.5, true, true, '05:05:09', '09:00:01', 0, 0, 1, 'increasing', false, + 25.5, '0001-01-01 23:59:59', '2020-07-02 01:00:10', '2020-03-05 02:12:00', -99, -99, + Unit.areaUnitType.FEET, undefined); + await meterPreInsert.insert(conn); + const meterPostInsertByName = await Meter.getByName(meterPreInsert.name, conn); + expectMetersToBeEquivalent(meterPreInsert, meterPostInsertByName); + const meterPostInsertByID = await Meter.getByID(meterPreInsert.id, conn); + expectMetersToBeEquivalent(meterPreInsert, meterPostInsertByID); + }); + + mocha.it('can be saved, edited, and retrieved', async () => { + const conn = testDB.getConnection(); + const meterPreInsert = new Meter(undefined, 'Meter', null, false, true, Meter.type.MAMAC, 'UTC', gps, + 'Identified', 'notes', 35.0, true, true, '01:01:25', '00:00:00', 5, 0, 1, 'increasing', false, + 1.5, '0001-01-01 23:59:59+00:00', '2020-07-02 01:00:10+00:00', '2020-03-05 02:12:00+00:00', unitA.id, unitA.id, + Unit.areaUnitType.METERS, '1 day 3 hours 12 minutes'); + await meterPreInsert.insert(conn); + const meterPostInsertByID = await Meter.getByID(meterPreInsert.id, conn); + expectMetersToBeEquivalent(meterPreInsert, meterPostInsertByID); + + meterPreInsert.name = 'Something Else'; + meterPreInsert.enabled = true; + meterPreInsert.meterTimezone = 'GMT'; + meterPreInsert.unitId = 3; + await meterPreInsert.update(conn); + const meterPostUpdate = await Meter.getByID(meterPreInsert.id, conn); + expectMetersToBeEquivalent(meterPreInsert, meterPostUpdate); + }); + + mocha.it('can get only enabled meters', async () => { + const conn = testDB.getConnection(); + // Don't set timestamp values to see if defaults work. + const enabledMeter = new Meter(undefined, 'EnabledMeter', null, true, true, Meter.type.MAMAC, null, gps, + 'Identified', 'notes', 35.0, true, true, '01:01:25', '00:00:00', 7, 11, 1, 'increasing', false, + 1.5, '0001-01-01 23:59:59+00:00', '2020-07-02 01:00:10+00:00', '2020-03-05 02:12:00+00:00', unitA.id, unitA.id, + Unit.areaUnitType.METERS, undefined); + const disabledMeter = new Meter(undefined, 'DisabledMeter', null, false, true, Meter.type.MAMAC, null, gps, + 'Identified 1', 'Notes 1', 35.0, true, true, '01:01:25', '00:00:00', 5, 0, 1, 'increasing', false, + 1.5, '0002-01-01 23:59:59+00:00', '2020-07-02 01:00:10+00:00', '2020-03-05 02:12:00+00:00', unitA.id, unitA.id, + Unit.areaUnitType.METERS, undefined); + await enabledMeter.insert(conn); + await disabledMeter.insert(conn); + // set default timestamps for testing. + disabledMeter.startTimestamp = '1970-01-01 00:00:00+00:00'; + disabledMeter.endTimestamp = '1970-01-01 00:00:00+00:00'; + disabledMeter.previousEnd = '1970-01-01 00:00:00+00:00'; + + const enabledMeters = await Meter.getEnabled(conn); + expect(enabledMeters).to.have.lengthOf(1); + expectMetersToBeEquivalent(enabledMeter, enabledMeters[0]); + }); + + mocha.it('can get only visible meters', async () => { + const conn = testDB.getConnection(); + const visibleMeter = new Meter(undefined, 'VisibleMeter', null, true, true, Meter.type.MAMAC, null, gps, + 'Identified 1', 'notes 1', 35.0, true, true, '01:01:25', '00:00:00', 5, 0, 1, 'increasing', false, + 1.5, '0001-01-01 23:59:59', '2020-07-02 01:00:10', '2020-03-05 02:12:00', unitA.id, unitA.id, + Unit.areaUnitType.METERS, undefined); + const invisibleMeter = new Meter(undefined, 'InvisibleMeter', null, true, false, Meter.type.MAMAC, null, gps, + 'Identified 2', 'Notes 2', 35.0, true, true, '01:01:25', '00:00:00', 5, 0, 1, 'increasing', false, + 1.5, '0002-01-01 23:59:59', '2020-07-02 01:00:10', '2020-03-05 02:12:00', unitA.id, unitA.id, + Unit.areaUnitType.METERS, undefined); + + await visibleMeter.insert(conn); + await invisibleMeter.insert(conn); + + const visibleMeters = await Meter.getDisplayable(conn); + expect(visibleMeters).to.have.lengthOf(1); + expectMetersToBeEquivalent(visibleMeter, visibleMeters[0]); + }); + + mocha.it('can get all meter where unitId is not null', async () => { + const meterA = new Meter(undefined, 'MeterA', null, true, true, Meter.type.MAMAC, null, gps, + 'MeterA', 'notes 1', 35.0, true, true, '01:01:25', '00:00:00', 5, 0, 1, 'increasing', false, + 1.5, '0001-01-01 23:59:59', '2020-07-02 01:00:10', '2020-03-05 02:12:00', unitA.id, unitA.id, + Unit.areaUnitType.METERS, undefined); + const meterB = new Meter(undefined, 'MeterB', null, true, true, Meter.type.MAMAC, null, gps, + 'MeterB', 'notes 2', 35.0, true, true, '01:01:25', '00:00:00', 5, 0, 1, 'increasing', false, + 1.5, '0001-01-01 23:59:59', '2020-07-02 01:00:10', '2020-03-05 02:12:00', unitB.id, unitB.id, + Unit.areaUnitType.METERS, undefined); + const meterC = new Meter(undefined, 'Meter C', null, true, true, Meter.type.MAMAC, null); + + await Promise.all([meterA, meterB, meterC].map(meter => meter.insert(conn))); + const expectedMeters = [meterA, meterB]; + const actualMeters = await Meter.getUnitNotNull(conn); + actualMeters.sort((a, b) => a.id - b.id); + expectedMeters.sort((a, b) => a.id - b.id); + + expect(expectedMeters.length).to.be.equal(actualMeters.length); + for (let i = 0; i < expectedMeters.length; ++i) { + expectMetersToBeEquivalent(expectedMeters[i], actualMeters[i]); + } + }); + + mocha.it('can get meter by identifier', async () => { + const conn = testDB.getConnection(); + const meterA = new Meter(undefined, 'MeterA', null, true, true, Meter.type.MAMAC, null, gps, + 'MeterA', 'notes 1', 35.0, true, true, '01:01:25', '00:00:00', 5, 0, 1, 'increasing', false, + 1.5, '0001-01-01 23:59:59', '2020-07-02 01:00:10', '2020-03-05 02:12:00', unitA.id, unitA.id, + Unit.areaUnitType.METERS, undefined); + await meterA.insert(conn); + + const identifierMeter = await Meter.getByIdentifier('MeterA', conn); + expectMetersToBeEquivalent(meterA, identifierMeter); + }); + + mocha.it('can get all meters', async () => { + const conn = testDB.getConnection(); + const meterA = new Meter(undefined, 'MeterA', null, true, true, Meter.type.MAMAC, null, gps, + 'MeterA', 'notes 1', 35.0, true, true, '01:01:25', '00:00:00', 5, 0, 1, 'increasing', false, + 1.5, '0001-01-01 23:59:59', '2020-07-02 01:00:10', '2020-03-05 02:12:00', unitA.id, unitA.id, + Unit.areaUnitType.METERS, undefined); + const meterB = new Meter(undefined, 'MeterB', null, true, true, Meter.type.MAMAC, null, gps, + 'MeterB', 'notes 1', 35.0, true, true, '01:01:25', '00:00:00', 5, 0, 1, 'increasing', false, + 1.5, '0001-01-01 23:59:59', '2020-07-02 01:00:10', '2020-03-05 02:12:00', unitA.id, unitA.id, + Unit.areaUnitType.METERS, undefined); + const meterC= new Meter(undefined, 'MeterC', null, true, true, Meter.type.MAMAC, null, gps, + 'MeterC', 'notes 1', 35.0, true, true, '01:01:25', '00:00:00', 5, 0, 1, 'increasing', false, + 1.5, '0001-01-01 23:59:59', '2020-07-02 01:00:10', '2020-03-05 02:12:00', unitA.id, unitA.id, + Unit.areaUnitType.METERS, undefined); + + await Promise.all([meterA, meterB, meterC].map(meter => meter.insert(conn))); + const allExpectedMeters = await Meter.getAll(conn); + const allActualMeters = [meterA, meterB, meterC]; + allExpectedMeters.sort((a,b) => a.id - b.id); + allActualMeters.sort((a,b) => a.id - b.id); + + expect(allExpectedMeters.length).to.be.equal(allActualMeters.length); + for (let i = 0; i < allExpectedMeters.length; ++i) { + expectMetersToBeEquivalent(allExpectedMeters[i], allActualMeters[i]); + } + }); +}); + +module.exports = { + expectMetersToBeEquivalent +};