Added tests for checking ventilation

This commit is contained in:
Khalim Conn-Kowlessar 2026-02-23 13:35:31 +00:00
parent 5305643991
commit 84aef79735
3 changed files with 87 additions and 3 deletions

View file

@ -1060,9 +1060,10 @@ async def model_engine(body: PlanTriggerRequest):
# If a measure requiring ventilation is selected, and the property does not have ventilation, we enfore
# its inclusion
needs_ventilation = any(
x in property_measure_types for x in assumptions.measures_needing_ventilation
) and not p.has_ventilation and ventilation_included
needs_ventilation = optimiser_functions.check_needs_ventilation(
property_measure_types, assumptions.measures_needing_ventilation, p.has_ventilation,
ventilation_included
)
if not measures_to_optimise:
# Nothing to do, we just reshape the recommendations

View file

@ -398,3 +398,26 @@ def flatten_recommendations_with_defaults(property_id, recommendations, selected
# Flatten the nested list of lists into a single list
return [rec for recommendations_by_type in final_recommendations for rec in recommendations_by_type]
def check_needs_ventilation(
property_measure_types: Set[str],
measures_needing_ventilation: List[str],
has_ventilation: bool,
ventilation_included: bool
) -> bool:
"""
Function to check if we need to include ventilation based on the measures selected and the property
features
:param property_measure_types: The set of measure types recommended for the property
:param measures_needing_ventilation: The set of measure types that require ventilation
:param has_ventilation: Whether the property currently has ventilation
:param ventilation_included: Whether ventilation is already included in the recommended measures
:return: Boolean indicating whether ventilation needs to be included in the recommendations
# TODO - none of the inputs of this function are well structured and so this is quite brittle - we should
consider refactoring to make this more robust
"""
return any(
x in property_measure_types for x in measures_needing_ventilation
) and not has_ventilation and ventilation_included

View file

@ -510,3 +510,63 @@ class TestStrategicOptimiser:
assert opt.strategy_used.value == "case_2_solve_max_gain_under_budget"
assert opt.solution_cost == 7787.068
assert opt.solution_gain == 28.8
class TestCheckNeedsVentilation:
def measure_types_includes_ventilation_no_existing_ventilation(self):
property_measure_types = {'mechanical_ventilation', 'cavity_wall_insulation', 'suspended_floor_insulation',
'secondary_heating', 'loft_insulation', 'heating', 'low_energy_lighting'}
measures_needing_ventilation = ['internal_wall_insulation', 'external_wall_insulation',
'cavity_wall_insulation']
has_ventilation = False
ventilation_included = True
result = optimiser_functions.check_needs_ventilation(
property_measure_types, measures_needing_ventilation, has_ventilation,
ventilation_included
)
assert result == True
def measure_types_includes_ventilation_existing_ventilation(self):
property_measure_types = {'mechanical_ventilation', 'cavity_wall_insulation', 'suspended_floor_insulation',
'secondary_heating', 'loft_insulation', 'heating', 'low_energy_lighting'}
measures_needing_ventilation = ['internal_wall_insulation', 'external_wall_insulation',
'cavity_wall_insulation']
has_ventilation = True
ventilation_included = True
result = optimiser_functions.check_needs_ventilation(
property_measure_types, measures_needing_ventilation, has_ventilation,
ventilation_included
)
assert result == False
def measure_types_includes_ventilation_existing_ventilation(self):
property_measure_types_without_ventilation = {
'cavity_wall_insulation', 'suspended_floor_insulation',
'secondary_heating', 'loft_insulation', 'heating',
'low_energy_lighting'
}
measures_needing_ventilation = ['internal_wall_insulation', 'external_wall_insulation',
'cavity_wall_insulation']
has_ventilation = False
ventilation_included = True
result = optimiser_functions.check_needs_ventilation(
property_measure_types_without_ventilation, measures_needing_ventilation, has_ventilation,
ventilation_included
)
assert result == False