Skip to content

Commit 01ead58

Browse files
committed
fix: improve research() cache read path and deduplicate test mocks
- Use .get() instead of .setdefault() for cache reads in research() so the local-only path never mutates _data unnecessarily - Move .setdefault() to the network-write path only - Use result.setdefault() for word/confirmed keys to ensure consistent return shape across all _wikipedia_lookup error paths - Extract duplicated mock_result dict into _MOCK_SAOIRSE_PERSON constant shared by 3 test functions
1 parent b51261b commit 01ead58

2 files changed

Lines changed: 26 additions & 27 deletions

File tree

mempalace/entity_registry.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,8 @@ def research(self, word: str, auto_confirm: bool = False, allow_network: bool =
523523
Caches result. If *auto_confirm* is ``False``, marks the entry
524524
as unconfirmed (needs user review).
525525
"""
526-
# Already cached?
527-
cache = self._data.setdefault("wiki_cache", {})
526+
# Check cache (read-only — no mutation when allow_network is False)
527+
cache = self._data.get("wiki_cache", {})
528528
if word in cache:
529529
return cache[word]
530530

@@ -539,9 +539,11 @@ def research(self, word: str, auto_confirm: bool = False, allow_network: bool =
539539
"note": "network lookup disabled — pass allow_network=True to query Wikipedia",
540540
}
541541

542+
# Network path — ensure wiki_cache key exists before writing
543+
cache = self._data.setdefault("wiki_cache", {})
542544
result = _wikipedia_lookup(word)
543-
result["word"] = word
544-
result["confirmed"] = auto_confirm
545+
result.setdefault("word", word)
546+
result.setdefault("confirmed", auto_confirm)
545547

546548
cache[word] = result
547549
self.save()

tests/test_entity_registry.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
EntityRegistry,
99
)
1010

11+
# Shared mock result for Wikipedia person lookup tests
12+
_MOCK_SAOIRSE_PERSON = {
13+
"inferred_type": "person",
14+
"confidence": 0.80,
15+
"wiki_summary": "Saoirse is an Irish given name.",
16+
"wiki_title": "Saoirse",
17+
}
18+
1119

1220
# ── COMMON_ENGLISH_WORDS ────────────────────────────────────────────────
1321

@@ -238,14 +246,10 @@ def test_research_with_allow_network(tmp_path):
238246
registry = EntityRegistry.load(config_dir=tmp_path)
239247
registry.seed(mode="personal", people=[], projects=[])
240248

241-
mock_result = {
242-
"inferred_type": "person",
243-
"confidence": 0.80,
244-
"wiki_summary": "Saoirse is an Irish given name.",
245-
"wiki_title": "Saoirse",
246-
}
247-
248-
with patch("mempalace.entity_registry._wikipedia_lookup", return_value=mock_result):
249+
with patch(
250+
"mempalace.entity_registry._wikipedia_lookup",
251+
return_value=dict(_MOCK_SAOIRSE_PERSON),
252+
):
249253
result = registry.research("Saoirse", auto_confirm=True, allow_network=True)
250254
assert result["inferred_type"] == "person"
251255

@@ -255,14 +259,10 @@ def test_research_caches_result(tmp_path):
255259
registry = EntityRegistry.load(config_dir=tmp_path)
256260
registry.seed(mode="personal", people=[], projects=[])
257261

258-
mock_result = {
259-
"inferred_type": "person",
260-
"confidence": 0.80,
261-
"wiki_summary": "Saoirse is an Irish given name.",
262-
"wiki_title": "Saoirse",
263-
}
264-
265-
with patch("mempalace.entity_registry._wikipedia_lookup", return_value=mock_result):
262+
with patch(
263+
"mempalace.entity_registry._wikipedia_lookup",
264+
return_value=dict(_MOCK_SAOIRSE_PERSON),
265+
):
266266
result = registry.research("Saoirse", auto_confirm=True, allow_network=True)
267267
assert result["inferred_type"] == "person"
268268

@@ -288,13 +288,10 @@ def test_confirm_research_adds_to_people(tmp_path):
288288
registry = EntityRegistry.load(config_dir=tmp_path)
289289
registry.seed(mode="personal", people=[], projects=[])
290290

291-
mock_result = {
292-
"inferred_type": "person",
293-
"confidence": 0.80,
294-
"wiki_summary": "Saoirse is a name",
295-
"wiki_title": "Saoirse",
296-
}
297-
with patch("mempalace.entity_registry._wikipedia_lookup", return_value=mock_result):
291+
with patch(
292+
"mempalace.entity_registry._wikipedia_lookup",
293+
return_value=dict(_MOCK_SAOIRSE_PERSON),
294+
):
298295
registry.research("Saoirse", auto_confirm=False, allow_network=True)
299296

300297
registry.confirm_research("Saoirse", entity_type="person", relationship="friend")

0 commit comments

Comments
 (0)