Retry failed Wikidata SPARQL queries

Closes: #11
This commit is contained in:
Edward Betts 2024-02-25 18:56:46 +00:00
parent c3355c7154
commit 89fe3404aa
2 changed files with 21 additions and 1 deletions

View file

@ -106,6 +106,7 @@ def qid_to_commons_category(qid: str, check_p910: bool = True) -> str | None:
Row = dict[str, dict[str, typing.Any]] Row = dict[str, dict[str, typing.Any]]
@backoff.on_exception(backoff.expo, QueryError, max_tries=5)
def wdqs(query: str) -> list[Row]: def wdqs(query: str) -> list[Row]:
"""Pass query to the Wikidata Query Service.""" """Pass query to the Wikidata Query Service."""
r = requests.post( r = requests.post(

View file

@ -2,7 +2,7 @@ import pytest
import pytest_mock import pytest_mock
import requests import requests
import responses import responses
from geocode.wikidata import APIResponseError, api_call from geocode.wikidata import APIResponseError, QueryError, api_call, wdqs
max_tries = 5 max_tries = 5
@ -51,3 +51,22 @@ def test_api_call_retries_on_connection_error(
api_call({"action": "wbgetentities", "ids": "Q42"}) api_call({"action": "wbgetentities", "ids": "Q42"})
assert mocked_sleep.call_count == max_tries - 1 assert mocked_sleep.call_count == max_tries - 1
def test_wdqs_retry(mocker: pytest_mock.plugin.MockerFixture) -> None:
"""Test retry for WDQS API calls."""
# Patch 'time.sleep' to instantly return, effectively skipping the sleep
mocked_sleep = mocker.patch("time.sleep", return_value=None)
responses.add(
responses.POST,
"https://query.wikidata.org/bigdata/namespace/wdq/sparql",
body="bad request",
status=400,
)
with pytest.raises(QueryError):
wdqs("test query")
max_tries = 5
assert mocked_sleep.call_count == max_tries - 1