""" Test db factory SPDX - License - Identifier: LGPL - 3.0 - or -later Copyright © 2022 Concordia CERC group Project Coder Peter Yefi peteryefi@gmail.com """ import glob import os import unittest from unittest import TestCase import hub.helpers.constants as cte from unittests.control import Control control = Control() class TestDBFactory(TestCase): """ TestDBFactory """ @unittest.skipIf(control.skip_test, control.skip_reason) def test_save_city(self): control.city.name = "Montreal" city_id = control.database.persist_city( control.city, control.pickle_path, control.city.name, control.application_id, control.user_id) control.database.delete_city(city_id) @unittest.skipIf(control.skip_test, control.skip_reason) def test_get_update_city(self): city_id = control.database.persist_city(control.city, control.pickle_path, control.city.name, control.application_id, control.user_id) control.city.name = "Ottawa" control.database.update_city(city_id, control.city) cities = control.database.cities_by_user_and_application( control.user_id, control.application_id) for updated_city in cities: if updated_city.id == city_id: self.assertEqual(updated_city.name, control.city.name) break control.database.delete_city(city_id) @unittest.skipIf(control.skip_test, control.skip_reason) @unittest.skipIf(control.skip_insel_test, 'insel is not installed') def test_save_results(self): city_id = control.database.persist_city(control.city, control.pickle_path, 'current status', control.application_id, control.user_id) city_objects_id = [] request_values = { 'scenarios': [{'current status': ['1']}] } for building in control.city.buildings: _building = control.database.building_info(building.name, city_id) if cte.MONTH not in building.cooling_demand: print(f'building {building.name} not calculated') continue monthly_cooling_peak_load = building.cooling_peak_load[cte.MONTH] yearly_cooling_peak_load = building.cooling_peak_load[cte.YEAR] monthly_heating_peak_load = building.heating_peak_load[cte.MONTH] yearly_heating_peak_load = building.heating_peak_load[cte.YEAR] monthly_lighting_peak_load = building.lighting_peak_load[cte.MONTH] yearly_lighting_peak_load = building.lighting_peak_load[cte.YEAR] monthly_appliances_peak_load = building.appliances_peak_load[cte.MONTH] yearly_appliances_peak_load = building.appliances_peak_load[cte.YEAR] monthly_cooling_demand = building.cooling_demand[cte.MONTH] yearly_cooling_demand = building.cooling_demand[cte.YEAR] monthly_heating_demand = building.heating_demand[cte.MONTH] yearly_heating_demand = building.heating_demand[cte.YEAR] monthly_lighting_electrical_demand = building.lighting_electrical_demand[cte.MONTH] yearly_lighting_electrical_demand = building.lighting_electrical_demand[cte.YEAR] monthly_appliances_electrical_demand = building.appliances_electrical_demand[cte.MONTH] yearly_appliances_electrical_demand = building.appliances_electrical_demand[cte.YEAR] monthly_domestic_hot_water_heat_demand = building.domestic_hot_water_heat_demand[cte.MONTH] yearly_domestic_hot_water_heat_demand = building.domestic_hot_water_heat_demand[cte.YEAR] monthly_heating_consumption = building.heating_consumption[cte.MONTH] yearly_heating_consumption = building.heating_consumption[cte.YEAR] monthly_cooling_consumption = building.cooling_consumption[cte.MONTH] yearly_cooling_consumption = building.cooling_consumption[cte.YEAR] monthly_domestic_hot_water_consumption = building.domestic_hot_water_consumption[cte.MONTH] yearly_domestic_hot_water_consumption = building._domestic_hot_water_consumption[cte.YEAR] monthly_distribution_systems_electrical_consumption = building.distribution_systems_electrical_consumption[ cte.MONTH] yearly_distribution_systems_electrical_consumption = building.distribution_systems_electrical_consumption[ cte.YEAR] monthly_on_site_electrical_production = [x * cte.WATTS_HOUR_TO_JULES for x in building.onsite_electrical_production[cte.MONTH]] yearly_on_site_electrical_production = [x * cte.WATTS_HOUR_TO_JULES for x in building.onsite_electrical_production[cte.YEAR]] results = {cte.INSEL_MEB: { 'monthly_cooling_peak_load': monthly_cooling_peak_load, 'yearly_cooling_peak_load': yearly_cooling_peak_load, 'monthly_heating_peak_load': monthly_heating_peak_load, 'yearly_heating_peak_load': yearly_heating_peak_load, 'monthly_lighting_peak_load': monthly_lighting_peak_load, 'yearly_lighting_peak_load': yearly_lighting_peak_load, 'monthly_appliances_peak_load': monthly_appliances_peak_load, 'yearly_appliances_peak_load': yearly_appliances_peak_load, 'monthly_cooling_demand': monthly_cooling_demand, 'yearly_cooling_demand': yearly_cooling_demand, 'monthly_heating_demand': monthly_heating_demand, 'yearly_heating_demand': yearly_heating_demand, 'monthly_lighting_electrical_demand': monthly_lighting_electrical_demand, 'yearly_lighting_electrical_demand': yearly_lighting_electrical_demand, 'monthly_appliances_electrical_demand': monthly_appliances_electrical_demand, 'yearly_appliances_electrical_demand': yearly_appliances_electrical_demand, 'monthly_domestic_hot_water_heat_demand': monthly_domestic_hot_water_heat_demand, 'yearly_domestic_hot_water_heat_demand': yearly_domestic_hot_water_heat_demand, 'monthly_heating_consumption': monthly_heating_consumption, 'yearly_heating_consumption': yearly_heating_consumption, 'monthly_cooling_consumption': monthly_cooling_consumption, 'yearly_cooling_consumption': yearly_cooling_consumption, 'monthly_domestic_hot_water_consumption': monthly_domestic_hot_water_consumption, 'yearly_domestic_hot_water_consumption': yearly_domestic_hot_water_consumption, 'monthly_distribution_systems_electrical_consumption': monthly_distribution_systems_electrical_consumption, 'yearly_distribution_systems_electrical_consumption': yearly_distribution_systems_electrical_consumption, 'monthly_on_site_electrical_production': monthly_on_site_electrical_production, 'yearly_on_site_electrical_production': yearly_on_site_electrical_production }} db_building_id = _building.id city_objects_id.append(db_building_id) control.database.add_simulation_results( cte.INSEL_MEB, results, city_object_id=db_building_id) self.assertEqual(17, len(city_objects_id), 'wrong number of results') self.assertIsNotNone(city_objects_id[0], 'city_object_id is None') results = control.database.results(control.user_id, control.application_id, request_values) self.assertEqual( 28, len(results['current status'][0]['insel meb'].keys()), 'wrong number of results after retrieve' ) for _id in city_objects_id: control.database.delete_results_by_name('insel meb', city_object_id=_id) control.database.delete_city(city_id) @classmethod @unittest.skipIf(control.skip_test, control.skip_reason) def tearDownClass(cls): control.database.delete_application(control.application_uuid) control.database.delete_user(control.user_id) os.unlink(control.pickle_path) output_files = glob.glob('./tests_outputs/*') for output_file in output_files: os.unlink(output_file)