|
| 1 | +import json |
| 2 | +import sys |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pandas as pd |
| 6 | +import yaml |
| 7 | + |
| 8 | +sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "scripts")) |
| 9 | + |
| 10 | +from apply_rules import ( # noqa: E402 |
| 11 | + apply_regola_001, |
| 12 | + apply_regola_002, |
| 13 | + apply_regola_003, |
| 14 | + stable_id, |
| 15 | + _derive_gen_date, |
| 16 | + WHAT_CANNOT, |
| 17 | + DISCLAIMER, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +def _df(rows, cols): |
| 22 | + return pd.DataFrame(rows, columns=cols) |
| 23 | + |
| 24 | + |
| 25 | +def test_regola_001_soglia(): |
| 26 | + rows = [ |
| 27 | + ["MARIO ROSSI", "E1", "2025", "inc", "a1"], |
| 28 | + ["MARIO ROSSI", "E2", "2025", "inc", "a2"], |
| 29 | + ["MARIO ROSSI", "E3", "2025", "inc", "a3"], |
| 30 | + ["MARIO ROSSI", "E4", "2025", "inc", "a4"], |
| 31 | + ["MARIO ROSSI", "E5", "2025", "inc", "a5"], |
| 32 | + ] |
| 33 | + df = _df(rows, ["person_name", "entity_id", "year"]) |
| 34 | + leads = apply_regola_001(df, 5, "same_calendar_year") |
| 35 | + assert len(leads) == 1 |
| 36 | + assert leads[0]["rule_id"] == "REGOLA-001" |
| 37 | + assert leads[0]["disclaimer"] == DISCLAIMER |
| 38 | + assert leads[0]["what_cannot_be_claimed"] == WHAT_CANNOT |
| 39 | + |
| 40 | + |
| 41 | +def test_regola_001_sotto_soglia(): |
| 42 | + rows = [ |
| 43 | + ["MARIO ROSSI", "E1", "2025", "inc", "a1"], |
| 44 | + ["MARIO ROSSI", "E2", "2025", "inc", "a2"], |
| 45 | + ["MARIO ROSSI", "E3", "2025", "inc", "a3"], |
| 46 | + ["MARIO ROSSI", "E4", "2025", "inc", "a4"], |
| 47 | + ] |
| 48 | + df = _df(rows, ["person_name", "entity_id", "year"]) |
| 49 | + assert apply_regola_001(df, 5, "same_calendar_year") == [] |
| 50 | + |
| 51 | + |
| 52 | +def test_regola_001_fail_closed_colonne_mancanti(): |
| 53 | + df = _df([["X", "E1"]], ["person_name", "entity_id"]) |
| 54 | + assert apply_regola_001(df, 5, "same_calendar_year") == [] |
| 55 | + |
| 56 | + |
| 57 | +def test_regola_002_diretti(): |
| 58 | + rows = [[f"AWARD", "ENT", f"2025-0{i}-01", "affidamento diretto", "s", f"r{i}"] for i in range(1, 9)] |
| 59 | + df = _df(rows, ["awardee", "entity_id", "award_date", "procedure_type", "source_dataset", "source_record_id"]) |
| 60 | + leads = apply_regola_002(df, 8, "12_months_rolling") |
| 61 | + assert len(leads) == 1 |
| 62 | + assert leads[0]["rule_id"] == "REGOLA-002" |
| 63 | + |
| 64 | + |
| 65 | +def test_regola_002_non_diretti(): |
| 66 | + rows = [[f"AWARD", "ENT", f"2025-0{i}-01", "gara ordinaria", "s", f"r{i}"] for i in range(1, 9)] |
| 67 | + df = _df(rows, ["awardee", "entity_id", "award_date", "procedure_type", "source_dataset", "source_record_id"]) |
| 68 | + assert apply_regola_002(df, 8, "12_months_rolling") == [] |
| 69 | + |
| 70 | + |
| 71 | +def test_regola_003_cig(): |
| 72 | + rows = [ |
| 73 | + ["CIG1", "SUB1", "s", "t1"], |
| 74 | + ["CIG1", "SUB2", "s", "t2"], |
| 75 | + ] |
| 76 | + df = _df(rows, ["cig", "subject_id", "source_dataset", "source_record_id"]) |
| 77 | + leads = apply_regola_003(df, 2, "full_dataset_coverage") |
| 78 | + assert len(leads) == 1 |
| 79 | + assert leads[0]["rule_id"] == "REGOLA-003" |
| 80 | + assert leads[0]["period"] == "intero periodo coperto dal dataset" |
| 81 | + |
| 82 | + |
| 83 | +def test_regola_003_fail_closed_senza_cig(): |
| 84 | + df = _df([["SUB1", "s", "t1"]], ["subject_id", "source_dataset", "source_record_id"]) |
| 85 | + assert apply_regola_003(df, 2, "full_dataset_coverage") == [] |
| 86 | + |
| 87 | + |
| 88 | +def test_determinismo_id_e_data(): |
| 89 | + rows = [ |
| 90 | + ["MARIO ROSSI", "E1", "2025", "inc", "a1"], |
| 91 | + ["MARIO ROSSI", "E2", "2025", "inc", "a2"], |
| 92 | + ["MARIO ROSSI", "E3", "2025", "inc", "a3"], |
| 93 | + ["MARIO ROSSI", "E4", "2025", "inc", "a4"], |
| 94 | + ["MARIO ROSSI", "E5", "2025", "inc", "a5"], |
| 95 | + ] |
| 96 | + df = _df(rows, ["person_name", "entity_id", "year"]) |
| 97 | + a = apply_regola_001(df, 5, "same_calendar_year") |
| 98 | + b = apply_regola_001(df, 5, "same_calendar_year") |
| 99 | + assert json.dumps(a, sort_keys=True) == json.dumps(b, sort_keys=True) |
| 100 | + assert a[0]["generation_date"] == "2025-01-01" |
| 101 | + assert a[0]["id"] == stable_id("REGOLA-001", "MARIO ROSSI|2025", "2025") |
| 102 | + |
| 103 | + |
| 104 | +def test_derive_gen_date_senza_anno(): |
| 105 | + leads = [{"period": "intero periodo coperto dal dataset"}] |
| 106 | + assert _derive_gen_date(leads) == "0000-01-01" |
| 107 | + |
| 108 | + |
| 109 | +def test_derive_gen_date_max_anno(): |
| 110 | + leads = [{"period": "2024"}, {"period": "2025"}] |
| 111 | + assert _derive_gen_date(leads) == "2025-01-01" |
| 112 | + |
| 113 | + |
| 114 | +def test_regola_004_disabilitata(): |
| 115 | + data = yaml.safe_load( |
| 116 | + open(Path(__file__).resolve().parent.parent / "rules" / "rules_v0.1.yaml", encoding="utf-8") |
| 117 | + ) |
| 118 | + r004 = next(r for r in data["rules"] if r["id"] == "REGOLA-004") |
| 119 | + assert r004["enabled"] is False |
0 commit comments