Model/epc_data/tests/test_EpcClean.py
2023-06-08 16:02:50 +01:00

94 lines
3.4 KiB
Python

import pytest
import pickle
from epc_data.EpcClean import EpcClean
from pathlib import Path
from epc_data.tests.test_data.EpcClean_test_roof_cases import clean_roof_test_cases
# For local testing
if __file__ == "<input>":
input_data_path = Path("./epc_data/tests/test_data/EpcClean_inputs.obj")
else:
current_file_path = Path(__file__)
input_data_path = current_file_path.parent / 'test_data' / 'EpcClean_inputs.obj'
class TestEpcClean:
@staticmethod
def load_data(path):
with open(path, "rb") as file:
return pickle.load(file)
@pytest.fixture(autouse=True)
def setup_class(self):
self.cleaner = EpcClean(self.load_data(input_data_path))
def test_clean(self):
self.cleaner.clean()
assert len(self.cleaner.cleaned["roof-description"]) == len(self.cleaner.unique_vals["roof-description"])
def test__init_empty_cleaned_obj(self):
self.cleaner._init_empty_cleaned_obj()
assert all([len(values) == 0 for values in self.cleaner.cleaned.values()])
def test__search_split_roof_description(self):
assert self.cleaner._search_split_roof_description("insulated") == "average"
assert self.cleaner._search_split_roof_description("limited") == "below average"
with pytest.raises(NotImplementedError):
self.cleaner._search_split_roof_description("unknown")
def test__find_insulation_thickness(self):
assert self.cleaner._find_insulation_thickness("no insulation", False, False, False) == 0
def test__extract_thermal_transmittence(self):
description = "U-value of 2.3 w/m-¦k"
assert self.cleaner._extract_thermal_transmittence(description) == (2.3, "w/m-¦k")
def test_clean_roof(self):
result = self.cleaner.clean_roof('Pitched, 270 mm loft insulation')
# change the expected output based on your requirement
expected_output = {
"is_valid": True,
"at_rafters": False,
"is_pitched": True,
"is_roof_room": False,
"has_loft": True,
"insulation_thickness": 270,
"has_dwelling_above": False,
"assumed": False,
"is_flat": False,
"is_thatched": False,
"thermal_transmittence": None,
"thermal_transmittence_unit": None
}
assert result == expected_output
for test_case in clean_roof_test_cases:
result = self.cleaner.clean_roof(test_case['original_description'])
# Ensure the output ordering is correct
expected_result = {key: test_case[key] for key in result.keys()}
expected_result["desc"] = test_case["original_description"]
result["desc"] = test_case["original_description"]
assert result == expected_result
def test_clean_roof_with_dwelling_above(self):
result = self.cleaner.clean_roof('(another dwelling above)')
expected_output = {
"is_valid": True,
"at_rafters": False,
"is_pitched": False,
"is_roof_room": False,
"has_loft": False,
"insulation_thickness": 0,
"has_dwelling_above": True,
"assumed": False,
"is_flat": False,
"is_thatched": False,
"thermal_transmittence": None,
"thermal_transmittence_unit": None,
}
assert result == expected_output