From af93dbb3a10ccda7976c738f43ddae65b2e43789 Mon Sep 17 00:00:00 2001 From: Guille Date: Mon, 10 Jul 2023 14:55:36 -0400 Subject: [PATCH] improve unit tests and bug fix --- .../energy_systems/heat_pump_export.py | 3 +- tests/test_city_merge.py | 58 +++++++++++-------- 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/hub/exports/energy_systems/heat_pump_export.py b/hub/exports/energy_systems/heat_pump_export.py index 1f3ac933..b842d895 100644 --- a/hub/exports/energy_systems/heat_pump_export.py +++ b/hub/exports/energy_systems/heat_pump_export.py @@ -6,6 +6,7 @@ Project Coder Peter Yefi peteryefi@gmail.com """ import os import logging +import subprocess from typing import List, Union, Dict import yaml from string import Template @@ -58,7 +59,7 @@ class HeatPumpExport: # Now run insel self._delete_existing_output_files() logging.info(f'Running Insel with user input: {user_input} and coefficients {capacity_coefficient}') - os.system('insel {}'.format(insel_file)) + subprocess.run(['insel', str(insel_file)]) # Writer headers to csv output files generated by insel self._write_insel_output_headers() # User output diff --git a/tests/test_city_merge.py b/tests/test_city_merge.py index f5c1474a..8687e8e9 100644 --- a/tests/test_city_merge.py +++ b/tests/test_city_merge.py @@ -37,19 +37,19 @@ class TestCityMerge(TestCase): full_city = GeometryFactory('geojson', file_path, height_field='citygml_me').city self.assertEqual(17, len(full_city.buildings), 'Wrong number of buildings') odd_city = City(full_city.lower_corner, full_city.upper_corner, full_city.srs_name) - par_city = City(full_city.lower_corner, full_city.upper_corner, full_city.srs_name) + even_city = City(full_city.lower_corner, full_city.upper_corner, full_city.srs_name) for building in full_city.buildings: if int(building.name) % 2 == 0: - par_city.add_city_object(copy.deepcopy(building)) + even_city.add_city_object(copy.deepcopy(building)) else: odd_city.add_city_object(copy.deepcopy(building)) self.assertEqual(8, len(odd_city.buildings), 'Wrong number of odd buildings') - self.assertEqual(9, len(par_city.buildings), 'Wrong number of par buildings') - merged_city = odd_city.merge(par_city) + self.assertEqual(9, len(even_city.buildings), 'Wrong number of par buildings') + merged_city = odd_city.merge(even_city) self.assertEqual(17, len(merged_city.buildings), 'Wrong number of buildings in merged city') - merged_city = par_city.merge(odd_city) + merged_city = even_city.merge(odd_city) self.assertEqual(17, len(merged_city.buildings), 'Wrong number of buildings in merged city') - merged_city = full_city.merge(odd_city).merge(par_city) + merged_city = full_city.merge(odd_city).merge(even_city) self.assertEqual(17, len(merged_city.buildings), 'Wrong number of buildings in merged city') def test_merge_with_radiation(self): @@ -57,42 +57,50 @@ class TestCityMerge(TestCase): file_path = Path('./tests_data/test.geojson').resolve() output_path = Path('./tests_outputs/') full_city = GeometryFactory('geojson', file_path, height_field='citygml_me').city - par_city = City(full_city.lower_corner, full_city.upper_corner, full_city.srs_name) + even_city = City(full_city.lower_corner, full_city.upper_corner, full_city.srs_name) for building in full_city.buildings: if int(building.name) % 2 == 0: - par_city.add_city_object(copy.deepcopy(building)) + even_city.add_city_object(copy.deepcopy(building)) ExportsFactory('sra', full_city, output_path).export() sra_file = str((output_path / f'{full_city.name}_sra.xml').resolve()) subprocess.run([sra, sra_file], stdout=subprocess.DEVNULL) ResultFactory('sra', full_city, output_path).enrich() self.assertEqual(17, len(full_city.buildings), 'Wrong number of buildings') - merged_city = full_city.merge(par_city) - merged_city_building_total_radiation = 0 - for building in merged_city.buildings: - for surface in building.surfaces: - if surface.global_irradiance: - merged_city_building_total_radiation += surface.global_irradiance[cte.YEAR].iloc[0, 0] - self.assertEqual(438405954, merged_city_building_total_radiation) - merged_city = par_city.merge(full_city) - merged_city_building_total_radiation = 0 - for building in merged_city.buildings: - for surface in building.surfaces: - if surface.global_irradiance: - merged_city_building_total_radiation += surface.global_irradiance[cte.YEAR].iloc[0, 0] - self.assertEqual(438405954, merged_city_building_total_radiation) + merged_city = full_city.merge(even_city) - for building in par_city.buildings: + full_city_building_total_radiation = 0 + for building in merged_city.buildings: + for surface in building.surfaces: + if surface.global_irradiance: + full_city_building_total_radiation += surface.global_irradiance[cte.YEAR].iloc[0, 0] + + merged_city_building_total_radiation = 0 + for building in merged_city.buildings: + for surface in building.surfaces: + if surface.global_irradiance: + merged_city_building_total_radiation += surface.global_irradiance[cte.YEAR].iloc[0, 0] + self.assertEqual(full_city_building_total_radiation, merged_city_building_total_radiation) + + merged_city = even_city.merge(full_city) + merged_city_building_total_radiation = 0 + for building in merged_city.buildings: + for surface in building.surfaces: + if surface.global_irradiance: + merged_city_building_total_radiation += surface.global_irradiance[cte.YEAR].iloc[0, 0] + self.assertEqual(full_city_building_total_radiation, merged_city_building_total_radiation) + + for building in even_city.buildings: for surface in building.surfaces: surface.global_irradiance[cte.YEAR] = pd.DataFrame([3], columns=['sra_mockup_value']) - merged_city = full_city.merge(par_city) + merged_city = full_city.merge(even_city) merged_city_building_total_radiation = 0 for building in merged_city.buildings: for surface in building.surfaces: if surface.global_irradiance: merged_city_building_total_radiation += surface.global_irradiance[cte.YEAR].iloc[0, 0] self.assertEqual(189336013, merged_city_building_total_radiation) - merged_city = par_city.merge(full_city) + merged_city = even_city.merge(full_city) merged_city_building_total_radiation = 0 for building in merged_city.buildings: for surface in building.surfaces: