diff --git a/hub/DEPLOYMENT.md b/hub/DEPLOYMENT.md index 422b1aa7..e93fdb83 100644 --- a/hub/DEPLOYMENT.md +++ b/hub/DEPLOYMENT.md @@ -58,7 +58,7 @@ section in persistence/README.md file. as shown below: ```python -from hub.exports.db_factory import DBFactory +from hub.persistence.db_control import DBFactory from pathlib import Path dotenv_path = (Path(__file__).parent / '.env').resolve() diff --git a/hub/__init__.py b/hub/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/hub/imports/db_factory.py b/hub/imports/db_factory.py deleted file mode 100644 index e58ce073..00000000 --- a/hub/imports/db_factory.py +++ /dev/null @@ -1,81 +0,0 @@ -""" -DBFactory performs database create, delete and update operations -SPDX - License - Identifier: LGPL - 3.0 - or -later -Copyright © 2022 Concordia CERC group -Project CoderPeter Yefi peteryefi@gmail.com -""" -from hub.city_model_structure.city import City -from hub.persistence import City as CityRepository -from hub.persistence import SimulationResults -from hub.persistence import Application - - -class DBFactory: - """ - DBFactory class - """ - - def __init__(self, db_name, dotenv_path, app_env): - self._city_repository = CityRepository(db_name=db_name, dotenv_path=dotenv_path, app_env=app_env) - self._simulation_results = SimulationResults(db_name=db_name, dotenv_path=dotenv_path, app_env=app_env) - self._application = Application(db_name=db_name, dotenv_path=dotenv_path, app_env=app_env) - - def persist_city(self, city: City, pickle_path, application_id: int, user_id: int): - """ - Persist city into postgres database - :param city: City to be stored - :param pickle_path: Path to save the pickle file - :param application_id: Application id owning this city - :param user_id: User who create the city - """ - return self._city_repository.insert(city, pickle_path, application_id, user_id) - - def update_city(self, city_id, city): - """ - Update an existing city in postgres database - :param city_id: the id of the city to update - :param city: the updated city object - """ - return self._city_repository.update(city_id, city) - - def persist_application(self, name: str, description: str, application_uuid: str): - """ - Creates an application - :param name: name of application - :param description: the description of the application - :param application_uuid: the uuid of the application to be created - """ - return self._application.insert(name, description, application_uuid) - - def update_application(self, name: str, description: str, application_uuid: str): - """ - Update an application - :param name: name of application - :param description: the description of the application - :param application_uuid: the uuid of the application to be created - """ - return self._application.update(application_uuid, name, description) - - def delete_city(self, city_id): - """ - Deletes a single city from postgres - :param city_id: the id of the city to get - """ - self._city_repository.delete(city_id) - - def delete_application(self, application_uuid): - """ - Deletes a single application from postgres - :param application_uuid: the id of the application to get - """ - self._application.delete(application_uuid) - - def add_simulation_results(self, name, values, city_id=None, city_object_id=None): - """ - Add simulation results to the city or to the city_object - :param name: simulation and simulation engine name - :param values: simulation values in json format - :param city_id: city id or None - :param city_object_id: city object id or None - """ - self._simulation_results.insert(name, values, city_id, city_object_id) diff --git a/hub/imports/user_factory.py b/hub/imports/user_factory.py deleted file mode 100644 index ebd00087..00000000 --- a/hub/imports/user_factory.py +++ /dev/null @@ -1,46 +0,0 @@ -""" -User performs user-related crud operations -SPDX - License - Identifier: LGPL - 3.0 - or -later -Copyright © 2022 Concordia CERC group -Project CoderPeter Yefi peteryefi@gmail.com -""" - -from hub.persistence import User -from hub.persistence import UserRoles - - -class UserFactory: - """ - UserFactory class - """ - - def __init__(self, db_name, app_env, dotenv_path): - self._user_repo = User(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path) - - def create_user(self, name: str, application_id: int, password: str, role: UserRoles): - """ - Creates a new user - :param name: the name of the user - :param application_id: the application id of the user - :param password: the password of the user - :param role: the role of the user - """ - return self._user_repo.insert(name, password, role, application_id) - - def update_user(self, user_id: int, name: str, password: str, role: UserRoles): - """ - Creates a new user - :param user_id: the id of the user - :param name: the name of the user - :param email: the email of the user - :param password: the password of the user - :param role: the role of the user - """ - return self._user_repo.update(user_id, name, password, role) - - def delete_user(self, user_id): - """ - Retrieve a single user - :param user_id: the id of the user to delete - """ - return self._user_repo.delete(user_id) diff --git a/hub/exports/db_factory.py b/hub/persistence/db_control.py similarity index 50% rename from hub/exports/db_factory.py rename to hub/persistence/db_control.py index 959d1906..06471fde 100644 --- a/hub/exports/db_factory.py +++ b/hub/persistence/db_control.py @@ -7,20 +7,22 @@ Project CoderPeter Yefi peteryefi@gmail.com import json from typing import Union, Dict -from hub.persistence import City +from hub.city_model_structure.city import City from hub.persistence import Application -from hub.persistence import User +from hub.persistence import City as CityRepository from hub.persistence import CityObject from hub.persistence import SimulationResults +from hub.persistence import User +from hub.persistence import UserRoles -class DBFactory: +class DBControl: """ DBFactory class """ def __init__(self, db_name, app_env, dotenv_path): - self._city = City(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path) + self._city_repository = CityRepository(db_name=db_name, dotenv_path=dotenv_path, app_env=app_env) self._application = Application(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path) self._user = User(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path) self._city_object = CityObject(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path) @@ -61,7 +63,7 @@ class DBFactory: :param application_id: Application id :return: [City] """ - return self._city.get_by_user_id_and_application_id(user_id, application_id) + return self._city_repository.get_by_user_id_and_application_id(user_id, application_id) def building_info(self, name, city_id) -> Union[CityObject, None]: """ @@ -85,7 +87,7 @@ class DBFactory: results = {} for city in cities['cities']: city_name = next(iter(city)) - result_set = self._city.get_by_user_id_application_id_and_name(user_id, application_id, city_name) + result_set = self._city_repository.get_by_user_id_application_id_and_name(user_id, application_id, city_name) if result_set is None: continue city_id = result_set.id @@ -104,3 +106,92 @@ class DBFactory: values["building"] = building_name results[city_name].append(values) return results + + def persist_city(self, city: City, pickle_path, application_id: int, user_id: int): + """ + Persist city into postgres database + :param city: City to be stored + :param pickle_path: Path to save the pickle file + :param application_id: Application id owning this city + :param user_id: User who create the city + """ + return self._city_repository.insert(city, pickle_path, application_id, user_id) + + def update_city(self, city_id, city): + """ + Update an existing city in postgres database + :param city_id: the id of the city to update + :param city: the updated city object + """ + return self._city_repository.update(city_id, city) + + def persist_application(self, name: str, description: str, application_uuid: str): + """ + Creates an application + :param name: name of application + :param description: the description of the application + :param application_uuid: the uuid of the application to be created + """ + return self._application.insert(name, description, application_uuid) + + def update_application(self, name: str, description: str, application_uuid: str): + """ + Update an application + :param name: name of application + :param description: the description of the application + :param application_uuid: the uuid of the application to be created + """ + return self._application.update(application_uuid, name, description) + + def delete_city(self, city_id): + """ + Deletes a single city from postgres + :param city_id: the id of the city to get + """ + self._city_repository.delete(city_id) + + def delete_application(self, application_uuid): + """ + Deletes a single application from postgres + :param application_uuid: the id of the application to get + """ + self._application.delete(application_uuid) + + def add_simulation_results(self, name, values, city_id=None, city_object_id=None): + """ + Add simulation results to the city or to the city_object + :param name: simulation and simulation engine name + :param values: simulation values in json format + :param city_id: city id or None + :param city_object_id: city object id or None + """ + self._simulation_results.insert(name, values, city_id, city_object_id) + + def create_user(self, name: str, application_id: int, password: str, role: UserRoles): + """ + Creates a new user + :param name: the name of the user + :param application_id: the application id of the user + :param password: the password of the user + :param role: the role of the user + """ + return self._user.insert(name, password, role, application_id) + + def update_user(self, user_id: int, name: str, password: str, role: UserRoles): + """ + Creates a new user + :param user_id: the id of the user + :param name: the name of the user + :param email: the email of the user + :param password: the password of the user + :param role: the role of the user + """ + return self._user.update(user_id, name, password, role) + + def delete_user(self, user_id): + """ + Retrieve a single user + :param user_id: the id of the user to delete + """ + return self._user.delete(user_id) + diff --git a/hub/unittests/test_db_factory.py b/hub/unittests/test_db_factory.py index 391f21ff..6c373ceb 100644 --- a/hub/unittests/test_db_factory.py +++ b/hub/unittests/test_db_factory.py @@ -13,9 +13,7 @@ import sqlalchemy.exc from hub.imports.geometry_factory import GeometryFactory from hub.imports.construction_factory import ConstructionFactory from hub.imports.usage_factory import UsageFactory -from hub.imports.db_factory import DBFactory as ImportDBFactory -from hub.imports.user_factory import UserFactory -from hub.exports.db_factory import DBFactory as ExportDBFactory +from hub.persistence.db_control import DBControl from hub.persistence.repository import Repository from sqlalchemy import create_engine from hub.persistence.models import City, Application, CityObject @@ -25,7 +23,7 @@ from sqlalchemy.exc import ProgrammingError import uuid -class Configure: +class Control: _skip_test = False _skip_reason = 'PostgreSQL not properly installed in host machine' @@ -68,30 +66,19 @@ setup ConstructionFactory('nrcan', self._city).enrich() UsageFactory('nrcan', self._city).enrich() - self._import_db_factory = ImportDBFactory( - db_name=repository.configuration.db_name, - app_env='TEST', - dotenv_path=dotenv_path) - self._export_db_factory = ExportDBFactory( - db_name=repository.configuration.db_name, - app_env='TEST', - dotenv_path=dotenv_path) - user_factory = UserFactory( + self._database = DBControl( db_name=repository.configuration.db_name, app_env='TEST', dotenv_path=dotenv_path) + self._unique_id = str(uuid.uuid4()) - self._application = self.import_db_factory.persist_application("test", "test application", self.unique_id) - self._user = user_factory.create_user("Admin", self.application.id, "Admin@123", UserRoles.Admin) + self._application = self._database.persist_application("test", "test application", self.unique_id) + self._user = self._database.create_user("Admin", self.application.id, "Admin@123", UserRoles.Admin) self._pickle_path = 'tests_data/pickle_path.bz2' @property - def import_db_factory(self): - return self._import_db_factory - - @property - def export_db_factory(self): - return self._export_db_factory + def database(self): + return self._database @property def unique_id(self): @@ -126,7 +113,7 @@ setup return self._pickle_path -configure = Configure() +control = Control() class TestDBFactory(TestCase): @@ -134,38 +121,38 @@ class TestDBFactory(TestCase): TestDBFactory """ - @unittest.skipIf(configure.skip_test, configure.skip_reason) + @unittest.skipIf(control.skip_test, control.skip_reason) def test_save_application(self): - self.assertEqual(configure.application.name, "test") - self.assertEqual(configure.application.description, "test application") - self.assertEqual(str(configure.application.application_uuid), configure.unique_id) + self.assertEqual(control.application.name, "test") + self.assertEqual(control.application.description, "test application") + self.assertEqual(str(control.application.application_uuid), control.unique_id) - @unittest.skipIf(configure.skip_test, configure.skip_reason) + @unittest.skipIf(control.skip_test, control.skip_reason) def test_save_city(self): - configure.city.name = "Montreal" - city = configure.import_db_factory.persist_city( - configure.city, - configure.pickle_path, - configure.application.id, - configure.user.id) + control.city.name = "Montreal" + city = control.database.persist_city( + control.city, + control.pickle_path, + control.application.id, + control.user.id) self.assertEqual(city.name, 'Montreal') - self.assertEqual(city.pickle_path, configure.pickle_path) - self.assertEqual(city.level_of_detail, configure.city.level_of_detail.geometry) - configure.import_db_factory.delete_city(city.id) + self.assertEqual(city.pickle_path, control.pickle_path) + self.assertEqual(city.level_of_detail, control.city.level_of_detail.geometry) + control.database.delete_city(city.id) - @unittest.skipIf(configure.skip_test, configure.skip_reason) + @unittest.skipIf(control.skip_test, control.skip_reason) def test_get_update_city(self): - city = configure.import_db_factory.persist_city(configure.city, - configure.pickle_path, - configure.application.id, - configure._user.id) + city = control.database.persist_city(control.city, + control.pickle_path, + control.application.id, + control._user.id) city.name = "Ottawa" - configure.import_db_factory.update_city(city.id, configure.city) - cities = configure.export_db_factory.cities_by_user_and_application( - configure.user.id, - configure.application.id) + control.database.update_city(city.id, control.city) + cities = control.database.cities_by_user_and_application( + control.user.id, + control.application.id) for updated_city in cities: if updated_city.id == city.id: self.assertEqual(updated_city.name, city.name) break - configure.import_db_factory.delete_city(city.id) + control.database.delete_city(city.id) diff --git a/hub/unittests/tests_data/pickle_path.bz2 b/hub/unittests/tests_data/pickle_path.bz2 index df2daadf..c88706a3 100644 Binary files a/hub/unittests/tests_data/pickle_path.bz2 and b/hub/unittests/tests_data/pickle_path.bz2 differ