From 47d9d9d7754284277d27f8512ba83721476b2af3 Mon Sep 17 00:00:00 2001 From: guille Date: Mon, 27 Mar 2023 13:55:32 -0400 Subject: [PATCH] Correct all the unit tests --- .../insel/insel_monthly_energy_balance.py | 3 +- hub/imports/geometry/geojson.py | 9 +--- hub/requirements.txt | 2 +- hub/unittests/test_city_layers.py | 2 - hub/unittests/test_db_factory.py | 49 ++++++++++++++++++- 5 files changed, 51 insertions(+), 14 deletions(-) diff --git a/hub/exports/building_energy/insel/insel_monthly_energy_balance.py b/hub/exports/building_energy/insel/insel_monthly_energy_balance.py index 966c7586..2e37ab87 100644 --- a/hub/exports/building_energy/insel/insel_monthly_energy_balance.py +++ b/hub/exports/building_energy/insel/insel_monthly_energy_balance.py @@ -151,7 +151,8 @@ class InselMonthlyEnergyBalance(Insel): type_code = _CONSTRUCTION_CODE[thermal_boundary.type] wall_area = thermal_boundary.opaque_area * (1 + thermal_boundary.window_ratio) if thermal_boundary.type == cte.WALL: - wall_area = wall_area * (1 - thermal_boundary.parent_surface.percentage_shared) + if thermal_boundary.parent_surface.percentage_shared is not None: + wall_area = wall_area * (1 - thermal_boundary.parent_surface.percentage_shared) window_area = wall_area * thermal_boundary.window_ratio parameters.append(type_code) diff --git a/hub/imports/geometry/geojson.py b/hub/imports/geometry/geojson.py index d183d29d..029966c8 100644 --- a/hub/imports/geometry/geojson.py +++ b/hub/imports/geometry/geojson.py @@ -4,7 +4,7 @@ SPDX - License - Identifier: LGPL - 3.0 - or -later Copyright © 2022 Concordia CERC group Project Coder Guillermo Gutierrez Guillermo.GutierrezMorote@concordia.ca """ -import datetime + import json import numpy as np @@ -181,7 +181,6 @@ class Geojson: Get city out of a Geojson file """ if self._city is None: - start = datetime.datetime.now() missing_functions = [] buildings = [] building_id = 0 @@ -230,18 +229,12 @@ class Geojson: [polygon]) self._city = City([self._min_x, self._min_y, 0.0], [self._max_x, self._max_y, self._max_z], 'epsg:26911') - print(f'features: {datetime.datetime.now()-start}') - for building in buildings: self._city.add_city_object(building) self._city.level_of_detail.geometry = lod if lod == 1: - start = datetime.datetime.now() lines_information = GeometryHelper.city_mapping(self._city, plot=False) - print(f'mapping: {datetime.datetime.now() - start}') - start = datetime.datetime.now() self._store_shared_percentage_to_walls(self._city, lines_information) - print(f'shared_walls: {datetime.datetime.now() - start}') if len(missing_functions) > 0: print(f'There are unknown functions {missing_functions}') return self._city diff --git a/hub/requirements.txt b/hub/requirements.txt index 306c0a4b..1d53e0cb 100644 --- a/hub/requirements.txt +++ b/hub/requirements.txt @@ -23,4 +23,4 @@ shapely geopandas triangle psycopg2-binary -PIL \ No newline at end of file +Pillow \ No newline at end of file diff --git a/hub/unittests/test_city_layers.py b/hub/unittests/test_city_layers.py index a763e895..0d9b02be 100644 --- a/hub/unittests/test_city_layers.py +++ b/hub/unittests/test_city_layers.py @@ -59,7 +59,6 @@ class CityLayerTest(TestCase): return gdf, target_buildings, adjacent_buildings def _genidf(self, bldgs_group): - t0 = time.time() buildings_df, target_buildings, adjacent_buildings = self._prepare_buildings(bldgs_group) output_path = (Path(__file__).parent / 'tests_outputs').resolve() city = GeometryFactory('gpandas', data_frame=buildings_df).city @@ -70,7 +69,6 @@ class CityLayerTest(TestCase): filepath = os.path.join(output_path, city.name + ".idf") newfilepath = filepath[:-4] + "_" + uuid.uuid4().hex[:10] + ".idf" os.rename(filepath, newfilepath) - print(f"It took {round((time.time() - t0), 0)} seconds") return newfilepath def test_city_layers(self): diff --git a/hub/unittests/test_db_factory.py b/hub/unittests/test_db_factory.py index 9d815bf3..ee5730af 100644 --- a/hub/unittests/test_db_factory.py +++ b/hub/unittests/test_db_factory.py @@ -4,7 +4,11 @@ SPDX - License - Identifier: LGPL - 3.0 - or -later Copyright © 2022 Concordia CERC group Project Coder Peter Yefi peteryefi@gmail.com """ +import unittest from unittest import TestCase + +import sqlalchemy.exc + from hub.imports.geometry_factory import GeometryFactory from hub.imports.db_factory import DBFactory from hub.imports.user_factory import UserFactory @@ -16,6 +20,40 @@ from hub.persistence.models import User, UserRoles from sqlalchemy.exc import ProgrammingError import uuid +class Skip: + + _value = False + _message = 'PostgreSQL not properly installed in host machine' + + def __init__(self): + # Create test database + env = '/usr/local/etc/hub/.env' + repo = Repository(db_name='test_db', app_env='TEST', dotenv_path=env) + eng = create_engine(f'postgresql://{repo.configuration.get_db_user()}@/{repo.configuration.get_db_user()}') + 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') + except sqlalchemy.exc.OperationalError: + self._value = True + + @property + def value(self): + return self._value + + @property + def message(self): + return self._message + + @value.setter + def value(self, skip_value): + self._value = skip_value + +skip = Skip() class TestDBFactory(TestCase): """ @@ -32,7 +70,6 @@ class TestDBFactory(TestCase): env = '/usr/local/etc/hub/.env' repo = Repository(db_name='test_db', app_env='TEST', dotenv_path=env) eng = create_engine(f'postgresql://{repo.configuration.get_db_user()}@/{repo.configuration.get_db_user()}') - try: # delete test database if it exists conn = eng.connect() @@ -41,7 +78,9 @@ class TestDBFactory(TestCase): conn.close() except ProgrammingError as err: print(f'Database does not exist. Nothing to delete') - + except sqlalchemy.exc.OperationalError: + skip.value = True + return cnn = eng.connect() cnn.execute('commit') cnn.execute("CREATE DATABASE test_db") @@ -62,11 +101,13 @@ class TestDBFactory(TestCase): cls._user = user_factory.create_user("Admin", cls.application.id, "Admin@123", UserRoles.Admin) cls.pickle_path = 'tests_data/pickle_path.bz2' + @unittest.skipIf(skip.value, skip.message) 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) + @unittest.skipIf(skip.value, skip.message) def test_save_city(self): self.city.name = "Montréal" saved_city = self._db_factory.persist_city(self.city, self.pickle_path, self.application.id, self._user.id) @@ -75,6 +116,7 @@ class TestDBFactory(TestCase): self.assertEqual(saved_city.level_of_detail, self.city.level_of_detail.geometry) self._db_factory.delete_city(saved_city.id) + @unittest.skipIf(skip.value, skip.message) def test_get_city_by_name(self): city = self._db_factory.persist_city(self.city, self.pickle_path, self.application.id, self._user.id) retrieved_city = self._export_db_factory.get_city_by_name(city.name) @@ -82,18 +124,21 @@ class TestDBFactory(TestCase): self.assertEqual(retrieved_city[0].user_id, self._user.id) self._db_factory.delete_city(city.id) + @unittest.skipIf(skip.value, skip.message) def test_get_city_by_user(self): city = self._db_factory.persist_city(self.city, self.pickle_path, self.application.id, self._user.id) retrieved_city = self._export_db_factory.get_city_by_user(self._user.id) self.assertEqual(retrieved_city[0].pickle_path, self.pickle_path) self._db_factory.delete_city(city.id) + @unittest.skipIf(skip.value, skip.message) def test_get_city_by_id(self): city = self._db_factory.persist_city(self.city, self.pickle_path, self.application.id, self._user.id) retrieved_city = self._export_db_factory.get_city(city.id) self.assertEqual(retrieved_city.level_of_detail, self.city.level_of_detail.geometry) self._db_factory.delete_city(city.id) + @unittest.skipIf(skip.value, skip.message) def test_get_update_city(self): city = self._db_factory.persist_city(self.city, self.pickle_path, self.application.id, self._user.id) self.city.name = "Ottawa"