Log Wikimedia API requests
This commit is contained in:
parent
0188cbe0bf
commit
93a4572a5d
3 changed files with 241 additions and 3 deletions
|
|
@ -1,7 +1,12 @@
|
|||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
import pytest_mock
|
||||
import requests
|
||||
import responses
|
||||
from geocode import headers
|
||||
from geocode.wikimedia_api_logging import WikimediaApiLogConfig
|
||||
from geocode.wikidata import (
|
||||
APIResponseError,
|
||||
QueryError,
|
||||
|
|
@ -92,6 +97,39 @@ def test_mediawiki_error_message_falls_back_to_response_text() -> None:
|
|||
assert mediawiki_error_message(response) == "Please slow down"
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_api_call_logs_wikimedia_request(
|
||||
mocker: pytest_mock.plugin.MockerFixture, tmp_path: Path
|
||||
) -> None:
|
||||
"""Test Wikimedia API requests are logged as JSONL metrics."""
|
||||
log_path = tmp_path / "wikimedia-api.jsonl"
|
||||
mocker.patch(
|
||||
"geocode.wikidata.wikimedia_log_config",
|
||||
WikimediaApiLogConfig(
|
||||
tool="geocode",
|
||||
log_path=log_path,
|
||||
user_agent=headers["User-Agent"],
|
||||
),
|
||||
)
|
||||
responses.add(
|
||||
responses.GET,
|
||||
"https://www.wikidata.org/w/api.php",
|
||||
json={"entities": {"Q42": {"id": "Q42"}}},
|
||||
status=200,
|
||||
)
|
||||
|
||||
api_call({"action": "wbgetentities", "ids": "Q42"})
|
||||
|
||||
record = json.loads(log_path.read_text().strip())
|
||||
assert record["tool"] == "geocode"
|
||||
assert record["method"] == "GET"
|
||||
assert record["api_host"] == "www.wikidata.org"
|
||||
assert record["path"] == "/w/api.php"
|
||||
assert record["action"] == "wbgetentities"
|
||||
assert record["status_code"] == 200
|
||||
assert record["user_agent"] == headers["User-Agent"]
|
||||
|
||||
|
||||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue