Build S3 key from sanitised postcode and propagate non-missing read errors 🟩

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jun-te Kim 2026-06-29 14:47:18 +00:00
parent 88f4739ce8
commit cd503411d2

View file

@ -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")