Generate property reference to uprn map from csv bytes 🟩

This commit is contained in:
Daniel Roth 2026-02-04 11:28:41 +00:00
parent b143169659
commit 03a0ad238c
3 changed files with 22 additions and 5 deletions

View file

@ -1,7 +1,9 @@
from abc import ABC, abstractmethod
from typing import Dict
from typing import BinaryIO, Dict
class UprnLookup(ABC):
def get_location_ref_to_uprn_map(self, lookup_filepath: str) -> Dict[str, int]:
def get_location_ref_to_uprn_map(
self, lookup_file_stream: BinaryIO
) -> Dict[str, int]:
pass

View file

@ -1,4 +1,6 @@
from typing import BinaryIO, Dict
import csv
from io import TextIOWrapper
from typing import BinaryIO, Dict, TextIO
from backend.condition.lookups.uprn_lookup import UprnLookup
@ -6,4 +8,17 @@ class UprnLookupCsv(UprnLookup):
def get_location_ref_to_uprn_map(
self, lookup_file_stream: BinaryIO
) -> Dict[str, int]:
raise NotImplementedError
text_stream: TextIO = TextIOWrapper(lookup_file_stream, encoding="utf-8")
location_ref_to_uprn_map: Dict[str, int] = {}
reader = csv.DictReader(text_stream)
for row in reader:
if not row["reference"] or not row["out_uprn"]:
# skip empty rows
continue
ref = row["reference"].strip()
uprn = int(row["out_uprn"].strip())
location_ref_to_uprn_map[ref] = uprn
return location_ref_to_uprn_map

View file

@ -7,7 +7,7 @@ from backend.condition.lookups.uprn_lookup_csv import UprnLookupCsv
@pytest.fixture
def prop_ref_uprn_csv_bytes() -> BytesIO:
csv_bytes = b"""reference, out_uprn
csv_bytes = b"""reference,out_uprn
ABC123,10000000001
DEF456,10000000002
GHI789,10000000003