From ae31fc86c26e96fd9185cf04c3ab1678cc7ba3c4 Mon Sep 17 00:00:00 2001 From: Pauliina Ilmanen Date: Thu, 15 Feb 2024 09:12:15 +0200 Subject: [PATCH] Skip parking payzones with no geometry --- .../update_vantaa_parking_payzones.py | 15 ++++++++----- ...vantaa_parking_payzones_null_geometry.json | 17 +++++++++++++++ .../test_update_vantaa_parking_payzones.py | 21 +++++++++++++++++-- 3 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 services/tests/data/vantaa_parking_payzones_null_geometry.json diff --git a/services/management/commands/update_vantaa_parking_payzones.py b/services/management/commands/update_vantaa_parking_payzones.py index bdb8b00a1..643bb48ad 100644 --- a/services/management/commands/update_vantaa_parking_payzones.py +++ b/services/management/commands/update_vantaa_parking_payzones.py @@ -58,16 +58,21 @@ def update_parking_payzones(self): updated_parking_payzones = [] for feature in features: - geom = MultiPolygon( - GEOSGeometry(str(feature.get("geometry")), srid=SRC_SRID) - ) - geom.transform(src_to_dest) props = feature.get("properties") + name_fi = props.get("maksullisu") + geometry = feature.get("geometry") + + if not geometry: + logger.warning(f"Parking payzone {name_fi} has no geometry, skipping") + continue + + geom = MultiPolygon(GEOSGeometry(str(geometry), srid=SRC_SRID)) + geom.transform(src_to_dest) + origin_id = str(props.get("objectid")) if props.get("objectid") else None if not origin_id: logger.warning("Parking payzone has no origin ID, skipping") continue - name_fi = props.get("maksullisu") defaults = { "name_fi": name_fi, diff --git a/services/tests/data/vantaa_parking_payzones_null_geometry.json b/services/tests/data/vantaa_parking_payzones_null_geometry.json new file mode 100644 index 000000000..0c3b15e91 --- /dev/null +++ b/services/tests/data/vantaa_parking_payzones_null_geometry.json @@ -0,0 +1,17 @@ +{ + "features": [ + { + "geometry": null, + "id": 3, + "properties": { + "maksullisu": "3 € / tunti", + "objectid": 3 + }, + "type": "Feature" + } + ], + "properties": { + "exceededTransferLimit": false + }, + "type": "FeatureCollection" +} diff --git a/services/tests/test_update_vantaa_parking_payzones.py b/services/tests/test_update_vantaa_parking_payzones.py index dce6363fe..0db78f0d8 100644 --- a/services/tests/test_update_vantaa_parking_payzones.py +++ b/services/tests/test_update_vantaa_parking_payzones.py @@ -10,8 +10,11 @@ ) -def get_mock_data(): - file_path = "services/tests/data/vantaa_parking_payzones.json" +def get_mock_data(geometry=True): + if geometry: + file_path = "services/tests/data/vantaa_parking_payzones.json" + else: + file_path = "services/tests/data/vantaa_parking_payzones_null_geometry.json" with open(file_path, "r") as json_file: contents = json.load(json_file) return contents.get("features") @@ -89,3 +92,17 @@ def test_delete_removed_parking_payzones(get_features_mock): == 2 ) assert not AdministrativeDivision.objects.filter(origin_id="3").exists() + + +@pytest.mark.django_db +@patch( + "services.management.commands.update_vantaa_parking_payzones.Command.get_features" +) +def test_skip_parking_payzone_with_no_geometry(get_features_mock): + get_features_mock.return_value = get_mock_data(geometry=False) + Municipality.objects.create(id="vantaa", name="Vantaa") + AdministrativeDivisionType.objects.create(type="parking_payzone") + + assert AdministrativeDivision.objects.count() == 0 + call_command("update_vantaa_parking_payzones") + assert AdministrativeDivision.objects.count() == 0