Add geolocation tests

This commit is contained in:
Cédric Leporcq 2021-10-17 10:14:19 +02:00
parent 8f27a84571
commit 0ae9f3118a
2 changed files with 22 additions and 9 deletions

View File

@ -24,15 +24,7 @@ class GeoLocation:
self.prefer_english_names = prefer_english_names
self.timeout = timeout
def coordinates_by_name(self, name, db, timeout=options.default_timeout):
# Try to get cached location first
cached_coordinates = db.get_location_coordinates(name)
if cached_coordinates is not None:
return {
'latitude': cached_coordinates[0],
'longitude': cached_coordinates[1],
}
def coordinates_by_name(self, name, timeout=options.default_timeout):
# If the name is not cached then we go ahead with an API lookup
geocoder = self.geocoder
if geocoder == 'Nominatim':

21
tests/test_geolocation.py Normal file
View File

@ -0,0 +1,21 @@
from ordigi.geolocation import GeoLocation
import pytest
class TestGeoLocation:
def setup_class(cls):
cls.loc = GeoLocation()
def test_coordinates_by_name(self):
coordinates = self.loc.coordinates_by_name('Sunnyvale, CA')
assert coordinates['latitude'] == 37.3688301
assert coordinates['longitude'] == -122.036349
def test_place_name(self):
place_name = self.loc.place_name(lat=37.368, lon=-122.03)
assert place_name['city'] == 'Sunnyvale', place_name
# Invalid lat/lon
with pytest.warns(UserWarning):
place_name = self.loc.place_name(lat=999, lon=999)
assert place_name == {'default': None}, place_name