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())
|
).order_by(cls.area, cast(cls.admin_level, Integer).desc())
|
||||||
return q # type: ignore
|
return q # type: ignore
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def nearest(
|
def nearest(
|
||||||
cls,
|
cls,
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
"""Wikidata API functions."""
|
"""Wikidata API functions."""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
|
||||||
import typing
|
import typing
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
import backoff
|
import backoff
|
||||||
import backoff.types
|
import backoff.types
|
||||||
|
|
@ -255,7 +255,7 @@ def hit_from_row(row: Row) -> Hit:
|
||||||
commons_cat = row["commonsCat"]["value"]
|
commons_cat = row["commonsCat"]["value"]
|
||||||
elif "commonsSiteLink" in row:
|
elif "commonsSiteLink" in row:
|
||||||
site_link = row["commonsSiteLink"]["value"]
|
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}
|
return {"wikidata": qid, "commons_cat": commons_cat}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
24
lookup.py
24
lookup.py
|
|
@ -346,7 +346,9 @@ def validate_coordinates(lat: float, lon: float) -> str | None:
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@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."""
|
"""Index page."""
|
||||||
t0 = time()
|
t0 = time()
|
||||||
database.session.execute(text("SELECT 1"))
|
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)
|
lat = parse_coordinate(lat_str)
|
||||||
lon = parse_coordinate(lon_str)
|
lon = parse_coordinate(lon_str)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return jsonify(
|
return (
|
||||||
coords={"lat": lat_str, "lon": lon_str},
|
jsonify(
|
||||||
error="Invalid coordinate format. "
|
coords={"lat": lat_str, "lon": lon_str},
|
||||||
+ "Please use decimal degrees (e.g., 56.099600) "
|
error="Invalid coordinate format. "
|
||||||
+ "or DMS format (e.g., 56°5'58.56\"N)",
|
+ "Please use decimal degrees (e.g., 56.099600) "
|
||||||
), 400
|
+ "or DMS format (e.g., 56°5'58.56\"N)",
|
||||||
|
),
|
||||||
|
400,
|
||||||
|
)
|
||||||
|
|
||||||
if error_msg := validate_coordinates(lat, lon):
|
if error_msg := validate_coordinates(lat, lon):
|
||||||
return jsonify(coords={"lat": lat_str, "lon": lon_str}, error=error_msg), 400
|
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,
|
503,
|
||||||
)
|
)
|
||||||
except requests.exceptions.RequestException:
|
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("element", None)
|
||||||
result.pop("geojson", None)
|
result.pop("geojson", None)
|
||||||
if logging_enabled:
|
if logging_enabled:
|
||||||
|
|
|
||||||
|
|
@ -63,9 +63,9 @@ def capture_sample(i: int, lat: float, lon: float, name: str) -> None:
|
||||||
"wdqs": wdqs_calls,
|
"wdqs": wdqs_calls,
|
||||||
"api": api_calls,
|
"api": api_calls,
|
||||||
"expected_wikidata": result.get("wikidata"),
|
"expected_wikidata": result.get("wikidata"),
|
||||||
"expected_commons_cat": commons_cat["title"]
|
"expected_commons_cat": (
|
||||||
if isinstance(commons_cat, dict)
|
commons_cat["title"] if isinstance(commons_cat, dict) else None
|
||||||
else None,
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
fixture_path = FIXTURES_DIR / f"sample_{i:02d}.json"
|
fixture_path = FIXTURES_DIR / f"sample_{i:02d}.json"
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import pytest
|
||||||
def flask_app():
|
def flask_app():
|
||||||
"""Return the Flask application."""
|
"""Return the Flask application."""
|
||||||
from lookup import app
|
from lookup import app
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ def test_invalid_coordinates_return_400(monkeypatch, parameter, value):
|
||||||
"lat, lon, expected",
|
"lat, lon, expected",
|
||||||
[
|
[
|
||||||
("55.7644", "-4.177", (55.7644, -4.177)),
|
("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)),
|
("-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"]
|
expected_cat = fixture["expected_commons_cat"]
|
||||||
if expected_cat:
|
if expected_cat:
|
||||||
commons = result.get("commons_cat")
|
commons = result.get("commons_cat")
|
||||||
assert isinstance(commons, dict), (
|
assert isinstance(
|
||||||
f"{name}: expected commons_cat={expected_cat!r} but result has none"
|
commons, dict
|
||||||
)
|
), f"{name}: expected commons_cat={expected_cat!r} but result has none"
|
||||||
assert commons["title"] == expected_cat, (
|
assert commons["title"] == expected_cat, (
|
||||||
f"{name}: commons_cat mismatch — expected {expected_cat!r}, "
|
f"{name}: commons_cat mismatch — expected {expected_cat!r}, "
|
||||||
f"got {commons['title']!r}"
|
f"got {commons['title']!r}"
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
assert not result.get("commons_cat"), (
|
assert not result.get(
|
||||||
f"{name}: expected no commons_cat but got {result.get('commons_cat')!r}"
|
"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 pytest_mock
|
||||||
import requests
|
import requests
|
||||||
import responses
|
import responses
|
||||||
|
|
||||||
from geocode import headers
|
from geocode import headers
|
||||||
from geocode.wikimedia_api_logging import WikimediaApiLogConfig
|
|
||||||
from geocode.wikidata import (
|
from geocode.wikidata import (
|
||||||
APIResponseError,
|
APIResponseError,
|
||||||
QueryError,
|
QueryError,
|
||||||
|
|
@ -14,6 +14,7 @@ from geocode.wikidata import (
|
||||||
mediawiki_error_message,
|
mediawiki_error_message,
|
||||||
wdqs,
|
wdqs,
|
||||||
)
|
)
|
||||||
|
from geocode.wikimedia_api_logging import WikimediaApiLogConfig
|
||||||
|
|
||||||
max_tries = 5
|
max_tries = 5
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue