From 169c79650e2af8f673b4880fd05a6b38c321bd83 Mon Sep 17 00:00:00 2001 From: Peter Yefi Date: Tue, 17 Jan 2023 19:53:46 -0500 Subject: [PATCH 1/2] Included message for wrong authentication --- persistence/repositories/user_repo.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/persistence/repositories/user_repo.py b/persistence/repositories/user_repo.py index 6abda5bf..585628a9 100644 --- a/persistence/repositories/user_repo.py +++ b/persistence/repositories/user_repo.py @@ -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}') From f1b16dde6d997a17b00ada680251a5c449648de5 Mon Sep 17 00:00:00 2001 From: Peter Yefi Date: Tue, 17 Jan 2023 20:04:56 -0500 Subject: [PATCH 2/2] Removed city object from db factory constructor --- imports/db_factory.py | 9 ++++----- unittests/test_db_factory.py | 16 ++++++++-------- unittests/test_heat_pump_simulation.py | 13 ++++++++++--- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/imports/db_factory.py b/imports/db_factory.py index b46845be..00f7e53f 100644 --- a/imports/db_factory.py +++ b/imports/db_factory.py @@ -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) - diff --git a/unittests/test_db_factory.py b/unittests/test_db_factory.py index de4cd2f2..09a062ab 100644 --- a/unittests/test_db_factory.py +++ b/unittests/test_db_factory.py @@ -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) diff --git a/unittests/test_heat_pump_simulation.py b/unittests/test_heat_pump_simulation.py index de47d227..1fa31e4f 100644 --- a/unittests/test_heat_pump_simulation.py +++ b/unittests/test_heat_pump_simulation.py @@ -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