Skip to content

Commit

Permalink
added more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgreen-moj committed Dec 4, 2023
1 parent fdb78e8 commit 7660b7a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/geocode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
68 changes: 66 additions & 2 deletions test/test_geocode.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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')

0 comments on commit 7660b7a

Please sign in to comment.