city_retrofit/hub/unittests/test_db_factory.py

260 lines
12 KiB
Python
Raw Normal View History

"""
2023-05-10 17:06:51 -04:00
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 json
import logging
2023-05-10 17:06:51 -04:00
import os
import subprocess
2023-03-27 13:55:32 -04:00
import unittest
from unittest import TestCase
2023-05-10 17:06:51 -04:00
from pathlib import Path
2023-03-27 13:55:32 -04:00
import sqlalchemy.exc
2023-01-24 10:51:50 -05:00
from hub.imports.geometry_factory import GeometryFactory
from hub.imports.construction_factory import ConstructionFactory
from hub.imports.usage_factory import UsageFactory
from hub.imports.results_factory import ResultFactory
from hub.imports.weather_factory import WeatherFactory
from hub.exports.energy_building_exports_factory import EnergyBuildingsExportsFactory
from hub.exports.exports_factory import ExportsFactory
2023-05-17 12:30:11 -04:00
from hub.persistence.db_control import DBControl
2023-02-22 22:45:33 -05:00
from hub.persistence.repository import Repository
from sqlalchemy import create_engine
from hub.persistence.models import City, Application, CityObject, SimulationResults
2023-01-24 10:51:50 -05:00
from hub.persistence.models import User, UserRoles
from hub.helpers.dictionaries import Dictionaries
from sqlalchemy.exc import ProgrammingError
2023-02-22 22:45:33 -05:00
import uuid
import hub.helpers.constants as cte
import distutils.spawn
2023-03-27 13:55:32 -04:00
2023-05-17 12:30:11 -04:00
class Control:
2023-05-10 17:06:51 -04:00
_skip_test = False
_skip_reason = 'PostgreSQL not properly installed in host machine'
2023-03-27 13:55:32 -04:00
def __init__(self):
2023-05-10 17:06:51 -04:00
"""
2023-05-18 12:29:28 -04:00
Test
setup
:return: None
"""
2023-05-10 17:06:51 -04:00
self._skip_test = False
2023-03-27 13:55:32 -04:00
# Create test database
dotenv_path = Path("{}/.local/etc/hub/.env".format(os.path.expanduser('~'))).resolve()
if not dotenv_path.exists():
self._skip_test = True
self._skip_reason = f'.env file missing at {dotenv_path}'
return
dotenv_path = str(dotenv_path)
repository = Repository(db_name='hub_unittests', app_env='TEST', dotenv_path=dotenv_path)
2023-05-10 17:06:51 -04:00
engine = create_engine(repository.configuration.connection_string)
2023-03-27 13:55:32 -04:00
try:
# delete test database if it exists
2023-05-10 17:06:51 -04:00
connection = engine.connect()
connection.close()
except ProgrammingError:
logging.info('Database does not exist. Nothing to delete')
2023-05-10 17:06:51 -04:00
except sqlalchemy.exc.OperationalError as operational_error:
self._skip_test = True
self._skip_reason = f'{operational_error}'
return
Application.__table__.create(bind=repository.engine, checkfirst=True)
User.__table__.create(bind=repository.engine, checkfirst=True)
City.__table__.create(bind=repository.engine, checkfirst=True)
CityObject.__table__.create(bind=repository.engine, checkfirst=True)
SimulationResults.__table__.create(bind=repository.engine, checkfirst=True)
2023-05-10 17:06:51 -04:00
city_file = "tests_data/FZK_Haus_LoD_2.gml"
weather_file = 'CAN_PQ_Montreal.Intl.AP.716270_CWEC.epw'
output_path = Path('./tests_outputs/').resolve()
self._city = GeometryFactory('citygml',
city_file,
function_to_hub=Dictionaries().alkis_function_to_hub_function).city
ConstructionFactory('nrcan', self._city).enrich()
UsageFactory('nrcan', self._city).enrich()
WeatherFactory('epw', self._city, file_name='CAN_PQ_Montreal.Intl.AP.716270_CWEC.epw').enrich()
ExportsFactory('sra', self._city, output_path, weather_file=weather_file, weather_format='epw').export()
sra_file = str((output_path / f'{self._city.name}_sra.xml').resolve())
subprocess.run([self.sra, sra_file], stdout=subprocess.DEVNULL)
ResultFactory('sra', self._city, output_path).enrich()
EnergyBuildingsExportsFactory('insel_monthly_energy_balance', self._city, output_path).export()
_insel_files = glob.glob(f'{output_path}/*.insel')
for insel_file in _insel_files:
subprocess.run([self.insel, str(insel_file)], stdout=subprocess.DEVNULL)
ResultFactory('insel_monthly_energy_balance', self._city, output_path).enrich()
2023-05-17 12:30:11 -04:00
self._database = DBControl(
2023-05-10 17:06:51 -04:00
db_name=repository.configuration.db_name,
app_env='TEST',
dotenv_path=dotenv_path)
2023-05-17 12:30:11 -04:00
self._application_uuid = str(uuid.uuid4())
self._application_id = self._database.persist_application("test", "test application", self.application_uuid)
2023-05-19 13:15:40 -04:00
self._user_id = self._database.create_user("Admin", self._application_id, "Admin@123", UserRoles.Admin)
2023-05-10 17:06:51 -04:00
self._pickle_path = 'tests_data/pickle_path.bz2'
@property
2023-05-17 12:30:11 -04:00
def database(self):
return self._database
2023-05-10 17:06:51 -04:00
@property
def application_uuid(self):
return self._application_uuid
2023-05-10 17:06:51 -04:00
@property
2023-05-19 13:15:40 -04:00
def application_id(self):
return self._application_id
2023-05-10 17:06:51 -04:00
@property
2023-05-19 13:15:40 -04:00
def user_id(self):
return self._user_id
2023-05-10 17:06:51 -04:00
@property
def skip_test(self):
return self._skip_test
2023-03-27 13:55:32 -04:00
@property
def insel(self):
return distutils.spawn.find_executable("insel")
@property
def sra(self):
return distutils.spawn.find_executable("sra")
@property
def skip_insel_test(self):
return self.insel is None
2023-03-27 13:55:32 -04:00
@property
2023-05-10 17:06:51 -04:00
def skip_reason(self):
return self._skip_reason
2023-03-27 13:55:32 -04:00
@property
def message(self):
return self._skip_reason
2023-05-10 17:06:51 -04:00
@property
def city(self):
return self._city
2023-03-27 13:55:32 -04:00
2023-05-10 17:06:51 -04:00
@property
def pickle_path(self):
return self._pickle_path
2023-03-27 13:55:32 -04:00
2023-05-17 12:30:11 -04:00
control = Control()
class TestDBFactory(TestCase):
"""
TestDBFactory
"""
2023-05-17 12:30:11 -04:00
@unittest.skipIf(control.skip_test, control.skip_reason)
def test_save_city(self):
2023-05-17 12:30:11 -04:00
control.city.name = "Montreal"
2023-05-19 13:15:40 -04:00
city_id = control.database.persist_city(
2023-05-17 12:30:11 -04:00
control.city,
control.pickle_path,
2023-05-19 13:15:40 -04:00
control.application_id,
control.user_id)
control.database.delete_city(city_id)
2023-05-17 17:10:30 -04:00
os.unlink(control.pickle_path)
2023-05-17 12:30:11 -04:00
@unittest.skipIf(control.skip_test, control.skip_reason)
def test_get_update_city(self):
2023-05-19 13:15:40 -04:00
city_id = control.database.persist_city(control.city,
control.pickle_path,
control.application_id,
control.user_id)
2023-05-19 13:15:40 -04:00
control.city.name = "Ottawa"
control.database.update_city(city_id, control.city)
2023-05-17 12:30:11 -04:00
cities = control.database.cities_by_user_and_application(
2023-05-19 13:15:40 -04:00
control.user_id,
control.application_id)
for updated_city in cities:
2023-05-19 13:15:40 -04:00
if updated_city.id == city_id:
self.assertEqual(updated_city.name, control.city.name)
break
2023-05-19 13:15:40 -04:00
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,
control.application_id,
control.user_id)
city_objects_id = []
for building in control.city.buildings:
_building = control.database.building_info(building.name, city_id)
if cte.MONTH not in building.cooling:
print(f'building {building.name} not calculated')
continue
monthly_cooling = building.cooling[cte.MONTH][cte.INSEL_MEB]
yearly_cooling = building.cooling[cte.YEAR][cte.INSEL_MEB]
monthly_heating = building.heating[cte.MONTH][cte.INSEL_MEB]
yearly_heating = building.heating[cte.YEAR][cte.INSEL_MEB]
monthly_cooling_peak_load = building.cooling_peak_load[cte.MONTH][cte.COOLING_PEAK_LOAD]
yearly_cooling_peak_load = building.cooling_peak_load[cte.YEAR][cte.COOLING_PEAK_LOAD]
monthly_heating_peak_load = building.heating_peak_load[cte.MONTH][cte.HEATING_PEAK_LOAD]
yearly_heating_peak_load = building.heating_peak_load[cte.YEAR][cte.HEATING_PEAK_LOAD]
monthly_lighting_electrical_demand = building.lighting_electrical_demand[cte.MONTH][cte.INSEL_MEB]
yearly_lighting_electrical_demand = building.lighting_electrical_demand[cte.YEAR][cte.INSEL_MEB]
monthly_appliances_electrical_demand = building.appliances_electrical_demand[cte.MONTH][cte.INSEL_MEB]
yearly_appliances_electrical_demand = building.appliances_electrical_demand[cte.YEAR][cte.INSEL_MEB]
monthly_domestic_hot_water_heat_demand = building.domestic_hot_water_heat_demand[cte.MONTH][cte.INSEL_MEB]
yearly_domestic_hot_water_heat_demand = building.domestic_hot_water_heat_demand[cte.YEAR][cte.INSEL_MEB]
monthly_heating_consumption = building.domestic_hot_water_heat_demand[cte.YEAR][cte.INSEL_MEB] # building.heating_consumption[cte.MONTH][cte.INSEL_MEB]
yearly_heating_consumption = building.domestic_hot_water_heat_demand[cte.YEAR][cte.INSEL_MEB] #building.heating_consumption[cte.YEAR][cte.INSEL_MEB]
monthly_cooling_consumption = building.domestic_hot_water_heat_demand[cte.YEAR][cte.INSEL_MEB] #building.cooling_consumption[cte.MONTH][cte.INSEL_MEB]
yearly_cooling_consumption = building.domestic_hot_water_heat_demand[cte.YEAR][cte.INSEL_MEB] #building.cooling_consumption[cte.YEAR][cte.INSEL_MEB]
monthly_domestic_hot_water_consumption = building.domestic_hot_water_heat_demand[cte.YEAR][cte.INSEL_MEB] #building.domestic_hot_water_consumption[cte.MONTH][cte.INSEL_MEB]
yearly_domestic_hot_water_consumption = building.domestic_hot_water_heat_demand[cte.YEAR][cte.INSEL_MEB] #building._domestic_hot_water_consumption[cte.YEAR][cte.INSEL_MEB]
db_building_id = _building.id
city_objects_id.append(db_building_id)
control.database.add_simulation_results(
cte.INSEL_MEB,
json.dumps({cte.INSEL_MEB: [
{"monthly_cooling_peak_load": monthly_cooling_peak_load.to_json()},
{"yearly_cooling_peak_load": yearly_cooling_peak_load.to_json()},
{"monthly_heating_peak_load": monthly_heating_peak_load.to_json()},
{"yearly_heating_peak_load": yearly_heating_peak_load.to_json()},
2023-05-29 11:59:45 -04:00
{"monthly_electrical_peak_load": monthly_electrical_peak_load.to_json()},
{"yearly_electrical_peak_load": yearly_electrical_peak_load.to_json()},
{"monthly_cooling_demand": monthly_cooling.to_json()},
{"yearly_cooling_demand": yearly_cooling.to_json()},
{"monthly_heating_demand": monthly_heating.to_json()},
{"yearly_heating_demand": yearly_heating.to_json()},
{"monthly_lighting_electrical_demand": monthly_lighting_electrical_demand.to_json()},
{"yearly_lighting_electrical_demand": yearly_lighting_electrical_demand.to_json()},
{"monthly_appliances_electrical_demand": monthly_appliances_electrical_demand.to_json()},
{"yearly_appliances_electrical_demand": yearly_appliances_electrical_demand.to_json()},
{"monthly_domestic_hot_water_heat_demand": monthly_domestic_hot_water_heat_demand.to_json()},
{"yearly_domestic_hot_water_heat_demand": yearly_domestic_hot_water_heat_demand.to_json()},
{"monthly_heating_consumption": monthly_heating_consumption.to_json()},
{"yearly_heating_consumption": yearly_heating_consumption.to_json()},
{"monthly_cooling_consumption": monthly_cooling_consumption.to_json()},
{"yearly_cooling_consumption": yearly_cooling_consumption.to_json()},
{"monthly_domestic_hot_water_consumption": monthly_domestic_hot_water_consumption.to_json()},
{"yearly_domestic_hot_water_consumption": yearly_domestic_hot_water_consumption.to_json()}
]}), city_object_id=db_building_id)
self.assertEqual(1, len(city_objects_id), 'wrong number of results')
self.assertIsNotNone(city_objects_id[0], 'city_object_id is None')
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)