Example #1
0
    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)
Example #2
0
    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()])
Example #4
0
    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)
Example #5
0
    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)
Example #6
0
 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"))
Example #7
0
 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])
Example #8
0
 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"))
Example #9
0
 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"))
Example #10
0
    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")
Example #11
0
 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"))
Example #12
0
    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)))
Example #14
0
 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),
     ])
Example #15
0
 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)
Example #16
0
 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)
Example #17
0
 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"))
Example #19
0
 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())
Example #20
0
 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()]})
Example #21
0
 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)
Example #22
0
    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)
Example #23
0
    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)
Example #25
0
    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)))
Example #26
0
    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)
Example #27
0
 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)
Example #29
0
 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)
Example #30
0
 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)