from __future__ import annotations from datetime import datetime, timezone from types import SimpleNamespace from uuid import uuid4 from fastapi.testclient import TestClient from app.db.session import get_db from app.main import create_app from app.services.area_service import AreaService def test_municipality_service_matches_all_official_names_and_nis(monkeypatch): records = [ {"niscode": "63004", "namedut": "Baelen", "namefre": "Baelen", "nameger": "Balen"}, {"niscode": "13025", "namedut": "Mol", "namefre": "Mol", "nameger": "Mol"}, ] by_german_name, name_total = AreaService._filter_municipality_properties(records, "Balen", 20) by_nis, nis_total = AreaService._filter_municipality_properties(records, "13025", 20) assert name_total == 1 assert by_german_name[0]["niscode"] == "63004" assert nis_total == 1 assert by_nis[0]["name"] == "Mol" def test_municipality_search_uses_authoritative_catalog(monkeypatch): project_id = uuid4() monkeypatch.setattr( AreaService, "search_municipalities", staticmethod(lambda _db, requested_project_id, query, limit: ( [{"niscode": "13025", "name": "Mol", "name_nl": "Mol", "name_fr": "Mol", "name_de": "Mol"}], 1, ) if requested_project_id == project_id and query == "mol" and limit == 12 else ([], 0)), ) app = create_app() app.dependency_overrides[get_db] = lambda: object() try: response = TestClient(app).get(f"/api/v1/projects/{project_id}/areas/municipalities?query=mol&limit=12") finally: app.dependency_overrides.clear() assert response.status_code == 200 assert response.json()["data"] == { "items": [{"niscode": "13025", "name": "Mol", "name_nl": "Mol", "name_fr": "Mol", "name_de": "Mol"}], "total": 1, } def test_municipality_activation_returns_persisted_area(monkeypatch): project_id = uuid4() area_id = uuid4() area = SimpleNamespace(id=area_id, project_id=project_id) monkeypatch.setattr(AreaService, "activate_municipality", staticmethod(lambda _db, requested_project_id, niscode: area)) monkeypatch.setattr( AreaService, "serialize_area", staticmethod(lambda _area: { "id": area_id, "project_id": project_id, "name": "Gemeente Mol - NIS 13025", "original_crs": "EPSG:4326", "area_m2": 114000000.0, "created_at": datetime(2026, 7, 26, tzinfo=timezone.utc), "geometry_type": "MultiPolygon", "geometry": {"type": "MultiPolygon", "coordinates": []}, }), ) app = create_app() app.dependency_overrides[get_db] = lambda: object() try: response = TestClient(app).post(f"/api/v1/projects/{project_id}/areas/municipalities/13025/activate", json={}) finally: app.dependency_overrides.clear() assert response.status_code == 200 assert response.json()["data"]["id"] == str(area_id) assert response.json()["data"]["name"] == "Gemeente Mol - NIS 13025"