import tempfile
import unittest
import zipfile
from pathlib import Path
from src.kml_parser import parse_kml_file
KML_TEXT = """
Site boundary
70.2450,21.0020,0 70.2460,21.0020,0 70.2460,21.0030,0 70.2450,21.0030,0 70.2450,21.0020,0
"""
class KmlParserTests(unittest.TestCase):
def test_kml_polygon_creates_wgs84_selection(self):
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "site.kml"
path.write_text(KML_TEXT, encoding="utf-8")
selection = parse_kml_file(path)
self.assertEqual(selection.selection_type, "kml_boundary")
self.assertEqual(selection.coordinate_mode, "wgs84")
self.assertGreater(selection.area_sqm or 0, 0)
self.assertAlmostEqual(selection.anchor_lat or 0, 21.0024, places=3)
self.assertAlmostEqual(selection.anchor_lon or 0, 70.2454, places=3)
self.assertIn("legal/cadastral", " ".join(selection.limitations))
def test_kmz_polygon_creates_selection(self):
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "site.kmz"
with zipfile.ZipFile(path, "w") as archive:
archive.writestr("doc.kml", KML_TEXT)
selection = parse_kml_file(path)
self.assertEqual(selection.selection_type, "kml_boundary")
self.assertEqual(selection.source_files, ["site.kmz"])
def test_kml_without_polygon_is_rejected(self):
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "empty.kml"
path.write_text("", encoding="utf-8")
with self.assertRaises(ValueError):
parse_kml_file(path)
if __name__ == "__main__":
unittest.main()