Skip to content

Commit 7f0aae8

Browse files
derek73claude
andcommitted
test: prime cache in invalidation tests; use is-None cache guard
Six of the suffixes_prefixes_titles tests passed against the unfixed code because they read the property only once on a cold cache, so they asserted "the union includes its source sets" rather than "the cache stays consistent after a mutation." Prime the cache before mutating so they actually exercise invalidation; verified they now fail (14 across the dual-run fixture) against master's pre-fix config. Change the property guard from `if not self._pst` to `if self._pst is None` so a legitimately empty union caches instead of recomputing on every access — and so "cached" means "computed," which is the state the primed tests rely on. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 5fc4b6b commit 7f0aae8

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

nameparser/config/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def _invalidate_pst(self) -> None:
281281

282282
@property
283283
def suffixes_prefixes_titles(self) -> Set[str]:
284-
if not self._pst:
284+
if self._pst is None:
285285
self._pst = self.prefixes | self.suffix_acronyms | self.suffix_not_acronyms | self.titles
286286
return self._pst
287287

tests/test_constants.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,14 @@ def test_add_constant_with_explicit_encoding(self) -> None:
108108
def test_suffixes_prefixes_titles_reflects_add_title(self) -> None:
109109
"""suffixes_prefixes_titles must include titles added after construction."""
110110
c = Constants()
111+
_ = c.suffixes_prefixes_titles # prime the cache so invalidation is exercised
111112
c.titles.add('emerita')
112113
self.assertIn('emerita', c.suffixes_prefixes_titles)
113114

114115
def test_suffixes_prefixes_titles_reflects_add_prefix(self) -> None:
115116
"""suffixes_prefixes_titles must include prefixes added after construction."""
116117
c = Constants()
118+
_ = c.suffixes_prefixes_titles # prime the cache so invalidation is exercised
117119
c.prefixes.add('xpfx')
118120
self.assertIn('xpfx', c.suffixes_prefixes_titles)
119121

@@ -136,12 +138,14 @@ def test_suffixes_prefixes_titles_reflects_remove_prefix(self) -> None:
136138
def test_suffixes_prefixes_titles_reflects_add_suffix_acronym(self) -> None:
137139
"""suffixes_prefixes_titles must include suffix acronyms added after construction."""
138140
c = Constants()
141+
_ = c.suffixes_prefixes_titles # prime the cache so invalidation is exercised
139142
c.suffix_acronyms.add('xsfx')
140143
self.assertIn('xsfx', c.suffixes_prefixes_titles)
141144

142145
def test_suffixes_prefixes_titles_reflects_add_suffix_not_acronym(self) -> None:
143146
"""suffixes_prefixes_titles must include non-acronym suffixes added after construction."""
144147
c = Constants()
148+
_ = c.suffixes_prefixes_titles # prime the cache so invalidation is exercised
145149
c.suffix_not_acronyms.add('xsfx')
146150
self.assertIn('xsfx', c.suffixes_prefixes_titles)
147151

@@ -155,11 +159,13 @@ def test_suffixes_prefixes_titles_reflects_add_after_initial_read(self) -> None:
155159
def test_is_rootname_consistent_with_is_title(self) -> None:
156160
"""is_rootname must return False for words recognised by is_title."""
157161
hn = HumanName("", constants=None)
162+
_ = hn.C.suffixes_prefixes_titles # prime the cache so a stale entry would be observable
158163
hn.C.titles.add('emerita')
159164
self.assertFalse(hn.is_rootname('emerita'))
160165

161166
def test_is_rootname_consistent_with_is_prefix(self) -> None:
162167
"""is_rootname must return False for words recognised by is_prefix."""
163168
hn = HumanName("", constants=None)
169+
_ = hn.C.suffixes_prefixes_titles # prime the cache so a stale entry would be observable
164170
hn.C.prefixes.add('xpfx')
165171
self.assertFalse(hn.is_rootname('xpfx'))

0 commit comments

Comments
 (0)