costs/tests/unit_tests.py

90 lines
3.5 KiB
Python
Raw Normal View History

import datetime
2023-07-18 16:31:49 -04:00
import glob
import os
import subprocess
import unittest
from pathlib import Path
from hub.exports.energy_building_exports_factory import EnergyBuildingsExportsFactory
from hub.exports.exports_factory import ExportsFactory
from hub.imports.construction_factory import ConstructionFactory
from hub.imports.energy_systems_factory import EnergySystemsFactory
from hub.imports.geometry_factory import GeometryFactory
from hub.imports.results_factory import ResultFactory
from hub.imports.usage_factory import UsageFactory
from hub.helpers.dictionaries import Dictionaries
from costs.cost import Cost
from costs.constants import SKIN_RETROFIT, SKIN_RETROFIT_AND_SYSTEM_RETROFIT_AND_PV, SYSTEM_RETROFIT_AND_PV
2023-07-28 13:33:47 -04:00
2023-07-18 16:31:49 -04:00
2023-07-21 16:59:24 -04:00
class Initialize:
def __init__(self):
self._city = None
@property
def city(self):
if self._city is None:
start = datetime.datetime.now()
2023-07-21 16:59:24 -04:00
print('init tests')
city_file = (Path(__file__).parent / 'data/test.geojson').resolve()
output_path = (Path(__file__).parent / 'output').resolve()
city = GeometryFactory('geojson',
city_file,
height_field='citygml_me',
year_of_construction_field='ANNEE_CONS',
function_field='CODE_UTILI',
function_to_hub=Dictionaries().montreal_function_to_hub_function).city
ConstructionFactory('nrcan', city).enrich()
UsageFactory('nrcan', city).enrich()
ExportsFactory('sra', city, output_path).export()
sra_file = str((output_path / f'{city.name}_sra.xml').resolve())
subprocess.run(['sra', sra_file], stdout=subprocess.DEVNULL)
ResultFactory('sra', city, output_path).enrich()
2023-08-10 12:36:31 -04:00
print(f'sra completed {datetime.datetime.now() - start}')
2023-07-21 16:59:24 -04:00
for building in city.buildings:
building.energy_systems_archetype_name = 'system 1 gas pv'
EnergySystemsFactory('montreal_custom', city).enrich()
2023-08-10 12:36:31 -04:00
print(f'energy systems completed {datetime.datetime.now() - start}')
2023-07-21 16:59:24 -04:00
EnergyBuildingsExportsFactory('insel_monthly_energy_balance', city, output_path).export()
_insel_files = glob.glob(f'{output_path}/*.insel')
for insel_file in _insel_files:
subprocess.run(['insel', str(insel_file)], stdout=subprocess.DEVNULL)
2023-08-10 12:36:31 -04:00
print(f'insel completed {datetime.datetime.now() - start}')
2023-07-21 16:59:24 -04:00
ResultFactory('insel_monthly_energy_balance', city, output_path).enrich()
self._city = city
print(f'init completed {datetime.datetime.now() - start}')
2023-07-21 16:59:24 -04:00
return self._city
2023-07-18 16:31:49 -04:00
2023-07-21 16:59:24 -04:00
class UnitTests(unittest.TestCase):
init = Initialize()
2023-07-18 16:31:49 -04:00
def test_current_status(self):
2023-07-21 16:59:24 -04:00
for building in self.init.city.buildings:
2023-07-18 16:31:49 -04:00
result = Cost(building).life_cycle
self.assertIsNotNone(result)
2023-07-19 13:14:52 -04:00
self.assertEqual(0, result.values[0])
2023-07-18 16:31:49 -04:00
def test_scenario_1(self):
2023-07-21 16:59:24 -04:00
for building in self.init.city.buildings:
2023-07-18 16:31:49 -04:00
result = Cost(building, retrofit_scenario=SKIN_RETROFIT).life_cycle
self.assertIsNotNone(result)
def test_scenario_2(self):
2023-07-21 16:59:24 -04:00
for building in self.init.city.buildings:
2023-07-18 16:31:49 -04:00
result = Cost(building, retrofit_scenario=SYSTEM_RETROFIT_AND_PV).life_cycle
self.assertIsNotNone(result)
self.assertEqual(0, result.values[0])
def test_scenario_3(self):
2023-07-21 16:59:24 -04:00
for building in self.init.city.buildings:
2023-07-18 16:31:49 -04:00
result = Cost(building, retrofit_scenario=SKIN_RETROFIT_AND_SYSTEM_RETROFIT_AND_PV).life_cycle
self.assertIsNotNone(result)
def tearDown(self):
files = glob.glob('output/[!.]*')
for file in files:
os.unlink(file)