diff --git a/datatypes/epc/domain/mapper.py b/datatypes/epc/domain/mapper.py index 064a42d09..39a541b8f 100644 --- a/datatypes/epc/domain/mapper.py +++ b/datatypes/epc/domain/mapper.py @@ -2946,11 +2946,12 @@ class EpcPropertyDataMapper: return [EpcPropertyDataMapper._map_energy_element(e) for e in elements] @staticmethod - def from_api_response(data: Dict[str, Any]) -> "EpcPropertyData": + def from_api_response(data: Dict[str, Any]) -> Optional["EpcPropertyData"]: """ Dispatch to the correct schema mapper based on schema_type. - Supports RdSAP-Schema-21.0.0 and RdSAP-Schema-21.0.1 only. - Raises ValueError for unsupported schemas — add cases here as needed. + Any `schema_type` not in the allow-list below (the schemas with a + dedicated mapper) is logged as a warning and skipped — returns None + rather than raising, so one unrecognised cert doesn't break a batch. """ data = _normalize_shower_outlets(data) @@ -3053,7 +3054,12 @@ class EpcPropertyDataMapper: "mapped to EpcPropertyData, which models a single dwelling." ) else: - raise ValueError(f"Unsupported EPC schema: {schema!r}") + logger.warning( + "Unsupported EPC schema %r — skipping this EPC (uprn=%s)", + schema, + data.get("uprn"), + ) + return None return _clear_basement_flag_when_system_built( _with_renewable_heat_incentive( diff --git a/infrastructure/epc_client/epc_client_service.py b/infrastructure/epc_client/epc_client_service.py index 692b10a2f..8cc50ca8f 100644 --- a/infrastructure/epc_client/epc_client_service.py +++ b/infrastructure/epc_client/epc_client_service.py @@ -38,7 +38,7 @@ class EpcClientService: except (TypeError, ValueError): return None - def get_by_certificate_number(self, cert_num: str) -> EpcPropertyData: + def get_by_certificate_number(self, cert_num: str) -> Optional[EpcPropertyData]: raw = call_with_retry(lambda: self._fetch_certificate(cert_num)) return EpcPropertyDataMapper.from_api_response(raw) diff --git a/tests/infrastructure/epc_client/test_mapper_dispatcher.py b/tests/infrastructure/epc_client/test_mapper_dispatcher.py index efb9c4ec4..805cb40b1 100644 --- a/tests/infrastructure/epc_client/test_mapper_dispatcher.py +++ b/tests/infrastructure/epc_client/test_mapper_dispatcher.py @@ -1,5 +1,3 @@ -import pytest - from datatypes.epc.domain.mapper import EpcPropertyDataMapper from datatypes.epc.domain.epc_property_data import EpcPropertyData @@ -23,9 +21,13 @@ def test_from_api_response_rdsap_21_0_1(rdsap_21_0_1_cert): # --------------------------------------------------------------------------- -# Test 3: unknown schema_type → ValueError +# Test 3: unknown schema_type → logged warning, None returned (not raised) # --------------------------------------------------------------------------- -def test_from_api_response_unknown_schema_raises(): - with pytest.raises(ValueError, match="Unsupported EPC schema"): - EpcPropertyDataMapper.from_api_response({"schema_type": "RdSAP-Schema-99.0.0"}) +def test_from_api_response_unknown_schema_returns_none(caplog): + with caplog.at_level("WARNING"): + result = EpcPropertyDataMapper.from_api_response( + {"schema_type": "RdSAP-Schema-99.0.0"} + ) + assert result is None + assert "RdSAP-Schema-99.0.0" in caplog.text