From 80b85e648f0d40325964517ecc9db7cd999009de Mon Sep 17 00:00:00 2001 From: Hemna Date: Thu, 22 Jun 2023 08:58:32 -0400 Subject: [PATCH] Fixed unit tests for Location plugin --- aprsd/plugins/location.py | 6 ++++-- tests/plugins/test_location.py | 38 +++++++++++++++++++++------------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/aprsd/plugins/location.py b/aprsd/plugins/location.py index c0ffdc0..7663746 100644 --- a/aprsd/plugins/location.py +++ b/aprsd/plugins/location.py @@ -68,10 +68,12 @@ class LocationPlugin(plugin.APRSDRegexCommandPluginBase, plugin.APRSFIKEYMixin): area_info = f"{address.get('county')}, {address.get('state')}" else: # what to do for address for non US? - area_info = f"{address.get('country')}" + area_info = f"{address.get('country'), 'Unknown'}" except Exception as ex: LOG.error(f"Failed to fetch Geopy address {ex}") - area_info = "" + print("FUICK") + print(ex) + area_info = "Unknown Location" try: # altitude not always provided alt = float(aprs_data["entries"][0]["altitude"]) diff --git a/tests/plugins/test_location.py b/tests/plugins/test_location.py index b71e54b..6c60998 100644 --- a/tests/plugins/test_location.py +++ b/tests/plugins/test_location.py @@ -49,51 +49,61 @@ class TestLocationPlugin(test_plugin.TestPlugin): self.assertEqual(expected, actual) @mock.patch("aprsd.plugin_utils.get_aprs_fi") - @mock.patch("aprsd.plugin_utils.get_weather_gov_for_gps") + @mock.patch("geopy.geocoders.Nominatim.reverse") @mock.patch("time.time") - def test_location_unknown_gps(self, mock_time, mock_weather, mock_check_aprs): + def test_location_unknown_gps(self, mock_time, mock_geocode, mock_check_aprs): # When the aprs.fi api key isn't set, then # the LocationPlugin will be disabled. mock_check_aprs.return_value = { "entries": [ { - "lat": 10, - "lng": 11, + "lat": 1, + "lng": 1, "lasttime": 10, }, ], } - mock_weather.side_effect = Exception + mock_geocode.side_effect = Exception mock_time.return_value = 10 CONF.callsign = fake.FAKE_TO_CALLSIGN fortune = location_plugin.LocationPlugin() - expected = "KFAKE: Unknown Location 0' 10,11 0.0h ago" + expected = "KFAKE: Unknown Location 0' 1.00,1.00 0.0h ago" packet = fake.fake_packet(message="location") actual = fortune.filter(packet) self.assertEqual(expected, actual) @mock.patch("aprsd.plugin_utils.get_aprs_fi") - @mock.patch("aprsd.plugin_utils.get_weather_gov_for_gps") + @mock.patch("geopy.geocoders.Nominatim.reverse") @mock.patch("time.time") - def test_location_works(self, mock_time, mock_weather, mock_check_aprs): + def test_location_works(self, mock_time, mock_geocode, mock_check_aprs): # When the aprs.fi api key isn't set, then # the LocationPlugin will be disabled. mock_check_aprs.return_value = { "entries": [ { - "lat": 10, - "lng": 11, + "lat": 1, + "lng": 1, "lasttime": 10, }, ], } - expected_town = "Appomattox, VA" - wx_data = {"location": {"areaDescription": expected_town}} - mock_weather.return_value = wx_data + expected = "Appomattox" + state = "VA" + + class TempLocation: + raw = { + "address": { + "county": expected, + "country_code": "us", + "state": state, + "country": "United States", + }, + } + mock_geocode.return_value = TempLocation() mock_time.return_value = 10 CONF.callsign = fake.FAKE_TO_CALLSIGN fortune = location_plugin.LocationPlugin() - expected = f"KFAKE: {expected_town} 0' 10,11 0.0h ago" + expected = f"KFAKE: {expected}, {state} 0' 1.00,1.00 0.0h ago" packet = fake.fake_packet(message="location") actual = fortune.filter(packet) self.assertEqual(expected, actual)