diff --git a/tests/repositories/historic_epc/test_historic_epc_s3_repository.py b/tests/repositories/historic_epc/test_historic_epc_s3_repository.py index 7bebcb651..2f84d7e65 100644 --- a/tests/repositories/historic_epc/test_historic_epc_s3_repository.py +++ b/tests/repositories/historic_epc/test_historic_epc_s3_repository.py @@ -13,6 +13,7 @@ from contextlib import contextmanager from unittest.mock import patch import pandas as pd +import pytest from botocore.exceptions import ClientError from backend.utils.addressMatch import AddressMatch @@ -74,3 +75,36 @@ def test_valid_postcode_with_no_stored_object_returns_empty_list(): # Assert — a miss is the normal, expected outcome, not an exception. assert records == [] + + +def test_builds_s3_key_from_sanitised_postcode_and_default_root(): + # Arrange + calls: list[tuple[str, str]] = [] + + def capture(bucket: str, key: str) -> pd.DataFrame: + calls.append((bucket, key)) + return _df([_row("47 GORDON ROAD", "100")]) + + repo = HistoricEpcS3Repository(read_csv_gz=capture) + + # Act + with _valid_postcode(): + repo.get_for_postcode("ab33 8al") + + # Assert + assert calls == [("retrofit-data-dev", "historical_epc/AB338AL/data.csv.gz")] + + +def test_non_missing_read_error_propagates(): + # Arrange — an error that is NOT a missing object must not be swallowed. + def denied(bucket: str, key: str) -> pd.DataFrame: + raise ClientError( + {"Error": {"Code": "AccessDenied", "Message": "nope"}}, "GetObject" + ) + + repo = HistoricEpcS3Repository(read_csv_gz=denied) + + # Act / Assert + with _valid_postcode(): + with pytest.raises(ClientError): + repo.get_for_postcode("AB33 8AL")