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.6 KB · 103 lines python
Raw Blame History
1from __future__ import annotations23import pytest45from tests.api.conftest import assert_meta, assert_provenance, value678def test_rankings_list(get):9    body = get("/rankings").json()10    assert_meta(body)11    assert body["items"][0]["featured"] is True12    assert {"gdp", "life-expectancy"} <= {i["id"] for i in body["items"]}13    assert all(i["ranking_eligible"] for i in body["items"])141516def test_ranking_table(get):17    body = get("/rankings/gdp-per-capita?limit=3").json()18    assert body["year_used"] == 2024 and body["years_available"][0] == 1990 and body["n"] == 8 and body["sort"] == "desc"19    rows = body["rows"]20    assert [r["rank"] for r in rows] == [1, 2, 3] and rows[0]["country"]["id"] == "JPN" and rows[0]["rank_world"] == 121    assert rows[0]["value"] == pytest.approx(value("JPN", "gdp-per-capita", 2024))22    assert rows[0]["change_1y"]["pct"] == pytest.approx(3.0) and rows[0]["change_10y"]["pct"] == pytest.approx(34.39, abs=0.01)23    assert len(rows[0]["sparkline"]) == 30 and rows[0]["sparkline"][-1][0] == 202424    assert_provenance(rows[0]["provenance"])25    page2 = get("/rankings/gdp-per-capita?limit=3&offset=3").json()26    assert page2["rows"][0]["rank"] == 427    asc = get("/rankings/gdp-per-capita?sort=asc&limit=1").json()28    assert asc["rows"][0]["country"]["id"] == "IND"29    # lower-is-better default sort ascending30    infl = get("/rankings/inflation?limit=8").json()31    assert infl["sort"] == "asc" and infl["rows"][0]["value"] <= infl["rows"][-1]["value"]32    # nearest year fallback33    assert get("/rankings/gdp-per-capita?year=2030").json()["year_used"] == 202434    y2000 = get("/rankings/gdp-per-capita?year=2000&limit=1").json()35    assert y2000["year_used"] == 2000 and y2000["rows"][0]["year"] == 200036    get("/rankings/nothing", status=404)373839def test_ranking_group(get):40    body = get("/rankings/unemployment-rate?group=g7&limit=10").json()41    assert body["group"]["id"] == "g7" and body["n"] == 5 and [r["rank"] for r in body["rows"]] == [1, 2, 3, 4, 5]42    assert body["rows"][0]["country"]["id"] == "JPN"  # lowest unemployment in fixture43    assert body["rows"][0]["rank_world"] >= 1 and body["rows"][0]["n_world"] == 8444546def test_ranking_history(get):47    body = get("/rankings/gdp-per-capita/history?countries=CAN,usa&from=2020").json()48    assert [c["id"] for c in body["countries"]] == ["CAN", "USA"] and body["years"] == [2020, 2021, 2022, 2023, 2024]49    assert body["series"]["CAN"][-1]["year"] == 2024 and 1 <= body["series"]["CAN"][-1]["rank"] <= 850    get("/rankings/gdp-per-capita/history?countries=XXX", status=404)515253def test_series_bundle(get):54    body = get("/series?country=CAN,FRA&indicator=gdp,inflation&from=2020&to=2024").json()55    assert_meta(body)56    assert body["n"] == 457    s = body["series"][0]58    assert s["indicator"]["id"] == "gdp" and s["country"]["id"] == "CAN" and [v["year"] for v in s["values"]] == list(range(2020, 2025))59    assert_provenance(s["values"][0]["provenance"])60    get("/series?country=CAN&indicator=nope", status=404)61    get("/series?country=CAN", status=422)626364def test_compare_modes(get):65    body = get("/compare?countries=CAN,USA,FRA&indicators=gdp,gdp-per-capita&from=2000&to=2024").json()66    assert_meta(body)67    assert body["mode"] == "absolute" and len(body["series"]) == 6 and [c["id"] for c in body["countries"]] == ["CAN", "USA", "FRA"]68    idx = get("/compare?countries=CAN,USA&indicators=gdp&mode=index100&from=2000").json()69    s = idx["series"][0]70    assert s["transform"]["applied"] and s["transform"]["base_year"] == 2000 and s["values"][0]["value"] == pytest.approx(100.0)71    assert s["values"][1]["value"] == pytest.approx(value("CAN", "gdp", 2001) / value("CAN", "gdp", 2000) * 100)72    assert s["unit"].startswith("index")73    pc = get("/compare?countries=CAN&indicators=gdp,gdp-per-capita&mode=per-capita&from=2024&to=2024").json()74    gdp_pc, gpc = pc["series"]75    assert gdp_pc["transform"]["applied"] and gdp_pc["values"][0]["value"] == pytest.approx(value("CAN", "gdp-per-capita", 2024))76    assert gpc["transform"]["applied"] is False  # already per-capita77    pct = get("/compare?countries=CAN&indicators=gdp&mode=pct&from=2020&to=2022").json()["series"][0]78    assert pct["values"][0]["value"] is None and pct["values"][1]["value"] == pytest.approx(79        (value("CAN", "gdp", 2021) / value("CAN", "gdp", 2020) - 1) * 100)80    get("/compare?countries=CAN&indicators=gdp&mode=weird", status=422)81    get("/compare?countries=CAN,XXX&indicators=gdp", status=404)828384def test_compare_snapshot(get):85    body = get("/compare/snapshot?countries=CAN,USA&topic=economy").json()86    assert body["topic"]["id"] == "economy" and [c["id"] for c in body["countries"]] == ["CAN", "USA"]87    row = next(r for r in body["rows"] if r["indicator"]["id"] == "gdp-per-capita")88    assert row["values"]["CAN"]["has_data"] and row["best"] == "USA"89    assert_provenance(row["values"]["CAN"]["provenance"])90    missing = next(r for r in body["rows"] if r["indicator"]["id"] == "gdp-growth")91    assert missing["values"]["CAN"]["has_data"]92    default = get("/compare/snapshot?countries=CAN").json()93    assert default["topic"] is None and default["rows"][0]["indicator"]["id"] == "population"949596def test_compare_download(client):97    r = client.get("/api/v1/compare/download.csv?countries=CAN,USA&indicators=gdp&from=2023&include_forecast=false")98    assert r.status_code == 20099    lines = [l for l in r.text.splitlines() if l and not l.startswith("#")]100    assert len(lines) - 1 == 4 and "series_code" in lines[0]101    j = client.get("/api/v1/compare/download.json?countries=CAN&indicators=gdp&from=2024&to=2024&include_forecast=false").json()102    assert j["n"] == 1 and j["rows"][0]["value"] == pytest.approx(value("CAN", "gdp", 2024))103