Spaces:
Running
Running
Download tests/test_normalize.py from Melikshah/geoql-env: direct link, hf CLI and curl.
- Browser
- Download file 6.76 kB
-
https://huggingface.co/spaces/Melikshah/geoql-env/resolve/main/tests/test_normalize.py
- Command line
-
hf download hf://spaces/Melikshah/geoql-env/tests/test_normalize.py
-
curl -L -o test_normalize.py https://huggingface.co/spaces/Melikshah/geoql-env/resolve/main/tests/test_normalize.py
6.76 kB
| import shutil | |
| from pathlib import Path | |
| import pytest | |
| from geoql_env import gazetteer | |
| from geoql_env import normalize as N | |
| from geoql_env.paths import DATA, DBS | |
| FIXTURE = Path(__file__).parent / "fixtures" / "mini.osm" | |
| DB = "test-normalize" | |
| CTX = N.Context("w", bbox=(7.40, 43.72, 7.44, 43.75)) | |
| HEAD = N.SETTINGS + ";\n" | |
| def norm(query, ctx=CTX): | |
| return N.normalize(query, ctx) | |
| def test_extract(response, query, status): | |
| assert N.extract(response) == N.Extraction(query, status) | |
| def test_settings_are_forced(query, expected): | |
| assert norm(query) == expected | |
| def fake_geocode(world, place): | |
| if (world, place) == ("w", "Monaco"): | |
| return 3601124039 | |
| raise LookupError(f'no area named "{place}" in world {world}') | |
| def test_shortcuts(monkeypatch): | |
| monkeypatch.setattr(gazetteer, "geocode_area", fake_geocode) | |
| assert norm('{{geocodeArea:"Monaco"}}->.a; node(area.a)({{bbox}}); out;') == ( | |
| HEAD + "area(id:3601124039)->.a; node(area.a)(43.72,7.4,43.75,7.44); out;") | |
| assert norm("node(around:{{ center }},50); out;") == HEAD + "node(around:43.735,7.42,50); out;" | |
| with pytest.raises(N.Rejected, match="map view"): | |
| norm("node({{bbox}}); out;", N.Context("w")) | |
| with pytest.raises(N.Rejected, match="no area named"): | |
| norm("{{geocodeArea:Atlantis}}->.a; out;") | |
| for unsupported in ("{{date:1 day}}", "{{style: node{color:red} }}", "{{name=value}}"): | |
| with pytest.raises(N.Rejected, match="unsupported shortcut"): | |
| norm(unsupported + " node(1); out;") | |
| def test_history_and_metadata_are_rejected(query, word): | |
| with pytest.raises(N.Rejected, match=f"`{word}` is not supported"): | |
| norm(query) | |
| def test_lookalikes_are_not_rejected(query): | |
| assert norm(query) == HEAD + query | |
| def test_xml_queries_are_rejected(): | |
| with pytest.raises(N.Rejected, match="XML"): | |
| norm('<osm-script><query type="node"/><print/></osm-script>') | |
| def test_out_prints_ids_and_tags(statement, expected): | |
| assert norm("node(1); " + statement) == HEAD + "node(1); " + expected | |
| def test_only_out_statements_are_rewritten(): | |
| query = 'node[ out ]["note"="out ids;"]; /* out skel; */ out ids;' | |
| assert norm(query) == HEAD + 'node[ out ]["note"="out ids;"]; /* out skel; */ out tags;' | |
| def test_only_display_recursion_is_removed(query, expected): | |
| assert norm(query) == HEAD + expected | |
| CAFES = [("node", 21), ("node", 23), ("way", 103)] | |
| FORMS = [ | |
| 'area["name"="Testville"]->.a; nwr["amenity"="cafe"](area.a); out;', | |
| '[out:xml][timeout:5]; {{geocodeArea:"Testville"}}->.a; nwr[amenity=cafe](area.a); out ids;', | |
| '[out:csv(::id, name; true; ";")]; area(id:3600001001)->.a;\n' | |
| "(node[amenity=cafe](area.a); way[amenity=cafe](area.a););\nout body;\n>;\nout skel qt;", | |
| "area[name=Testville]->.a; nwr[amenity=cafe](area.a); (._;>;); out meta;", | |
| "area[name=Testville]->.a; (node[amenity=cafe](area.a); way[amenity=cafe](area.a); >;); out geom;", | |
| 'nwr["amenity"="cafe"]({{bbox}}); out center;', | |
| ] | |
| def test_equivalent_forms_print_the_same_tagged_elements(engine, tmp_path, monkeypatch): | |
| monkeypatch.setattr(gazetteer, "GAZETTEER", tmp_path) | |
| src = DATA / "test" / "normalize.osm" | |
| src.parent.mkdir(parents=True, exist_ok=True) | |
| shutil.copyfile(FIXTURE, src) | |
| try: | |
| engine.build_db(src, DB, force=True) | |
| gazetteer.build(DB, engine) | |
| ctx = N.Context(DB, bbox=(9.99, 9.99, 10.016, 10.016)) | |
| for form in FORMS: | |
| result = engine.query(DB, N.normalize(form, ctx)) | |
| assert result.ok, (form, result.message) | |
| assert sorted({(e["type"], e["id"]) for e in result.elements}) == CAFES, form | |
| assert all(e.get("tags") for e in result.elements), form | |
| finally: | |
| gazetteer.load.cache_clear() | |
| shutil.rmtree(DBS / DB, ignore_errors=True) | |