import httpx import pytest from assessments.geocoding import AddressNotFound, GeocodingError, OutOfCoverage, geocode from assessments.geocoding.base import region_from_postal_code from .conftest import NOMINATIM, nominatim @pytest.mark.parametrize( ("postal_code", "region"), [ ("1000", "BXL"), ("1210", "BXL"), ("1348", "WAL"), ("4000", "WAL"), ("7999", "WAL"), ("2000", None), ("9000", None), ("", None), ], ) def test_region_from_postal_code(postal_code, region): assert region_from_postal_code(postal_code) == region async def test_geocode_wallonia(mock_http): mock_http.get(NOMINATIM).respond(json=nominatim("1348")) async with httpx.AsyncClient() as client: location = await geocode(client, "Place de l'Université 1, 1348 Louvain-la-Neuve") assert location.region == "WAL" assert location.lat == pytest.approx(50.6697) async def test_geocode_not_found(mock_http): mock_http.get(NOMINATIM).respond(json=[]) async with httpx.AsyncClient() as client: with pytest.raises(AddressNotFound): await geocode(client, "rue inexistante") async def test_geocode_flanders_is_out_of_coverage(mock_http): mock_http.get(NOMINATIM).respond(json=nominatim("9000")) async with httpx.AsyncClient() as client: with pytest.raises(OutOfCoverage): await geocode(client, "Korenmarkt 1, 9000 Gent") async def test_geocode_service_down(mock_http): mock_http.get(NOMINATIM).respond(503) async with httpx.AsyncClient() as client: with pytest.raises(GeocodingError): await geocode(client, "Place de l'Université 1, 1348 Louvain-la-Neuve")