|
| 1 | +import json |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +from src.tools.io_tools import ( |
| 5 | + load_overpass_elements, |
| 6 | + save_enriched_elements, |
| 7 | + to_geojson, |
| 8 | +) |
| 9 | + |
| 10 | +ELEMENTS = [ |
| 11 | + {"id": 1, "lat": 10.0, "lon": 20.0, "tags": {"foo": "bar"}, "analysis": {"a": 1}}, |
| 12 | + {"id": 2}, # should be skipped by to_geojson (no lat/lon) |
| 13 | + {"id": 3, "lat": 0.0, "lon": 0.0, "tags": {}, "analysis": {}}, |
| 14 | +] |
| 15 | + |
| 16 | + |
| 17 | +def test_load_overpass_elements(tmp_path): |
| 18 | + # prepare a dump with a few elements |
| 19 | + dump = {"elements": [{"id": "x"}, {"id": "y"}]} |
| 20 | + dump_path = tmp_path / "dump.json" |
| 21 | + dump_path.write_text(json.dumps(dump), encoding="utf-8") |
| 22 | + |
| 23 | + loaded = load_overpass_elements(dump_path) |
| 24 | + assert isinstance(loaded, list) |
| 25 | + assert loaded == dump["elements"] |
| 26 | + |
| 27 | + |
| 28 | +def test_save_enriched_elements(tmp_path): |
| 29 | + # create a fake source file so save_enriched_elements can build its name |
| 30 | + source = tmp_path / "city.json" |
| 31 | + source.write_text("{}", encoding="utf-8") |
| 32 | + |
| 33 | + enriched = [{"id": 42}, {"id": 43}] |
| 34 | + out = save_enriched_elements(enriched, source) |
| 35 | + out_path = Path(out) |
| 36 | + |
| 37 | + # file should exist and contain exactly {"elements": enriched} |
| 38 | + assert out_path.exists() |
| 39 | + text = out_path.read_text(encoding="utf-8") |
| 40 | + parsed = json.loads(text) |
| 41 | + assert parsed == {"elements": enriched} |
| 42 | + |
| 43 | + |
| 44 | +def test_to_geojson_without_writing(tmp_path): |
| 45 | + # write an enriched JSON file with ELEMENTS |
| 46 | + enriched = {"elements": ELEMENTS} |
| 47 | + enriched_path = tmp_path / "in.json" |
| 48 | + enriched_path.write_text(json.dumps(enriched), encoding="utf-8") |
| 49 | + |
| 50 | + geo = to_geojson(enriched_path) |
| 51 | + # should be a FeatureCollection |
| 52 | + assert geo["type"] == "FeatureCollection" |
| 53 | + # only two elements have both lat and lon |
| 54 | + assert len(geo["features"]) == 2 |
| 55 | + |
| 56 | + # check geometry & properties merging |
| 57 | + feat = geo["features"][0] |
| 58 | + assert feat["geometry"] == {"type": "Point", "coordinates": [20.0, 10.0]} |
| 59 | + # props should include both tags and analysis |
| 60 | + assert feat["properties"]["foo"] == "bar" |
| 61 | + assert feat["properties"]["a"] == 1 |
| 62 | + |
| 63 | + |
| 64 | +def test_to_geojson_with_writing(tmp_path): |
| 65 | + enriched = {"elements": ELEMENTS} |
| 66 | + enriched_path = tmp_path / "data.json" |
| 67 | + enriched_path.write_text(json.dumps(enriched), encoding="utf-8") |
| 68 | + out_geojson = tmp_path / "out.geojson" |
| 69 | + |
| 70 | + geo = to_geojson(enriched_path, out_geojson) |
| 71 | + # file was written |
| 72 | + assert out_geojson.exists() |
| 73 | + # contents match returned dict |
| 74 | + written = json.loads(out_geojson.read_text(encoding="utf-8")) |
| 75 | + assert written == geo |
| 76 | + assert written["type"] == "FeatureCollection" |
0 commit comments