From 9f24ed8fae1abe3437c4ef9bc39a624e535d6162 Mon Sep 17 00:00:00 2001 From: Guille Gutierrez Date: Wed, 2 Aug 2023 15:49:54 -0400 Subject: [PATCH 1/6] Update hub/version.py --- hub/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hub/version.py b/hub/version.py index 258ed666..3d51aa9c 100644 --- a/hub/version.py +++ b/hub/version.py @@ -1,4 +1,4 @@ """ Hub version number """ -__version__ = '0.1.7.29' +__version__ = '0.1.8.0' From 75848ef250a6cc2afecd4cc2c0fcfe8f03b2130b Mon Sep 17 00:00:00 2001 From: Guille Date: Wed, 2 Aug 2023 15:55:11 -0400 Subject: [PATCH 2/6] Correct bd test, add dictionary for custom fuel to hub fuel --- tests/test_db_factory.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/test_db_factory.py b/tests/test_db_factory.py index fb11fea8..6b5d0622 100644 --- a/tests/test_db_factory.py +++ b/tests/test_db_factory.py @@ -52,7 +52,7 @@ class Control: self._skip_reason = f'.env file missing at {dotenv_path}' return dotenv_path = str(dotenv_path) - repository = Repository(db_name='montreal_retrofit_test', app_env='TEST', dotenv_path=dotenv_path) + repository = Repository(db_name='test_db', app_env='TEST', dotenv_path=dotenv_path) engine = create_engine(repository.configuration.connection_string) try: # delete test database if it exists @@ -285,15 +285,12 @@ TestDBFactory 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') - """ 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) - """ \ No newline at end of file From 9365b47dc2086b47b983731584a45c2a5e10e439 Mon Sep 17 00:00:00 2001 From: Guille Date: Thu, 3 Aug 2023 12:06:45 -0400 Subject: [PATCH 3/6] Add retrieve result test and correct lock in sqlalchemy --- hub/persistence/db_control.py | 52 +++---- hub/persistence/repositories/city.py | 5 + hub/persistence/repositories/city_object.py | 5 +- tests/test_db_retrieve.py | 152 ++++++++++++++++++++ 4 files changed, 189 insertions(+), 25 deletions(-) create mode 100644 tests/test_db_retrieve.py diff --git a/hub/persistence/db_control.py b/hub/persistence/db_control.py index 8e6bb999..5bc7df35 100644 --- a/hub/persistence/db_control.py +++ b/hub/persistence/db_control.py @@ -114,32 +114,36 @@ class DBControl: result_names = [] results = {} for scenario in request_values['scenarios']: - scenario_name = next(iter(scenario)) - result_sets = self._city.get_by_user_id_application_id_and_scenario( - user_id, - application_id, - scenario_name - ) - if result_sets is None: - continue - for result_set in result_sets: - city_id = result_set[0].id + print('scenario', scenario, results) + for scenario_name in scenario.keys(): + print('scenario name', scenario_name) - results[scenario_name] = [] - for building_name in scenario[scenario_name]: - _building = self._city_object.get_by_name_or_alias_and_city(building_name, city_id) - if _building is None: - continue - city_object_id = _building.id - _ = self._simulation_results.get_simulation_results_by_city_id_city_object_id_and_names( - city_id, - city_object_id, - result_names) + result_sets = self._city.get_by_user_id_application_id_and_scenario( + user_id, + application_id, + scenario_name + ) + if result_sets is None: + continue + for result_set in result_sets: + city_id = result_set[0].id - for value in _: - values = json.loads(value.values) - values["building"] = building_name - results[scenario_name].append(values) + results[scenario_name] = [] + for building_name in scenario[scenario_name]: + _building = self._city_object.get_by_name_or_alias_and_city(building_name, city_id) + if _building is None: + continue + city_object_id = _building.id + _ = self._simulation_results.get_simulation_results_by_city_id_city_object_id_and_names( + city_id, + city_object_id, + result_names) + + for value in _: + values = json.loads(value.values) + values["building"] = building_name + results[scenario_name].append(values) + print(scenario, results) return results def persist_city(self, city: City, pickle_path, scenario, application_id: int, user_id: int): diff --git a/hub/persistence/repositories/city.py b/hub/persistence/repositories/city.py index 29048442..bef0aa55 100644 --- a/hub/persistence/repositories/city.py +++ b/hub/persistence/repositories/city.py @@ -9,6 +9,7 @@ import logging from sqlalchemy import select from sqlalchemy.exc import SQLAlchemyError +from sqlalchemy.orm import Session from hub.city_model_structure.city import City as CityHub from hub.persistence.repository import Repository @@ -111,6 +112,8 @@ class City(Repository): Model.application_id == application_id, Model.scenario == scenario )).all() + self.session.close() + self.session = Session(self.engine) return result_set except SQLAlchemyError as err: logging.error('Error while fetching city by name %s', err) @@ -127,6 +130,8 @@ class City(Repository): result_set = self.session.execute( select(Model).where(Model.user_id == user_id, Model.application_id == application_id) ) + self.session.close() + self.session = Session(self.engine) return [r[0] for r in result_set] except SQLAlchemyError as err: logging.error('Error while fetching city by name %s', err) diff --git a/hub/persistence/repositories/city_object.py b/hub/persistence/repositories/city_object.py index 6b82c6e4..aa69a0e3 100644 --- a/hub/persistence/repositories/city_object.py +++ b/hub/persistence/repositories/city_object.py @@ -9,6 +9,7 @@ import logging from sqlalchemy import select, or_ from sqlalchemy.exc import SQLAlchemyError +from sqlalchemy.orm import Session from hub.city_model_structure.building import Building from hub.persistence.repository import Repository @@ -108,10 +109,12 @@ class CityObject(Repository): city_object = self.session.execute(select(Model).where(Model.name == name, Model.city_id == city_id)).first() if city_object is not None: return city_object[0] + # name not found, so search by alias instead city_objects = self.session.execute( select(Model).where(Model.aliases.contains(name), Model.city_id == city_id) ).all() - # name not found, so search by alias instead + self.session.close() + self.session = Session(self.engine) for city_object in city_objects: aliases = city_object[0].aliases.replace('{', '').replace('}', '').split(',') for alias in aliases: diff --git a/tests/test_db_retrieve.py b/tests/test_db_retrieve.py new file mode 100644 index 00000000..bb7532c3 --- /dev/null +++ b/tests/test_db_retrieve.py @@ -0,0 +1,152 @@ +""" +Test db factory +SPDX - License - Identifier: LGPL - 3.0 - or -later +Copyright © 2022 Concordia CERC group +Project Coder Peter Yefi peteryefi@gmail.com +""" +import distutils.spawn +import glob +import json +import logging +import os +import subprocess +import unittest +from pathlib import Path +from unittest import TestCase + +import sqlalchemy.exc +from sqlalchemy import create_engine +from sqlalchemy.exc import ProgrammingError + +import hub.helpers.constants as cte +from hub.exports.energy_building_exports_factory import EnergyBuildingsExportsFactory +from hub.exports.exports_factory import ExportsFactory +from hub.helpers.data.montreal_function_to_hub_function import MontrealFunctionToHubFunction +from hub.imports.construction_factory import ConstructionFactory +from hub.imports.energy_systems_factory import EnergySystemsFactory +from hub.imports.geometry_factory import GeometryFactory +from hub.imports.results_factory import ResultFactory +from hub.imports.usage_factory import UsageFactory +from hub.imports.weather_factory import WeatherFactory +from hub.persistence.db_control import DBControl +from hub.persistence.models import City, Application, CityObject, SimulationResults +from hub.persistence.models import User, UserRoles +from hub.persistence.repository import Repository + + +class Control: + _skip_test = False + _skip_reason = 'PostgreSQL not properly installed in host machine' + + def __init__(self): + """ + Test + setup + :return: None + """ + self._skip_test = False + # 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='montreal_retrofit_test', app_env='TEST', dotenv_path=dotenv_path) + engine = create_engine(repository.configuration.connection_string) + try: + # delete test database if it exists + connection = engine.connect() + connection.close() + except ProgrammingError: + logging.info('Database does not exist. Nothing to delete') + except sqlalchemy.exc.OperationalError as operational_error: + self._skip_test = True + self._skip_reason = f'{operational_error}' + return + + self._database = DBControl( + db_name=repository.configuration.db_name, + app_env='TEST', + dotenv_path=dotenv_path) + + self._application_uuid = '60b7fc1b-f389-4254-9ffd-22a4cf32c7a3' + self._application_id = 1 + self._user_id = 1 + self._pickle_path = 'tests_data/pickle_path.bz2' + + @property + def database(self): + return self._database + + @property + def application_uuid(self): + return self._application_uuid + + @property + def application_id(self): + return self._application_id + + @property + def user_id(self): + return self._user_id + + @property + def skip_test(self): + return self._skip_test + + @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 + + @property + def skip_reason(self): + return self._skip_reason + + @property + def message(self): + return self._skip_reason + + @property + def pickle_path(self): + return self._pickle_path + + +control = Control() + + +class TestDBFactory(TestCase): + """ +TestDBFactory +""" + + @unittest.skipIf(control.skip_test, control.skip_reason) + def test_retrieve_results(self): + request_values = { + "scenarios": [ + { + "current status": ["01002777", "01002773", "01036804"] + }, + { + "skin retrofit": ["01002777", "01002773", "01036804"] + }, + { + "system retrofit and pv": ["01002777", "01002773", "01036804"] + }, + { + "skin and system retrofit with pv": ["01002777", "01002773", "01036804"] + } + + + ] + } + results = control.database.results(control.user_id, control.application_id, request_values) + print(results) From 4bbd65bb414ed6fb5d74821320d6abf1a24054ae Mon Sep 17 00:00:00 2001 From: Guille Gutierrez Date: Thu, 3 Aug 2023 12:07:37 -0400 Subject: [PATCH 4/6] Update hub/version.py --- hub/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hub/version.py b/hub/version.py index 3d51aa9c..3ee4b9e5 100644 --- a/hub/version.py +++ b/hub/version.py @@ -1,4 +1,4 @@ """ Hub version number """ -__version__ = '0.1.8.0' +__version__ = '0.1.8.1' From 1691ada01cb58f7666e1f4a3d300e2aeb15751bc Mon Sep 17 00:00:00 2001 From: Guille Date: Thu, 3 Aug 2023 12:21:40 -0400 Subject: [PATCH 5/6] Add retrieve result test and correct lock in sqlalchemy --- hub/persistence/repositories/city.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/hub/persistence/repositories/city.py b/hub/persistence/repositories/city.py index bef0aa55..9c20346f 100644 --- a/hub/persistence/repositories/city.py +++ b/hub/persistence/repositories/city.py @@ -130,8 +130,6 @@ class City(Repository): result_set = self.session.execute( select(Model).where(Model.user_id == user_id, Model.application_id == application_id) ) - self.session.close() - self.session = Session(self.engine) return [r[0] for r in result_set] except SQLAlchemyError as err: logging.error('Error while fetching city by name %s', err) From e138a24555648700a00771f4cb8e46a57519ebc4 Mon Sep 17 00:00:00 2001 From: Guille Gutierrez Date: Thu, 3 Aug 2023 12:22:57 -0400 Subject: [PATCH 6/6] Update hub/version.py --- hub/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hub/version.py b/hub/version.py index 3ee4b9e5..c7d736f2 100644 --- a/hub/version.py +++ b/hub/version.py @@ -1,4 +1,4 @@ """ Hub version number """ -__version__ = '0.1.8.1' +__version__ = '0.1.8.2'