"""Offline helpers for the WHO / FRED / BIS / ILO connector tests (kept out of the shared conftest). * `make_response` — build an httpx.Response as if returned by `Connector.get`. * `install_fake_get` — body of a `fake_get` fixture: patch `connector.get` with a router(url, params) → (body, content_type); returns the call log. Each test module wraps it in its own `@pytest.fixture def fake_get(monkeypatch)`. * `raw_from_file` — RawPayload from a recorded fixture file. """ from __future__ import annotations from collections.abc import Callable from datetime import UTC, datetime from pathlib import Path from typing import Any import httpx import pytest from countryatlas.models import RawPayload Router = Callable[[str, dict[str, Any] | None], tuple[bytes, str]] def make_response(url: str, body: bytes, content_type: str, params: dict[str, Any] | None = None) -> httpx.Response: req = httpx.Request("GET", httpx.URL(url, params=params or {})) return httpx.Response(200, content=body, headers={"Content-Type": content_type}, request=req) def install_fake_get(monkeypatch: pytest.MonkeyPatch) -> Callable[[Any, Router], list[dict[str, Any]]]: """Body of the `fake_get` fixture; each test module declares `fake_get = fixture(lambda monkeypatch: install_fake_get(…))`.""" def _install(connector: Any, router: Router) -> list[dict[str, Any]]: calls: list[dict[str, Any]] = [] def _get(url: str, params: dict[str, Any] | None = None, headers: dict[str, str] | None = None) -> httpx.Response: calls.append({"url": url, "params": dict(params or {}), "headers": dict(headers or {})}) body, ct = router(url, params) return make_response(url, body, ct, params) monkeypatch.setattr(connector, "get", _get) return calls return _install def raw_from_file(connector: str, dataset: str, code: str, path: Path, content_type: str, **meta: Any) -> RawPayload: return RawPayload( connector=connector, dataset=dataset, code=code, url=f"fixture://{path.name}", retrieved_at=datetime.now(UTC), status_code=200, content_type=content_type, body=path.read_bytes(), meta=meta, )