Model/tests/domain/property_baseline/test_sap_fuel.py
Khalim Conn-Kowlessar d559298de2 feat(baseline): sap_code_to_fuel normalizes via the calculator's own helper
The fuel codes the calculator now puts on SapResult are its own codes — raw
gov-API enums or already-Table-32, depending on the source mapper (ADR-0015).
sap_code_to_fuel now runs the code through table_32.to_table_32_code
(promoted from private _to_table_32_code) — T32-first, then API-translate,
the SAME normalization the calculator's pricing/CO2 helpers use — before the
Table-32 -> Fuel dispatch, so the bill's carrier matches what the calculator
billed (incl. the API/T32 collision codes, e.g. 20 = wood-logs not heat-net).

Falls back to the raw code for billing fuels the price table omits (the 41-58
heat-network range), which resolve to HEAT_NETWORK -> UnpricedFuel — stricter
than, and intentionally divergent from, the calculator's lossy
default-to-mains-gas for an unpriced code (ADR-0014 §5).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-05 18:59:25 +00:00

58 lines
2 KiB
Python

from __future__ import annotations
import pytest
from domain.fuel_rates.fuel import Fuel
from domain.property_baseline.sap_fuel import sap_code_to_fuel
from domain.sap10_calculator.exceptions import UnmappedSapCode
def test_mains_gas_code_maps_to_mains_gas() -> None:
# Arrange / Act / Assert — Table 32 code 1 is mains gas.
assert sap_code_to_fuel(1) == Fuel.MAINS_GAS
@pytest.mark.parametrize(
("code", "fuel"),
[
(1, Fuel.MAINS_GAS),
(2, Fuel.LPG),
(4, Fuel.OIL),
(76, Fuel.OIL), # bioethanol — a liquid fuel row
(11, Fuel.COAL), # house coal
(15, Fuel.COAL), # anthracite
(12, Fuel.SMOKELESS),
(20, Fuel.WOOD_LOGS),
(23, Fuel.WOOD_PELLETS),
(30, Fuel.ELECTRICITY), # standard tariff
(32, Fuel.ELECTRICITY_OFF_PEAK), # 7-hour tariff
(41, Fuel.HEAT_NETWORK), # heat from electric heat pump (community)
(50, Fuel.HEAT_NETWORK), # electricity for distribution pumping
],
)
def test_table_32_codes_map_to_their_billing_fuel(code: int, fuel: Fuel) -> None:
# Arrange / Act / Assert
assert sap_code_to_fuel(code) == fuel
@pytest.mark.parametrize(
("api_code", "fuel"),
[
(26, Fuel.MAINS_GAS), # gov-API mains-gas enum -> Table 32 code 1
(0, Fuel.ELECTRICITY), # API "electricity" -> Table 32 code 30
(25, Fuel.HEAT_NETWORK), # API community heat -> Table 32 code 41
(14, Fuel.COAL), # API house coal -> Table 32 code 11
],
)
def test_raw_api_fuel_codes_normalize_before_mapping(api_code: int, fuel: Fuel) -> None:
# Arrange — the calculator may carry a raw gov-API fuel code (not yet a Table
# 32 code); sap_code_to_fuel normalizes via the calculator's own helper first.
# Act / Assert
assert sap_code_to_fuel(api_code) == fuel
def test_an_unmapped_code_raises_rather_than_guessing() -> None:
# Arrange — code 10 (dual fuel) has no single billing fuel.
# Act / Assert
with pytest.raises(UnmappedSapCode):
sap_code_to_fuel(10)