Added uvalue unit tests

This commit is contained in:
Khalim Conn-Kowlessar 2023-10-09 06:59:22 +08:00
parent 0da24a955c
commit 9ee8b53cae
4 changed files with 46 additions and 18 deletions

View file

@ -65,7 +65,7 @@ class WallRecommendations(Definitions):
# Current logic: If the property is in a conservation area/heritage building/listed building or a flat, # Current logic: If the property is in a conservation area/heritage building/listed building or a flat,
# it is not suitable for EWI # it is not suitable for EWI
if (not self.property.restricted_measures) or (self.property.data["property-type"].lower() == "flat"): if self.property.restricted_measures or (self.property.data["property-type"].lower() == "flat"):
return False return False
return True return True

View file

@ -70,7 +70,7 @@ solid_floor_insulation_parts = [
parts = suspended_floor_insulation_parts + solid_floor_insulation_parts parts = suspended_floor_insulation_parts + solid_floor_insulation_parts
class TestWallRecommendations: class TestFloorRecommendations:
@pytest.fixture @pytest.fixture
def input_properties(self): def input_properties(self):
@ -86,23 +86,20 @@ class TestWallRecommendations:
property_mock.full_sap_epc = {"lodgement-date": "2000-01-01"} # or any date you want property_mock.full_sap_epc = {"lodgement-date": "2000-01-01"} # or any date you want
property_mock.data = {"construction-age-band": "1950"} # or any other data that fits your tests property_mock.data = {"construction-age-band": "1950"} # or any other data that fits your tests
mock_wall_rec_instance = FloorRecommendations(property_mock, "Decile 1", parts) mock_wall_rec_instance = FloorRecommendations(property_mock, parts)
return mock_wall_rec_instance return mock_wall_rec_instance
def test_init(self, input_properties): def test_init(self, input_properties):
obj = FloorRecommendations( obj = FloorRecommendations(
property_instance=input_properties[0], property_instance=input_properties[0],
total_floor_area_group_decile="Decile 1",
materials=parts materials=parts
) )
assert obj assert obj
assert obj.property assert obj.property
assert obj.total_floor_area_group_decile == "Decile 1"
def test_other_premises_below(self, input_properties): def test_other_premises_below(self, input_properties):
recommender = FloorRecommendations( recommender = FloorRecommendations(
property_instance=input_properties[0], property_instance=input_properties[0],
total_floor_area_group_decile="Decile 1",
materials=parts materials=parts
) )
recommender.recommend() recommender.recommend()
@ -119,16 +116,17 @@ class TestWallRecommendations:
input_properties[2].floor_area = 50 input_properties[2].floor_area = 50
input_properties[2].walls["is_park_home"] = False input_properties[2].walls["is_park_home"] = False
input_properties[2].age_band = "A" input_properties[2].age_band = "A"
input_properties[2].perimeter = 20
input_properties[2].wall_type = "solid brick"
recommender = FloorRecommendations( recommender = FloorRecommendations(
property_instance=input_properties[2], property_instance=input_properties[2],
total_floor_area_group_decile="Decile 1",
materials=parts materials=parts
) )
assert recommender.estimated_u_value is None assert recommender.estimated_u_value is None
recommender.recommend() recommender.recommend()
assert recommender.property.floor["is_suspended"] assert recommender.property.floor["is_suspended"]
assert recommender.estimated_u_value == 0.52 assert recommender.estimated_u_value == 0.39
assert recommender.recommendations assert recommender.recommendations
types = {part["type"] for x in recommender.recommendations for part in x["parts"]} types = {part["type"] for x in recommender.recommendations for part in x["parts"]}
@ -143,7 +141,6 @@ class TestWallRecommendations:
""" """
recommender = FloorRecommendations( recommender = FloorRecommendations(
property_instance=input_properties[3], property_instance=input_properties[3],
total_floor_area_group_decile="Decile 1",
materials=parts materials=parts
) )
assert recommender.estimated_u_value is None assert recommender.estimated_u_value is None
@ -161,17 +158,18 @@ class TestWallRecommendations:
input_properties[4].floor_area = 100 input_properties[4].floor_area = 100
input_properties[4].walls["is_park_home"] = False input_properties[4].walls["is_park_home"] = False
input_properties[4].age_band = "B" input_properties[4].age_band = "B"
input_properties[4].perimeter = 50
input_properties[4].wall_type = "solid brick"
recommender = FloorRecommendations( recommender = FloorRecommendations(
property_instance=input_properties[4], property_instance=input_properties[4],
total_floor_area_group_decile="Decile 1",
materials=parts materials=parts
) )
assert recommender.estimated_u_value is None assert recommender.estimated_u_value is None
recommender.recommend() recommender.recommend()
assert not recommender.property.floor["is_suspended"] assert not recommender.property.floor["is_suspended"]
assert recommender.property.floor["is_solid"] assert recommender.property.floor["is_solid"]
assert recommender.estimated_u_value == 0.63 assert recommender.estimated_u_value == 0.71
assert recommender.recommendations assert recommender.recommendations
types = {part["type"] for x in recommender.recommendations for part in x["parts"]} types = {part["type"] for x in recommender.recommendations for part in x["parts"]}
@ -185,7 +183,6 @@ class TestWallRecommendations:
recommender = FloorRecommendations( recommender = FloorRecommendations(
property_instance=input_properties[6], property_instance=input_properties[6],
total_floor_area_group_decile="Decile 1",
materials=parts materials=parts
) )
assert recommender.estimated_u_value is None assert recommender.estimated_u_value is None

View file

@ -304,3 +304,32 @@ def test_estimate_perimeter_invalid_inputs():
recommendation_utils.estimate_perimeter(-100, 5) recommendation_utils.estimate_perimeter(-100, 5)
with pytest.raises(ValueError): with pytest.raises(ValueError):
recommendation_utils.estimate_perimeter(100, -5) recommendation_utils.estimate_perimeter(100, -5)
def test_solid_floor():
assert math.isclose(
recommendation_utils.get_floor_u_value(
'solid', 100, 40, 'A', 'stone', insulation_thickness="20mm"
),
0.4, rel_tol=1e-2)
def test_suspended_floor():
assert math.isclose(
recommendation_utils.get_floor_u_value(
'suspended', 100, 40, 'A', 'timber frame', insulation_thickness="20mm"
),
0.49, rel_tol=1e-2)
def test_invalid_floor_type():
with pytest.raises(ValueError):
recommendation_utils.get_floor_u_value(
'invalid_type', 100, 40, 'A', 'stone', insulation_thickness="20mm"
)
def test_park_home():
assert recommendation_utils.get_floor_u_value(
'suspended', 100, 40, 'A', 'park home', insulation_thickness="20mm"
) == 0

View file

@ -11,12 +11,6 @@ from recommendations.recommendation_utils import is_diminishing_returns
# os.path.abspath(os.path.dirname(__file__)) + "/recommendations/tests/test_data/input_properties.pkl", "rb" # os.path.abspath(os.path.dirname(__file__)) + "/recommendations/tests/test_data/input_properties.pkl", "rb"
# ) as f: # ) as f:
# input_properties = pickle.load(f) # input_properties = pickle.load(f)
#
# with open(
# os.path.abspath(os.path.dirname(__file__)) + "/recommendations/tests/test_data/uvalue_estimates.pkl", "rb"
# ) as f:
# uvalue_estimates = pickle.load(f)
external_wall_insulation_parts = [ external_wall_insulation_parts = [
{ {
@ -248,6 +242,9 @@ class TestWallRecommendations:
already has really good insulation, we do NOT recommend any measures for this property already has really good insulation, we do NOT recommend any measures for this property
""" """
input_properties[0].year_built = 2014 input_properties[0].year_built = 2014
input_properties[0].in_conservation_area = None
input_properties[0].restricted_measures = False
recommender = WallRecommendations( recommender = WallRecommendations(
property_instance=input_properties[0], property_instance=input_properties[0],
materials=wall_parts materials=wall_parts
@ -271,6 +268,7 @@ class TestWallRecommendations:
input_properties[1].walls["clean_description"] = "Solid brick, as built, no insulation" input_properties[1].walls["clean_description"] = "Solid brick, as built, no insulation"
input_properties[1].walls["is_sandstone_or_limestone"] = False input_properties[1].walls["is_sandstone_or_limestone"] = False
input_properties[1].age_band = "A" input_properties[1].age_band = "A"
input_properties[1].restricted_measures = False
recommender = WallRecommendations( recommender = WallRecommendations(
property_instance=input_properties[1], property_instance=input_properties[1],
@ -307,6 +305,7 @@ class TestWallRecommendations:
""" """
input_properties[6].year_built = 1991 input_properties[6].year_built = 1991
input_properties[6].restricted_measures = False
recommender = WallRecommendations( recommender = WallRecommendations(
property_instance=input_properties[6], property_instance=input_properties[6],
materials=wall_parts materials=wall_parts
@ -383,6 +382,7 @@ class TestWallRecommendationsBase:
property_mock = MagicMock(spec=Property) property_mock = MagicMock(spec=Property)
property_mock.full_sap_epc = {"lodgement-date": "1999-12-31"} property_mock.full_sap_epc = {"lodgement-date": "1999-12-31"}
property_mock.in_conservation_area = "not_in_conservation_area" property_mock.in_conservation_area = "not_in_conservation_area"
property_mock.restricted_measures = False
return property_mock return property_mock
@pytest.fixture @pytest.fixture
@ -394,6 +394,7 @@ class TestWallRecommendationsBase:
def test_ewi_valid_in_conservation_area(self, wall_recommendations_instance): def test_ewi_valid_in_conservation_area(self, wall_recommendations_instance):
wall_recommendations_instance.property.in_conservation_area = "in_conversation_area" wall_recommendations_instance.property.in_conservation_area = "in_conversation_area"
wall_recommendations_instance.property.restricted_measures = True
assert wall_recommendations_instance.ewi_valid is False assert wall_recommendations_instance.ewi_valid is False
def test_ewi_valid_is_flat(self, wall_recommendations_instance): def test_ewi_valid_is_flat(self, wall_recommendations_instance):
@ -402,6 +403,7 @@ class TestWallRecommendationsBase:
def test_ewi_valid_not_in_conservation_area_and_not_flat(self, wall_recommendations_instance): def test_ewi_valid_not_in_conservation_area_and_not_flat(self, wall_recommendations_instance):
wall_recommendations_instance.property.in_conservation_area = "not_in_conversation_area" wall_recommendations_instance.property.in_conservation_area = "not_in_conversation_area"
wall_recommendations_instance.property.restricted_measures = False
wall_recommendations_instance.property.data = {"property-type": "house"} wall_recommendations_instance.property.data = {"property-type": "house"}
assert wall_recommendations_instance.ewi_valid is True assert wall_recommendations_instance.ewi_valid is True