spb/countryatlas
Public
TypeScript 57%
Python 38.6%
JavaScript 3.6%
CSS 0.6%
1"""WHO GHO connector — offline tests on a trimmed recorded payload (M_Est_tob_curr, CAN + USA + decoys) + live smoke."""2from __future__ import annotations34from datetime import UTC, datetime56import orjson7import pytest89from countryatlas.connectors.who import WHOConnector10from countryatlas.models import IndicatorSourceSpec11from tests.connectors._helpers import install_fake_get, raw_from_file121314@pytest.fixture15def fake_get(monkeypatch):16 return install_fake_get(monkeypatch)171819SPEC =IndicatorSourceSpec(indicator_id="smoking-prevalence", connector="who", dataset="GHO", code="M_Est_tob_curr",20 params={"Dim1": "SEX_BTSX"}, priority=2)212223def _raw(fixtures_dir):24 return raw_from_file("who", "GHO", "M_Est_tob_curr", fixtures_dir / "who" / "M_Est_tob_curr.json", "application/json",25 indicator_meta={"name": "Estimate of current tobacco use prevalence (%)"})262728def test_build_filter_includes_country_type_and_dims():29 f = WHOConnector.build_filter({"Dim1": "SEX_BTSX", "Dim2": "AGEGROUP_YEARSALL"})30 assert f == "SpatialDimType eq 'COUNTRY' and Dim1 eq 'SEX_BTSX' and Dim2 eq 'AGEGROUP_YEARSALL'"31 assert WHOConnector.build_filter(None) == "SpatialDimType eq 'COUNTRY'"323334def test_normalize_maps_iso3_filters_dims_and_flags_projections(fixtures_dir):35 conn = WHOConnector()36 rows = conn.normalize(_raw(fixtures_dir), SPEC)37 # decoys: REGION row (AMR), SEX_MLE row, unknown ISO3 'ZZZ' are all dropped38 assert {r.country_id for r in rows} == {"CAN", "USA"}39 assert all(r.frequency == "A" and r.period.month == 1 and r.period.day == 1 for r in rows)40 assert all(r.unit == "% of adults" for r in rows)41 can = {r.year: r for r in rows if r.country_id == "CAN"}42 assert can[2030].is_forecast is True and can[2030].value == pytest.approx(8.3)43 assert can[2024].is_forecast is False44 assert all(r.year <= datetime.now(UTC).year or r.is_forecast for r in rows)45 # confidence bounds go to metadata; the row `Date` feeds source_updated_at46 assert "low" in rows[0].metadata and "high" in rows[0].metadata47 assert rows[0].source_updated_at is not None and rows[0].source_updated_at.year >= 202448 # no duplicate keys49 keys = {(r.country_id, r.period) for r in rows}50 assert len(keys) == len(rows)51 assert conn.validate(rows).quarantine_dataset is False525354def test_normalize_applies_scalar_transform(fixtures_dir):55 spec = SPEC.model_copy(update={"transform": "x/10", "indicator_id": "nurses-per-1000"})56 rows = WHOConnector().normalize(_raw(fixtures_dir), spec)57 can = {r.year: r for r in rows if r.country_id == "CAN"}58 assert can[2030].value == pytest.approx(0.83)59 assert can[2030].unit == "per 1,000 people"606162def test_fetch_uses_filter_and_metadata(fixtures_dir, fake_get):63 conn = WHOConnector()64 data = (fixtures_dir / "who" / "M_Est_tob_curr.json").read_bytes()65 meta = (fixtures_dir / "who" / "Indicator_M_Est_tob_curr.json").read_bytes()6667 def router(url, params):68 if url.endswith("/Indicator"):69 return meta, "application/json"70 return data, "application/json"7172 calls = fake_get(conn, router)73 payloads = conn.fetch(SPEC)74 assert len(payloads) == 1 and payloads[0].pages == 175 assert calls[0]["url"].endswith("/Indicator") and "M_Est_tob_curr" in calls[0]["params"]["$filter"]76 assert calls[1]["url"].endswith("/M_Est_tob_curr")77 assert calls[1]["params"]["$filter"] == "SpatialDimType eq 'COUNTRY' and Dim1 eq 'SEX_BTSX'"78 assert payloads[0].meta["indicator_meta"]["name"].startswith("Estimate of current tobacco")79 assert orjson.loads(payloads[0].body)["value"]808182@pytest.mark.live83def test_live_suicide_rate_has_no_duplicates():84 conn = WHOConnector()85 spec = IndicatorSourceSpec(indicator_id="suicide-rate", connector="who", dataset="GHO", code="SDGSUICIDE",86 params={"Dim1": "SEX_BTSX", "Dim2": "AGEGROUP_YEARSALL"})87 rows = conn.normalize(conn.fetch(spec), spec)88 assert len(rows) > 3000 and "CAN" in {r.country_id for r in rows}89 assert conn.validate(rows).quarantine_dataset is False90