forked from s_ranjbar/city_retrofit
Merge branch 'db_persistence' into 'master'
Db persistence See merge request Guille/hub!47
This commit is contained in:
commit
ddf8e6dcfe
|
@ -7,6 +7,7 @@ Project CoderPeter Yefi peteryefi@gmail.com
|
|||
from persistence import CityRepo
|
||||
from persistence import HeatPumpSimulationRepo
|
||||
from typing import Dict
|
||||
from city_model_structure.city import City
|
||||
|
||||
|
||||
class DBFactory:
|
||||
|
@ -14,16 +15,15 @@ class DBFactory:
|
|||
DBFactory class
|
||||
"""
|
||||
|
||||
def __init__(self, city, db_name, dotenv_path, app_env):
|
||||
self._city = city
|
||||
def __init__(self, db_name, dotenv_path, app_env):
|
||||
self._city_repo = CityRepo(db_name=db_name, dotenv_path=dotenv_path, app_env=app_env)
|
||||
self._hp_simulation_repo = HeatPumpSimulationRepo(db_name=db_name, dotenv_path=dotenv_path, app_env=app_env)
|
||||
|
||||
def persist_city(self, user_id: int):
|
||||
def persist_city(self, user_id: int, city: City):
|
||||
"""
|
||||
Persist city into postgres database
|
||||
"""
|
||||
return self._city_repo.insert(self._city, user_id)
|
||||
return self._city_repo.insert(city, user_id)
|
||||
|
||||
def update_city(self, city_id, city):
|
||||
"""
|
||||
|
@ -55,4 +55,3 @@ class DBFactory:
|
|||
:param hp_sim_id: the id of the heat pump simulation to get
|
||||
"""
|
||||
self._hp_simulation_repo.delete_hp_simulation(hp_sim_id)
|
||||
|
||||
|
|
|
@ -100,6 +100,8 @@ class UserRepo(BaseRepo):
|
|||
if user:
|
||||
if Auth.check_password(password, user[0].password):
|
||||
return user
|
||||
else:
|
||||
return {'message': 'Wrong email/password combination'}
|
||||
return {'message': 'user not found'}
|
||||
except SQLAlchemyError as err:
|
||||
logger.error(f'Error while fetching user by email: {err}')
|
||||
|
|
|
@ -50,13 +50,13 @@ class TestDBFactory(TestCase):
|
|||
|
||||
city_file = "../unittests/tests_data/C40_Final.gml"
|
||||
cls.city = GeometryFactory('citygml', city_file).city
|
||||
cls._db_factory = DBFactory(city=cls.city, db_name='test_db', app_env='TEST', dotenv_path='../.env')
|
||||
cls._db_factory = DBFactory(db_name='test_db', app_env='TEST', dotenv_path='../.env')
|
||||
cls._export_db_factory = ExportDBFactory(db_name='test_db', app_env='TEST', dotenv_path='../.env')
|
||||
user_factory = UserFactory(db_name='test_db', app_env='TEST', dotenv_path='../.env')
|
||||
cls._user = user_factory.create_user("Admin", "admin@hub.com", "Admin@123", UserRoles.Admin)
|
||||
|
||||
def test_save_city(self):
|
||||
saved_city = self._db_factory.persist_city(self._user.id)
|
||||
saved_city = self._db_factory.persist_city(self._user.id, self.city)
|
||||
self.assertEqual(saved_city.name, 'Montréal')
|
||||
pickled_city = loads(saved_city.city)
|
||||
self.assertEqual(len(pickled_city.buildings), 10)
|
||||
|
@ -64,33 +64,33 @@ class TestDBFactory(TestCase):
|
|||
self._db_factory.delete_city(saved_city.id)
|
||||
|
||||
def test_save_same_city_with_same_hub_version(self):
|
||||
first_city = self._db_factory.persist_city(self._user.id)
|
||||
second_city = self._db_factory.persist_city(self._user.id)
|
||||
first_city = self._db_factory.persist_city(self._user.id, self.city)
|
||||
second_city = self._db_factory.persist_city(self._user.id, self.city)
|
||||
self.assertEqual(second_city['message'], f'Same version of {self.city.name} exist')
|
||||
self.assertEqual(first_city.name, 'Montréal')
|
||||
self.assertEqual(first_city.country_code, 'ca')
|
||||
self._db_factory.delete_city(first_city.id)
|
||||
|
||||
def test_get_city_by_name(self):
|
||||
city = self._db_factory.persist_city(self._user.id)
|
||||
city = self._db_factory.persist_city(self._user.id, self.city)
|
||||
retrieved_city = self._export_db_factory.get_city_by_name(city.name)
|
||||
self.assertEqual(retrieved_city[0].lower_corner[0], 610610.7547462888)
|
||||
self._db_factory.delete_city(city.id)
|
||||
|
||||
def test_get_city_by_user(self):
|
||||
city = self._db_factory.persist_city(self._user.id)
|
||||
city = self._db_factory.persist_city(self._user.id, self.city)
|
||||
retrieved_city = self._export_db_factory.get_city_by_user(self._user.id)
|
||||
self.assertEqual(retrieved_city[0].user_id, self._user.id)
|
||||
self._db_factory.delete_city(city.id)
|
||||
|
||||
def test_get_city_by_id(self):
|
||||
city = self._db_factory.persist_city(self._user.id)
|
||||
city = self._db_factory.persist_city(self._user.id, self.city)
|
||||
retrieved_city = self._export_db_factory.get_city(city.id)
|
||||
self.assertEqual(retrieved_city.upper_corner[0], 610818.6731258357)
|
||||
self._db_factory.delete_city(city.id)
|
||||
|
||||
def test_get_update_city(self):
|
||||
city = self._db_factory.persist_city(self._user.id)
|
||||
city = self._db_factory.persist_city(self._user.id, self.city)
|
||||
self.city.longitude = 1.43589
|
||||
self.city.latitude = -9.38928339
|
||||
self._db_factory.update_city(city.id, self.city)
|
||||
|
|
|
@ -16,7 +16,10 @@ from persistence.models import City
|
|||
from persistence.models import SimulationTypes
|
||||
from persistence.models import HeatPumpTypes
|
||||
from persistence.models import HeatPumpSimulation
|
||||
from persistence.models import User
|
||||
from sqlalchemy.exc import ProgrammingError
|
||||
from imports.user_factory import UserFactory
|
||||
from persistence.models import UserRoles
|
||||
|
||||
# User defined paramenters
|
||||
hp_sim_data = {
|
||||
|
@ -61,15 +64,19 @@ class TestHeatPumpSimulation(TestCase):
|
|||
cnn.close()
|
||||
|
||||
# Create test tables if they do not exit
|
||||
User.__table__.create(bind=repo.engine, checkfirst=True)
|
||||
City.__table__.create(bind=repo.engine, checkfirst=True)
|
||||
HeatPumpSimulation.__table__.create(bind=repo.engine, checkfirst=True)
|
||||
|
||||
|
||||
city_file = "../unittests/tests_data/C40_Final.gml"
|
||||
cls._city = GeometryFactory('citygml', city_file).city
|
||||
EnergySystemsFactory('air source hp', cls._city).enrich()
|
||||
|
||||
cls._db_factory = DBFactory(city=cls._city, db_name='test_db', app_env='TEST', dotenv_path='../.env')
|
||||
cls._db_factory = DBFactory(db_name='test_db', app_env='TEST', dotenv_path='../.env')
|
||||
cls._export_db_factory = ExportDBFactory(db_name='test_db', app_env='TEST', dotenv_path='../.env')
|
||||
user_factory = UserFactory(db_name='test_db', app_env='TEST', dotenv_path='../.env')
|
||||
cls._user = user_factory.create_user("Admin", "admin@hub.com", "Admin@123", UserRoles.Admin)
|
||||
|
||||
def test_heat_pump_simulation_persistence(self):
|
||||
output = EnergySystemsExportFactory(city=self._city, user_input=hp_sim_data, hp_model='018',
|
||||
|
@ -83,7 +90,7 @@ class TestHeatPumpSimulation(TestCase):
|
|||
hp_sim_data["DailyFossilFuelConsumption"] = output["daily_fossil_consumption"]
|
||||
hp_sim_data["MonthlyFossilFuelConsumption"] = output["monthly_fossil_consumption"]
|
||||
|
||||
saved_city = self._db_factory.persist_city()
|
||||
saved_city = self._db_factory.persist_city(self._user.id, self._city)
|
||||
hp_sim = self._db_factory.persist_hp_simulation(hp_sim_data, saved_city.id)
|
||||
self.assertEqual(hp_sim.heat_pump_type, HeatPumpTypes.Air)
|
||||
self.assertEqual(hp_sim.simulation_type, SimulationTypes.Parallel)
|
||||
|
@ -104,7 +111,7 @@ class TestHeatPumpSimulation(TestCase):
|
|||
hp_sim_data["DailyFossilFuelConsumption"] = output["daily_fossil_consumption"]
|
||||
hp_sim_data["MonthlyFossilFuelConsumption"] = output["monthly_fossil_consumption"]
|
||||
|
||||
saved_city = self._db_factory.persist_city()
|
||||
saved_city = self._db_factory.persist_city(self._user.id, self._city)
|
||||
self._db_factory.persist_hp_simulation(hp_sim_data, saved_city.id)
|
||||
|
||||
# retrieved saved simulation by city id
|
||||
|
|
Loading…
Reference in New Issue
Block a user