Model/tests/domain/modelling/test_solar_potential.py
Khalim Conn-Kowlessar f31d5bcff9 feat(modelling): typed SolarPotential projection over Google buildingInsights
Slice 2 of the Solar PV Recommendation Generator (ADR-0026). Adds the
strictly-typed `SolarPotential` domain projection over the raw Google Solar
`buildingInsights` JSON that Ingestion persists (SolarRepository): the
`solarPanelConfigs` ladder, each rung broken into its roof segments with
Google's continuous azimuth/tilt mapped to the SAP octant
(`azimuth_to_sap_octant`, 0°=N clockwise → 1=N..8=NW, matching the
calculator's ORIENTATION_BY_SAP10_CODE) and RdSAP §11.1 pitch code
(`pitch_to_sap_code`, snap to {0→1,30→2,45→3,60→4,90→5}).

Pinned against the real London buildingInsights example (mirrored into
fixtures from the user-provided RTF): 400 W panels, maxArrayPanelsCount 49,
46-rung ladder, per-segment SE/NW/NE/SW octants at ~32° → pitch code 2.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-08 09:55:55 +00:00

102 lines
3.3 KiB
Python

"""Slice 2 — the typed `SolarPotential` projection over a Google Solar
`buildingInsights` response (ADR-0026).
Pins the orientation/pitch mappings and the projection against the real
London `buildingInsights` example (mirrored into fixtures from the
user-provided RTF).
"""
import json
from pathlib import Path
from typing import Any
from domain.modelling.solar_potential import (
SolarPotential,
azimuth_to_sap_octant,
pitch_to_sap_code,
)
_FIXTURE: Path = (
Path(__file__).resolve().parent
/ "fixtures"
/ "google_building_insights_001431.json"
)
def _insights() -> dict[str, Any]:
with _FIXTURE.open(encoding="utf-8") as handle:
data: dict[str, Any] = json.load(handle)
return data
def test_azimuth_to_sap_octant_cardinals_and_diagonals() -> None:
# Arrange / Act / Assert — Google azimuth 0=N clockwise → SAP octant code
assert azimuth_to_sap_octant(0.0) == 1 # N
assert azimuth_to_sap_octant(45.0) == 2 # NE
assert azimuth_to_sap_octant(90.0) == 3 # E
assert azimuth_to_sap_octant(135.0) == 4 # SE
assert azimuth_to_sap_octant(180.0) == 5 # S
assert azimuth_to_sap_octant(225.0) == 6 # SW
assert azimuth_to_sap_octant(270.0) == 7 # W
assert azimuth_to_sap_octant(315.0) == 8 # NW
assert azimuth_to_sap_octant(360.0) == 1 # wraps to N
def test_pitch_to_sap_code_snaps_to_rdsap_enum() -> None:
# Arrange / Act / Assert — RdSAP 10 §11.1 fixed tilts
assert pitch_to_sap_code(0.0) == 1
assert pitch_to_sap_code(30.0) == 2
assert pitch_to_sap_code(45.0) == 3
assert pitch_to_sap_code(60.0) == 4
assert pitch_to_sap_code(90.0) == 5
# Real Google pitches (~32-34°) snap to the 30° code
assert pitch_to_sap_code(33.65681) == 2
assert pitch_to_sap_code(31.896425) == 2
def test_projection_reads_potential_level_fields() -> None:
# Arrange
insights = _insights()
# Act
potential = SolarPotential.from_building_insights(insights)
# Assert
assert abs(potential.panel_capacity_watts - 400.0) <= 1e-4
assert potential.max_array_panels_count == 49
assert len(potential.configurations) == 46
def test_projection_first_config_single_segment() -> None:
# Arrange
insights = _insights()
# Act
potential = SolarPotential.from_building_insights(insights)
first = potential.configurations[0]
# Assert — the smallest rung: 4 panels on one SE roof plane
assert first.panels_count == 4
assert len(first.segments) == 1
segment = first.segments[0]
assert segment.segment_index == 1
assert segment.panels_count == 4
assert abs(segment.azimuth_degrees - 136.27895) <= 1e-4
assert abs(segment.yearly_energy_dc_kwh - 1617.0192) <= 1e-4
assert segment.sap_orientation == 4 # SE
assert segment.sap_pitch_code == 2 # ~32° → 30°
def test_projection_largest_config_spans_all_segments() -> None:
# Arrange
insights = _insights()
# Act
potential = SolarPotential.from_building_insights(insights)
largest = potential.configurations[-1]
# Assert — the 49-panel rung spans all four roof planes
assert largest.panels_count == 49
assert sum(s.panels_count for s in largest.segments) == 49
octants = {s.sap_orientation for s in largest.segments}
assert octants == {8, 4, 2, 6} # NW, SE, NE, SW