From ff3ef42433bdfd2fa1cd346e618ff237c3ae6b31 Mon Sep 17 00:00:00 2001 From: Dan Jacob Date: Thu, 14 Dec 2023 15:32:12 +0200 Subject: [PATCH] Check if Swedish address is not null before parsing. Refs: PV-742 --- parking_permits/services/dvv.py | 10 +++++++--- parking_permits/tests/services/test_dvv.py | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/parking_permits/services/dvv.py b/parking_permits/services/dvv.py index 4982d23b..37f72f45 100644 --- a/parking_permits/services/dvv.py +++ b/parking_permits/services/dvv.py @@ -97,10 +97,14 @@ def format_address(address_data) -> DvvAddressInfo: street_name, street_number, apartment = parse_street_data( address_data["LahiosoiteS"] ) - street_name_sv, street_number, apartment_sv = parse_street_data( - address_data["LahiosoiteR"] - ) + + if swedish_address := address_data.get("LahiosoiteR"): + street_name_sv, __, apartment_sv = parse_street_data(swedish_address) + else: + street_name_sv, apartment_sv = "", "" + address_detail = get_address_details(street_name, street_number) + try: zone = ParkingZone.objects.get_for_location(address_detail["location"]) except ParkingZone.DoesNotExist: diff --git a/parking_permits/tests/services/test_dvv.py b/parking_permits/tests/services/test_dvv.py index 8e383261..9342df84 100644 --- a/parking_permits/tests/services/test_dvv.py +++ b/parking_permits/tests/services/test_dvv.py @@ -82,6 +82,26 @@ def test_get_customer_info_addresses_not_in_helsinki( self.assertEqual(customer["other_address"], None) self.assertEqual(customer["other_address_apartment"], "") + @patch("parking_permits.services.dvv.get_address_details") + @patch("requests.post") + def test_mock_customer_info_null_swedish_address( + self, mock_post, mock_get_address_details + ): + mock_info = self.get_mock_info() + mock_info["Henkilo"]["VakinainenKotimainenLahiosoite"]["LahiosoiteR"] = None + + mock_post.return_value = self.MockResponse(data=mock_info) + mock_get_address_details.return_value = {"location": generate_multi_polygon()} + customer = get_person_info("12345") + self.assertEqual(customer["first_name"], "Heikki") + self.assertEqual(customer["last_name"], "Häkkinen") + self.assertEqual(customer["primary_address"]["street_name"], "Käsivoide") + self.assertEqual(customer["primary_address"]["street_name_sv"], "") + self.assertEqual(customer["primary_address"]["street_number"], "1") + self.assertEqual(customer["primary_address"]["postal_code"], "10001") + self.assertEqual(customer["primary_address"]["city"], "Helsinki") + self.assertEqual(customer["primary_address_apartment"], "A6") + def get_mock_info(self, **kwargs): return { "Henkilo": {