idiouf's picture
Upload 19 files
419e4fd verified
Raw History Blame Contribute Delete
7.77 kB
"""Offline end-to-end checks: every page renders, and the analysis layer is correct."""
from __future__ import annotations
import os
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent
sys.path.insert(0, str(ROOT))
os.environ["CONSERVATION_HD_FIXTURES"] = str(ROOT)
import pandas as pd # noqa: E402
from streamlit.testing.v1 import AppTest # noqa: E402
import analysis as an # noqa: E402
FAILURES = 0
def check(label: str, condition: bool, detail: str = "") -> None:
global FAILURES
if condition:
print(f"ok {label}")
else:
FAILURES += 1
print(f"FAIL {label} {detail}")
# --------------------------------------------------------------------------
# 1. Unit checks on the typology
# --------------------------------------------------------------------------
check("synergy = conservation ok + development ok",
an.classify(True, True) == an.SYNERGY)
check("economy impaired = conservation ok + development lagging",
an.classify(True, False) == an.ECONOMY_IMPAIRED)
check("conservation impaired = loss + development ok",
an.classify(False, True) == an.CONSERVATION_IMPAIRED)
check("retreat = loss + development lagging",
an.classify(False, False) == an.RETREAT)
check("unknown inputs give no class", an.classify(None, True) is None)
check("orient flips 'lower is better' indicators",
list(an.orient(pd.Series([2.0, -3.0]), "low")) == [-2.0, 3.0])
check("orient leaves 'higher is better' alone",
list(an.orient(pd.Series([2.0, -3.0]), "high")) == [2.0, -3.0])
ind = pd.read_parquet(ROOT / "fixture_indicators_1990_2024.parquet")
chg = an.change_between(ind, "SP.DYN.LE00.IN", 1995, 2020)
check("change_between returns one row per country", len(chg) > 0 and
chg["iso3"].is_unique, f"({len(chg)} rows)")
check("change_between endpoints respect the window",
bool(((chg["start_year"] - 1995).abs() <= 3).all()
and ((chg["end_year"] - 2020).abs() <= 3).all()))
res = an.build_typology(ind, "AG.LND.FRST.ZS", "SP.DYN.LE00.IN", 1995, 2020)
check("typology classifies every matched country",
not res.frame.empty and res.frame["relationship"].notna().all())
check("typology shares sum to 100", abs(res.shares.sum() - 100) < 1e-6)
check("median reference splits the countries roughly in half",
0.3 <= res.frame["conservation_ok"].mean() <= 0.7)
un = pd.read_parquet(ROOT / "fixture_un_population.parquet")
obs = an.observations_only(un)
check("projections are excluded from the analytic frame",
obs["year"].max() <= 2024 and (obs["series"] == "Estimate").all(),
f"(max year {obs['year'].max()})")
check("projections are still present in the full frame",
un["year"].max() > 2024)
check("World Bank rows survive the projection filter",
len(an.observations_only(ind)) == len(ind))
combined = pd.concat([ind, un], ignore_index=True)
res_un = an.build_typology(an.observations_only(combined),
"AG.LND.FRST.ZS", "UN:67", 1995, 2020)
check("typology works across mixed sources",
not res_un.frame.empty and res_un.frame["relationship"].notna().all())
check("typology never reads a projection year",
res_un.frame["end_year_cons"].max() <= 2024)
pas = pd.read_parquet(ROOT / "fixture_protected_areas.parquet")
scores = pd.read_csv(ROOT / "fixture_effectiveness_upload.csv")
joined, report = an.match_effectiveness(pas, scores)
check("effectiveness join matches the uploaded rows",
report["matched"] >= len(scores) * 0.9,
f"(matched {report['matched']}/{report['uploaded']})")
no_match, report2 = an.match_effectiveness(
pas, pd.DataFrame({"name": ["Nowhere Park"], "score": [3.0]}))
check("unmatched uploads are reported, not silently dropped",
report2["matched"] == 0 and report2["unmatched"] == ["Nowhere Park"])
enriched = an.attach_country_context(
pas, an.observations_only(combined),
["EN.POP.DNST", "SP.RUR.TOTL.ZS", "UN:54", "UN:83"])
enriched["neighbours_50km"] = an.neighbour_counts(enriched).values
pressure = an.pressure_index(enriched)
check("pressure index is bounded 0-100",
float(pressure.min()) >= 0 and float(pressure.max()) <= 100)
components = an.pressure_components(enriched)
check("pressure index prefers UN density and child dependency",
"UN:54" in components and "UN:83" in components, f"(used {components})")
check("overlapping span excludes years one series lacks",
an.overlapping_span(combined, ["AG.LND.FRST.ZS", "UN:67"]) == (1990, 2024),
f"({an.overlapping_span(combined, ['AG.LND.FRST.ZS', 'UN:67'])})")
check("overlapping span is None when an indicator is absent",
an.overlapping_span(combined, ["AG.LND.FRST.ZS", "NOPE"]) is None)
res_span = an.build_typology(an.observations_only(combined), "AG.LND.FRST.ZS",
"UN:67", *an.overlapping_span(combined,
["AG.LND.FRST.ZS", "UN:67"]))
check("typology over the overlapping span returns rows", not res_span.frame.empty)
assoc = an.association(res_span)
check("association reports the full 2x2 count",
assoc["n"] == assoc["n11"] + assoc["n10"] + assoc["n01"] + assoc["n00"])
check("phi stays within [-1, 1]", -1.0 <= assoc["phi"] <= 1.0, f"(phi={assoc['phi']})")
check("odds ratio survives an empty cell",
an.association(an.build_typology(
pd.DataFrame({"iso3": ["A", "B"], "country": ["A", "B"],
"indicator": ["X", "X"], "year": [1990, 1990],
"value": [1.0, 2.0]}), "X", "X", 1990, 2000))["n"] == 0)
cov = an.coverage(ind)
check("coverage lists every indicator once",
len(cov) == ind["indicator"].nunique() and cov["indicator"].is_unique)
import json # noqa: E402
bounds = json.loads((ROOT / "fixture_boundaries_adm0.json").read_text())
regions = an.assign_regions(pas, bounds)
check("point-in-polygon assigns most protected areas to a region",
regions["region"].notna().mean() > 0.4,
f"({regions['region'].notna().mean():.0%} assigned)")
# --------------------------------------------------------------------------
# 2. Every page renders without raising
# --------------------------------------------------------------------------
APP = str(ROOT / "app.py")
def run_page(index: int, language: str | None = None, label: str = "") -> None:
at = AppTest.from_file(APP, default_timeout=240).run()
if language:
at.sidebar.radio[0].set_value(language).run()
options = at.sidebar.radio[1].options
at.sidebar.radio[1].set_value(options[index]).run()
detail = ""
if at.exception:
detail = " | ".join(str(e.value)[:200] for e in at.exception)
check(f"page renders: {label or options[index]}", not at.exception, detail)
base = AppTest.from_file(APP, default_timeout=240).run()
check("initial load", not base.exception,
" | ".join(str(e.value)[:300] for e in base.exception) if base.exception else "")
if not base.exception:
n_pages = len(base.sidebar.radio[1].options)
for i in range(n_pages):
run_page(i)
for i in range(n_pages):
run_page(i, language="Français", label=f"FR {i}")
# filters and empty selections must not break anything
at = AppTest.from_file(APP, default_timeout=240).run()
at.sidebar.multiselect[0].set_value(["Kenya", "Senegal"]).run()
check("country filter applied", not at.exception)
at = AppTest.from_file(APP, default_timeout=240).run()
at.sidebar.radio[1].set_value(at.sidebar.radio[1].options[4]).run() # trajectories
if at.multiselect:
at.multiselect[0].set_value([]).run()
check("trajectories with no country selected", not at.exception)
print(f"\nFAILURES: {FAILURES}")
raise SystemExit(1 if FAILURES else 0)