import pytest from unittest.mock import MagicMock from recommendations import recommendation_utils class TestRecommendationUtils: @pytest.fixture def property_mock(self): PropertyMock = MagicMock() PropertyMock.data = { 'total_floor_area_group_decile': 'Decile 1', 'number-habitable-rooms': 3, 'number-heated-rooms': 2 } return PropertyMock def test_r_value_per_mm_to_u_value(self): assert recommendation_utils.r_value_per_mm_to_u_value(1, 2) == 0.5 with pytest.raises(ZeroDivisionError): recommendation_utils.r_value_per_mm_to_u_value(0, 2) def test_calculate_u_value_uplift(self): assert recommendation_utils.calculate_u_value_uplift(1, 2) == (0.33333333333333337, 0.6666666666666666) with pytest.raises(ZeroDivisionError): recommendation_utils.calculate_u_value_uplift(0, 2) with pytest.raises(ZeroDivisionError): recommendation_utils.calculate_u_value_uplift(1, 0) def test_is_diminishing_returns(self): assert not recommendation_utils.is_diminishing_returns([1, 2, 3], 1, 1, 1) assert recommendation_utils.is_diminishing_returns([1, 2, 3], 0.5, 1, 1) assert not recommendation_utils.is_diminishing_returns([], 1, None, 1) def test_update_lowest_selected_u_value(self): assert recommendation_utils.update_lowest_selected_u_value(1, 2) == 1 assert recommendation_utils.update_lowest_selected_u_value(None, 2) == 2 assert recommendation_utils.update_lowest_selected_u_value(1, 0.5) == 0.5 def test_get_recommended_part(self): part = {'depths': [1, 2, 3]} assert recommendation_utils.get_recommended_part(part, 1) == {'depths': [1]} def test_get_uvalue_estimate(self, property_mock): uvalue_estimates = [ { 'total-floor-area_group': 'Decile 1', 'number-habitable-rooms': 3, 'number-heated-rooms': 2, 'median_thermal_transmittance': 1 }, { 'total-floor-area_group': 'Decile 1', 'number-habitable-rooms': 3, 'number-heated-rooms': 2, 'median_thermal_transmittance': 2 } ] assert recommendation_utils.get_uvalue_estimate(uvalue_estimates, property_mock, "Decile 1") == 1.5 with pytest.raises(ValueError): recommendation_utils.get_uvalue_estimate([], property_mock, "Decile 1") # Test with missing 'median_thermal_transmittance' key uvalue_estimates_missing_key = [ { 'total-floor-area_group': 'Decile 1', 'number-habitable-rooms': 3, 'number-heated-rooms': 2 } ] with pytest.raises(KeyError): recommendation_utils.get_uvalue_estimate(uvalue_estimates_missing_key, property_mock, "Decile 1")