"""Attrition of a CaCu cohort: candidates, eligible, pending and reasons, each counted once.""" from datetime import date import pandas as pd from models.cacu import reports def test_a_candidate_with_two_reasons_is_excluded_once(): """Pins the totals, the pending rule, the people without a candidate and the as-of cut.""" rows = pd.DataFrame( [ ("eligible", "2025-01-01", None, None), ("two", "2025-01-01", None, "hysterectomy"), ("two", "2025-01-01", None, "under_18"), ("waiting", "2025-01-01", None, "no_index_result"), ("waiting_and_out", "2025-01-01", None, "no_index_result"), ("waiting_and_out", "2025-01-01", None, "prior_outcome"), ("after", "2025-07-01", None, "hysterectomy"), ], columns=["person_id", "index_date", "index_sample_id", "criterion"], ) found = reports.attrition("cacu_cohort_surveillance", rows, 10, date(2025, 6, 30)) assert (found["candidates"], found["eligible"], found["excluded"]) == (4, 1, 3) assert (found["pending"], found["without_candidate"]) == (1, 6) assert found["by_reason"] == { "hysterectomy": 1, "no_index_result": 2, "prior_outcome": 1, "under_18": 1, } # fmt: skip assert found["by_stage"] == {"eligibility": 1, "history": 1, "index": 2} assert found["stage_by_reason"]["no_index_result"] == "index" assert found["stage_counts_overlap"] is True