import json import pytest from muscari_widget.geometry import GeometryValidationError, parse_polygon_records def test_parse_polygon_records_preserves_feature_labels_without_union(): geojson = { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": {"polygon_id": "polygon_1", "color": "#2563eb"}, "geometry": { "type": "Polygon", "coordinates": [[[7.0, 46.0], [7.2, 46.0], [7.2, 46.2], [7.0, 46.2], [7.0, 46.0]]], }, }, { "type": "Feature", "properties": {"polygon_id": "polygon_2", "color": "#dc2626"}, "geometry": { "type": "Polygon", "coordinates": [[[7.3, 46.0], [7.5, 46.0], [7.5, 46.2], [7.3, 46.2], [7.3, 46.0]]], }, }, ], } records = parse_polygon_records(json.dumps(geojson)) assert [record.record_id for record in records] == ["polygon_1", "polygon_2"] assert [record.map_color for record in records] == ["#2563eb", "#dc2626"] assert all(record.area_m2 > 0 for record in records) def test_parse_polygon_records_rejects_points(): point_geojson = {"type": "Point", "coordinates": [7.0, 46.0]} with pytest.raises(GeometryValidationError, match="Unsupported"): parse_polygon_records(json.dumps(point_geojson)) def test_parse_polygon_records_warns_for_tiny_area(): tiny_geojson = { "type": "Polygon", "coordinates": [[[7.0, 46.0], [7.001, 46.0], [7.001, 46.001], [7.0, 46.001], [7.0, 46.0]]], } records = parse_polygon_records(json.dumps(tiny_geojson)) assert records[0].warnings assert "below" in records[0].warnings[0]