"""regions, search, home, changes, sources, methodology, health.""" from __future__ import annotations import pytest from tests.api.conftest import COUNTRIES, RUN_ID, assert_meta, assert_provenance, value def test_regions(get): body = get("/regions").json() assert_meta(body) ids = [g["id"] for g in body["items"]] assert ids[0] == "world" and "g7" in ids and "hic" in ids world = body["items"][0] assert world["n_members"] == 8 and world["population_latest"] == pytest.approx(sum(value(c, "population", 2024) for c in COUNTRIES)) assert all(g["kind"] == "org" for g in get("/regions?kind=org").json()["items"]) def test_region_detail(get): body = get("/regions/g7?indicator=life-expectancy").json() assert body["group"]["id"] == "g7" and body["n_members"] == 5 agg = body["aggregates"] assert agg["population"]["kind"] == "sum" and agg["gdp"]["kind"] == "sum" and agg["life-expectancy"]["kind"] == "median" assert agg["gdp"]["value"] == pytest.approx(sum(value(c, "gdp", 2024) for c in ("CAN", "USA", "FRA", "DEU", "JPN"))) m = body["members"][0] assert m["id"] == "USA" and m["values"]["population"]["value"] and m["values"]["gdp"]["formatted"].endswith("T") assert_provenance(m["values"]["gdp"]["provenance"]) rk = body["ranking"] assert rk["indicator"]["id"] == "life-expectancy" and rk["rows"][0]["rank"] == 1 and rk["rows"][0]["country"]["id"] == "JPN" assert_provenance(rk["rows"][0]["provenance"]) assert get("/regions/europe-central-asia").json()["n_members"] == 2 get("/regions/mars", status=404) def test_search(get): body = get("/search?q=infl").json() assert_meta(body) assert body["hits"][0]["type"] == "indicator" and body["hits"][0]["id"] == "inflation" assert body["hits"][0]["hint"].startswith("Indicator · Economy") can = get("/search?q=can").json()["hits"] assert can[0]["type"] == "country" and can[0]["id"] == "CAN" and can[0]["hint"] == "Country · North America" and can[0]["url"] == "/countries/canada" fuzzy = get("/search?q=canda").json()["hits"] assert fuzzy and fuzzy[0]["id"] == "CAN" combo = get("/search?q=housing canada").json()["hits"] types = {h["type"]: h for h in combo} assert "country_topic" in types and types["country_topic"]["url"] == "/countries/canada/housing" and types["country_topic"]["topic"] == "housing" assert "country" in types and "topic" in types combo2 = get("/search?q=canada gdp").json()["hits"] assert any(h["type"] == "country_indicator" and h["indicator"] == "gdp" for h in combo2) assert get("/search?q=oecd").json()["hits"][0]["type"] == "region" assert get("/search?q=zzzzqqq").json()["n"] == 0 assert all(h["type"] == "indicator" for h in get("/search?q=a&type=indicator&limit=3").json()["hits"]) get("/search", status=422) def test_home(get): body = get("/home").json() assert_meta(body) snap = body["snapshot"] assert snap["n_countries"] == 8 and snap["n_indicators"] == 14 and snap["n_observations"] > 3000 and snap["built_at"] assert snap["world_population"] == pytest.approx(sum(value(c, "population", 2024) for c in COUNTRIES)) assert snap["world_gdp_formatted"].startswith("US$") and snap["world_gdp_formatted"].endswith("T") and snap["median_life_expectancy"] > 60 lists = body["lists"] assert set(lists) == {"largest_economies", "fastest_gdp_growth", "fastest_population_growth", "highest_life_expectancy", "energy_transition_leaders", "highest_gdp_per_capita_ppp", "lowest_unemployment"} le = lists["largest_economies"] assert le["rows"][0]["country"]["id"] == "USA" and le["rows"][0]["rank"] == 1 and len(le["rows"]) == 8 assert le["filter_note"] is None and le["rows"][0]["formatted"].startswith("US$") and le["rows"][0]["formatted_short"] assert_provenance(le["rows"][0]["provenance"]) assert lists["lowest_unemployment"]["rows"][0]["country"]["id"] == "JPN" and lists["lowest_unemployment"]["filter_note"] == "Countries above 5M inhabitants" assert lists["energy_transition_leaders"]["rows"][0]["country"]["id"] == "BRA" for k in ("fastest_gdp_growth", "fastest_population_growth", "highest_life_expectancy", "energy_transition_leaders"): assert lists[k]["filter_note"] == "Countries above 1M inhabitants" and lists[k]["min_population"] == 1_000_000 assert lists["fastest_gdp_growth"]["indicator"]["id"] == "gdp-growth" and lists["fastest_gdp_growth"]["rows"][0]["year"] == 2024 assert len(body["recent_changes"]) == 12 and body["recent_changes"][0]["country"]["id"] and body["recent_changes"][0]["headline"] assert body["recently_updated"] and body["featured_indicators"] and body["trending"] assert all(i["featured"] for i in body["featured_indicators"]) def test_changes_feed(get): body = get("/changes?limit=5").json() assert body["n"] == 5 and body["kinds"] and body["items"][0]["country"]["slug"] sev = [c["severity"] for c in body["items"]] assert sev == sorted(sev, reverse=True) assert_provenance(body["items"][0]["provenance"]) rec = get("/changes?kind=record_high&indicator=population").json() assert rec["n"] > 0 and all(c["kind"] == "record_high" and c["indicator"]["id"] == "population" for c in rec["items"]) assert get("/changes?country=CAN").json()["items"][0]["country"]["id"] == "CAN" get("/changes?indicator=nope", status=404) def test_sources(get): body = get("/sources").json() assert_meta(body) ids = [s["id"] for s in body["items"]] assert ids[0] == "worldbank" and set(ids) == {"worldbank", "imf", "owid", "who"} wb = body["items"][0] assert wb["licence"] == "CC BY 4.0" and wb["n_observations"] > 2000 and wb["n_indicators"] >= 10 det = get("/sources/worldbank").json() assert det["source"]["id"] == "worldbank" and det["indicators"][0]["series_code"] and det["datasets"][0]["dataset"] == "WDI" assert det["import_runs"] and det["import_runs"][0]["run_id"] == RUN_ID and det["import_runs"][0]["status"] == "ok" assert det["freshness"]["retrieved_at"].startswith("2026-09-10") get("/sources/nasa", status=404) def test_methodology(get): body = get("/methodology").json() assert body["meta"]["run_id"] == RUN_ID assert body["topics"][0]["id"] == "economy" and body["headline_indicators"][0] == "population" assert body["indicators"]["n"] > 200 and body["validation"]["rules"] and body["validation"]["stale_after_days"]["A"] == 800 assert "similarity" in body["derived"] and "world_aggregates" in body["derived"] assert body["sources"]["urls"]["worldbank"].startswith("https://data.worldbank.org") def test_health(client): r = client.get("/api/v1/health") assert r.status_code == 200 body = r.json() assert body["status"] == "ok" and body["run_id"] == RUN_ID and body["observations"] > 3000 and body["countries"] == 8 and body["indicators"] == 14 assert client.get("/health").json()["status"] == "ok" assert client.get("/").json()["docs"] == "/api/v1/docs"