city_retrofit/hub/unittests/test_db_factory.py

160 lines
4.9 KiB
Python
Raw Normal View History

"""
2023-05-10 17:06:51 -04:00
Test db factory
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
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
from hub.imports.construction_factory import ConstructionFactory
from hub.imports.usage_factory import UsageFactory
2023-05-17 12:30:11 -04:00
from hub.persistence.db_control import DBControl
2023-02-22 22:45:33 -05:00
from hub.persistence.repository import Repository
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
from hub.helpers.dictionaries import Dictionaries
from sqlalchemy.exc import ProgrammingError
2023-02-22 22:45:33 -05:00
import uuid
2023-03-27 13:55:32 -04:00
2023-05-17 12:30:11 -04:00
class Control:
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
"""
2023-05-10 17:06:51 -04:00
self._skip_test = False
2023-03-27 13:55:32 -04:00
# 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)
2023-05-10 17:06:51 -04:00
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.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,
function_to_hub=Dictionaries().alkis_function_to_hub_function).city
ConstructionFactory('nrcan', self._city).enrich()
UsageFactory('nrcan', self._city).enrich()
2023-05-17 12:30:11 -04:00
self._database = DBControl(
2023-05-10 17:06:51 -04:00
db_name=repository.configuration.db_name,
app_env='TEST',
dotenv_path=dotenv_path)
2023-05-17 12:30:11 -04:00
2023-05-10 17:06:51 -04:00
self._unique_id = str(uuid.uuid4())
2023-05-17 12:30:11 -04:00
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)
2023-05-10 17:06:51 -04:00
self._pickle_path = 'tests_data/pickle_path.bz2'
@property
2023-05-17 12:30:11 -04:00
def database(self):
return self._database
2023-05-10 17:06:51 -04:00
@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):
return self._skip_reason
2023-05-10 17:06:51 -04:00
@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-17 12:30:11 -04:00
control = Control()
class TestDBFactory(TestCase):
"""
TestDBFactory
"""
2023-05-17 12:30:11 -04:00
@unittest.skipIf(control.skip_test, control.skip_reason)
2023-02-22 22:45:33 -05:00
def test_save_application(self):
2023-05-17 12:30:11 -04:00
self.assertEqual(control.application.name, "test")
self.assertEqual(control.application.description, "test application")
self.assertEqual(str(control.application.application_uuid), control.unique_id)
2023-05-17 12:30:11 -04:00
@unittest.skipIf(control.skip_test, control.skip_reason)
def test_save_city(self):
2023-05-17 12:30:11 -04:00
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')
2023-05-17 12:30:11 -04:00
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)
2023-05-17 17:10:30 -04:00
os.unlink(control.pickle_path)
2023-05-17 12:30:11 -04:00
@unittest.skipIf(control.skip_test, control.skip_reason)
def test_get_update_city(self):
2023-05-17 12:30:11 -04:00
city = control.database.persist_city(control.city,
control.pickle_path,
control.application.id,
control._user.id)
city.name = "Ottawa"
2023-05-17 12:30:11 -04:00
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
2023-05-17 12:30:11 -04:00
control.database.delete_city(city.id)