Guille
a33bf0b366
Results are still missing and need to be added to the final commit. including the db table creation that seems to be missing
172 lines
5.6 KiB
Python
172 lines
5.6 KiB
Python
"""
|
|
Test db factory
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
|
Copyright © 2022 Concordia CERC group
|
|
Project Coder Peter Yefi peteryefi@gmail.com
|
|
"""
|
|
import os
|
|
import unittest
|
|
from unittest import TestCase
|
|
from pathlib import Path
|
|
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.repository import Repository
|
|
from sqlalchemy import create_engine
|
|
from hub.persistence.models import City, Application, CityObject
|
|
from hub.persistence.models import User, UserRoles
|
|
from hub.helpers.dictionaries import Dictionaries
|
|
from sqlalchemy.exc import ProgrammingError
|
|
import uuid
|
|
|
|
|
|
class Configure:
|
|
_skip_test = False
|
|
_skip_reason = 'PostgreSQL not properly installed in host machine'
|
|
|
|
def __init__(self):
|
|
"""
|
|
Test
|
|
setup
|
|
:return: None
|
|
"""
|
|
self._skip_test = False
|
|
# Create test database
|
|
dotenv_path = Path("{}/.local/etc/hub/.env".format(os.path.expanduser('~'))).resolve()
|
|
if not dotenv_path.exists():
|
|
self._skip_test = True
|
|
self._skip_reason = f'.env file missing at {dotenv_path}'
|
|
return
|
|
dotenv_path = str(dotenv_path)
|
|
repository = Repository(db_name='hub_unittests', app_env='TEST', dotenv_path=dotenv_path)
|
|
engine = create_engine(repository.configuration.connection_string)
|
|
try:
|
|
# delete test database if it exists
|
|
connection = engine.connect()
|
|
connection.close()
|
|
except ProgrammingError:
|
|
print(f'Database does not exist. Nothing to delete')
|
|
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,
|
|
function_to_hub=Dictionaries().alkis_function_to_hub_function).city
|
|
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(
|
|
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
|
|
|
|
@property
|
|
def skip_reason(self):
|
|
return self._skip_reason
|
|
|
|
@property
|
|
def message(self):
|
|
return self._skip_reason
|
|
|
|
@property
|
|
def city(self):
|
|
return self._city
|
|
|
|
@property
|
|
def pickle_path(self):
|
|
return self._pickle_path
|
|
|
|
|
|
configure = Configure()
|
|
|
|
|
|
class TestDBFactory(TestCase):
|
|
"""
|
|
TestDBFactory
|
|
"""
|
|
|
|
@unittest.skipIf(configure.skip_test, configure.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)
|
|
|
|
@unittest.skipIf(configure.skip_test, configure.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)
|
|
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)
|
|
|
|
@unittest.skipIf(configure.skip_test, configure.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.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)
|
|
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)
|