SPB Git

spb/countryatlas

Public
6.3 KB · 118 lines python
1"""regions, search, home, changes, sources, methodology, health."""2from __future__ import annotations34import pytest56from tests.api.conftest import COUNTRIES, RUN_ID, assert_meta, assert_provenance, value789def test_regions(get):10    body = get("/regions").json()11    assert_meta(body)12    ids = [g["id"] for g in body["items"]]13    assert ids[0] == "world" and "g7" in ids and "hic" in ids14    world = body["items"][0]15    assert world["n_members"] == 8 and world["population_latest"] == pytest.approx(sum(value(c, "population", 2024) for c in COUNTRIES))16    assert all(g["kind"] == "org" for g in get("/regions?kind=org").json()["items"])171819def test_region_detail(get):20    body = get("/regions/g7?indicator=life-expectancy").json()21    assert body["group"]["id"] == "g7" and body["n_members"] == 522    agg = body["aggregates"]23    assert agg["population"]["kind"] == "sum" and agg["gdp"]["kind"] == "sum" and agg["life-expectancy"]["kind"] == "median"24    assert agg["gdp"]["value"] == pytest.approx(sum(value(c, "gdp", 2024) for c in ("CAN", "USA", "FRA", "DEU", "JPN")))25    m = body["members"][0]26    assert m["id"] == "USA" and m["values"]["population"]["value"] and m["values"]["gdp"]["formatted"].endswith("T")27    assert_provenance(m["values"]["gdp"]["provenance"])28    rk = body["ranking"]29    assert rk["indicator"]["id"] == "life-expectancy" and rk["rows"][0]["rank"] == 1 and rk["rows"][0]["country"]["id"] == "JPN"30    assert_provenance(rk["rows"][0]["provenance"])31    assert get("/regions/europe-central-asia").json()["n_members"] == 232    get("/regions/mars", status=404)333435def test_search(get):36    body = get("/search?q=infl").json()37    assert_meta(body)38    assert body["hits"][0]["type"] == "indicator" and body["hits"][0]["id"] == "inflation"39    assert body["hits"][0]["hint"].startswith("Indicator · Economy")40    can = get("/search?q=can").json()["hits"]41    assert can[0]["type"] == "country" and can[0]["id"] == "CAN" and can[0]["hint"] == "Country · North America" and can[0]["url"] == "/countries/canada"42    fuzzy = get("/search?q=canda").json()["hits"]43    assert fuzzy and fuzzy[0]["id"] == "CAN"44    combo = get("/search?q=housing canada").json()["hits"]45    types = {h["type"]: h for h in combo}46    assert "country_topic" in types and types["country_topic"]["url"] == "/countries/canada/housing" and types["country_topic"]["topic"] == "housing"47    assert "country" in types and "topic" in types48    combo2 = get("/search?q=canada gdp").json()["hits"]49    assert any(h["type"] == "country_indicator" and h["indicator"] == "gdp" for h in combo2)50    assert get("/search?q=oecd").json()["hits"][0]["type"] == "region"51    assert get("/search?q=zzzzqqq").json()["n"] == 052    assert all(h["type"] == "indicator" for h in get("/search?q=a&type=indicator&limit=3").json()["hits"])53    get("/search", status=422)545556def test_home(get):57    body = get("/home").json()58    assert_meta(body)59    snap = body["snapshot"]60    assert snap["n_countries"] == 8 and snap["n_indicators"] == 14 and snap["n_observations"] > 3000 and snap["built_at"]61    assert snap["world_population"] == pytest.approx(sum(value(c, "population", 2024) for c in COUNTRIES))62    assert snap["world_gdp_formatted"].endswith("T") and snap["median_life_expectancy"] > 6063    lists = body["lists"]64    assert set(lists) == {"largest_economies", "fastest_population_growth", "highest_life_expectancy", "energy_transition_leaders",65                          "highest_gdp_per_capita_ppp", "lowest_unemployment"}66    le = lists["largest_economies"]67    assert le["rows"][0]["country"]["id"] == "USA" and le["rows"][0]["rank"] == 1 and len(le["rows"]) == 868    assert_provenance(le["rows"][0]["provenance"])69    assert lists["lowest_unemployment"]["rows"][0]["country"]["id"] == "JPN"70    assert lists["energy_transition_leaders"]["rows"][0]["country"]["id"] == "BRA"71    assert len(body["recent_changes"]) == 12 and body["recent_changes"][0]["country"]["id"] and body["recent_changes"][0]["headline"]72    assert body["recently_updated"] and body["featured_indicators"] and body["trending"]73    assert all(i["featured"] for i in body["featured_indicators"])747576def test_changes_feed(get):77    body = get("/changes?limit=5").json()78    assert body["n"] == 5 and body["kinds"] and body["items"][0]["country"]["slug"]79    sev = [c["severity"] for c in body["items"]]80    assert sev == sorted(sev, reverse=True)81    assert_provenance(body["items"][0]["provenance"])82    rec = get("/changes?kind=record_high&indicator=population").json()83    assert rec["n"] > 0 and all(c["kind"] == "record_high" and c["indicator"]["id"] == "population" for c in rec["items"])84    assert get("/changes?country=CAN").json()["items"][0]["country"]["id"] == "CAN"85    get("/changes?indicator=nope", status=404)868788def test_sources(get):89    body = get("/sources").json()90    assert_meta(body)91    ids = [s["id"] for s in body["items"]]92    assert ids[0] == "worldbank" and set(ids) == {"worldbank", "imf", "owid", "who"}93    wb = body["items"][0]94    assert wb["licence"] == "CC BY 4.0" and wb["n_observations"] > 2000 and wb["n_indicators"] >= 1095    det = get("/sources/worldbank").json()96    assert det["source"]["id"] == "worldbank" and det["indicators"][0]["series_code"] and det["datasets"][0]["dataset"] == "WDI"97    assert det["import_runs"] and det["import_runs"][0]["run_id"] == RUN_ID and det["import_runs"][0]["status"] == "ok"98    assert det["freshness"]["retrieved_at"].startswith("2026-09-10")99    get("/sources/nasa", status=404)100101102def test_methodology(get):103    body = get("/methodology").json()104    assert body["meta"]["run_id"] == RUN_ID105    assert body["topics"][0]["id"] == "economy" and body["headline_indicators"][0] == "population"106    assert body["indicators"]["n"] > 200 and body["validation"]["rules"] and body["validation"]["stale_after_days"]["A"] == 800107    assert "similarity" in body["derived"] and "world_aggregates" in body["derived"]108    assert body["sources"]["urls"]["worldbank"].startswith("https://data.worldbank.org")109110111def test_health(client):112    r = client.get("/api/v1/health")113    assert r.status_code == 200114    body = r.json()115    assert body["status"] == "ok" and body["run_id"] == RUN_ID and body["observations"] > 3000 and body["countries"] == 8 and body["indicators"] == 14116    assert client.get("/health").json()["status"] == "ok"117    assert client.get("/").json()["docs"] == "/api/v1/docs"118
Files · b828cc86d5b2223081f42e670e6d1f2379c43230
Menu

Theme