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%
6.9 KB · 122 lines python
Raw Blame History
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"].startswith("US$") and snap["world_gdp_formatted"].endswith("T") and snap["median_life_expectancy"] > 6063    lists = body["lists"]64    assert set(lists) == {"largest_economies", "fastest_gdp_growth", "fastest_population_growth", "highest_life_expectancy",65                          "energy_transition_leaders", "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 le["filter_note"] is None and le["rows"][0]["formatted"].startswith("US$") and le["rows"][0]["formatted_short"]69    assert_provenance(le["rows"][0]["provenance"])70    assert lists["lowest_unemployment"]["rows"][0]["country"]["id"] == "JPN" and lists["lowest_unemployment"]["filter_note"] == "Countries above 5M inhabitants"71    assert lists["energy_transition_leaders"]["rows"][0]["country"]["id"] == "BRA"72    for k in ("fastest_gdp_growth", "fastest_population_growth", "highest_life_expectancy", "energy_transition_leaders"):73        assert lists[k]["filter_note"] == "Countries above 1M inhabitants" and lists[k]["min_population"] == 1_000_00074    assert lists["fastest_gdp_growth"]["indicator"]["id"] == "gdp-growth" and lists["fastest_gdp_growth"]["rows"][0]["year"] == 202475    assert len(body["recent_changes"]) == 12 and body["recent_changes"][0]["country"]["id"] and body["recent_changes"][0]["headline"]76    assert body["recently_updated"] and body["featured_indicators"] and body["trending"]77    assert all(i["featured"] for i in body["featured_indicators"])787980def test_changes_feed(get):81    body = get("/changes?limit=5").json()82    assert body["n"] == 5 and body["kinds"] and body["items"][0]["country"]["slug"]83    sev = [c["severity"] for c in body["items"]]84    assert sev == sorted(sev, reverse=True)85    assert_provenance(body["items"][0]["provenance"])86    rec = get("/changes?kind=record_high&indicator=population").json()87    assert rec["n"] > 0 and all(c["kind"] == "record_high" and c["indicator"]["id"] == "population" for c in rec["items"])88    assert get("/changes?country=CAN").json()["items"][0]["country"]["id"] == "CAN"89    get("/changes?indicator=nope", status=404)909192def test_sources(get):93    body = get("/sources").json()94    assert_meta(body)95    ids = [s["id"] for s in body["items"]]96    assert ids[0] == "worldbank" and set(ids) == {"worldbank", "imf", "owid", "who"}97    wb = body["items"][0]98    assert wb["licence"] == "CC BY 4.0" and wb["n_observations"] > 2000 and wb["n_indicators"] >= 1099    det = get("/sources/worldbank").json()100    assert det["source"]["id"] == "worldbank" and det["indicators"][0]["series_code"] and det["datasets"][0]["dataset"] == "WDI"101    assert det["import_runs"] and det["import_runs"][0]["run_id"] == RUN_ID and det["import_runs"][0]["status"] == "ok"102    assert det["freshness"]["retrieved_at"].startswith("2026-09-10")103    get("/sources/nasa", status=404)104105106def test_methodology(get):107    body = get("/methodology").json()108    assert body["meta"]["run_id"] == RUN_ID109    assert body["topics"][0]["id"] == "economy" and body["headline_indicators"][0] == "population"110    assert body["indicators"]["n"] > 200 and body["validation"]["rules"] and body["validation"]["stale_after_days"]["A"] == 800111    assert "similarity" in body["derived"] and "world_aggregates" in body["derived"]112    assert body["sources"]["urls"]["worldbank"].startswith("https://data.worldbank.org")113114115def test_health(client):116    r = client.get("/api/v1/health")117    assert r.status_code == 200118    body = r.json()119    assert body["status"] == "ok" and body["run_id"] == RUN_ID and body["observations"] > 3000 and body["countries"] == 8 and body["indicators"] == 14120    assert client.get("/health").json()["status"] == "ok"121    assert client.get("/").json()["docs"] == "/api/v1/docs"122