2424os .environ ['TZ' ] = 'GMT'
2525
2626
27+ def require_exiftool_geolocation ():
28+ if not geolocation .is_exiftool_available ():
29+ pytest .skip ('ExifTool geolocation is not available in this environment' )
30+
31+
32+
2733def test_decimal_to_dms ():
2834
2935 for x in range (0 , 1000 ):
@@ -86,6 +92,7 @@ def test_dms_string_longitude():
8692
8793def test_exiftool_coordinates_by_name_sunnyvale ():
8894 """Test ExifTool coordinates lookup for Sunnyvale, California."""
95+ require_exiftool_geolocation ()
8996 result = geolocation .exiftool_coordinates_by_name ("Sunnyvale, California" )
9097
9198 assert result is not None , "Should find coordinates for Sunnyvale, California"
@@ -109,10 +116,11 @@ def test_exiftool_coordinates_unavailable(mock_available):
109116def test_exiftool_is_available ():
110117 """Test if ExifTool geolocation is available."""
111118 available = geolocation .is_exiftool_available ()
112- assert available == True , "ExifTool geolocation should be available "
119+ assert isinstance ( available , bool ) , "ExifTool availability should be reported as a boolean "
113120
114121def test_exiftool_place_name_sunnyvale ():
115122 """Test ExifTool place name lookup with known Sunnyvale coordinates."""
123+ require_exiftool_geolocation ()
116124 lat , lon = 37.3688 , - 122.0365
117125 result = geolocation .exiftool_place_name (lat , lon )
118126
@@ -134,14 +142,22 @@ def test_exiftool_place_name_unavailable(mock_available):
134142@mock .patch ('elodie.geolocation.__KEY__' , None )
135143def test_coordinates_by_name_fallback_to_exiftool ():
136144 """Test that coordinates_by_name falls back to ExifTool when MapQuest key is not available."""
145+ require_exiftool_geolocation ()
137146 result = geolocation .coordinates_by_name ("Sunnyvale, California" )
138147
139148 assert result is not None , "Should return coordinates using ExifTool fallback"
140149 assert 'latitude' in result and 'longitude' in result , "Should include lat/lon"
141150 assert abs (result ['latitude' ] - 37.3688 ) < 0.01 , f"Should get correct latitude from ExifTool"
142151 assert abs (result ['longitude' ] - (- 122.0365 )) < 0.01 , f"Should get correct longitude from ExifTool"
143152
144- def test_reverse_lookup_with_valid_key ():
153+ @mock .patch ('requests.get' )
154+ @mock .patch ('elodie.geolocation.__KEY__' , 'configured-key' )
155+ def test_reverse_lookup_with_valid_key (mock_get ):
156+ mock_get .return_value .json .return_value = {
157+ "info" : {"statuscode" : 0 , "copyright" : {"text" : "© 2022 MapQuest, Inc." , "imageUrl" : "http://api.mqcdn.com/res/mqlogo.gif" , "imageAltText" : "© 2022 MapQuest, Inc." }, "messages" : []},
158+ "options" : {"maxResults" : 1 , "ignoreLatLngInput" : False },
159+ "results" : [{"providedLocation" : {"latLng" : {"lat" : 37.368 , "lng" : - 122.03 }}, "locations" : [{"street" : "312 Old San Francisco Rd" , "adminArea6" : "Heritage District" , "adminArea6Type" : "Neighborhood" , "adminArea5" : "Sunnyvale" , "adminArea5Type" : "City" , "adminArea4" : "Santa Clara" , "adminArea4Type" : "County" , "adminArea3" : "CA" , "adminArea3Type" : "State" , "adminArea1" : "US" , "adminArea1Type" : "Country" , "postalCode" : "94086" , "geocodeQualityCode" : "P1AAA" , "geocodeQuality" : "POINT" , "dragPoint" : False , "sideOfStreet" : "R" , "linkId" : "0" , "unknownInput" : "" , "type" : "s" , "latLng" : {"lat" : 37.36798 , "lng" : - 122.03018 }, "displayLatLng" : {"lat" : 37.36785 , "lng" : - 122.03021 }, "mapUrl" : "" }]}]
160+ }
145161 res = geolocation .lookup (lat = 37.368 , lon = - 122.03 )
146162 assert res ['address' ]['city' ] == 'Sunnyvale' , res
147163
@@ -154,7 +170,14 @@ def test_reverse_lookup_with_invalid_key():
154170 res = geolocation .lookup (lat = 37.368 , lon = - 122.03 )
155171 assert res is None , res
156172
157- def test_lookup_with_valid_key ():
173+ @mock .patch ('requests.get' )
174+ @mock .patch ('elodie.geolocation.__KEY__' , 'configured-key' )
175+ def test_lookup_with_valid_key (mock_get ):
176+ mock_get .return_value .json .return_value = {
177+ "info" : {"statuscode" : 0 , "copyright" : {"text" : "© 2022 MapQuest, Inc." , "imageUrl" : "http://api.mqcdn.com/res/mqlogo.gif" , "imageAltText" : "© 2022 MapQuest, Inc." }, "messages" : []},
178+ "options" : {"maxResults" : - 1 , "ignoreLatLngInput" : False },
179+ "results" : [{"providedLocation" : {"location" : "Sunnyvale,CA" }, "locations" : [{"street" : "" , "adminArea6" : "" , "adminArea6Type" : "Neighborhood" , "adminArea5" : "Sunnyvale" , "adminArea5Type" : "City" , "adminArea4" : "Santa Clara" , "adminArea4Type" : "County" , "adminArea3" : "CA" , "adminArea3Type" : "State" , "adminArea1" : "US" , "adminArea1Type" : "Country" , "postalCode" : "" , "geocodeQualityCode" : "A5XAX" , "geocodeQuality" : "CITY" , "dragPoint" : False , "sideOfStreet" : "N" , "linkId" : "0" , "unknownInput" : "" , "type" : "s" , "latLng" : {"lat" : 37.37188 , "lng" : - 122.03751 }, "displayLatLng" : {"lat" : 37.37188 , "lng" : - 122.03751 }, "mapUrl" : "" }]}]
180+ }
158181 res = geolocation .lookup (location = 'Sunnyvale, CA' )
159182 latLng = res ['results' ][0 ]['locations' ][0 ]['latLng' ]
160183 assert latLng ['lat' ] == 37.37188 , latLng
@@ -186,7 +209,9 @@ def test_lookup_debug_mapquest_url():
186209 assert 'MapQuest url:' in output , output
187210
188211@mock .patch ('elodie.constants.location_db' , return_value = '%s/location.json-cached' % gettempdir ())
189- def test_place_name_deprecated_string_cached (mock_location_db ):
212+ @mock .patch ('elodie.geolocation.lookup' , return_value = {'address' : {'city' : 'Sunnyvale' }})
213+ @mock .patch ('elodie.geolocation.__KEY__' , 'configured-key' )
214+ def test_place_name_deprecated_string_cached (mock_lookup , mock_location_db ):
190215 # See gh-160 for backwards compatability needed when a string is stored instead of a dict
191216 helper .reset_dbs ()
192217 with open (mock_location_db .return_value , 'w' ) as f :
@@ -223,6 +248,7 @@ def test_place_name_no_default():
223248@mock .patch ('elodie.geolocation.__KEY__' , None )
224249def test_place_name_fallback_to_exiftool ():
225250 """Test that place_name falls back to ExifTool when MapQuest key is not available."""
251+ require_exiftool_geolocation ()
226252 # Test with known coordinates for Sunnyvale
227253 lat , lon = 37.3688 , - 122.0365
228254 result = geolocation .place_name (lat , lon )
0 commit comments