Spaces:
Running
Running
Download tests/test_engine.py from Melikshah/geoql-env: direct link, hf CLI and curl.
- Browser
- Download file 4.86 kB
-
https://huggingface.co/spaces/Melikshah/geoql-env/resolve/main/tests/test_engine.py
- Command line
-
hf download hf://spaces/Melikshah/geoql-env/tests/test_engine.py
-
curl -L -o test_engine.py https://huggingface.co/spaces/Melikshah/geoql-env/resolve/main/tests/test_engine.py
4.86 kB
| import shutil | |
| import subprocess | |
| from pathlib import Path | |
| import pytest | |
| from geoql_env.engine import Engine, EngineError, classify | |
| from geoql_env.paths import DATA | |
| FIXTURE = Path(__file__).parent / "fixtures" / "mini.osm" | |
| DB = "test-mini" | |
| JSON_OK = '{"version": 0.6, "elements": [{"type": "node", "id": 1}]}' | |
| JSON_EMPTY = '{"version": 0.6, "elements": []}' | |
| BENIGN = "encoding remark: Please enter your query.\nruntime remark: Timeout is 180 and maxsize is 536870912.\n" | |
| def test_classify(rc, stdout, stderr, status): | |
| assert classify(rc, stdout, stderr).status == status | |
| def test_classify_keeps_elements_only_when_ok(): | |
| assert classify(0, JSON_OK, "").elements == [{"type": "node", "id": 1}] | |
| assert classify(0, JSON_EMPTY, "runtime error: Query run out of memory").elements == [] | |
| def test_modes_map_paths(monkeypatch): | |
| monkeypatch.setenv("GEOQL_ENGINE", "docker") | |
| assert Engine().path(DATA / "dbs" / "x") == "/data/dbs/x" | |
| with pytest.raises(EngineError, match="is not inside"): | |
| Engine().path(Path("/elsewhere/x")) | |
| monkeypatch.setenv("GEOQL_ENGINE", "local") | |
| assert Engine().path(DATA / "dbs" / "x") == f"{DATA.resolve()}/dbs/x" | |
| monkeypatch.setenv("GEOQL_ENGINE", "podman") | |
| with pytest.raises(ValueError, match="GEOQL_ENGINE"): | |
| Engine() | |
| def test_local_mode_runs_commands_directly(monkeypatch): | |
| calls = [] | |
| def run(cmd, **kwargs): | |
| calls.append(cmd) | |
| return subprocess.CompletedProcess(cmd, 0, "", "") | |
| monkeypatch.setattr(subprocess, "run", run) | |
| for mode in ("local", "docker"): | |
| monkeypatch.setenv("GEOQL_ENGINE", mode) | |
| Engine().run(["osmium", "--version"]) | |
| assert calls == [["osmium", "--version"], ["docker", "exec", "geoql-engine", "osmium", "--version"]] | |
| def mini(engine): | |
| src = DATA / "test" / "mini.osm" | |
| src.parent.mkdir(parents=True, exist_ok=True) | |
| shutil.copyfile(FIXTURE, src) | |
| engine.build_db(src, DB, force=True) | |
| return engine | |
| def ids(engine, query): | |
| r = engine.query(DB, "[out:json];" + query) | |
| assert r.ok, r.message | |
| return sorted((e["type"], e["id"]) for e in r.elements) | |
| def test_boundary_relation_area_includes_its_border(mini): | |
| assert ids(mini, 'area["name"="Testville"]; out ids;') == [("area", 3600001001)] | |
| assert ids(mini, 'area["name"="Testville"]->.a; nwr["amenity"="cafe"](area.a); out ids;') == [ | |
| ("node", 21), ("node", 23), ("way", 103)] | |
| def test_closed_way_is_an_area_and_border_nodes_count_inside(mini): | |
| assert ids(mini, 'area["name"="Test Park"]; out ids;') == [("way", 101)] | |
| assert ids(mini, 'area["name"="Test Park"]->.p; node["amenity"](area.p); out ids;') == [ | |
| ("node", 21), ("node", 23), ("node", 24), ("node", 25)] | |
| def test_around_uses_the_overpass_distance(mini): | |
| assert ids(mini, 'node(21)->.c; node["amenity"="restaurant"](around.c:54); out ids;') == [] | |
| assert ids(mini, 'node(21)->.c; node["amenity"="restaurant"](around.c:55); out ids;') == [("node", 24)] | |
| def test_absent_key_differs_from_not_equal(mini): | |
| assert ids(mini, 'node["amenity"="restaurant"][!"wheelchair"]; out ids;') == [("node", 24)] | |
| assert ids(mini, 'node["amenity"="restaurant"]["wheelchair"!="yes"]; out ids;') == [("node", 24), ("node", 25)] | |
| def test_nwr_equals_the_type_union(mini): | |
| union = '(node["amenity"="cafe"]; way["amenity"="cafe"]; rel["amenity"="cafe"];); out ids;' | |
| assert ids(mini, 'nwr["amenity"="cafe"]; out ids;') == ids(mini, union) | |
| def test_errors_are_classified(mini): | |
| assert mini.query(DB, '[out:json]; node["amenity"] out;').status == "query_error" | |
| assert mini.query(DB, '[out:json][maxsize:1]; nwr["name"]; out geom;').status == "runtime_error" | |
| with pytest.raises(EngineError): | |
| mini.query("not-built", "[out:json]; node(1); out;") | |