Model/tests/repositories/geospatial/test_geospatial_repository.py
Khalim Conn-Kowlessar 3998ef586c feat(geospatial): GeospatialRepo — OS Open-UPRN coordinate lookup (#1131)
Add Coordinates value object + GeospatialRepository port + GeospatialS3Repository
adapter. Resolves a Property's lon/lat from the partitioned Ordnance Survey
Open-UPRN parquet (filename_meta -> partition -> UPRN row). A Repo, not a
Fetcher (ADR-0011): no live OS API call. The parquet reader is injected, so it's
unit-tested against fixture parquets with no S3/network; returns None when the
UPRN is uncovered or absent. pyright strict clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-05-30 19:55:46 +00:00

71 lines
2.1 KiB
Python

"""GeospatialRepo resolves a Property's coordinates from the OS Open-UPRN data.
A reference-data lookup, not a Fetcher (ADR-0011): no live OS API call. The
adapter reads the partitioned Open-UPRN parquet via an injected reader, so the
test exercises the partition lookup + filter against real fixture parquets with
no network.
"""
from __future__ import annotations
from collections.abc import Callable
from pathlib import Path
import pandas as pd
from domain.geospatial.coordinates import Coordinates
from repositories.geospatial.geospatial_s3_repository import GeospatialS3Repository
def _reader(base: Path) -> Callable[[str], pd.DataFrame]:
def read(key: str) -> pd.DataFrame:
return pd.read_parquet(base / key)
return read
def _write_open_uprn(base: Path) -> None:
spatial = base / "spatial"
spatial.mkdir(parents=True, exist_ok=True)
pd.DataFrame(
{"lower": [0], "upper": [100000], "filenames": ["0_100000.parquet"]}
).to_parquet(spatial / "filename_meta.parquet")
pd.DataFrame(
{
"UPRN": [12345, 12346],
"LATITUDE": [51.5074, 51.6000],
"LONGITUDE": [-0.1278, -0.2000],
}
).to_parquet(spatial / "0_100000.parquet")
def test_coordinates_for_returns_lon_lat(tmp_path: Path) -> None:
# Arrange
_write_open_uprn(tmp_path)
repo = GeospatialS3Repository(_reader(tmp_path))
# Act
coords = repo.coordinates_for(12345)
# Assert
assert coords == Coordinates(longitude=-0.1278, latitude=51.5074)
def test_coordinates_for_returns_none_when_uprn_absent(tmp_path: Path) -> None:
# Arrange
_write_open_uprn(tmp_path)
repo = GeospatialS3Repository(_reader(tmp_path))
# Act / Assert — uprn inside the partition range but not present in the data
assert repo.coordinates_for(99999) is None
def test_coordinates_for_returns_none_when_no_partition_covers_uprn(
tmp_path: Path,
) -> None:
# Arrange
_write_open_uprn(tmp_path)
repo = GeospatialS3Repository(_reader(tmp_path))
# Act / Assert — uprn beyond every partition's range
assert repo.coordinates_for(500000) is None