from __future__ import annotations from datetime import UTC, date, datetime import numpy as np import polars as pl import pytest from countryatlas.pipeline.changes import Series, compute_changes_and_events, detect_changes, detect_events from countryatlas.registry import indicators_by_id def _series(ind: str, values: list[float], start: int = 2000, country: str = "CAN") -> Series: years = np.arange(start, start + len(values), dtype=np.int32) return Series(country, ind, [date(int(y), 1, 1) for y in years], years, np.array(values, dtype=float)) def test_inflation_drop_headline_and_since() -> None: ind = indicators_by_id()["inflation"] vals = [2.0, 2.1, 1.9, 2.2, 2.0, 2.3, 8.1, 5.9, 5.5, 3.4] # 2015: 5.5 → 2016: 3.4 (−2.1 pts) hmm: last drop 2.1 vals = [2.0, 2.1, 1.9, 2.2, 2.0, 2.3, 2.1, 2.0, 6.8, 3.4] out = detect_changes(_series("inflation", vals, 2016), ind) kinds = {r["kind"]: r for r in out} assert "yoy_drop" in kinds h = kinds["yoy_drop"]["headline"] assert h.startswith("Inflation fell 3.4 points to 3.4 % in 2025") assert "largest drop" in h assert 0 < kinds["yoy_drop"]["severity"] <= 1 def test_floor_blocks_small_moves() -> None: ind = indicators_by_id()["inflation"] # change_floor 2 points vals = [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 3.0] # +1 pt: huge z but below the floor out = detect_changes(_series("inflation", vals), ind) assert not any(r["kind"] in ("yoy_jump", "yoy_drop") for r in out) def test_record_high_and_relative_headline() -> None: ind = indicators_by_id()["gdp-per-capita"] vals = [1000 * 1.03**k for k in range(15)] vals[5] *= 0.9 # a dip: the series must not be monotone, otherwise records are (rightly) not newsworthy vals[-1] = vals[-2] * 1.25 out = detect_changes(_series("gdp-per-capita", vals), ind) kinds = {r["kind"] for r in out} assert "record_high" in kinds and "yoy_jump" in kinds jump = next(r for r in out if r["kind"] == "yoy_jump") assert "25.0 %" in jump["headline"] and "US$" in jump["headline"] def test_sign_flip_and_events_history() -> None: ind = indicators_by_id()["gdp-growth"] vals = [3.0, 2.5, 3.1, 2.8, 3.0, 2.9, -4.5, 5.0, 2.0, -1.0] ch = detect_changes(_series("gdp-growth", vals), ind) assert any(r["kind"] == "sign_flip" for r in ch) ev = detect_events(_series("gdp-growth", vals), ind) flips = [r for r in ev if r["kind"] == "sign_flip"] assert len(flips) == 3 # 2006, 2007, 2009 assert any(r["kind"] == "yoy_drop" and r["year"] == 2006 for r in ev) def test_monotone_and_cumulative_series_are_silent() -> None: inds = indicators_by_id() vals = [100.0 * 1.02**k for k in range(30)] # strictly increasing: every point is a "record" ch = detect_changes(_series("population", vals), inds["population"]) assert not any(r["kind"] in ("record_high", "n_year_high") for r in ch) rows = [{"country_id": "CAN", "indicator_id": "cumulative-co2", "period": date(1990 + k, 1, 1), "year": 1990 + k, "value": v} for k, v in enumerate(vals)] ch2, ev2 = compute_changes_and_events(pl.DataFrame(rows), inds) assert ch2.height == 0 and ev2.height == 0 def test_changes_are_recent_and_weighted() -> None: inds = indicators_by_id() old = [2.0, 2.1, 1.9, 2.2, 2.0, 2.3, 2.1, 2.0, 6.8, 3.4] # series ends in 2008 rows = [{"country_id": "CAN", "indicator_id": "inflation", "period": date(1999 + k, 1, 1), "year": 1999 + k, "value": v} for k, v in enumerate(old)] rows += [{"country_id": "FRA", "indicator_id": "inflation", "period": date(2016 + k, 1, 1), "year": 2016 + k, "value": v} for k, v in enumerate(old)] from datetime import UTC, datetime ch, ev = compute_changes_and_events(pl.DataFrame(rows), inds, headline_ids={"inflation"}, now=datetime(2026, 9, 1, tzinfo=UTC)) assert set(ch["country_id"].to_list()) == {"FRA"} # Canada's 2008 drop is an event, not a change assert "CAN" in set(ev["country_id"].to_list()) import json d = json.loads(ch["detail"][0]) assert d["weight"] == 1.0 and "raw_severity" in d ch_w, _ = compute_changes_and_events(pl.DataFrame(rows), inds, headline_ids=set(), now=datetime(2026, 9, 1, tzinfo=UTC)) assert json.loads(ch_w["detail"][0])["weight"] == 0.9 # inflation is featured but not headline here assert ch_w["severity"][0] == pytest.approx(min(1.0, d["raw_severity"] * 0.9), rel=1e-3) def test_index_series_need_10pct_and_quarterly_is_annualised() -> None: inds = indicators_by_id() ind = inds["real-house-price-index"] # format index, ranking_eligible false assert ind.format == "index" and not ind.ranking_eligible smooth = [100 + k for k in range(12)] smooth[-1] = smooth[-2] * 1.05 # +5 %: big z, but below the 10 % rule assert not any(r["kind"] in ("yoy_jump", "yoy_drop") for r in detect_changes(_series("real-house-price-index", smooth), ind)) big = list(smooth) big[-1] = big[-2] * 1.2 # +20 % assert any(r["kind"] == "yoy_jump" for r in detect_changes(_series("real-house-price-index", big), ind)) # quarterly rows collapse to one value per year (last value for an index) → at most one detection per year rows = [] for y in range(2015, 2026): for q, month in enumerate((1, 4, 7, 10)): v = 100 + (y - 2015) * 2 + q * 0.3 if y == 2025: v = 150.0 + q # +~25 % jump in 2025 rows.append({"country_id": "CAN", "indicator_id": "real-house-price-index", "period": date(y, month, 1), "year": y, "frequency": "Q", "value": v}) ch, ev = compute_changes_and_events(pl.DataFrame(rows), inds, now=datetime(2026, 1, 1, tzinfo=UTC)) assert ch.filter(pl.col("kind") == "yoy_jump").height == 1 assert ev.group_by(["indicator_id", "year"]).len()["len"].max() <= 3 assert ev["severity"].min() >= 0.35 assert ch.filter(pl.col("kind") == "yoy_jump")["value"][0] == 153.0 # last quarter of 2025 def test_driver_over_frame() -> None: inds = indicators_by_id() rows = [] for c in ("CAN", "FRA"): for k, v in enumerate([2.0, 2.1, 1.9, 2.2, 2.0, 2.3, 2.1, 2.0, 6.8, 3.4]): rows.append({"country_id": c, "indicator_id": "inflation", "period": date(2016 + k, 1, 1), "year": 2016 + k, "value": v}) df = pl.DataFrame(rows) ch, ev = compute_changes_and_events(df, inds) assert ch.filter(pl.col("kind") == "yoy_drop").height == 2 assert set(ch.columns) >= {"id", "country_id", "indicator_id", "kind", "headline", "severity", "detected_at"} assert ev.height >= 2