Format Python source and tests
This commit is contained in:
parent
45dc2c2e67
commit
fb9bb532ec
8 changed files with 31 additions and 22 deletions
|
|
@ -64,7 +64,6 @@ class Polygon(Base):
|
|||
).order_by(cls.area, cast(cls.admin_level, Integer).desc())
|
||||
return q # type: ignore
|
||||
|
||||
|
||||
@classmethod
|
||||
def nearest(
|
||||
cls,
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
"""Wikidata API functions."""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
import typing
|
||||
import urllib.parse
|
||||
from pathlib import Path
|
||||
|
||||
import backoff
|
||||
import backoff.types
|
||||
|
|
@ -255,7 +255,7 @@ def hit_from_row(row: Row) -> Hit:
|
|||
commons_cat = row["commonsCat"]["value"]
|
||||
elif "commonsSiteLink" in row:
|
||||
site_link = row["commonsSiteLink"]["value"]
|
||||
commons_cat = unescape_title(site_link[len(commons_cat_start):])
|
||||
commons_cat = unescape_title(site_link[len(commons_cat_start) :])
|
||||
return {"wikidata": qid, "commons_cat": commons_cat}
|
||||
|
||||
|
||||
|
|
|
|||
16
lookup.py
16
lookup.py
|
|
@ -346,7 +346,9 @@ def validate_coordinates(lat: float, lon: float) -> str | None:
|
|||
|
||||
|
||||
@app.route("/")
|
||||
def index() -> str | Response | tuple[Response, int] | tuple[Response, int, dict[str, str]]:
|
||||
def index() -> (
|
||||
str | Response | tuple[Response, int] | tuple[Response, int, dict[str, str]]
|
||||
):
|
||||
"""Index page."""
|
||||
t0 = time()
|
||||
database.session.execute(text("SELECT 1"))
|
||||
|
|
@ -364,12 +366,15 @@ def index() -> str | Response | tuple[Response, int] | tuple[Response, int, dict
|
|||
lat = parse_coordinate(lat_str)
|
||||
lon = parse_coordinate(lon_str)
|
||||
except ValueError:
|
||||
return jsonify(
|
||||
return (
|
||||
jsonify(
|
||||
coords={"lat": lat_str, "lon": lon_str},
|
||||
error="Invalid coordinate format. "
|
||||
+ "Please use decimal degrees (e.g., 56.099600) "
|
||||
+ "or DMS format (e.g., 56°5'58.56\"N)",
|
||||
), 400
|
||||
),
|
||||
400,
|
||||
)
|
||||
|
||||
if error_msg := validate_coordinates(lat, lon):
|
||||
return jsonify(coords={"lat": lat_str, "lon": lon_str}, error=error_msg), 400
|
||||
|
|
@ -401,7 +406,10 @@ def index() -> str | Response | tuple[Response, int] | tuple[Response, int, dict
|
|||
503,
|
||||
)
|
||||
except requests.exceptions.RequestException:
|
||||
return jsonify(error="Could not connect to Wikidata, please try again later"), 503
|
||||
return (
|
||||
jsonify(error="Could not connect to Wikidata, please try again later"),
|
||||
503,
|
||||
)
|
||||
result.pop("element", None)
|
||||
result.pop("geojson", None)
|
||||
if logging_enabled:
|
||||
|
|
|
|||
|
|
@ -63,9 +63,9 @@ def capture_sample(i: int, lat: float, lon: float, name: str) -> None:
|
|||
"wdqs": wdqs_calls,
|
||||
"api": api_calls,
|
||||
"expected_wikidata": result.get("wikidata"),
|
||||
"expected_commons_cat": commons_cat["title"]
|
||||
if isinstance(commons_cat, dict)
|
||||
else None,
|
||||
"expected_commons_cat": (
|
||||
commons_cat["title"] if isinstance(commons_cat, dict) else None
|
||||
),
|
||||
}
|
||||
|
||||
fixture_path = FIXTURES_DIR / f"sample_{i:02d}.json"
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import pytest
|
|||
def flask_app():
|
||||
"""Return the Flask application."""
|
||||
from lookup import app
|
||||
|
||||
return app
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ def test_invalid_coordinates_return_400(monkeypatch, parameter, value):
|
|||
"lat, lon, expected",
|
||||
[
|
||||
("55.7644", "-4.177", (55.7644, -4.177)),
|
||||
('56°6\'0"N', '3°30\'0"W', (56.1, -3.5)),
|
||||
("56°6'0\"N", "3°30'0\"W", (56.1, -3.5)),
|
||||
("-90", "180", (-90.0, 180.0)),
|
||||
],
|
||||
)
|
||||
|
|
|
|||
|
|
@ -83,14 +83,14 @@ def test_example(app_ctx, mocker, i: int, lat: float, lon: float, name: str) ->
|
|||
expected_cat = fixture["expected_commons_cat"]
|
||||
if expected_cat:
|
||||
commons = result.get("commons_cat")
|
||||
assert isinstance(commons, dict), (
|
||||
f"{name}: expected commons_cat={expected_cat!r} but result has none"
|
||||
)
|
||||
assert isinstance(
|
||||
commons, dict
|
||||
), f"{name}: expected commons_cat={expected_cat!r} but result has none"
|
||||
assert commons["title"] == expected_cat, (
|
||||
f"{name}: commons_cat mismatch — expected {expected_cat!r}, "
|
||||
f"got {commons['title']!r}"
|
||||
)
|
||||
else:
|
||||
assert not result.get("commons_cat"), (
|
||||
f"{name}: expected no commons_cat but got {result.get('commons_cat')!r}"
|
||||
)
|
||||
assert not result.get(
|
||||
"commons_cat"
|
||||
), f"{name}: expected no commons_cat but got {result.get('commons_cat')!r}"
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@ 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,
|
||||
|
|
@ -14,6 +14,7 @@ from geocode.wikidata import (
|
|||
mediawiki_error_message,
|
||||
wdqs,
|
||||
)
|
||||
from geocode.wikimedia_api_logging import WikimediaApiLogConfig
|
||||
|
||||
max_tries = 5
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue