Added geocoding and routing services
This commit is contained in:
@@ -4,3 +4,6 @@ def mapbox_api_key():
|
||||
"""Returns Mapbox API key. Requires setting MAPBOX_API_KEY environment variable."""
|
||||
return os.environ['MAPBOX_API_KEY']
|
||||
|
||||
def tomtom_api_key():
|
||||
"""Returns TomTom API key. Requires setting TOMTOM_API_KEY environment variable."""
|
||||
return os.environ['TOMTOM_API_KEY']
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import unittest
|
||||
from mock import Mock
|
||||
from cartodb_services.tomtom import TomTomGeocoder
|
||||
from cartodb_services.tools.exceptions import ServiceException
|
||||
from credentials import tomtom_api_key
|
||||
|
||||
INVALID_APIKEY = 'invalid_apikey'
|
||||
VALID_ADDRESS = 'Plaza Mayor 3, Valladolid'
|
||||
WELL_KNOWN_LONGITUDE = -4.728
|
||||
WELL_KNOWN_LATITUDE = 41.653
|
||||
|
||||
|
||||
class TomTomGeocoderTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.geocoder = TomTomGeocoder(apikey=tomtom_api_key(), logger=Mock())
|
||||
|
||||
def test_invalid_token(self):
|
||||
invalid_geocoder = TomTomGeocoder(apikey=INVALID_APIKEY, logger=Mock())
|
||||
with self.assertRaises(ServiceException):
|
||||
invalid_geocoder.geocode(VALID_ADDRESS)
|
||||
|
||||
def test_valid_request(self):
|
||||
place = self.geocoder.geocode(VALID_ADDRESS)
|
||||
|
||||
self.assertEqual('%.3f' % place[0], '%.3f' % WELL_KNOWN_LONGITUDE)
|
||||
self.assertEqual('%.3f' % place[1], '%.3f' % WELL_KNOWN_LATITUDE)
|
||||
|
||||
def test_valid_request_namedplace(self):
|
||||
place = self.geocoder.geocode(searchtext='Barcelona')
|
||||
|
||||
assert place
|
||||
|
||||
def test_valid_request_namedplace2(self):
|
||||
place = self.geocoder.geocode(searchtext='New York', country='us')
|
||||
|
||||
assert place
|
||||
|
||||
def test_odd_characters(self):
|
||||
place = self.geocoder.geocode(searchtext='Barcelona; "Spain"')
|
||||
|
||||
assert place
|
||||
|
||||
def test_empty_request(self):
|
||||
place = self.geocoder.geocode(searchtext='', country=None, city=None, state_province=None)
|
||||
|
||||
assert place == []
|
||||
|
||||
def test_empty_search_text_request(self):
|
||||
place = self.geocoder.geocode(searchtext=' ', country='us', city=None, state_province="")
|
||||
|
||||
assert place == []
|
||||
|
||||
def test_unknown_place_request(self):
|
||||
place = self.geocoder.geocode(searchtext='[unknown]', country='ch', state_province=None, city=None)
|
||||
|
||||
assert place == []
|
||||
@@ -0,0 +1,52 @@
|
||||
import unittest
|
||||
from mock import Mock
|
||||
from cartodb_services.tomtom import TomTomRouting
|
||||
from cartodb_services.tomtom.routing import DEFAULT_PROFILE
|
||||
from cartodb_services.tools.exceptions import ServiceException
|
||||
from cartodb_services.tools import Coordinate
|
||||
from credentials import tomtom_api_key
|
||||
|
||||
INVALID_APIKEY = 'invalid_apikey'
|
||||
VALID_WAYPOINTS = [Coordinate(13.42936, 52.50931),
|
||||
Coordinate(13.43872, 52.50274)]
|
||||
NUM_WAYPOINTS_MAX = 20
|
||||
INVALID_WAYPOINTS_EMPTY = []
|
||||
INVALID_WAYPOINTS_MIN = [Coordinate(13.42936, 52.50931)]
|
||||
INVALID_WAYPOINTS_MAX = [Coordinate(13.42936, 52.50931)
|
||||
for x in range(0, NUM_WAYPOINTS_MAX + 2)]
|
||||
VALID_PROFILE = DEFAULT_PROFILE
|
||||
INVALID_PROFILE = 'invalid_profile'
|
||||
|
||||
|
||||
class TomTomRoutingTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.routing = TomTomRouting(apikey=tomtom_api_key(), logger=Mock())
|
||||
|
||||
def test_invalid_profile(self):
|
||||
with self.assertRaises(ValueError):
|
||||
self.routing.directions(VALID_WAYPOINTS, INVALID_PROFILE)
|
||||
|
||||
def test_invalid_waypoints_empty(self):
|
||||
with self.assertRaises(ValueError):
|
||||
self.routing.directions(INVALID_WAYPOINTS_EMPTY, VALID_PROFILE)
|
||||
|
||||
def test_invalid_waypoints_min(self):
|
||||
with self.assertRaises(ValueError):
|
||||
self.routing.directions(INVALID_WAYPOINTS_MIN, VALID_PROFILE)
|
||||
|
||||
def test_invalid_waypoints_max(self):
|
||||
with self.assertRaises(ValueError):
|
||||
self.routing.directions(INVALID_WAYPOINTS_MAX, VALID_PROFILE)
|
||||
|
||||
def test_invalid_token(self):
|
||||
invalid_routing = TomTomRouting(apikey=INVALID_APIKEY, logger=Mock())
|
||||
with self.assertRaises(ServiceException):
|
||||
invalid_routing.directions(VALID_WAYPOINTS,
|
||||
VALID_PROFILE)
|
||||
|
||||
def test_valid_request(self):
|
||||
route = self.routing.directions(VALID_WAYPOINTS, VALID_PROFILE)
|
||||
|
||||
assert route.shape
|
||||
assert route.length
|
||||
assert route.duration
|
||||
Reference in New Issue
Block a user