-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathtest_suffixes.py
More file actions
205 lines (169 loc) · 7.92 KB
/
Copy pathtest_suffixes.py
File metadata and controls
205 lines (169 loc) · 7.92 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import pytest
from nameparser import HumanName
from tests.base import HumanNameTestBase
class SuffixesTestCase(HumanNameTestBase):
def test_suffix(self) -> None:
hn = HumanName("Joe Franklin Jr")
self.m(hn.first, "Joe", hn)
self.m(hn.last, "Franklin", hn)
self.m(hn.suffix, "Jr", hn)
def test_suffix_with_periods(self) -> None:
hn = HumanName("Joe Dentist D.D.S.")
self.m(hn.first, "Joe", hn)
self.m(hn.last, "Dentist", hn)
self.m(hn.suffix, "D.D.S.", hn)
def test_two_suffixes(self) -> None:
hn = HumanName("Kenneth Clarke QC MP")
self.m(hn.first, "Kenneth", hn)
self.m(hn.last, "Clarke", hn)
# NOTE: this adds a comma when the original format did not have one.
# not ideal but at least its in the right bucket
self.m(hn.suffix, "QC, MP", hn)
def test_two_suffixes_lastname_comma_format(self) -> None:
hn = HumanName("Washington Jr. MD, Franklin")
self.m(hn.first, "Franklin", hn)
self.m(hn.last, "Washington", hn)
# NOTE: this adds a comma when the original format did not have one.
self.m(hn.suffix, "Jr., MD", hn)
def test_two_suffixes_suffix_comma_format(self) -> None:
hn = HumanName("Franklin Washington, Jr. MD")
self.m(hn.first, "Franklin", hn)
self.m(hn.last, "Washington", hn)
self.m(hn.suffix, "Jr. MD", hn)
def test_suffix_containing_periods(self) -> None:
hn = HumanName("Kenneth Clarke Q.C.")
self.m(hn.first, "Kenneth", hn)
self.m(hn.last, "Clarke", hn)
self.m(hn.suffix, "Q.C.", hn)
def test_suffix_containing_periods_lastname_comma_format(self) -> None:
hn = HumanName("Clarke, Kenneth, Q.C. M.P.")
self.m(hn.first, "Kenneth", hn)
self.m(hn.last, "Clarke", hn)
self.m(hn.suffix, "Q.C. M.P.", hn)
def test_suffix_containing_periods_suffix_comma_format(self) -> None:
hn = HumanName("Kenneth Clarke Q.C., M.P.")
self.m(hn.first, "Kenneth", hn)
self.m(hn.last, "Clarke", hn)
self.m(hn.suffix, "Q.C., M.P.", hn)
def test_suffix_with_single_comma_format(self) -> None:
hn = HumanName("John Doe jr., MD")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "jr., MD", hn)
def test_suffix_with_double_comma_format(self) -> None:
hn = HumanName("Doe, John jr., MD")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "jr., MD", hn)
def test_phd_with_erroneous_space(self) -> None:
hn = HumanName("John Smith, Ph. D.")
self.m(hn.first, "John", hn)
self.m(hn.last, "Smith", hn)
self.m(hn.suffix, "Ph. D.", hn)
def test_phd_extracted_without_comma(self) -> None:
hn = HumanName("John Smith Ph. D.")
self.m(hn.first, "John", hn)
self.m(hn.last, "Smith", hn)
self.m(hn.suffix, "Ph. D.", hn)
def test_phd_conflict(self) -> None:
hn = HumanName("Adolph D")
self.m(hn.first, "Adolph", hn)
self.m(hn.last, "D", hn)
# http://en.wikipedia.org/wiki/Ma_(surname)
def test_potential_suffix_that_is_also_last_name(self) -> None:
hn = HumanName("Jack Ma")
self.m(hn.first, "Jack", hn)
self.m(hn.last, "Ma", hn)
def test_potential_suffix_that_is_also_last_name_comma(self) -> None:
hn = HumanName("Ma, Jack")
self.m(hn.first, "Jack", hn)
self.m(hn.last, "Ma", hn)
def test_potential_suffix_that_is_also_last_name_with_suffix(self) -> None:
hn = HumanName("Jack Ma Jr")
self.m(hn.first, "Jack", hn)
self.m(hn.last, "Ma", hn)
self.m(hn.suffix, "Jr", hn)
def test_potential_suffix_that_is_also_last_name_with_suffix_comma(self) -> None:
hn = HumanName("Ma III, Jack Jr")
self.m(hn.first, "Jack", hn)
self.m(hn.last, "Ma", hn)
self.m(hn.suffix, "III, Jr", hn)
# https://github.com/derek73/python-nameparser/issues/27
@pytest.mark.xfail
def test_king(self) -> None:
hn = HumanName("Dr King Jr")
self.m(hn.title, "Dr", hn)
self.m(hn.last, "King", hn)
self.m(hn.suffix, "Jr", hn)
def test_multiple_letter_suffix_with_periods(self) -> None:
hn = HumanName("John Doe Msc.Ed.")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "Msc.Ed.", hn)
def test_suffix_with_periods_with_comma(self) -> None:
hn = HumanName("John Doe, Msc.Ed.")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "Msc.Ed.", hn)
def test_suffix_with_periods_with_lastname_comma(self) -> None:
hn = HumanName("Doe, John Msc.Ed.")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "Msc.Ed.", hn)
def test_suffix_delimiter_default_on_constants(self) -> None:
from nameparser.config import CONSTANTS
self.assertIs(CONSTANTS.suffix_delimiter, None)
def test_suffix_delimiter_kwarg_accepted(self) -> None:
hn = HumanName("Steven Hardman, RN - CRNA", suffix_delimiter=" - ")
self.assertEqual(hn.suffix_delimiter, " - ")
def test_suffix_delimiter_basic(self) -> None:
hn = HumanName("Steven Hardman, RN - CRNA", suffix_delimiter=" - ")
self.m(hn.first, "Steven", hn)
self.m(hn.last, "Hardman", hn)
self.m(hn.suffix, "RN, CRNA", hn)
def test_suffix_delimiter_multiple(self) -> None:
hn = HumanName("John Doe, MD - PhD - FACS", suffix_delimiter=" - ")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "MD, PhD, FACS", hn)
def test_suffix_delimiter_no_effect_without_comma(self) -> None:
# suffix_delimiter only applies after the comma split; space-separated
# suffixes already work via the no-comma parse path
hn = HumanName("John Doe MD PhD", suffix_delimiter=" - ")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "MD, PhD", hn)
def test_suffix_delimiter_constants_level(self) -> None:
from nameparser.config import CONSTANTS
_orig = CONSTANTS.suffix_delimiter
try:
CONSTANTS.suffix_delimiter = " - "
hn = HumanName("Steven Hardman, RN - CRNA")
self.m(hn.first, "Steven", hn)
self.m(hn.last, "Hardman", hn)
self.m(hn.suffix, "RN, CRNA", hn)
finally:
CONSTANTS.suffix_delimiter = _orig
def test_suffix_delimiter_none_by_default_known_limitation(self) -> None:
# Without suffix_delimiter set, " - " between suffixes breaks parsing.
# This test documents the known limitation — do not "fix" it.
hn = HumanName("Steven Hardman, RN - CRNA")
self.m(hn.first, "RN", hn)
self.m(hn.last, "Steven Hardman", hn)
self.m(hn.suffix, "CRNA", hn)
def test_suffix_delimiter_trailing_delimiter_ignored(self) -> None:
# Trailing delimiter produces an empty token that must be filtered out.
# Using a non-whitespace-terminated delimiter so stripping doesn't consume it.
hn = HumanName("John Doe, MD-PhD-", suffix_delimiter="-")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "MD, PhD", hn)
def test_suffix_delimiter_comma_space_is_noop(self) -> None:
hn = HumanName("John Doe, MD, PhD", suffix_delimiter=", ")
self.m(hn.suffix, "MD, PhD", hn)
def test_suffix_delimiter_inverted_format_known_limitation(self) -> None:
# In inverted format, the first-name part is also split on the delimiter.
# "Mary - Kate" becomes two separate parts, causing a wrong parse.
# This is a documented limitation — do not "fix" it without a broader solution.
hn = HumanName("Doe, Mary - Kate, RN", suffix_delimiter=" - ")
self.assertNotEqual(hn.first, "Mary - Kate")