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%
5.5 KB · 97 lines python
Raw Blame History
1from __future__ import annotations23import pytest45from tests.api.conftest import COUNTRIES, assert_meta, assert_provenance, value678def test_list_indicators(get):9    body = get("/indicators").json()10    assert_meta(body)11    assert body["n"] == 1412    assert body["items"][0]["featured"] is True  # featured first13    gdp = next(i for i in body["items"] if i["id"] == "gdp")14    assert gdp["n_countries"] == 8 and gdp["coverage_pct"] == 100.0 and gdp["last_year"] == 2024 and gdp["primary_source_id"] == "worldbank"15    eco = get("/indicators?topic=economy").json()16    assert [i["id"] for i in eco["items"]][:3] == ["gdp", "gdp-per-capita", "gdp-per-capita-ppp"]  # topics.yaml order17    assert all(i["featured"] for i in get("/indicators?featured=true").json()["items"])18    assert get("/indicators?q=infl").json()["items"][0]["id"] == "inflation"192021def test_indicator_detail(get):22    body = get("/indicators/gdp-per-capita").json()23    ind = body["indicator"]24    assert ind["slug"] == "gdp-per-capita" and ind["unit"] == "current US$" and ind["format"] == "currency" and ind["higher_is_better"] is True25    assert ind["source_priority"] == ["worldbank", "imf"]26    srcs = body["sources"]27    assert [s["source_id"] for s in srcs] == ["worldbank", "imf"] and srcs[0]["series_code"] == "NY.GDP.PCAP.CD"28    assert srcs[0]["url"].startswith("https://data.worldbank.org/indicator/NY.GDP.PCAP.CD") and srcs[0]["licence"] == "CC BY 4.0"29    assert body["coverage"]["n_countries"] == 8 and body["coverage"]["by_year"][0]["year"] == 199030    wl = body["world_latest"]31    assert wl["kind"] == "weighted_mean" and wl["weights"] == "population" and wl["year"] == 2024 and wl["n"] == 832    pops = {c: value(c, "population", 2024) for c in COUNTRIES}33    expected = sum(value(c, "gdp-per-capita", 2024) * pops[c] for c in COUNTRIES) / sum(pops.values())34    assert wl["value"] == pytest.approx(expected, rel=1e-6)35    assert body["top5"][0]["country"]["id"] == "JPN" and body["top5"][0]["rank"] == 136    assert body["bottom5"][-1]["country"]["id"] == "IND" and body["bottom5"][-1]["rank"] == 8  # IND has the lowest synthetic GDP pc37    assert_provenance(body["top5"][0]["provenance"])38    assert body["freshness"]["retrieved_at"] and body["years"] == {"first": 1990, "last": 2026, "last_actual": 2024, "latest_common": 2024}39    assert "economy" in body["topics"]404142def test_indicator_world_latest_kinds(get):43    assert get("/indicators/gdp").json()["world_latest"]["kind"] == "sum"44    le = get("/indicators/life-expectancy").json()["world_latest"]45    assert le["kind"] == "weighted_mean" and le["median"] is not None46    # lower-is-better indicator → top5 = lowest values47    infl = get("/indicators/inflation").json()48    assert infl["top5"][0]["value"] <= infl["top5"][-1]["value"]495051def test_indicator_404(client):52    r = client.get("/api/v1/indicators/happiness-of-cats")53    assert r.status_code == 404 and r.json()["title"] == "Indicator not found" and "gdp-per-capita" in r.json()["detail"]545556def test_indicator_map(get):57    body = get("/indicators/life-expectancy/map").json()58    assert body["year"] is None and body["year_used"] == 2024 and body["n"] == 8 and body["nearest"] is False59    assert body["values"]["CAN"] == pytest.approx(value("CAN", "life-expectancy", 2024))60    assert body["formatted"]["CAN"].endswith("yrs")61    lg = body["legend"]62    assert lg["min"] < lg["max"] and lg["breaks"] == sorted(lg["breaks"]) and 3 <= lg["n_classes"] <= 763    assert_provenance(body["provenance"])64    y23 = get("/indicators/life-expectancy/map?year=2023").json()65    assert y23["year_used"] == 2023 and y23["values"]["CAN"] == pytest.approx(value("CAN", "life-expectancy", 2023))66    # internet-users has no values for NGA before 2005 → nearest within 3 years67    m = get("/indicators/internet-users/map?year=2006&nearest=true").json()68    assert m["years"]["NGA"] == 2006 and m["years"]["CAN"] == 200669    m2 = get("/indicators/internet-users/map?year=2004&nearest=true").json()70    assert "NGA" not in m2["values"] and m2["years"]["CAN"] == 200471    empty = get("/indicators/internet-users/map?year=1990").json()72    assert empty["n"] == 0 and empty["legend"]["breaks"] == []737475def test_indicator_trend(get):76    body = get("/indicators/gdp/trend?group=world").json()77    assert body["group"]["id"] == "world" and body["preferred"] == "sum"78    p = body["points"]79    assert p[0]["year"] == 1990 and p[-1]["year"] == 2024 and p[-1]["n"] == 880    assert p[-1]["sum"] == pytest.approx(sum(value(c, "gdp", 2024) for c in COUNTRIES))81    assert p[-1]["median"] is not None and p[-1]["mean"] is not None82    g7 = get("/indicators/life-expectancy/trend?group=g7&from=2020").json()83    assert g7["preferred"] == "weighted_mean" and g7["weights"] == "population" and g7["points"][0]["year"] == 2020 and g7["points"][0]["n"] == 584    assert g7["points"][-1]["weighted_mean"] is not None85    assert body["provenance"] and body["provenance"][0]["source"] == "worldbank"86    get("/indicators/gdp/trend?group=narnia", status=404)878889def test_indicator_download(client):90    r = client.get("/api/v1/indicators/gdp/download.csv?from=2020&include_forecast=false")91    assert r.status_code == 200 and r.headers["content-type"].startswith("text/csv")92    lines = [l for l in r.text.splitlines() if l and not l.startswith("#")]93    assert lines[0].split(",")[:4] == ["country_id", "country_name", "indicator_id", "indicator_name"]94    assert len(lines) - 1 == 8 * 595    j = client.get("/api/v1/indicators/gdp/download.json?from=2024&to=2024").json()96    assert j["n"] == 8 and j["rows"][0]["series_code"] == "NY.GDP.MKTP.CD"97