Model/tests/domain/modelling/test_overlay_applicator.py
Khalim Conn-Kowlessar 139c90c885 feat(modelling): whole-dwelling LightingOverlay surface on EpcSimulation
Slice 1 of the lighting generator (ADR-0023): the first whole-dwelling,
top-level overlay surface. LightingOverlay carries the four fixed-lighting
bulb-count fields by their exact EPC names (all Optional, absolute counts) +
EpcSimulation.lighting. The applicator's _fold_lighting writes the non-None
counts directly onto the result EpcPropertyData by name (setattr) — simpler
than ventilation's nested fold since the counts live top-level. Baseline
unmutated; pyright strict clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-05 12:21:19 +00:00

269 lines
8.8 KiB
Python

"""Behaviour of the Overlay Applicator: folding Simulation Overlays
(EpcSimulation) onto a baseline EpcPropertyData to produce a new one for
the calculator. See ADR-0016 and the Modelling glossary in CONTEXT.md.
"""
from datatypes.epc.domain.epc_property_data import (
BuildingPartIdentifier,
EpcPropertyData,
SapBuildingPart,
SapVentilation,
)
from domain.modelling.simulation import (
BuildingPartOverlay,
EpcSimulation,
LightingOverlay,
VentilationOverlay,
WindowOverlay,
)
from domain.modelling.scoring.overlay_applicator import apply_simulations
from tests.domain.sap10_calculator.worksheet._elmhurst_worksheet_000490 import (
build_epc,
)
def _part(epc: EpcPropertyData, identifier: BuildingPartIdentifier) -> SapBuildingPart:
return next(p for p in epc.sap_building_parts if p.identifier is identifier)
def test_apply_writes_targeted_building_part_and_leaves_others_untouched() -> None:
# Arrange
baseline: EpcPropertyData = build_epc()
extension_before: int | str = _part(
baseline, BuildingPartIdentifier.EXTENSION_1
).wall_insulation_type
simulation = EpcSimulation(
building_parts={
BuildingPartIdentifier.MAIN: BuildingPartOverlay(wall_insulation_type=1)
}
)
# Act
result: EpcPropertyData = apply_simulations(baseline, [simulation])
# Assert
assert _part(result, BuildingPartIdentifier.MAIN).wall_insulation_type == 1
assert (
_part(result, BuildingPartIdentifier.EXTENSION_1).wall_insulation_type
== extension_before
)
def test_empty_simulation_is_a_no_op() -> None:
# Arrange
baseline: EpcPropertyData = build_epc()
# Act
result: EpcPropertyData = apply_simulations(baseline, [EpcSimulation()])
# Assert
assert result == baseline
def test_later_simulation_wins_on_a_shared_field() -> None:
# Arrange
baseline: EpcPropertyData = build_epc()
first = EpcSimulation(
building_parts={
BuildingPartIdentifier.MAIN: BuildingPartOverlay(wall_insulation_type=1)
}
)
second = EpcSimulation(
building_parts={
BuildingPartIdentifier.MAIN: BuildingPartOverlay(wall_insulation_type=2)
}
)
# Act
result: EpcPropertyData = apply_simulations(baseline, [first, second])
# Assert
assert _part(result, BuildingPartIdentifier.MAIN).wall_insulation_type == 2
def test_apply_writes_dwelling_ventilation_onto_sap_ventilation() -> None:
# Arrange — a Measure Dependency overlay targets the whole-dwelling
# ventilation system (no building part), e.g. retrofit MEV.
baseline: EpcPropertyData = build_epc()
simulation = EpcSimulation(
ventilation=VentilationOverlay(
mechanical_ventilation_kind="EXTRACT_OR_PIV_OUTSIDE"
)
)
# Act
result: EpcPropertyData = apply_simulations(baseline, [simulation])
# Assert
assert result.sap_ventilation is not None
assert (
result.sap_ventilation.mechanical_ventilation_kind
== "EXTRACT_OR_PIV_OUTSIDE"
)
def test_ventilation_overlay_creates_sap_ventilation_when_baseline_has_none() -> None:
# Arrange — a naturally-ventilated baseline that lodged no SapVentilation.
baseline: EpcPropertyData = build_epc()
baseline.sap_ventilation = None
simulation = EpcSimulation(
ventilation=VentilationOverlay(
mechanical_ventilation_kind="EXTRACT_OR_PIV_OUTSIDE"
)
)
# Act
result: EpcPropertyData = apply_simulations(baseline, [simulation])
# Assert
assert isinstance(result.sap_ventilation, SapVentilation)
assert (
result.sap_ventilation.mechanical_ventilation_kind
== "EXTRACT_OR_PIV_OUTSIDE"
)
def test_ventilation_overlay_leaves_building_parts_and_baseline_untouched() -> None:
# Arrange
baseline: EpcPropertyData = build_epc()
main_before: int | str = _part(
baseline, BuildingPartIdentifier.MAIN
).wall_insulation_type
simulation = EpcSimulation(
ventilation=VentilationOverlay(
mechanical_ventilation_kind="EXTRACT_OR_PIV_OUTSIDE"
)
)
# Act
result: EpcPropertyData = apply_simulations(baseline, [simulation])
# Assert — ventilation overlay touches only sap_ventilation; the baseline
# is never mutated.
assert _part(result, BuildingPartIdentifier.MAIN).wall_insulation_type == main_before
assert baseline.sap_ventilation is not None
assert baseline.sap_ventilation.mechanical_ventilation_kind is None
def test_baseline_is_not_mutated() -> None:
# Arrange
baseline: EpcPropertyData = build_epc()
original: int | str = _part(
baseline, BuildingPartIdentifier.MAIN
).wall_insulation_type
# Act
_: EpcPropertyData = apply_simulations(
baseline,
[
EpcSimulation(
building_parts={
BuildingPartIdentifier.MAIN: BuildingPartOverlay(
wall_insulation_type=1
)
}
)
],
)
# Assert
assert (
_part(baseline, BuildingPartIdentifier.MAIN).wall_insulation_type == original
)
def test_apply_folds_a_window_overlay_by_index_into_transmission_details() -> None:
# Arrange — window 0 starts double (glazing_type 2, U 2.8, g 0.76); the
# overlay upgrades it to a modern double spec, writing the U-value and
# solar-g into the nested WindowTransmissionDetails (ADR-0022).
baseline: EpcPropertyData = build_epc()
# Act — target window 0 by its sap_windows index.
result: EpcPropertyData = apply_simulations(
baseline,
[
EpcSimulation(
windows={
0: WindowOverlay(
glazing_type=5, u_value=1.40, solar_transmittance=0.72
)
}
)
],
)
# Assert — glazing_type set on the window; U/g routed into the transmission
# details (where the cascade reads them); other windows untouched.
upgraded = result.sap_windows[0]
assert upgraded.glazing_type == 5
assert upgraded.window_transmission_details is not None
assert abs(upgraded.window_transmission_details.u_value - 1.40) <= 1e-9
assert abs(upgraded.window_transmission_details.solar_transmittance - 0.72) <= 1e-9
assert result.sap_windows[1].window_transmission_details is not None
assert abs(result.sap_windows[1].window_transmission_details.u_value - 2.8) <= 1e-9
def test_baseline_windows_are_not_mutated_by_a_window_overlay() -> None:
# Arrange
baseline: EpcPropertyData = build_epc()
assert baseline.sap_windows[0].window_transmission_details is not None
original_u: float = baseline.sap_windows[0].window_transmission_details.u_value
# Act
_: EpcPropertyData = apply_simulations(
baseline,
[EpcSimulation(windows={0: WindowOverlay(u_value=1.40)})],
)
# Assert
assert baseline.sap_windows[0].window_transmission_details is not None
assert (
baseline.sap_windows[0].window_transmission_details.u_value == original_u
)
def test_apply_writes_dwelling_lighting_onto_top_level_bulb_counts() -> None:
# Arrange — a whole-dwelling lighting change (no building part), e.g. an
# all-LED upgrade folded onto the top-level bulb counts (ADR-0023).
baseline: EpcPropertyData = build_epc()
simulation = EpcSimulation(
lighting=LightingOverlay(
led_fixed_lighting_bulbs_count=8,
cfl_fixed_lighting_bulbs_count=0,
incandescent_fixed_lighting_bulbs_count=0,
low_energy_fixed_lighting_bulbs_count=0,
)
)
# Act
result: EpcPropertyData = apply_simulations(baseline, [simulation])
# Assert
assert result.led_fixed_lighting_bulbs_count == 8
assert result.cfl_fixed_lighting_bulbs_count == 0
assert result.incandescent_fixed_lighting_bulbs_count == 0
assert result.low_energy_fixed_lighting_bulbs_count == 0
def test_baseline_lighting_is_not_mutated_by_a_lighting_overlay() -> None:
# Arrange — 000490 lodges 8 low-energy-unknown bulbs, 0 LED.
baseline: EpcPropertyData = build_epc()
original_led: int = baseline.led_fixed_lighting_bulbs_count
original_lel: int = baseline.low_energy_fixed_lighting_bulbs_count
# Act — fold an all-LED overlay (led = the 8 total).
_: EpcPropertyData = apply_simulations(
baseline,
[
EpcSimulation(
lighting=LightingOverlay(
led_fixed_lighting_bulbs_count=8,
low_energy_fixed_lighting_bulbs_count=0,
)
)
],
)
# Assert — the baseline's counts are untouched.
assert baseline.led_fixed_lighting_bulbs_count == original_led
assert baseline.low_energy_fixed_lighting_bulbs_count == original_lel