Merge branch 'main' into reviewing_hub
This commit is contained in:
commit
83fc351fc7
@ -114,32 +114,36 @@ class DBControl:
|
|||||||
result_names = []
|
result_names = []
|
||||||
results = {}
|
results = {}
|
||||||
for scenario in request_values['scenarios']:
|
for scenario in request_values['scenarios']:
|
||||||
scenario_name = next(iter(scenario))
|
print('scenario', scenario, results)
|
||||||
result_sets = self._city.get_by_user_id_application_id_and_scenario(
|
for scenario_name in scenario.keys():
|
||||||
user_id,
|
print('scenario name', scenario_name)
|
||||||
application_id,
|
|
||||||
scenario_name
|
|
||||||
)
|
|
||||||
if result_sets is None:
|
|
||||||
continue
|
|
||||||
for result_set in result_sets:
|
|
||||||
city_id = result_set[0].id
|
|
||||||
|
|
||||||
results[scenario_name] = []
|
result_sets = self._city.get_by_user_id_application_id_and_scenario(
|
||||||
for building_name in scenario[scenario_name]:
|
user_id,
|
||||||
_building = self._city_object.get_by_name_or_alias_and_city(building_name, city_id)
|
application_id,
|
||||||
if _building is None:
|
scenario_name
|
||||||
continue
|
)
|
||||||
city_object_id = _building.id
|
if result_sets is None:
|
||||||
_ = self._simulation_results.get_simulation_results_by_city_id_city_object_id_and_names(
|
continue
|
||||||
city_id,
|
for result_set in result_sets:
|
||||||
city_object_id,
|
city_id = result_set[0].id
|
||||||
result_names)
|
|
||||||
|
|
||||||
for value in _:
|
results[scenario_name] = []
|
||||||
values = json.loads(value.values)
|
for building_name in scenario[scenario_name]:
|
||||||
values["building"] = building_name
|
_building = self._city_object.get_by_name_or_alias_and_city(building_name, city_id)
|
||||||
results[scenario_name].append(values)
|
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
|
return results
|
||||||
|
|
||||||
def persist_city(self, city: City, pickle_path, scenario, application_id: int, user_id: int):
|
def persist_city(self, city: City, pickle_path, scenario, application_id: int, user_id: int):
|
||||||
|
@ -9,6 +9,7 @@ import logging
|
|||||||
|
|
||||||
from sqlalchemy import select
|
from sqlalchemy import select
|
||||||
from sqlalchemy.exc import SQLAlchemyError
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from hub.city_model_structure.city import City as CityHub
|
from hub.city_model_structure.city import City as CityHub
|
||||||
from hub.persistence.repository import Repository
|
from hub.persistence.repository import Repository
|
||||||
@ -111,6 +112,8 @@ class City(Repository):
|
|||||||
Model.application_id == application_id,
|
Model.application_id == application_id,
|
||||||
Model.scenario == scenario
|
Model.scenario == scenario
|
||||||
)).all()
|
)).all()
|
||||||
|
self.session.close()
|
||||||
|
self.session = Session(self.engine)
|
||||||
return result_set
|
return result_set
|
||||||
except SQLAlchemyError as err:
|
except SQLAlchemyError as err:
|
||||||
logging.error('Error while fetching city by name %s', err)
|
logging.error('Error while fetching city by name %s', err)
|
||||||
|
@ -9,6 +9,7 @@ import logging
|
|||||||
|
|
||||||
from sqlalchemy import select, or_
|
from sqlalchemy import select, or_
|
||||||
from sqlalchemy.exc import SQLAlchemyError
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from hub.city_model_structure.building import Building
|
from hub.city_model_structure.building import Building
|
||||||
from hub.persistence.repository import Repository
|
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()
|
city_object = self.session.execute(select(Model).where(Model.name == name, Model.city_id == city_id)).first()
|
||||||
if city_object is not None:
|
if city_object is not None:
|
||||||
return city_object[0]
|
return city_object[0]
|
||||||
|
# name not found, so search by alias instead
|
||||||
city_objects = self.session.execute(
|
city_objects = self.session.execute(
|
||||||
select(Model).where(Model.aliases.contains(name), Model.city_id == city_id)
|
select(Model).where(Model.aliases.contains(name), Model.city_id == city_id)
|
||||||
).all()
|
).all()
|
||||||
# name not found, so search by alias instead
|
self.session.close()
|
||||||
|
self.session = Session(self.engine)
|
||||||
for city_object in city_objects:
|
for city_object in city_objects:
|
||||||
aliases = city_object[0].aliases.replace('{', '').replace('}', '').split(',')
|
aliases = city_object[0].aliases.replace('{', '').replace('}', '').split(',')
|
||||||
for alias in aliases:
|
for alias in aliases:
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
"""
|
"""
|
||||||
Hub version number
|
Hub version number
|
||||||
"""
|
"""
|
||||||
__version__ = '0.1.7.29'
|
__version__ = '0.1.8.2'
|
||||||
|
@ -52,7 +52,7 @@ class Control:
|
|||||||
self._skip_reason = f'.env file missing at {dotenv_path}'
|
self._skip_reason = f'.env file missing at {dotenv_path}'
|
||||||
return
|
return
|
||||||
dotenv_path = str(dotenv_path)
|
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)
|
engine = create_engine(repository.configuration.connection_string)
|
||||||
try:
|
try:
|
||||||
# delete test database if it exists
|
# delete test database if it exists
|
||||||
@ -285,15 +285,12 @@ TestDBFactory
|
|||||||
results, city_object_id=db_building_id)
|
results, city_object_id=db_building_id)
|
||||||
self.assertEqual(17, len(city_objects_id), 'wrong number of results')
|
self.assertEqual(17, len(city_objects_id), 'wrong number of results')
|
||||||
self.assertIsNotNone(city_objects_id[0], 'city_object_id is None')
|
self.assertIsNotNone(city_objects_id[0], 'city_object_id is None')
|
||||||
"""
|
|
||||||
for _id in city_objects_id:
|
for _id in city_objects_id:
|
||||||
control.database.delete_results_by_name('insel meb', city_object_id=_id)
|
control.database.delete_results_by_name('insel meb', city_object_id=_id)
|
||||||
control.database.delete_city(city_id)
|
control.database.delete_city(city_id)
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@unittest.skipIf(control.skip_test, control.skip_reason)
|
@unittest.skipIf(control.skip_test, control.skip_reason)
|
||||||
def tearDownClass(cls):
|
def tearDownClass(cls):
|
||||||
control.database.delete_application(control.application_uuid)
|
control.database.delete_application(control.application_uuid)
|
||||||
control.database.delete_user(control.user_id)
|
control.database.delete_user(control.user_id)
|
||||||
"""
|
|
152
tests/test_db_retrieve.py
Normal file
152
tests/test_db_retrieve.py
Normal file
@ -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)
|
Loading…
Reference in New Issue
Block a user