Merge pull request 'test_correction' (#18) from test_correction into main

Reviewed-on: https://nextgenerations-cities.encs.concordia.ca/gitea/CERC/hub/pulls/18
This commit is contained in:
Guille Gutierrez 2023-03-27 13:56:55 -04:00
commit dde1c796f7
7 changed files with 53 additions and 16 deletions

View File

@ -173,6 +173,7 @@ class InselMonthlyEnergyBalance(Insel):
type_code = _CONSTRUCTION_CODE[thermal_boundary.type] type_code = _CONSTRUCTION_CODE[thermal_boundary.type]
wall_area = thermal_boundary.opaque_area * (1 + thermal_boundary.window_ratio) wall_area = thermal_boundary.opaque_area * (1 + thermal_boundary.window_ratio)
if thermal_boundary.type == cte.WALL: if thermal_boundary.type == cte.WALL:
if thermal_boundary.parent_surface.percentage_shared is not None:
wall_area = wall_area * (1 - thermal_boundary.parent_surface.percentage_shared) wall_area = wall_area * (1 - thermal_boundary.parent_surface.percentage_shared)
window_area = wall_area * thermal_boundary.window_ratio window_area = wall_area * thermal_boundary.window_ratio

View File

@ -77,7 +77,7 @@ class NrcanPhysicsParameters:
nrcan_archetypes = nrcan_catalog.entries('archetypes') nrcan_archetypes = nrcan_catalog.entries('archetypes')
for building_archetype in nrcan_archetypes: for building_archetype in nrcan_archetypes:
construction_period_limits = building_archetype.construction_period.split('_') construction_period_limits = building_archetype.construction_period.split('_')
if int(construction_period_limits[0]) <= year_of_construction <= int(construction_period_limits[1]): if int(construction_period_limits[0]) <= int(year_of_construction) <= int(construction_period_limits[1]):
if (str(function) == str(building_archetype.function)) and \ if (str(function) == str(building_archetype.function)) and \
(climate_zone == str(building_archetype.climate_zone)): (climate_zone == str(building_archetype.climate_zone)):
return building_archetype return building_archetype

View File

@ -78,7 +78,7 @@ class NrelPhysicsParameters:
construction_period_limits = building_archetype.construction_period.split(' - ') construction_period_limits = building_archetype.construction_period.split(' - ')
if construction_period_limits[1] == 'PRESENT': if construction_period_limits[1] == 'PRESENT':
construction_period_limits[1] = 3000 construction_period_limits[1] = 3000
if int(construction_period_limits[0]) <= year_of_construction < int(construction_period_limits[1]): if int(construction_period_limits[0]) <= int(year_of_construction) < int(construction_period_limits[1]):
if (str(function) == str(building_archetype.function)) and \ if (str(function) == str(building_archetype.function)) and \
(climate_zone == str(building_archetype.climate_zone)): (climate_zone == str(building_archetype.climate_zone)):
return building_archetype return building_archetype

View File

@ -4,7 +4,7 @@ SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group Copyright © 2022 Concordia CERC group
Project Coder Guillermo Gutierrez Guillermo.GutierrezMorote@concordia.ca Project Coder Guillermo Gutierrez Guillermo.GutierrezMorote@concordia.ca
""" """
import datetime
import json import json
import numpy as np import numpy as np
@ -181,7 +181,6 @@ class Geojson:
Get city out of a Geojson file Get city out of a Geojson file
""" """
if self._city is None: if self._city is None:
start = datetime.datetime.now()
missing_functions = [] missing_functions = []
buildings = [] buildings = []
building_id = 0 building_id = 0
@ -230,18 +229,12 @@ class Geojson:
[polygon]) [polygon])
self._city = City([self._min_x, self._min_y, 0.0], [self._max_x, self._max_y, self._max_z], 'epsg:26911') 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: for building in buildings:
self._city.add_city_object(building) self._city.add_city_object(building)
self._city.level_of_detail.geometry = lod self._city.level_of_detail.geometry = lod
if lod == 1: if lod == 1:
start = datetime.datetime.now()
lines_information = GeometryHelper.city_mapping(self._city, plot=False) 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) self._store_shared_percentage_to_walls(self._city, lines_information)
print(f'shared_walls: {datetime.datetime.now() - start}')
if len(missing_functions) > 0: if len(missing_functions) > 0:
print(f'There are unknown functions {missing_functions}') print(f'There are unknown functions {missing_functions}')
return self._city return self._city

View File

@ -23,4 +23,4 @@ shapely
geopandas geopandas
triangle triangle
psycopg2-binary psycopg2-binary
PIL Pillow

View File

@ -59,7 +59,6 @@ class CityLayerTest(TestCase):
return gdf, target_buildings, adjacent_buildings return gdf, target_buildings, adjacent_buildings
def _genidf(self, bldgs_group): def _genidf(self, bldgs_group):
t0 = time.time()
buildings_df, target_buildings, adjacent_buildings = self._prepare_buildings(bldgs_group) buildings_df, target_buildings, adjacent_buildings = self._prepare_buildings(bldgs_group)
output_path = (Path(__file__).parent / 'tests_outputs').resolve() output_path = (Path(__file__).parent / 'tests_outputs').resolve()
city = GeometryFactory('gpandas', data_frame=buildings_df).city city = GeometryFactory('gpandas', data_frame=buildings_df).city
@ -70,7 +69,6 @@ class CityLayerTest(TestCase):
filepath = os.path.join(output_path, city.name + ".idf") filepath = os.path.join(output_path, city.name + ".idf")
newfilepath = filepath[:-4] + "_" + uuid.uuid4().hex[:10] + ".idf" newfilepath = filepath[:-4] + "_" + uuid.uuid4().hex[:10] + ".idf"
os.rename(filepath, newfilepath) os.rename(filepath, newfilepath)
print(f"It took {round((time.time() - t0), 0)} seconds")
return newfilepath return newfilepath
def test_city_layers(self): def test_city_layers(self):

View File

@ -4,7 +4,11 @@ SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group Copyright © 2022 Concordia CERC group
Project Coder Peter Yefi peteryefi@gmail.com Project Coder Peter Yefi peteryefi@gmail.com
""" """
import unittest
from unittest import TestCase from unittest import TestCase
import sqlalchemy.exc
from hub.imports.geometry_factory import GeometryFactory from hub.imports.geometry_factory import GeometryFactory
from hub.imports.db_factory import DBFactory from hub.imports.db_factory import DBFactory
from hub.imports.user_factory import UserFactory from hub.imports.user_factory import UserFactory
@ -16,6 +20,40 @@ from hub.persistence.models import User, UserRoles
from sqlalchemy.exc import ProgrammingError from sqlalchemy.exc import ProgrammingError
import uuid 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): class TestDBFactory(TestCase):
""" """
@ -32,7 +70,6 @@ class TestDBFactory(TestCase):
env = '/usr/local/etc/hub/.env' env = '/usr/local/etc/hub/.env'
repo = Repository(db_name='test_db', app_env='TEST', dotenv_path=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()}') eng = create_engine(f'postgresql://{repo.configuration.get_db_user()}@/{repo.configuration.get_db_user()}')
try: try:
# delete test database if it exists # delete test database if it exists
conn = eng.connect() conn = eng.connect()
@ -41,7 +78,9 @@ class TestDBFactory(TestCase):
conn.close() conn.close()
except ProgrammingError as err: except ProgrammingError as err:
print(f'Database does not exist. Nothing to delete') print(f'Database does not exist. Nothing to delete')
except sqlalchemy.exc.OperationalError:
skip.value = True
return
cnn = eng.connect() cnn = eng.connect()
cnn.execute('commit') cnn.execute('commit')
cnn.execute("CREATE DATABASE test_db") 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._user = user_factory.create_user("Admin", cls.application.id, "Admin@123", UserRoles.Admin)
cls.pickle_path = 'tests_data/pickle_path.bz2' cls.pickle_path = 'tests_data/pickle_path.bz2'
@unittest.skipIf(skip.value, skip.message)
def test_save_application(self): def test_save_application(self):
self.assertEqual(self.application.name, "test") self.assertEqual(self.application.name, "test")
self.assertEqual(self.application.description, "test application") self.assertEqual(self.application.description, "test application")
self.assertEqual(str(self.application.application_uuid), self.unique_id) self.assertEqual(str(self.application.application_uuid), self.unique_id)
@unittest.skipIf(skip.value, skip.message)
def test_save_city(self): def test_save_city(self):
self.city.name = "Montréal" self.city.name = "Montréal"
saved_city = self._db_factory.persist_city(self.city, self.pickle_path, self.application.id, self._user.id) 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.assertEqual(saved_city.level_of_detail, self.city.level_of_detail.geometry)
self._db_factory.delete_city(saved_city.id) self._db_factory.delete_city(saved_city.id)
@unittest.skipIf(skip.value, skip.message)
def test_get_city_by_name(self): def test_get_city_by_name(self):
city = self._db_factory.persist_city(self.city, self.pickle_path, self.application.id, self._user.id) 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) 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.assertEqual(retrieved_city[0].user_id, self._user.id)
self._db_factory.delete_city(city.id) self._db_factory.delete_city(city.id)
@unittest.skipIf(skip.value, skip.message)
def test_get_city_by_user(self): def test_get_city_by_user(self):
city = self._db_factory.persist_city(self.city, self.pickle_path, self.application.id, self._user.id) 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) retrieved_city = self._export_db_factory.get_city_by_user(self._user.id)
self.assertEqual(retrieved_city[0].pickle_path, self.pickle_path) self.assertEqual(retrieved_city[0].pickle_path, self.pickle_path)
self._db_factory.delete_city(city.id) self._db_factory.delete_city(city.id)
@unittest.skipIf(skip.value, skip.message)
def test_get_city_by_id(self): def test_get_city_by_id(self):
city = self._db_factory.persist_city(self.city, self.pickle_path, self.application.id, self._user.id) 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) retrieved_city = self._export_db_factory.get_city(city.id)
self.assertEqual(retrieved_city.level_of_detail, self.city.level_of_detail.geometry) self.assertEqual(retrieved_city.level_of_detail, self.city.level_of_detail.geometry)
self._db_factory.delete_city(city.id) self._db_factory.delete_city(city.id)
@unittest.skipIf(skip.value, skip.message)
def test_get_update_city(self): def test_get_update_city(self):
city = self._db_factory.persist_city(self.city, self.pickle_path, self.application.id, self._user.id) city = self._db_factory.persist_city(self.city, self.pickle_path, self.application.id, self._user.id)
self.city.name = "Ottawa" self.city.name = "Ottawa"