site-intelligence-studio / tests /test_geojson_parser.py
Eishaan's picture
Harden uploads and map fallback
efcf0a3
Raw
History Blame Contribute Delete
1.86 kB
import json
import tempfile
import unittest
from pathlib import Path
from src.geojson_parser import parse_geojson_file
class GeojsonParserTests(unittest.TestCase):
def test_accepts_wgs84_polygon(self):
data = {
"type": "Polygon",
"coordinates": [
[
[70.0, 21.0],
[70.001, 21.0],
[70.001, 21.001],
[70.0, 21.001],
[70.0, 21.0],
]
],
}
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "site.geojson"
path.write_text(json.dumps(data), encoding="utf-8")
selection = parse_geojson_file(path)
self.assertEqual(selection.selection_type, "geojson_boundary")
self.assertGreater(selection.area_sqm, 0)
def test_rejects_non_wgs84_coordinates(self):
data = {
"type": "Polygon",
"coordinates": [
[
[700000, 2100000],
[700010, 2100000],
[700010, 2100010],
[700000, 2100010],
[700000, 2100000],
]
],
}
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "site.geojson"
path.write_text(json.dumps(data), encoding="utf-8")
with self.assertRaisesRegex(ValueError, "WGS84"):
parse_geojson_file(path)
def test_rejects_malformed_root(self):
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "site.geojson"
path.write_text("[]", encoding="utf-8")
with self.assertRaisesRegex(ValueError, "GeoJSON root"):
parse_geojson_file(path)
if __name__ == "__main__":
unittest.main()