-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest_piiregex.py
184 lines (144 loc) · 4.67 KB
/
test_piiregex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# coding: utf-8
from piiregex import PiiRegex
import pytest
@pytest.fixture
def pii_regex():
return PiiRegex()
def test_dates_numeric(pii_regex):
matching = ["1-19-14", "1.19.14", "1.19.14", "01.19.14"]
for s in matching:
assert pii_regex.dates(s) == [s]
def test_dates_verbose(pii_regex):
matching = [
"January 19th, 2014",
"Jan. 19th, 2014",
"Jan 19 2014",
"19 Jan 2014",
]
for s in matching:
assert pii_regex.dates(s) == [s]
def test_times(pii_regex):
matching = ["09:45", "9:45", "23:45", "9:00am", "9am", "9:00 A.M.", "9:00 pm"]
for s in matching:
assert pii_regex.times(s) == [s]
def test_phones(pii_regex):
matching = [
"12345678900",
"1234567890",
"+1 234 567 8900",
"234-567-8900",
"1-234-567-8900",
"1.234.567.8900",
"5678900",
"567-8900",
"(123) 456 7890",
"+41 22 730 5989",
"(+41) 22 730 5989",
"+442345678900",
]
for s in matching:
assert pii_regex.phones(s) == [s]
def test_emails(pii_regex):
matching = ["john.smith@gmail.com", "john_smith@gmail.com", "john@example.net"]
non_matching = ["john.smith@gmail..com"]
for s in matching:
assert pii_regex.emails(s) == [s]
for s in non_matching:
assert pii_regex.phones(s) != [s]
def test_ips(pii_regex):
matching = ["127.0.0.1", "192.168.1.1", "8.8.8.8"]
for s in matching:
assert pii_regex.ips(s) == [s]
def test_ipv6s(pii_regex):
matching = [
"fe80:0000:0000:0000:0204:61ff:fe9d:f156",
"fe80:0:0:0:204:61ff:fe9d:f156",
"fe80::204:61ff:fe9d:f156",
"fe80:0000:0000:0000:0204:61ff:254.157.241.86",
"fe80:0:0:0:0204:61ff:254.157.241.86",
"fe80::204:61ff:254.157.241.86",
"::1",
]
for s in matching:
assert pii_regex.ipv6s(s) == [s]
def test_creditcards(pii_regex):
matching = [
"0000-0000-0000-0000",
"0123456789012345",
"0000 0000 0000 0000",
"012345678901234",
]
for s in matching:
assert pii_regex.credit_cards(s) == [s]
def test_btc_addresses(pii_regex):
matching = [
"1LgqButDNV2rVHe9DATt6WqD8tKZEKvaK2",
"19P6EYhu6kZzRy9Au4wRRZVE8RemrxPbZP",
"1bones8KbQge9euDn523z5wVhwkTP3uc1",
"1Bow5EMqtDGV5n5xZVgdpRPJiiDK6XSjiC",
]
non_matching = [
"2LgqButDNV2rVHe9DATt6WqD8tKZEKvaK2",
"19Ry9Au4wRRZVE8RemrxPbZP",
"1bones8KbQge9euDn523z5wVhwkTP3uc12939",
"1Bow5EMqtDGV5n5xZVgdpR",
]
for s in matching:
assert pii_regex.btc_addresses(s) == [s]
for s in non_matching:
assert pii_regex.btc_addresses(s) != [s]
def test_street_addresses(pii_regex):
matching = [
"checkout the new place at 101 main st.",
"504 parkwood drive",
"3 elm boulevard",
"500 elm street ",
]
non_matching = ["101 main straight"]
for s in matching:
# Strings evaluate true.
assert pii_regex.street_addresses(s)
for s in non_matching:
assert pii_regex.street_addresses(s) != [s]
def test_po_boxes(pii_regex):
matching = ["PO Box 123456", "hey p.o. box 234234 hey"]
non_matching = ["101 main straight"]
for s in matching:
# Strings evaluate true.
assert pii_regex.po_boxes(s)
for s in non_matching:
assert pii_regex.po_boxes(s) == []
def test_zip_codes(pii_regex):
matching = ["02540", "02540-4119"]
non_matching = ["101 main straight", "123456"]
for s in matching:
# Strings evaluate true.
assert pii_regex.zip_codes(s)
for s in non_matching:
assert pii_regex.zip_codes(s) == []
def test_postcodes(pii_regex):
matching = ["E1 5JH", "EC1V 2NX", "SE1 1AA", "e15jh", "e151aa"]
non_matching = ["E1", "ec1v"]
for s in matching:
assert pii_regex.postcodes(s)
for s in non_matching:
assert pii_regex.postcodes(s) == []
def test_ukphones(pii_regex):
matching = ["07123123123", "07123 123123"]
non_matching = ["07123 123123123"]
for s in matching:
assert pii_regex.ukphones(s)
for s in non_matching:
assert pii_regex.ukphones(s) == []
def test_any_match(pii_regex):
# This matches a phone number.
assert pii_regex.any_match('07123 123123') is True
# This should match nothing.
assert pii_regex.any_match('asdasdasdasdasd') is False
def test_any_match_using_init_method():
# This matches a phone number.
parsed_text = PiiRegex("07123 123123")
assert parsed_text.any_match() is True
# This should match nothing.
parsed_text = PiiRegex('asdasdasdasdasd')
assert parsed_text.any_match() is False