system_assignation/unittests/test_db_factory.py

77 lines
2.8 KiB
Python
Raw Normal View History

"""
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
from imports.geometry_factory import GeometryFactory
from imports.db_factory import DBFactory
from exports.db_factory import DBFactory as ExportDBFactory
from persistence.db_config import BaseConfiguration
from sqlalchemy import create_engine
from persistence.models import City
from pickle import loads
class TestDBFactory(TestCase):
"""
TestDBFactory
"""
def setUp(self) -> None:
"""
Test setup
:return: None
"""
# Create test tables if they do not exit
config = BaseConfiguration(db_name='hub_test', app_env='TEST')
engine = create_engine(config.conn_string())
City.__table__.create(bind=engine, checkfirst=True)
city_file = "../unittests/tests_data/C40_Final.gml"
self.city = GeometryFactory('citygml', city_file).city
self._db_factory = DBFactory(city=self.city, db_name='hub_test', app_env='TEST')
self._export_db_factory = ExportDBFactory(city=self.city, db_name='hub_test', app_env='TEST')
def test_save_city(self):
2022-11-22 20:20:12 -05:00
saved_city = self._db_factory.persist_city()
self.assertEqual(saved_city.name, 'Montréal')
pickled_city = loads(saved_city.city)
self.assertEqual(len(pickled_city.buildings), 10)
self.assertEqual(pickled_city.buildings[0].floor_area, 1990.9913970530033)
2022-11-22 20:20:12 -05:00
self._db_factory.delete_city(saved_city.id)
def test_save_same_city_with_same_hub_version(self):
first_city = self._db_factory.persist_city()
second_city = self._db_factory.persist_city()
self.assertEqual(second_city['message'], f'Same version of {self.city.name} exist')
self.assertEqual(first_city.name, 'Montréal')
self.assertEqual(first_city.country_code, 'ca')
self._db_factory.delete_city(first_city.id)
def test_get_city_by_name(self):
city = self._db_factory.persist_city()
retrieved_city = self._export_db_factory.get_city_by_name(city.name)
self.assertEqual(retrieved_city[0].lower_corner[0], 610610.7547462888)
self._db_factory.delete_city(city.id)
def test_get_city_by_id(self):
city = self._db_factory.persist_city()
retrieved_city = self._export_db_factory.get_city(city.id)
self.assertEqual(retrieved_city.upper_corner[0], 610818.6731258357)
self._db_factory.delete_city(city.id)
def test_get_update_city(self):
city = self._db_factory.persist_city()
self.city.longitude = 1.43589
self.city.latitude = -9.38928339
self._db_factory.update_city(city.id, self.city)
updated_city = self._export_db_factory.get_city(city.id)
self.assertEqual(updated_city.longitude, 1.43589)
self.assertEqual(updated_city.latitude, -9.38928339)
self._db_factory.delete_city(city.id)