from datetime import date import pytest from core.tea import follow_up as fu def test_listed(): start, cutoff = date(2026, 1, 1), date(2026, 12, 31) row = { "person_id": "b", "birth_date": date(2020, 1, 1), "start_date": date(2025, 1, 1), "end_date": None, "step_date": start, "status": "active", } finished = { **row, "person_id": "a", "end_date": start, "status": "finished", "ados2_date": start, "ados2_module": "module_1", "ados2_pointing": 1, "ados2_intonation": 1, } found = fu.listed( [ row, {**row, "end_date": date(2025, 12, 31)}, {**row, "start_date": date(2027, 1, 1)}, {**row, "person_id": "c", "start_date": cutoff, "step_date": cutoff}, finished, ], start, cutoff, ) assert [r["person_id"] for r in found] == ["c", "a", "b"] assert found[1] == { **finished, "priority": None, "recent": start, "case": "in_process", "css": 1, "classification": "non_spectrum", } assert found[2] == {**row, "priority": None, "recent": start, "case": "in_process", "css": None, "classification": None} # fmt: skip assert fu.status(row, cutoff) == "active" assert fu.status({}, cutoff) is None assert fu.ordered(found, "-recent", {}, None) is found @pytest.mark.parametrize( ("confirmed_date", "not_confirmed_date", "expected"), [ ("2026-12-31", "2026-12-30", "confirmed"), ("2027-01-01", "2026-12-31", "not_confirmed"), (None, "2027-01-01", "in_process"), ], ) def test_case_at_cutoff(confirmed_date, not_confirmed_date, expected): start, cutoff = date(2026, 1, 1), date(2026, 12, 31) row = { "person_id": "child", "start_date": start, "end_date": None, "step_date": start, "confirmed_date": confirmed_date, "not_confirmed_date": not_confirmed_date, } (listed,) = fu.listed([row], start, cutoff) assert listed["case"] == expected @pytest.mark.parametrize( ("name", "values", "row", "expected"), [ ("tea-step", ["psychology,eeg"], {"step": "eeg"}, True), ("tea-step", ["psychology"], {"step": "eeg"}, False), ("tea-case", ["confirmed,not_confirmed"], {"case": "not_confirmed"}, True), ("tea-case", ["in_process"], {"case": "confirmed"}, False), ("tea-eeg-result", ["not_scored"], {"eeg_result": "not_scored"}, True), ("tea-model", ["yes"], {"eeg_scored": True}, True), ("tea-model", ["no"], {"eeg_scored": False}, True), ("tea-model", ["no"], {"eeg_scored": None}, True), ("tea-model", ["yes"], {"eeg_scored": None}, False), ("tea-age-group", ["no_data"], {"age_group": "no_data"}, True), ("tea-sex", ["female"], {"sex": "male"}, False), ("tea-alteration", ["spindles_n2,spindles_n3"], {"alt_spindles_n3": True}, True), ("tea-alteration", ["spindles_n2"], {"alt_spindles_n2": None}, False), ("tea-mchat", ["ge2", "le4"], {"mchat_score": 2}, True), ("tea-mchat", ["ge2", "le4"], {"mchat_score": 5}, False), ("tea-mchat", ["ge0"], {"mchat_score": None}, False), ("tea-css", ["ge4", "le4"], {"css": 4}, True), ("tea-css", ["le4"], {"css": None}, False), ], ) def test_matches(name, values, row, expected): assert fu.matches(row, {name: values}) is expected @pytest.mark.parametrize( "params", [{"tea-step": ["eeg,unknown"]}, {"tea-case": ["unknown"]}, {"tea-css": ["gt4"]}] ) def test_invalid_value(params): with pytest.raises(ValueError): fu.matches({}, {"tea-sex": ["female"], **params}) def test_summary(): rows = [ {"eeg_id": "1", "eeg_result": "abnormal", "alt_spindles_n3": True, "alt_posterior_rhythm": True}, {"eeg_id": "2", "eeg_result": "normal"}, {"eeg_id": "3", "eeg_result": "not_scored"}, {"eeg_id": None, "eeg_result": "no_valid_eeg", "alt_spindles_n2": None}, ] # fmt: skip assert fu.summary(rows) == { "valid": 3, "not_valid": 1, "normal": 1, "abnormal": 1, "by_alteration": [ {"alteration": "posterior_rhythm", "patients": 1}, {"alteration": "spindles_n3", "patients": 1}, ], }