polish-dynaword / src /test_scrub_pii.py
Maggio33's picture
Add in-tree PII scrub (no new parquet) (#20)
a916ede
Raw
History Blame
9.64 kB
"""TDD contract for regex PII scrub (email, phone, PESEL/NIP/REGON, account)."""
import sys
import unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
from scrub_pii import scrub_pii
# Checksum-valid fixtures (not live identifiers of private people).
PESEL = "44051401458"
NIP = "1234563218"
REGON9 = "123456785"
REGON14 = "12345678512347"
IBAN = "PL61109010140000071219812874"
class ScrubPiiTest(unittest.TestCase):
def _scrub(self, text):
return scrub_pii(text)
def test_email_becomes_pii(self):
out, counts = self._scrub("Pisz na jan.kowalski@example.com dziś.")
self.assertIn("[PII]", out)
self.assertNotIn("jan.kowalski@example.com", out)
self.assertEqual(counts["email"], 1)
self.assertIn("Pisz na", out)
self.assertIn("dziś.", out)
def test_mobile_and_grouped_phone_become_telefon(self):
out, counts = self._scrub("Zadzwoń 501 234 567 albo 501-234-567.")
self.assertEqual(out.count("[Telefon]"), 2)
self.assertNotIn("501", out)
self.assertEqual(counts["phone"], 2)
def test_leading_zero_landline(self):
out, counts = self._scrub("Siedziba: 022 123 45 67 w Warszawie.")
self.assertIn("[Telefon]", out)
self.assertNotIn("022", out)
self.assertEqual(counts["phone"], 1)
def test_plus48_paren(self):
out, counts = self._scrub("Kontakt: +48 (22) 123-45-67.")
self.assertIn("[Telefon]", out)
self.assertNotIn("+48", out)
self.assertNotIn("123-45-67", out)
self.assertEqual(counts["phone"], 1)
def test_dzwon_label_family(self):
out, counts = self._scrub("dzwoń: 601234567 lub tel. 602-234-567")
self.assertEqual(counts["phone"], 2)
self.assertNotIn("601234567", out)
self.assertNotIn("602-234-567", out)
def test_newline_over_span(self):
out, counts = self._scrub("tel.\n+48\n501\n234\n567\nproszę dzwonić")
self.assertIn("[Telefon]", out)
self.assertEqual(counts["phone"], 1)
self.assertNotIn("501", out)
def test_foreign_uk_bare(self):
out, counts = self._scrub("London office 020 7946 0958 weekday mornings.")
self.assertIn("[Telefon]", out)
self.assertNotIn("7946", out)
self.assertEqual(counts["phone"], 1)
def test_pesel_nip_regon_iban(self):
text = (
f"PESEL {PESEL}, NIP {NIP}, REGON {REGON9} / {REGON14}, "
f"konto {IBAN}."
)
out, counts = self._scrub(text)
self.assertNotIn(PESEL, out)
self.assertNotIn(NIP, out)
self.assertNotIn(REGON14, out)
self.assertNotIn(IBAN, out)
self.assertEqual(out.count("[PII]"), 5)
self.assertEqual(counts["pesel"], 1)
self.assertEqual(counts["nip"], 1)
self.assertEqual(counts["regon"], 2)
self.assertEqual(counts["account"], 1)
def test_nip_with_dashes(self):
out, counts = self._scrub("NIP 123-456-32-18 na fakturze.")
self.assertIn("[PII]", out)
self.assertNotIn("123-456-32-18", out)
self.assertEqual(counts["nip"], 1)
def test_invalid_eleven_digits_are_not_pesel(self):
# Same length as PESEL, fails checksum — statute / case-like numbers stay.
bogus = "44051401457"
out, counts = self._scrub(f"Sygnatura {bogus} pozostaje.")
self.assertIn(bogus, out)
self.assertEqual(counts["pesel"], 0)
def test_dates_and_case_numbers_survive(self):
text = "Wyrok z 2018-03-22, sygn. II K 336/17, art. 4 pkt 2."
out, counts = self._scrub(text)
self.assertEqual(out, text)
self.assertEqual(sum(counts.values()), 0)
def test_official_names_stay(self):
text = "Poseł Jan Kowalski złożył interpelację w Sejmie RP."
out, counts = self._scrub(text)
self.assertEqual(out, text)
self.assertEqual(sum(counts.values()), 0)
def test_idempotent(self):
once, _ = self._scrub(f"mail a@b.pl tel +48 501 234 567 PESEL {PESEL}")
twice, counts = self._scrub(once)
self.assertEqual(once, twice)
self.assertEqual(sum(counts.values()), 0)
def test_postal_code_and_krs_survive(self):
text = "Kod 02-123 Warszawa, KRS 0000123456, kwota 500,00 zł."
out, counts = self._scrub(text)
self.assertEqual(out, text)
self.assertEqual(counts["phone"], 0)
self.assertEqual(sum(counts.values()), 0)
def test_kw_and_postal_range_survive(self):
text = "KW KR1P/00012345/6, kod 00-950, okres 2020-2024."
out, counts = self._scrub(text)
self.assertEqual(out, text)
self.assertEqual(counts["phone"], 0)
def test_newline_phone_does_not_consume_unrelated_text(self):
text = "tel.\nW sprawie art. 5\nproszę dzwonić 501 234 567."
out, counts = self._scrub(text)
self.assertIn("W sprawie art. 5", out)
self.assertIn("[Telefon]", out)
self.assertEqual(counts["phone"], 1)
self.assertNotIn("501", out)
def test_iban_without_pl_prefix(self):
bare_iban = "61109010140000071219812874"
out, counts = self._scrub(f"Rachunek nr {bare_iban}")
self.assertNotIn(bare_iban, out)
self.assertEqual(counts["account"], 1)
self.assertEqual(counts["phone"], 0)
def test_spaced_iban_and_nip_prefixes(self):
spaced = "61 1090 1014 0000 0712 1981 2874"
text = f"nr rachunku: {spaced}; NIP:{NIP}; NIP-{NIP}."
out, counts = self._scrub(text)
self.assertNotIn("1090", out)
self.assertNotIn(NIP, out)
self.assertEqual(counts["account"], 1)
self.assertEqual(counts["nip"], 2)
self.assertEqual(counts["phone"], 0)
def test_execution_order_precedence(self):
text = f"Przelew na {IBAN}."
out, counts = self._scrub(text)
self.assertNotIn("611090", out)
self.assertEqual(counts["account"], 1)
self.assertEqual(counts["pesel"], 0)
self.assertEqual(counts["phone"], 0)
self.assertEqual(counts["regon"], 0)
def test_complex_email_formats(self):
addr = "jan.k+alert@pwr.edu.pl"
out, counts = self._scrub(f"Kontakt: {addr}")
self.assertNotIn(addr, out)
self.assertEqual(counts["email"], 1)
nested = "jan.kowalski+test@subdomain.example.pwr.edu.pl"
out, counts = self._scrub(nested)
self.assertNotIn(nested, out)
self.assertEqual(counts["email"], 1)
def test_pii_inside_brackets_or_quotes(self):
out, counts = self._scrub(f'("email: jan@ex.com", PESEL: "{PESEL}")')
self.assertNotIn("jan@ex.com", out)
self.assertNotIn(PESEL, out)
self.assertEqual(counts["email"], 1)
self.assertEqual(counts["pesel"], 1)
def test_iban_with_mixed_dashes_and_spaces(self):
# Same MOD-97 number as IBAN, invoice-style PL + space + hyphen groups.
mixed_iban = "PL 61-1090-1014-0000-0712-1981-2874"
out, counts = self._scrub(f"Rachunek: {mixed_iban}")
self.assertNotIn("1090", out)
self.assertNotIn("0712", out)
self.assertEqual(counts["account"], 1)
self.assertEqual(counts["phone"], 0)
def test_nip_and_regon_with_colons_and_newlines(self):
text = f"NIP:\n{NIP}\nREGON:\n{REGON9}"
out, counts = self._scrub(text)
self.assertNotIn(NIP, out)
self.assertNotIn(REGON9, out)
self.assertEqual(counts["nip"], 1)
self.assertEqual(counts["regon"], 1)
self.assertIn("NIP:", out)
self.assertIn("REGON:", out)
def test_postal_code_does_not_trigger_phone(self):
text = "Adres: ul. Wiejska 4, 00-902 Warszawa."
out, counts = self._scrub(text)
self.assertEqual(out, text)
self.assertEqual(counts["phone"], 0)
def test_newline_phone_is_bounded(self):
text = "tel.\n\n\nW sprawie umowy proszę dzwonić pod 501 234 567."
out, counts = self._scrub(text)
self.assertIn("W sprawie umowy", out)
self.assertEqual(counts["phone"], 1)
self.assertNotIn("501", out)
def test_datetime_stamps_are_not_phones(self):
# HPLT leftover: DD-MM-YYYY HH:MM looks like a leading-0 landline.
text = "wpis - 08-03-2020 13:27 | 08-07-2014 16:15:00 na liście."
out, counts = self._scrub(text)
self.assertEqual(out, text)
self.assertEqual(counts["phone"], 0)
def test_en_dash_grouped_phone(self):
out, counts = self._scrub("sekretariat tel. 65 544–47-32 od poniedziałku")
self.assertIn("[Telefon]", out)
self.assertNotIn("544", out)
self.assertEqual(counts["phone"], 1)
def test_ministry_file_numbers_are_not_phones(self):
# Real Sejm sample FPs: dotted znak / attachment stems look like landlines.
text = (
"decyzja (znak: BPRM.4820.2.3.2020). "
"pismem znak: DF.III.8200.12.2020.PP. "
"Zalacznik do pisma LUB-OMK.601.1.2024.3.pdf"
)
out, counts = self._scrub(text)
self.assertEqual(out, text)
self.assertEqual(counts["phone"], 0)
def test_iban_takes_precedence_over_substring_matches(self):
text = f"Numer konta do wpłaty: {IBAN}"
out, counts = self._scrub(text)
self.assertEqual(counts["account"], 1)
self.assertEqual(counts["pesel"], 0)
self.assertEqual(counts["phone"], 0)
self.assertEqual(counts["regon"], 0)
self.assertEqual(counts["nip"], 0)
if __name__ == "__main__":
unittest.main()