SPB Git forge

spb/countryatlas

Public
20commits 1branches 0releases
268.3 MBsize
maindefault branch
12 days agolast push
TypeScript 57% Python 38.6% JavaScript 3.6% CSS 0.6%
2.2 KB · 57 lines python
Raw Blame History
1"""Offline helpers for the WHO / FRED / BIS / ILO connector tests (kept out of the shared conftest).23* `make_response`  — build an httpx.Response as if returned by `Connector.get`.4* `install_fake_get` — body of a `fake_get` fixture: patch `connector.get` with a router(url, params) → (body, content_type);5                       returns the call log. Each test module wraps it in its own `@pytest.fixture def fake_get(monkeypatch)`.6* `raw_from_file`  — RawPayload from a recorded fixture file.7"""8from __future__ import annotations910from collections.abc import Callable11from datetime import UTC, datetime12from pathlib import Path13from typing import Any1415import httpx16import pytest1718from countryatlas.models import RawPayload1920Router = Callable[[str, dict[str, Any] | None], tuple[bytes, str]]212223def make_response(url: str, body: bytes, content_type: str, params: dict[str, Any] | None = None) -> httpx.Response:24    req = httpx.Request("GET", httpx.URL(url, params=params or {}))25    return httpx.Response(200, content=body, headers={"Content-Type": content_type}, request=req)262728def install_fake_get(monkeypatch: pytest.MonkeyPatch) -> Callable[[Any, Router], list[dict[str, Any]]]:29    """Body of the `fake_get` fixture; each test module declares `fake_get = fixture(lambda monkeypatch: install_fake_get(…))`."""3031    def _install(connector: Any, router: Router) -> list[dict[str, Any]]:32        calls: list[dict[str, Any]] = []3334        def _get(url: str, params: dict[str, Any] | None = None, headers: dict[str, str] | None = None) -> httpx.Response:35            calls.append({"url": url, "params": dict(params or {}), "headers": dict(headers or {})})36            body, ct = router(url, params)37            return make_response(url, body, ct, params)3839        monkeypatch.setattr(connector, "get", _get)40        return calls4142    return _install434445def raw_from_file(connector: str, dataset: str, code: str, path: Path, content_type: str, **meta: Any) -> RawPayload:46    return RawPayload(47        connector=connector,48        dataset=dataset,49        code=code,50        url=f"fixture://{path.name}",51        retrieved_at=datetime.now(UTC),52        status_code=200,53        content_type=content_type,54        body=path.read_bytes(),55        meta=meta,56    )57