"""Check attack.py on small cases you can verify by hand. Given; do not change it. python3 selfcheck.py PASS, FAIL (with what was expected), or SKIP for a function not written yet. """ import attack results = [] ROWS = [ {"name": "Ana", "zip": "80903", "dob": "1962-07-21", "sex": "F", "diagnosis": "asthma"}, {"name": "Ben", "zip": "80903", "dob": "1962-07-21", "sex": "M", "diagnosis": "asthma"}, {"name": "Cai", "zip": "80903", "dob": "1962-03-02", "sex": "F", "diagnosis": "anemia"}, {"name": "Dee", "zip": "80918", "dob": "1971-11-30", "sex": "F", "diagnosis": "migraine"}, ] def check(name, fn): try: fn() results.append("PASS") print(f"PASS {name}") except NotImplementedError: results.append("SKIP") print(f"SKIP {name} (not written yet)") except AssertionError as e: results.append("FAIL") print(f"FAIL {name}: {e}") def t_uniqueness(): got = attack.uniqueness(ROWS, ("zip", "dob")) assert abs(got - 0.5) < 1e-9, f"on zip + dob, Cai and Dee are unique and Ana and Ben share: 0.5, got {got}" got = attack.uniqueness(ROWS, ("zip", "dob", "sex")) assert got == 1.0, f"adding sex separates Ana from Ben: 1.0, got {got}" def t_link(): release = [{"zip": "80903", "dob": "1962-07-21", "sex": "F"}, {"zip": "80903", "dob": "1962-07-21", "sex": "X"}] got = attack.link(release, ROWS, ("zip", "dob", "sex")) assert got == ["Ana", None], f"expected ['Ana', None], got {got}" got = attack.link(release[:1], ROWS, ("zip", "dob")) assert got == [None], f"two voters match on zip + dob, so the attacker cannot say which: [None], got {got}" def t_generalize(): r = ROWS[0] assert attack.generalize(r, 0) == r, "level 0 leaves the record unchanged" g1 = attack.generalize(r, 1) assert g1["dob"] == "1962" and g1["zip"] == "80903", f"level 1: dob 1962, zip unchanged; got {g1}" g2 = attack.generalize(r, 2) assert (g2["dob"], g2["zip"], g2["sex"]) == ("1960s", "809**", "F"), f"level 2: 1960s, 809**, F; got {g2}" assert r["dob"] == "1962-07-21", "generalize must return a copy, not change the record it was given" def t_k(): assert attack.k_anonymity(ROWS, ("zip", "dob", "sex")) == 1, "every row is its own group: k = 1" g = [attack.generalize(r, 2) for r in ROWS] got = attack.k_anonymity(g, ("zip", "dob", "sex")) assert got == 1, f"at level 2, Ben (M) is still alone: k = 1, got {got}" assert attack.k_anonymity(g, ("zip",)) == 4, "on zip alone at level 2, all four share 809**" def t_suppress(): got = attack.suppress(ROWS, ("zip", "dob"), 2) assert [r["name"] for r in got] == ["Ana", "Ben"], f"only Ana and Ben's zip + dob group has 2 records; got {[r['name'] for r in got]}" assert attack.suppress(ROWS, ("zip", "dob"), 1) == ROWS, "k = 1 removes nothing" def t_homogeneity(): got = attack.homogeneity(ROWS, ("zip", "dob"), "diagnosis") assert got == 1.0, f"Ana and Ben share a group and a diagnosis; the others are alone: 1.0, got {got}" got = attack.homogeneity(ROWS, ("zip",), "diagnosis") assert abs(got - 0.25) < 1e-9, f"80903 holds two diagnoses, 80918 one person: 0.25, got {got}" for name, fn in [("uniqueness", t_uniqueness), ("link", t_link), ("generalize", t_generalize), ("k_anonymity", t_k), ("suppress", t_suppress), ("homogeneity", t_homogeneity)]: check(name, fn) print(f"\n{results.count('PASS')} passed, {results.count('FAIL')} failed, {results.count('SKIP')} not written yet") raise SystemExit(1 if "FAIL" in results else 0)