From 7660b7a3ece198c63dff4c0094294eb88f9e31d9 Mon Sep 17 00:00:00 2001 From: James Green Date: Mon, 4 Dec 2023 09:23:25 +0000 Subject: [PATCH] added more unit tests --- src/geocode.py | 2 +- test/test_geocode.py | 68 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/src/geocode.py b/src/geocode.py index 7049dd1..de0bf82 100644 --- a/src/geocode.py +++ b/src/geocode.py @@ -36,7 +36,7 @@ def find_timezone(gps) -> tuple: try: timezone_name = tf_init.timezone_at(lat=latitude, lng=longitude) except ValueError: - raise ValueError('The coordinates were out of bounds {latitude}:{longitude}'.format( + raise ValueError('The coordinates were out of bounds {lat}:{lng}'.format( lat=latitude, lng=longitude)) if timezone_name is None: raise ValueError('GPS coordinates did not match a time_zone') diff --git a/test/test_geocode.py b/test/test_geocode.py index 4fb8ffe..a903f49 100644 --- a/test/test_geocode.py +++ b/test/test_geocode.py @@ -1,6 +1,6 @@ import unittest -from unittest.mock import patch, Mock -from src.geocode import geocode +from unittest.mock import patch, MagicMock +from src.geocode import geocode, find_timezone, find_country_code class TestGeocode(unittest.TestCase): @@ -40,3 +40,67 @@ def test_geocode_invalid_address(self, mock_geocode): expected_error_message = 'geocode unable to find latitude & longitude for {address}'.format( address=address) self.assertEqual(str(context.exception), expected_error_message) + + @patch('src.geocode.TimezoneFinder') + def test_find_timezone_valid_coordinates(self, mock_timezone_finder): + tf_instance = MagicMock() + tf_instance.timezone_at.return_value = 'America/New_York' + mock_timezone_finder.return_value = tf_instance + + gps_coordinates = (40.7128, -74.0060) + result = find_timezone(gps_coordinates) + + self.assertEqual(result, 'America/New_York') + + @patch('src.geocode.TimezoneFinder') + def test_find_timezone_out_of_bounds(self, mock_timezone_finder): + tf_instance = MagicMock() + tf_instance.timezone_at.side_effect = ValueError('The coordinates were out of bounds 40.7128:-74.0060') + mock_timezone_finder.return_value = tf_instance + + gps_coordinates = (40.7128, -74.0060) + + with self.assertRaises(ValueError) as context: + find_timezone(gps_coordinates) + + self.assertEqual(str(context.exception), 'The coordinates were out of bounds 40.7128:-74.006') + + @patch('src.geocode.TimezoneFinder') + def test_find_timezone_no_matching_timezone(self, mock_timezone_finder): + tf_instance = MagicMock() + tf_instance.timezone_at.return_value = None + mock_timezone_finder.return_value = tf_instance + + gps_coordinates = (40.7128, -74.0060) + + with self.assertRaises(ValueError) as context: + find_timezone(gps_coordinates) + + self.assertEqual(str(context.exception), 'GPS coordinates did not match a time_zone') + +class TestFindCountryCode(unittest.TestCase): + @patch('src.geocode.Nominatim.geocode') + def test_find_country_code_valid_coordinates(self, mock_nominatim): + geolocator_instance = MagicMock() + location_instance = MagicMock() + location_instance.raw = {'address': {'country_code': 'us'}} + geolocator_instance.reverse.return_value = location_instance + mock_nominatim.return_value = geolocator_instance + + gps_coordinates = (40.7128, -74.0060) + result = find_country_code(gps_coordinates) + + self.assertEqual(result, 'US') + + @patch('src.geocode.Nominatim.geocode') + def test_find_country_code_invalid_coordinates(self, mock_nominatim): + geolocator_instance = MagicMock() + geolocator_instance.reverse.side_effect = Exception('Invalid coordinates') + mock_nominatim.return_value = geolocator_instance + + gps_coordinates = (1000.0, 2000.0) # Invalid coordinates + + with self.assertRaises(Exception) as context: + find_country_code(gps_coordinates) + + self.assertEqual(str(context.exception), 'Must be a coordinate pair or Point') \ No newline at end of file