wrongmove/crawler/tests/unit/test_districts.py
Viktor Barzin 150342bb9e
Refactor codebase following Clean Code principles and add 229 tests
- Extract helpers to reduce function sizes (listing_tasks, app.py, query.py, listing_fetcher)
  - Replace nonlocal mutations with _PipelineState dataclass in listing_tasks
  - Fix bugs: isinstance→equality check in repository, verify_exp for OIDC tokens
  - Consolidate duplicate filter methods in listing_repository
  - Move hardcoded config to env vars with backward-compatible defaults
  - Simplify CLI decorator to auto-build QueryParameters
  - Add deprecation docstring to data_access.py
  - Test count: 158 → 387 (all passing)
2026-02-07 20:19:57 +00:00

62 lines
2.3 KiB
Python

"""Unit tests for rec/districts.py and services/district_service.py."""
from rec.districts import get_districts, get_district_by_name
from services.district_service import get_all_districts, get_district_names, validate_districts
class TestGetDistricts:
def test_returns_non_empty_dict(self) -> None:
districts = get_districts()
assert isinstance(districts, dict)
assert len(districts) > 0
def test_values_start_with_region_prefix(self) -> None:
for name, region_id in get_districts().items():
assert region_id.startswith("REGION^"), (
f"District '{name}' has value '{region_id}' that doesn't start with REGION^"
)
def test_contains_expected_london_boroughs(self) -> None:
districts = get_districts()
for borough in ("Camden", "Westminster", "Hackney"):
assert borough in districts, f"Expected borough '{borough}' not found"
class TestGetDistrictByName:
def test_valid_name_returns_region_id(self) -> None:
result = get_district_by_name("Camden")
assert result == "REGION^93941"
def test_invalid_name_returns_none(self) -> None:
result = get_district_by_name("Nonexistent District")
assert result is None
class TestGetDistrictNames:
def test_returns_list_matching_dict_keys(self) -> None:
names = get_district_names()
assert isinstance(names, list)
assert names == list(get_districts().keys())
class TestGetAllDistricts:
def test_returns_same_as_get_districts(self) -> None:
assert get_all_districts() == get_districts()
class TestValidateDistricts:
def test_all_valid_returns_empty_list(self) -> None:
result = validate_districts(["Camden", "Westminster", "Hackney"])
assert result == []
def test_some_invalid_returns_invalid_ones(self) -> None:
result = validate_districts(["Camden", "Faketown", "Westminster", "Nowhere"])
assert result == ["Faketown", "Nowhere"]
def test_all_invalid_returns_all(self) -> None:
invalid = ["Faketown", "Nowhere", "Neverland"]
result = validate_districts(invalid)
assert result == invalid
def test_empty_list_returns_empty_list(self) -> None:
result = validate_districts([])
assert result == []