def test_addForecast_overwrite(self): self.maxDiff = None with self.createPlantmonitorDB() as db: data = { self.mainFacility(): [ (todtaware("2020-01-02 00:00:00"), 10), (todtaware("2020-01-02 01:00:00"), 20), ], } forecastDate = todt("2020-01-01 00:00:00") db.addForecast(data, forecastDate) data2 = { self.mainFacility(): [ (todtaware("2020-01-02 01:00:00"), 30), (todtaware("2020-01-02 02:00:00"), 40), ], } forecastDate = todt("2020-01-01 00:00:00") db.addForecast(data2, forecastDate) dataResult = { self.mainFacility(): [ (todtaware("2020-01-02 00:00:00"), 10), (todtaware("2020-01-02 01:00:00"), 30), (todtaware("2020-01-02 02:00:00"), 40), ], } result = db.getForecast() self.assertEqual(dataResult, result)
def test_uploadProductionFromDB_ExcludedFacilities(self): config = self.createConfig() #change this once the erp hourly-quarterly is fixed excludedFacility = 'test_facility' with self.createPlantmonitorDB() as db: db.addFacilityMeterRelation(excludedFacility, '432104321') db.addFacilityMeterRelation(self.mainFacility(), '123401234') data = { self.mainFacility(): [ (todt("2040-01-02 00:00:00"), 10), (todt("2040-01-02 01:00:00"), 20), ], excludedFacility: [ (todt("2040-01-02 00:00:00"), 210), (todt("2040-01-02 01:00:00"), 340), ], } db.addMeterData(data) responses = upload_meter_data(config, test_env=True) self.assertDictEqual( {self.mainFacility(): "OK"}, responses )
def __test_downloadForecastToEmptyDB_manyfacilities(self): configdb = self.createConfig() data = { self.mainFacility(): [ (todt("2040-01-02 00:00:00"), 10), (todt("2040-01-02 01:00:00"), 20), ], } fromDate = todt("{}-01-01 00:00:00".format(dt.datetime.now().year)) toDate = todt("{}-01-01 00:00:00".format(dt.datetime.now().year)) forecasts = {} with self.createApi() as api: for facility in data.keys(): forecast = api.getForecast(facility, fromDate, toDate) forecasts[facility] = forecast downloadResponses = download_meter_data(configdb, test_env=True) self.assertTrue((self.mainFacility(), "OK") in downloadResponses.items()) forecastDBData = {} with self.createPlantmonitorDB() as db: forecastDBData = db.getForecast(self.mainFacility()) logger.info(forecastDBData) logger.info(forecasts) self.assertDictEqual(forecastDBData, forecasts[self.mainFacility()])
def __test_downloadForecastToDB_ManyFacilities(self): config = self.createConfig() with self.createPlantmonitorDB() as db: db.addFacilityMeterRelation(self.otherFacility(), '432104321') db.addFacilityMeterRelation(self.mainFacility(), '123401234') data = { self.mainFacility(): [ (todt("2040-01-02 00:00:00"), 10), (todt("2040-01-02 01:00:00"), 20), ], self.otherFacility(): [ (todt("2040-01-02 00:00:00"), 210), (todt("2040-01-02 01:00:00"), 340), ], } db.addMeterData(data) responses = download_meter_data(config, test_env=True) self.assertDictEqual( { self.mainFacility(): "OK", self.otherFacility(): "OK" }, responses)
def test_uploadProduction_mixedFacilities(self): config = self.createConfig() with self.createPlantmonitorDB() as db: db.addFacilityMeterRelation(self.mainFacility(), '123401234') db.addFacilityMeterRelation(self.unexistantFacility(), '432104321') data = { self.mainFacility(): [ (todt("2040-01-02 01:00:00"), 50), (todt("2040-01-02 02:00:00"), 70), ], self.unexistantFacility(): [ (todt("2040-01-02 01:00:00"), 10), (todt("2040-01-02 02:00:00"), 20), ], } db.addMeterData(data) responses = upload_meter_data(configdb=config, test_env=True) self.assertDictEqual({ self.mainFacility(): "OK", self.unexistantFacility(): "INVALID_FACILITY_ID: {}".format(self.unexistantFacility()) }, responses ) with self.createApi() as api: self.assertEqual(api.lastDateUploaded(self.mainFacility()), todt("2040-01-02 02:00:00") - timedelta(hours=1)) self.assertEqual(api.lastDateUploaded(self.unexistantFacility()), None)
def __test_getLastApiDate(self): api = self.createApi() with api: api.uploadProduction(self.mainFacility(), [ (todt("2040-02-01 00:00:00"), 10), ]) result = api.getLastApiDate() self.assertEqual(result, todt("2040-02-01 00:00:00"))
def __test_write_meter(self): db = self.createPlantmonitorDB() facility = self.mainFacility() expected = {facility: [(todt("2020-01-01 00:00:00"), 10)]} result = db.addMeterData(facility, [(todt("2020-01-01 00:00:00"), 10)]) self.assertEqual(result, expected) allMeterData = db.getMeterData() self.assertEqual(allMeterData.get(facility, None), expected[facility])
def test_uploadProduction_lastDateUploadedIsPersistent(self): facility = self.mainFacility() api = self.createApi() api.uploadProduction(facility, [ (todt("2040-01-01 00:00:00"), 10), ]) api2 = self.createApi() self.assertEqual(api2.lastDateUploaded(facility), todt("2040-01-01 00:00:00"))
def test_uploadProduction_manyData(self): facility = self.mainFacility() api = self.createApi() api.uploadProduction(facility, [ (todt("2040-01-01 00:00:00"), 10), (todt("2040-01-02 00:00:00"), 10), ]) self.assertEqual(api.lastDateUploaded(facility), todt("2040-01-02 00:00:00"))
def test_uploadProduction(self): data = { self.mainFacility(): [ (todt("2040-01-02 00:00:00"), 10), (todt("2040-01-02 01:00:00"), 20), ], } with self.createApi() as api: for facility, values in data.items(): response = api.uploadProduction(facility, values) self.assertEqual(response, "OK")
def __test_getLastApiDateForOlderFacility(self): facility = self.mainFacility() otherfacility = self.otherFacility() api = self.createApi() with api: api.uploadProduction(facility, [ (todt("2040-01-02 00:00:00"), 10), ]) api.uploadProduction(otherfacility, [ (todt("2040-01-03 00:00:00"), 10), ]) result = api.getLastApiDate(facility) self.assertEqual(result, todt("2040-01-02 00:00:00"))
def test_shiftOneHour(self): data = { "fooFacility": [ (todt("2040-01-02 00:00:00"), 10), (todt("2040-01-02 01:00:00"), 20), ], "booFacility": [ (todt("2040-01-02 00:00:00"), 210), (todt("2040-01-02 01:00:00"), 340), ], } expectedData = { "fooFacility": [ (todt("2040-01-01 23:00:00"), 10), (todt("2040-01-02 00:00:00"), 20), ], "booFacility": [ (todt("2040-01-01 23:00:00"), 210), (todt("2040-01-02 00:00:00"), 340), ], } shiftedData = shiftOneHour(data) self.assertDictEqual(expectedData, shiftedData)
def test_downloadForecast(self): data = { self.mainFacility(): [ (todt("2040-01-02 00:00:00"), 10), (todt("2040-01-02 01:00:00"), 20), ], } fromDate = todt("{}-01-01 00:00:00".format(dt.datetime.now().year)) toDate = todt("{}-01-03 00:00:00".format(dt.datetime.now().year)) with self.createApi() as api: for facility, values in data.items(): resultForecast = api.getForecast(facility, fromDate, toDate) self.assertEqual(resultForecast[0][0], todtaware("{}-01-01 00:00:00".format(dt.datetime.now().year)))
def _test_uploadProduction_errorUpload(self): api = self.createApi() login = api.login('alberto', '1234') facility = self.mainFacility() api.uploadProduction(facility, [ (todt("2040-01-01 00:00:00"), 10), ])
def test_uploadProduction_doesNotChangeOtherFacility(self): facility = self.mainFacility() api = self.createApi() api.uploadProduction(facility, [ (todt("2040-01-01 00:00:00"), 10), ]) self.assertEqual(api.lastDateUploaded("OtherPlant"), None)
def test_addOneMeterData(self): with self.createPlantmonitorDB() as db: db.addFacilityMeterRelation(self.mainFacility(), '123401234') oneRow = {self.mainFacility(): [(todt('2020-01-01 00:00:00'), 10)]} db.addMeterData(oneRow) result = db.getMeterData() self.assertEqual(result, oneRow)
def test_getOneMeterData(self): with self.createPlantmonitorDB() as db: db.addFacilityMeterRelation(self.secondaryFacility(), '432104321') db.addFacilityMeterRelation(self.mainFacility(), '123401234') data = {self.mainFacility(): [(todt('2020-01-01 00:00:00'), 10)]} db.addMeterData(data) result = db.getMeterData() self.assertEqual(result, data)
def __test_downloadForecastToDB(self): configdb = self.createConfig() data = { self.mainFacility(): [ (todt("2040-01-02 00:00:00"), 10), (todt("2040-01-02 01:00:00"), 20), ], } fromDate = todt("{}-01-01 00:00:00".format(dt.datetime.now().year)) toDate = todt("{}-01-01 00:00:00".format(dt.datetime.now().year)) with self.createApi() as api: for facility in data.keys(): response = api.getForecast(facility, fromDate, toDate) with self.createPlantmonitorDB() as db: db.addFacilityMeterRelation(self.mainFacility(), '123401234') tworeadings = { self.mainFacility(): [ (todt("2040-01-02 00:00:00"), 10), (todt("2040-01-02 01:00:00"), 20), ], } db.addMeterData(tworeadings) responses = download_meter_data(configdb, test_env=True) self.assertDictEqual({self.mainFacility(): "OK"}, responses) # TODO check uploaded content from api if they ever add GetObservations with self.createApi() as api: self.assertEqual(api.lastDateUploaded(self.mainFacility()), todt("2040-01-02 01:00:00"))
def test_session_keepSession(self): facility = self.mainFacility() api = self.createApi() with api: session = api.session() api.uploadProduction(facility, [ (todt("2040-01-01 00:00:00"), 10), ]) self.assertEqual(session, api.session())
def test_getMeterDataFacility(self): with self.createPlantmonitorDB() as db: db.addFacilityMeterRelation(self.secondaryFacility(), '432104321') db.addFacilityMeterRelation(self.mainFacility(), '123401234') data = { self.mainFacility(): [ (todt("2020-01-01 00:00:00"), 10), (todt("2020-01-01 01:00:00"), 20), ], self.secondaryFacility(): [ (todt("2020-01-01 00:00:00"), 210), (todt("2020-01-01 03:00:00"), 340), ], } db.addMeterData(data) meterData = db.getMeterData(self.mainFacility()) self.assertEqual(meterData, {self.mainFacility(): data[self.mainFacility()]})
def test_uploadProduction_wrongFacility(self): api = self.createApi() with self.assertRaises(MeteologicaApiError) as ctx: api.uploadProduction("WrongPlant", [ (todt("2040-01-01 00:00:00"), 10), ]) self.assertEqual( type(u'')(ctx.exception), "INVALID_FACILITY_ID: WrongPlant") self.assertEqual(api.lastDateUploaded("WrongPlant"), None)
def test__lastForecastDownloaded__checkLastDateAdded(self): alcolea = self.pony.db.Plant( name=self.mainFacility(), codename='SomEnergia_{}'.format(self.mainFacility()), ) oldForecastDate = datetime.datetime(2020, 12, 10, 15, 5, 10, 588861, tzinfo=datetime.timezone.utc) newForecastDate = oldForecastDate + datetime.timedelta(hours=1) status = 'OK' oldForecastMetadata = self.pony.db.ForecastMetadata.create( plant=alcolea, forecastdate=oldForecastDate, errorcode=status) newForecastMetadata = self.pony.db.ForecastMetadata.create( plant=alcolea, forecastdate=newForecastDate, errorcode=status) oldForecast = [ (todt("2040-01-02 00:00:00"), 10), (todt("2040-01-02 01:00:00"), 20), ] newForecast = [ (todt("2040-01-02 02:00:00"), 30), (todt("2040-01-02 03:00:00"), 40), ] oldForecastMetadata.addForecasts(oldForecast) newForecastMetadata.addForecasts(newForecast) orm.flush() lastForecastDate = alcolea.lastForecastDownloaded() expectedForecastDate = newForecastDate self.assertEqual(lastForecastDate, expectedForecastDate)
def test_uploadProductionFromDB(self): configdb = self.createConfig() with self.createPlantmonitorDB() as db: db.addFacilityMeterRelation(self.mainFacility(), '123401234') tworeadings = { self.mainFacility(): [ (todt("2040-01-02 01:00:00"), 10), (todt("2040-01-02 02:00:00"), 20), ], } db.addMeterData(tworeadings) responses = upload_meter_data(configdb, test_env=True) self.assertDictEqual({self.mainFacility(): "OK"}, responses) # TODO check uploaded content from api if they ever add GetObservations with self.createApi() as api: self.assertEqual(api.lastDateUploaded(self.mainFacility()), todt("2040-01-02 02:00:00") - timedelta(hours=1))
def __test_downloadForecastToDB_mismatchFacilities(self): config = self.createConfig() with self.createPlantmonitorDB() as db: db.addFacilityMeterRelation(self.unexistantFacility(), '432104321') data = { self.unexistantFacility(): [ (todt("2040-01-02 00:00:00"), 10), (todt("2040-01-02 01:00:00"), 20), ], } db.addMeterData(data) responses = download_meter_data(configdb=config, test_env=True) self.assertDictEqual( {self.unexistantFacility(): "INVALID_FACILITY_ID: {}".format(self.unexistantFacility())}, responses ) with self.createApi() as api: self.assertEqual(api.lastDateUploaded(self.unexistantFacility()), None)
def test_getForecastFormatCheck(self): api = self.createApi() facility = self.mainFacility() api.uploadProduction(facility, [ (todt("{}-01-01 00:00:00".format( datetime.datetime.now().year)), 10), ]) result = api.getForecast( facility, todt("{}-01-01 00:00:00".format(datetime.datetime.now().year)), todt("{}-01-01 00:00:00".format(datetime.datetime.now().year)), ) #expected [("2020-01-01 00:00:00", _)] since we don't know meteologica's algorithm logger.debug(result) self.assertEqual(len(result), 1) self.assertEqual(len(result[0]), 2) self.assertEqual( result[0][0], todtaware("{}-01-01 00:00:00".format( datetime.datetime.now().year)))
def test__lastForecastDownloadDate__SomeData(self): alcolea = self.pony.db.Plant( name=self.mainFacility(), codename='SomEnergia_{}'.format(self.mainFacility()), ) time = datetime.datetime(2020, 12, 10, 15, 5, 10, 588861, tzinfo=datetime.timezone.utc) forecastMeta = self.sampleForecastMeta(self.mainFacility(), time) data = { self.mainFacility(): [ (todt("2040-01-02 00:00:00"), 10), (todt("2040-01-02 01:00:00"), 20), ], } status = 'OK' forecastDate = time forecastMetadata = self.pony.db.ForecastMetadata.create( plant=alcolea, forecastdate=forecastDate, errorcode=status) forecastMetadata.addForecasts(data[self.mainFacility()]) orm.flush() forecastDate = alcolea.lastForecastDownloaded() expectedForecastDate = time self.assertEqual(forecastDate, expectedForecastDate)
def test_getMeterData(self): with self.createPlantmonitorDB() as db: db.addFacilityMeterRelation(self.secondaryFacility(), '432104321') db.addFacilityMeterRelation(self.mainFacility(), '123401234') data = { self.mainFacility(): [ (todt('2020-01-01 00:00:00'), 10), (todt('2020-01-01 01:00:00'), 20), (todt('2020-01-01 02:00:00'), 30), (todt('2020-01-01 03:00:00'), 40), ], self.secondaryFacility(): [ (todt('2020-01-01 00:00:00'), 210), (todt('2020-01-01 01:00:00'), 320), (todt('2020-01-01 02:00:00'), 230), (todt('2020-01-01 03:00:00'), 340), ] } db.addMeterData(data) meter = db.getMeterData() self.assertEqual(meter, data)
def lastDateUploaded(self, facility): lastDates = ns.load(self._config.lastDateFile) lastDate = lastDates.get(facility, None) return todt(lastDate)
def test_addOneFromMissingFacility(self): with self.createPlantmonitorDB() as db: oneRow = {self.mainFacility(): [(todt('2020-01-01 00:00:00'), 10)]} self.assertRaises(PlantmonitorDBError, db.addMeterData, oneRow)
def test_getMeterDataFromToDate(self): with self.createPlantmonitorDB() as db: db.addFacilityMeterRelation(self.secondaryFacility(), '432104321') db.addFacilityMeterRelation(self.mainFacility(), '123401234') data = { self.mainFacility(): [ (todt("2020-01-01 00:00:00"), 10), (todt("2020-01-01 01:00:00"), 20), (todt("2020-01-01 02:00:00"), 30), (todt("2020-01-01 03:00:00"), 40), ], self.secondaryFacility(): [ (todt("2020-01-01 00:00:00"), 210), (todt("2020-01-01 01:00:00"), 320), (todt("2020-01-01 02:00:00"), 230), (todt("2020-01-01 03:00:00"), 340), ] } db.addMeterData(data) dataFromAndTo = { self.mainFacility(): [ (todt("2020-01-01 02:00:00"), 30), (todt("2020-01-01 03:00:00"), 40), (todt("2020-01-01 01:00:00"), 20), ], self.secondaryFacility(): [ (todt("2020-01-01 01:00:00"), 320), (todt("2020-01-01 02:00:00"), 230), (todt("2020-01-01 03:00:00"), 340), ] } meter = db.getMeterData(fromDate=todt("2020-01-01 01:00:00"), toDate=todt("2020-01-01 03:00:00")) self.assertCountEqual(meter, data)