Skip to content

Commit

Permalink
Give a more useful error message for entity merges
Browse files Browse the repository at this point in the history
  • Loading branch information
dseomn committed Nov 22, 2023
1 parent d954be5 commit 32a5fbb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 8 additions & 1 deletion rock_paper_sand/wikidata.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,15 @@ def entity(
f"https://www.wikidata.org/wiki/Special:EntityData/{entity_ref.id}.json" # pylint: disable=line-too-long
)
response.raise_for_status()
entities = response.json()["entities"]
if entity_ref.id not in entities:
raise ValueError(
f"JSON data for {entity_ref} does not contain "
f"{entity_ref.id!r}. Maybe it was merged with another "
"entity?"
)
self._entity_by_ref[entity_ref] = wikidata_value.Entity(
json_full=response.json()["entities"][entity_ref.id],
json_full=entities[entity_ref.id],
)
return self._entity_by_ref[entity_ref]

Expand Down
7 changes: 7 additions & 0 deletions rock_paper_sand/wikidata_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ def test_entity(self) -> None:
self._mock_session.mock_calls,
)

def test_entity_merged(self) -> None:
self._mock_session.get.return_value.json.return_value = {
"entities": {"Q2": {}}
}
with self.assertRaisesRegex(ValueError, "Q1.*merged"):
self._api.entity(wikidata_value.ItemRef("Q1"))

def test_sparql(self) -> None:
self._mock_session.get.return_value.json.return_value = {
"results": {"bindings": [{"foo": "bar"}]}
Expand Down

0 comments on commit 32a5fbb

Please sign in to comment.