python/tests/test_live.py
"""Opt-in: the client against a running korp-map server.
KORP_MAP_URL=https://karta.gunnar.rs pytest -m live
Skipped when KORP_MAP_URL is unset. Assumes a served region covering Stockholm
(the Sweden cut).
"""
from __future__ import annotations
import importlib.util
import os
import pyarrow as pa
import pyarrow.compute as pc
import pytest
import korp_map_client
URL = os.environ.get("KORP_MAP_URL")
pytestmark = [pytest.mark.live, pytest.mark.skipif(not URL, reason="set KORP_MAP_URL to run the live tests")]
HAS_GEO = importlib.util.find_spec("geopandas") is not None
STHLM = (18.05, 59.32, 18.08, 59.34) # Gamla stan / Norrmalm
@pytest.fixture(scope="module")
def c():
with korp_map_client.Client(URL) as client:
yield client
def test_health_regions(c):
assert c.health()
regs = c.regions()
assert regs and {"name", "bbox", "data_version"} <= set(regs[0])
assert c.region == regs[0]["name"]
@pytest.mark.parametrize("kind,depth", [("points", 0), ("lines", 2), ("areas", 3)])
def test_map_stockholm(c, kind, depth):
res = c.map(kind, bbox=STHLM, lod=14, max=20000)
t = res.table
assert t.num_rows > 0
assert {"id", "tags", "min_zoom", "geometry"} <= set(t.column_names)
assert res.status == "ok"
assert res.data_version == c.regions()[0]["data_version"]
assert res.geometry_source == "full"
assert res.row_groups_scanned is not None and res.row_groups_total is not None
typ = t.schema.field("geometry").type
for _ in range(depth):
assert pa.types.is_list(typ) or pa.types.is_large_list(typ)
typ = typ.value_type
assert pa.types.is_struct(typ) and [f.name for f in typ] == ["x", "y"]
assert pc.max(t.column("min_zoom")).as_py() <= 14
def test_truncated(c):
res = c.map("lines", bbox=STHLM, lod=14, max=10)
assert res.truncated is True and res.num_rows <= 10
def test_low_lod_uses_simplified_geometry(c):
res = c.map("areas", bbox=STHLM, lod=3)
assert res.geometry_source and res.geometry_source.startswith("z6")
def test_compress_zstd_ipc(c):
a = c.map("lines", bbox=STHLM, lod=14, max=500)
b = c.map("lines", bbox=STHLM, lod=14, max=500, compress="zstd")
assert a.table.equals(b.table)
def test_error_code(c):
with pytest.raises(korp_map_client.KorpMapError) as ei:
c.map("lines", bbox=STHLM, lod=14, region="atlantis")
assert ei.value.code == "korp-map-108" and ei.value.status == 404
def test_geocode(c):
hits = c.geocode("Drottninggatan 53 Stockholm", max=5)
assert 1 <= len(hits) <= 5
top = hits[0]
assert top["street"] == "Drottninggatan" and top["housenumber"] == "53"
assert 18.0 < top["lon"] < 18.2 and 59.3 < top["lat"] < 59.4
assert c.geocode("zzqqxxyy-no-such-street 999999") == []
def test_revgeocode(c):
hit = c.revgeocode(18.07, 59.33)
assert hit is not None and hit["street"] and hit["distance_m"] < 500
with pytest.raises(korp_map_client.KorpMapError) as ei:
c.revgeocode(500.0, 59.33)
assert ei.value.code == "korp-map-115"
@pytest.mark.skipif(not HAS_GEO, reason="needs the [geo] extra")
@pytest.mark.parametrize("kind,gtype", [("points", "Point"), ("lines", "MultiLineString"), ("areas", "MultiPolygon")])
def test_map_gdf(c, kind, gtype):
gdf = c.map_gdf(kind, bbox=STHLM, lod=14, max=5000)
assert len(gdf) > 0 and gdf.crs.to_epsg() == 4326
assert set(gdf.geom_type.dropna()) == {gtype}
assert gdf.is_valid.mean() > 0.9 if kind == "areas" else True
import shapely
# rows are the ones that touch the bbox (big areas and long lines reach far out)
assert gdf.intersects(shapely.box(*STHLM).buffer(1e-3)).mean() > 0.95