Exemple #1
0
    def list_plants(self):

        from conf import envinfo
        database_info = envinfo.DB_CONF

        solar_db = PonyManager(database_info)
        solar_db.define_solar_models()
        solar_db.binddb(create_tables=False)
        with orm.db_session:
            plant_info = orm.select(
                (p.name, p.location.latitude, p.location.longitude)
                for p in solar_db.db.Plant)[:]

        return plant_info
Exemple #2
0
    def sun_events_update(self, start, end, plants=None):

        from conf import envinfo
        database_info = envinfo.DB_CONF

        solar_db = PonyManager(database_info)
        solar_db.define_solar_models()
        solar_db.binddb(create_tables=True)

        with orm.db_session:
            if not plants:
                target_plants = orm.select(p for p in solar_db.db.Plant)[:]
            else:
                target_plants = solar_db.db.Plant.select(
                    lambda p: p.name in plants)[:]

            if not target_plants:
                print("No plants in database. Returning.")
                return

            for plant in target_plants:
                if not plant.location:
                    print("Plant {} doesn't have a location".format(
                        plant.name))
                    continue

                lat = plant.location.latitude
                lon = plant.location.longitude
                sun_events_gen = SunEventsGenerator(lat, lon)

                sun_events = sun_events_gen.generate_sunevents(start=start,
                                                               end=end)

                print(sun_events)

                solar_db.db.SolarEvent.insertPlantSolarEvents(
                    plant, sun_events)
class SunEvents_Test(unittest.TestCase):

    def setUp(self):

        from conf import envinfo
        self.assertEqual(envinfo.SETTINGS_MODULE, 'conf.settings.testing')

        os.environ['PGTZ'] = 'UTC'

        database_info = envinfo.DB_CONF

        self.pony = PonyManager(database_info)

        self.pony.define_solar_models()
        self.pony.binddb(create_tables=True)

    def tearDown(self):

        '''
        binddb calls gneerate_mapping which creates the tables outside the transaction
        drop them
        '''
        self.pony.db.drop_all_tables(with_all_data=True)
        self.pony.db.disconnect()

    def createPlant(self):

        with orm.db_session:
            p = self.pony.db.Plant(name='roger', codename='Som_roger')
            self.pony.db.PlantLocation(plant=p, latitude=41.967599, longitude=2.837782)

    def createPlantWithoutLocation(self):

        with orm.db_session:
            p = self.pony.db.Plant(name='roger', codename='Som_roger')

    def test__checkEnvironment(self):

        from conf import envinfo
        self.assertEqual(envinfo.SETTINGS_MODULE, 'conf.settings.testing')

    def test__sunevents_click(self):

        self.createPlant()

        runner = CliRunner()
        result = runner.invoke(
            plant_sun_events_update, '--start 2021-11-29 --end 2021-11-29 --plant roger'.split()
        )

        self.assertEqual(result.exit_code, 0)

    def test__sun_events_update__no_plant(self):

        # run sun_events

        start = datetime.datetime(2021, 11, 29, 3, 0, 0, 0, tzinfo=datetime.timezone.utc)
        end = datetime.datetime(2021, 11, 30, 3, 0, 0, 0, tzinfo=datetime.timezone.utc)

        se = SunEvents()

        se.sun_events_update(start=start, end=end)

        with orm.db_session:
            result = orm.select((s.plant.name, s.sunrise, s.sunset) for s in self.pony.db.SolarEvent)[:].to_list()

        expected = []

        self.assertListEqual(result, expected)


    def test__sun_events_update__no_plant_no_location(self):

        self.createPlantWithoutLocation()

        # run sun_events

        start = datetime.datetime(2021, 11, 29, 3, 0, 0, 0, tzinfo=datetime.timezone.utc)
        end = datetime.datetime(2021, 11, 30, 3, 0, 0, 0, tzinfo=datetime.timezone.utc)

        se = SunEvents()

        se.sun_events_update(start=start, end=end)

        with orm.db_session:
            result = orm.select((s.plant.name, s.sunrise, s.sunset) for s in self.pony.db.SolarEvent)[:].to_list()

        expected = []

        self.assertListEqual(result, expected)


    def test__sun_events_update__one_plant(self):

        self.createPlant()

        # run sun_events

        start = datetime.datetime(2021, 11, 29, 3, 0, 0, 0, tzinfo=datetime.timezone.utc)
        end = datetime.datetime(2021, 11, 30, 3, 0, 0, 0, tzinfo=datetime.timezone.utc)

        se = SunEvents()

        se.sun_events_update(start=start, end=end)

        with orm.db_session:
            result = orm.select((s.plant.name, s.sunrise, s.sunset) for s in self.pony.db.SolarEvent)[:].to_list()

        expected_sunrise = datetime.datetime(2021, 11, 29, 7, 23, 36, 140813, tzinfo=datetime.timezone.utc)
        expected_sunset = datetime.datetime(2021, 11, 29, 15, 50, 5, 924495, tzinfo=datetime.timezone.utc)

        expected = [('roger', expected_sunrise, expected_sunset)]

        self.assertListEqual(result, expected)