Removed city object from db factory constructor

This commit is contained in:
Peter Yefi 2023-01-17 20:04:56 -05:00
parent 169c79650e
commit f1b16dde6d
3 changed files with 22 additions and 16 deletions

View File

@ -7,6 +7,7 @@ Project CoderPeter Yefi peteryefi@gmail.com
from persistence import CityRepo from persistence import CityRepo
from persistence import HeatPumpSimulationRepo from persistence import HeatPumpSimulationRepo
from typing import Dict from typing import Dict
from city_model_structure.city import City
class DBFactory: class DBFactory:
@ -14,16 +15,15 @@ class DBFactory:
DBFactory class DBFactory class
""" """
def __init__(self, city, db_name, dotenv_path, app_env): def __init__(self, db_name, dotenv_path, app_env):
self._city = city
self._city_repo = CityRepo(db_name=db_name, dotenv_path=dotenv_path, app_env=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) 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 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): 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 :param hp_sim_id: the id of the heat pump simulation to get
""" """
self._hp_simulation_repo.delete_hp_simulation(hp_sim_id) self._hp_simulation_repo.delete_hp_simulation(hp_sim_id)

View File

@ -50,13 +50,13 @@ class TestDBFactory(TestCase):
city_file = "../unittests/tests_data/C40_Final.gml" city_file = "../unittests/tests_data/C40_Final.gml"
cls.city = GeometryFactory('citygml', city_file).city 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') 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') 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) cls._user = user_factory.create_user("Admin", "admin@hub.com", "Admin@123", UserRoles.Admin)
def test_save_city(self): 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') self.assertEqual(saved_city.name, 'Montréal')
pickled_city = loads(saved_city.city) pickled_city = loads(saved_city.city)
self.assertEqual(len(pickled_city.buildings), 10) self.assertEqual(len(pickled_city.buildings), 10)
@ -64,33 +64,33 @@ class TestDBFactory(TestCase):
self._db_factory.delete_city(saved_city.id) self._db_factory.delete_city(saved_city.id)
def test_save_same_city_with_same_hub_version(self): def test_save_same_city_with_same_hub_version(self):
first_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) 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(second_city['message'], f'Same version of {self.city.name} exist')
self.assertEqual(first_city.name, 'Montréal') self.assertEqual(first_city.name, 'Montréal')
self.assertEqual(first_city.country_code, 'ca') self.assertEqual(first_city.country_code, 'ca')
self._db_factory.delete_city(first_city.id) self._db_factory.delete_city(first_city.id)
def test_get_city_by_name(self): 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) retrieved_city = self._export_db_factory.get_city_by_name(city.name)
self.assertEqual(retrieved_city[0].lower_corner[0], 610610.7547462888) self.assertEqual(retrieved_city[0].lower_corner[0], 610610.7547462888)
self._db_factory.delete_city(city.id) self._db_factory.delete_city(city.id)
def test_get_city_by_user(self): 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) retrieved_city = self._export_db_factory.get_city_by_user(self._user.id)
self.assertEqual(retrieved_city[0].user_id, self._user.id) self.assertEqual(retrieved_city[0].user_id, self._user.id)
self._db_factory.delete_city(city.id) self._db_factory.delete_city(city.id)
def test_get_city_by_id(self): 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) retrieved_city = self._export_db_factory.get_city(city.id)
self.assertEqual(retrieved_city.upper_corner[0], 610818.6731258357) self.assertEqual(retrieved_city.upper_corner[0], 610818.6731258357)
self._db_factory.delete_city(city.id) self._db_factory.delete_city(city.id)
def test_get_update_city(self): 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.longitude = 1.43589
self.city.latitude = -9.38928339 self.city.latitude = -9.38928339
self._db_factory.update_city(city.id, self.city) self._db_factory.update_city(city.id, self.city)

View File

@ -16,7 +16,10 @@ from persistence.models import City
from persistence.models import SimulationTypes from persistence.models import SimulationTypes
from persistence.models import HeatPumpTypes from persistence.models import HeatPumpTypes
from persistence.models import HeatPumpSimulation from persistence.models import HeatPumpSimulation
from persistence.models import User
from sqlalchemy.exc import ProgrammingError from sqlalchemy.exc import ProgrammingError
from imports.user_factory import UserFactory
from persistence.models import UserRoles
# User defined paramenters # User defined paramenters
hp_sim_data = { hp_sim_data = {
@ -61,15 +64,19 @@ class TestHeatPumpSimulation(TestCase):
cnn.close() cnn.close()
# Create test tables if they do not exit # Create test tables if they do not exit
User.__table__.create(bind=repo.engine, checkfirst=True)
City.__table__.create(bind=repo.engine, checkfirst=True) City.__table__.create(bind=repo.engine, checkfirst=True)
HeatPumpSimulation.__table__.create(bind=repo.engine, checkfirst=True) HeatPumpSimulation.__table__.create(bind=repo.engine, checkfirst=True)
city_file = "../unittests/tests_data/C40_Final.gml" city_file = "../unittests/tests_data/C40_Final.gml"
cls._city = GeometryFactory('citygml', city_file).city cls._city = GeometryFactory('citygml', city_file).city
EnergySystemsFactory('air source hp', cls._city).enrich() 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') 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): def test_heat_pump_simulation_persistence(self):
output = EnergySystemsExportFactory(city=self._city, user_input=hp_sim_data, hp_model='018', 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["DailyFossilFuelConsumption"] = output["daily_fossil_consumption"]
hp_sim_data["MonthlyFossilFuelConsumption"] = output["monthly_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) 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.heat_pump_type, HeatPumpTypes.Air)
self.assertEqual(hp_sim.simulation_type, SimulationTypes.Parallel) 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["DailyFossilFuelConsumption"] = output["daily_fossil_consumption"]
hp_sim_data["MonthlyFossilFuelConsumption"] = output["monthly_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) self._db_factory.persist_hp_simulation(hp_sim_data, saved_city.id)
# retrieved saved simulation by city id # retrieved saved simulation by city id