2022-11-15 20:48:42 -05:00
|
|
|
"""
|
2023-05-10 17:06:51 -04:00
|
|
|
Test db factory
|
2022-11-15 20:48:42 -05:00
|
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
|
|
|
Copyright © 2022 Concordia CERC group
|
|
|
|
Project Coder Peter Yefi peteryefi@gmail.com
|
|
|
|
"""
|
2023-05-10 17:06:51 -04:00
|
|
|
import os
|
2023-03-27 13:55:32 -04:00
|
|
|
import unittest
|
2022-11-15 20:48:42 -05:00
|
|
|
from unittest import TestCase
|
2023-05-10 17:06:51 -04:00
|
|
|
from pathlib import Path
|
2023-03-27 13:55:32 -04:00
|
|
|
import sqlalchemy.exc
|
|
|
|
|
2023-01-24 10:51:50 -05:00
|
|
|
from hub.imports.geometry_factory import GeometryFactory
|
2023-05-10 17:06:51 -04:00
|
|
|
from hub.imports.db_factory import DBFactory as ImportDBFactory
|
2023-01-24 10:51:50 -05:00
|
|
|
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
|
|
|
|
2023-05-10 17:06:51 -04:00
|
|
|
class Configure:
|
2023-03-27 13:55:32 -04:00
|
|
|
|
2023-05-10 17:06:51 -04:00
|
|
|
_skip_test = False
|
|
|
|
_skip_reason = 'PostgreSQL not properly installed in host machine'
|
2023-03-27 13:55:32 -04:00
|
|
|
|
|
|
|
def __init__(self):
|
2023-05-10 17:06:51 -04:00
|
|
|
"""
|
|
|
|
Test
|
|
|
|
setup
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
self._skip_test = False
|
2023-03-27 13:55:32 -04:00
|
|
|
# Create test database
|
2023-05-10 17:06:51 -04:00
|
|
|
dotenv_path = str(Path("{}/.local/etc/hub/.env".format(os.path.expanduser('~'))).resolve())
|
|
|
|
repository = Repository(db_name='hub_unittest', app_env='TEST', dotenv_path=dotenv_path)
|
|
|
|
engine = create_engine(repository.configuration.connection_string)
|
2023-03-27 13:55:32 -04:00
|
|
|
try:
|
|
|
|
# delete test database if it exists
|
2023-05-10 17:06:51 -04:00
|
|
|
connection = engine.connect()
|
|
|
|
connection.execute('commit')
|
|
|
|
connection.close()
|
|
|
|
except ProgrammingError:
|
2023-03-27 13:55:32 -04:00
|
|
|
print(f'Database does not exist. Nothing to delete')
|
2023-05-10 17:06:51 -04:00
|
|
|
except sqlalchemy.exc.OperationalError as operational_error:
|
|
|
|
self._skip_test = True
|
|
|
|
self._skip_reason = f'{operational_error}'
|
|
|
|
return
|
|
|
|
|
|
|
|
Application.__table__.create(bind=repository.engine, checkfirst=True)
|
|
|
|
User.__table__.create(bind=repository.engine, checkfirst=True)
|
|
|
|
City.__table__.create(bind=repository.engine, checkfirst=True)
|
|
|
|
CityObject.__table__.create(bind=repository.engine, checkfirst=True)
|
|
|
|
|
|
|
|
city_file = "tests_data/FZK_Haus_LoD_2.gml"
|
|
|
|
self._city = GeometryFactory('citygml', city_file).city
|
|
|
|
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(
|
|
|
|
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._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
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
return self._unique_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def application(self):
|
|
|
|
return self._application
|
|
|
|
|
|
|
|
@property
|
|
|
|
def user(self):
|
|
|
|
return self._user
|
|
|
|
|
|
|
|
@property
|
|
|
|
def skip_test(self):
|
|
|
|
return self._skip_test
|
2023-03-27 13:55:32 -04:00
|
|
|
|
|
|
|
@property
|
2023-05-10 17:06:51 -04:00
|
|
|
def skip_reason(self):
|
|
|
|
return self._skip_reason
|
2023-03-27 13:55:32 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def message(self):
|
2023-05-10 17:06:51 -04:00
|
|
|
return self._skip_message
|
|
|
|
|
|
|
|
@property
|
|
|
|
def city(self):
|
|
|
|
return self._city
|
2023-03-27 13:55:32 -04:00
|
|
|
|
2023-05-10 17:06:51 -04:00
|
|
|
@property
|
|
|
|
def pickle_path(self):
|
|
|
|
return self._pickle_path
|
2023-03-27 13:55:32 -04:00
|
|
|
|
2023-05-10 17:06:51 -04:00
|
|
|
configure = Configure()
|
2022-11-15 20:48:42 -05:00
|
|
|
|
|
|
|
class TestDBFactory(TestCase):
|
|
|
|
"""
|
|
|
|
TestDBFactory
|
|
|
|
"""
|
|
|
|
|
2023-05-10 17:06:51 -04:00
|
|
|
@unittest.skipIf(configure.skip_test, configure.skip_reason)
|
2023-02-22 22:45:33 -05:00
|
|
|
def test_save_application(self):
|
2023-05-10 17:06:51 -04:00
|
|
|
self.assertEqual(configure.application.name, "test")
|
|
|
|
self.assertEqual(configure.application.description, "test application")
|
|
|
|
self.assertEqual(str(configure.application.application_uuid), configure.unique_id)
|
2022-11-15 20:48:42 -05:00
|
|
|
|
2023-05-10 17:06:51 -04:00
|
|
|
@unittest.skipIf(configure.skip_test, configure.skip_reason)
|
2022-11-15 20:48:42 -05:00
|
|
|
def test_save_city(self):
|
2023-05-10 17:06:51 -04:00
|
|
|
configure.city.name = "Montreal"
|
|
|
|
saved_city = configure.import_db_factory.persist_city(
|
|
|
|
configure.city,
|
|
|
|
configure.pickle_path,
|
|
|
|
configure.application.id,
|
|
|
|
configure.user.id)
|
|
|
|
self.assertEqual(saved_city.name, 'Montreal')
|
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
|
|
|
|
2023-05-10 17:06:51 -04:00
|
|
|
@unittest.skipIf(configure.skip_test, configure.skip_reason)
|
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-05-10 17:06:51 -04:00
|
|
|
@unittest.skipIf(configure.skip_test, configure.skip_reason)
|
2023-01-11 16:02:33 -05:00
|
|
|
def test_get_city_by_user(self):
|
2023-05-10 17:06:51 -04:00
|
|
|
city = self._import_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)
|
|
|
|
|
2023-05-10 17:06:51 -04:00
|
|
|
@unittest.skipIf(configure.skip_test, configure.skip_reason)
|
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)
|
|
|
|
|
2023-05-10 17:06:51 -04:00
|
|
|
@unittest.skipIf(configure.skip_test, configure.skip_reason)
|
2022-11-15 20:48:42 -05:00
|
|
|
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)
|