2022-11-15 20:48:42 -05:00
|
|
|
"""
|
|
|
|
Test EnergySystemsFactory and various heatpump models
|
|
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
|
|
|
Copyright © 2022 Concordia CERC group
|
|
|
|
Project Coder Peter Yefi peteryefi@gmail.com
|
|
|
|
"""
|
|
|
|
from unittest import TestCase
|
2023-01-24 10:51:50 -05:00
|
|
|
from hub.imports.geometry_factory import GeometryFactory
|
|
|
|
from hub.imports.db_factory import DBFactory
|
|
|
|
from hub.imports.user_factory import UserFactory
|
|
|
|
from hub.exports.db_factory import DBFactory as ExportDBFactory
|
2023-02-22 22:45:33 -05:00
|
|
|
from hub.persistence.repository import Repository
|
2022-11-15 20:48:42 -05:00
|
|
|
from sqlalchemy import create_engine
|
2023-02-22 22:45:33 -05:00
|
|
|
from hub.persistence.models import City, Application, CityObject
|
2023-01-24 10:51:50 -05:00
|
|
|
from hub.persistence.models import User, UserRoles
|
2022-11-25 18:21:09 -05:00
|
|
|
from sqlalchemy.exc import ProgrammingError
|
2023-02-22 22:45:33 -05:00
|
|
|
import uuid
|
2022-11-15 20:48:42 -05:00
|
|
|
|
|
|
|
|
|
|
|
class TestDBFactory(TestCase):
|
|
|
|
"""
|
|
|
|
TestDBFactory
|
|
|
|
"""
|
|
|
|
|
2022-11-25 18:21:09 -05:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls) -> None:
|
2022-11-15 20:48:42 -05:00
|
|
|
"""
|
|
|
|
Test setup
|
|
|
|
:return: None
|
|
|
|
"""
|
2022-11-25 18:21:09 -05:00
|
|
|
# Create test database
|
2023-02-22 22:45:33 -05:00
|
|
|
repo = Repository(db_name='test_db', app_env='TEST', dotenv_path='/usr/local/etc/hub/.env')
|
|
|
|
eng = create_engine(f'postgresql://{repo.configuration.get_db_user()}@/{repo.configuration.get_db_user()}')
|
2022-11-25 18:21:09 -05:00
|
|
|
|
|
|
|
try:
|
|
|
|
# delete test database if it exists
|
|
|
|
conn = eng.connect()
|
|
|
|
conn.execute('commit')
|
|
|
|
conn.execute('DROP DATABASE test_db')
|
|
|
|
conn.close()
|
|
|
|
except ProgrammingError as err:
|
|
|
|
print(f'Database does not exist. Nothing to delete')
|
|
|
|
|
|
|
|
cnn = eng.connect()
|
|
|
|
cnn.execute('commit')
|
|
|
|
cnn.execute("CREATE DATABASE test_db")
|
|
|
|
cnn.close()
|
2023-02-22 22:45:33 -05:00
|
|
|
|
|
|
|
Application.__table__.create(bind=repo.engine, checkfirst=True)
|
2023-01-11 16:02:33 -05:00
|
|
|
User.__table__.create(bind=repo.engine, checkfirst=True)
|
2022-11-25 18:21:09 -05:00
|
|
|
City.__table__.create(bind=repo.engine, checkfirst=True)
|
2023-02-22 22:45:33 -05:00
|
|
|
CityObject.__table__.create(bind=repo.engine, checkfirst=True)
|
2022-11-15 20:48:42 -05:00
|
|
|
|
2023-01-24 10:51:50 -05:00
|
|
|
city_file = "tests_data/C40_Final.gml"
|
2022-11-25 18:21:09 -05:00
|
|
|
cls.city = GeometryFactory('citygml', city_file).city
|
2023-01-17 20:04:56 -05:00
|
|
|
cls._db_factory = DBFactory(db_name='test_db', app_env='TEST', dotenv_path='../.env')
|
2022-12-07 19:06:17 -05:00
|
|
|
cls._export_db_factory = ExportDBFactory(db_name='test_db', app_env='TEST', dotenv_path='../.env')
|
2023-01-11 16:02:33 -05:00
|
|
|
user_factory = UserFactory(db_name='test_db', app_env='TEST', dotenv_path='../.env')
|
2023-02-22 22:45:33 -05:00
|
|
|
cls.unique_id = str(uuid.uuid4())
|
|
|
|
cls.application = cls._db_factory.persist_application("test", "test application", cls.unique_id)
|
|
|
|
cls._user = user_factory.create_user("Admin", cls.application.id, "Admin@123", UserRoles.Admin)
|
|
|
|
cls.pickle_path = 'tests_data/pickle_path.bz2'
|
|
|
|
|
|
|
|
def test_save_application(self):
|
|
|
|
self.assertEqual(self.application.name, "test")
|
|
|
|
self.assertEqual(self.application.description, "test application")
|
|
|
|
self.assertEqual(str(self.application.application_uuid), self.unique_id)
|
2022-11-15 20:48:42 -05:00
|
|
|
|
|
|
|
def test_save_city(self):
|
2023-02-22 22:45:33 -05:00
|
|
|
self.city.name = "Montréal"
|
|
|
|
saved_city = self._db_factory.persist_city(self.city, self.pickle_path, self.application.id, self._user.id)
|
2022-11-22 20:20:12 -05:00
|
|
|
self.assertEqual(saved_city.name, 'Montréal')
|
2023-02-22 22:45:33 -05:00
|
|
|
self.assertEqual(saved_city.pickle_path, self.pickle_path)
|
|
|
|
self.assertEqual(saved_city.level_of_detail, self.city.level_of_detail.geometry)
|
2022-11-22 20:20:12 -05:00
|
|
|
self._db_factory.delete_city(saved_city.id)
|
2022-11-15 20:48:42 -05:00
|
|
|
|
|
|
|
def test_get_city_by_name(self):
|
2023-02-22 22:45:33 -05:00
|
|
|
city = self._db_factory.persist_city(self.city, self.pickle_path, self.application.id, self._user.id)
|
2022-11-15 20:48:42 -05:00
|
|
|
retrieved_city = self._export_db_factory.get_city_by_name(city.name)
|
2023-02-22 22:45:33 -05:00
|
|
|
self.assertEqual(retrieved_city[0].application_id, 1)
|
|
|
|
self.assertEqual(retrieved_city[0].user_id, self._user.id)
|
2022-11-15 20:48:42 -05:00
|
|
|
self._db_factory.delete_city(city.id)
|
|
|
|
|
2023-01-11 16:02:33 -05:00
|
|
|
def test_get_city_by_user(self):
|
2023-02-22 22:45:33 -05:00
|
|
|
city = self._db_factory.persist_city(self.city, self.pickle_path, self.application.id, self._user.id)
|
2023-01-11 16:02:33 -05:00
|
|
|
retrieved_city = self._export_db_factory.get_city_by_user(self._user.id)
|
2023-02-22 22:45:33 -05:00
|
|
|
self.assertEqual(retrieved_city[0].pickle_path, self.pickle_path)
|
2023-01-11 16:02:33 -05:00
|
|
|
self._db_factory.delete_city(city.id)
|
|
|
|
|
2022-11-15 20:48:42 -05:00
|
|
|
def test_get_city_by_id(self):
|
2023-02-22 22:45:33 -05:00
|
|
|
city = self._db_factory.persist_city(self.city, self.pickle_path, self.application.id, self._user.id)
|
2022-11-15 20:48:42 -05:00
|
|
|
retrieved_city = self._export_db_factory.get_city(city.id)
|
2023-02-22 22:45:33 -05:00
|
|
|
self.assertEqual(retrieved_city.level_of_detail, self.city.level_of_detail.geometry)
|
2022-11-15 20:48:42 -05:00
|
|
|
self._db_factory.delete_city(city.id)
|
|
|
|
|
|
|
|
def test_get_update_city(self):
|
2023-02-22 22:45:33 -05:00
|
|
|
city = self._db_factory.persist_city(self.city, self.pickle_path, self.application.id, self._user.id)
|
|
|
|
self.city.name = "Ottawa"
|
2022-11-15 20:48:42 -05:00
|
|
|
self._db_factory.update_city(city.id, self.city)
|
|
|
|
updated_city = self._export_db_factory.get_city(city.id)
|
2023-02-22 22:45:33 -05:00
|
|
|
self.assertEqual(updated_city.name, self.city.name)
|
2022-11-15 20:48:42 -05:00
|
|
|
self._db_factory.delete_city(city.id)
|