2017-12-27 20:43:14 +08:00
|
|
|
import unittest
|
2017-12-28 18:36:02 +08:00
|
|
|
from mock import Mock
|
2017-12-27 20:43:14 +08:00
|
|
|
from cartodb_services.mapbox import MapboxGeocoder
|
2018-01-04 22:00:52 +08:00
|
|
|
from cartodb_services.tools.exceptions import ServiceException
|
2018-02-14 01:23:36 +08:00
|
|
|
from credentials import mapbox_api_key
|
2017-12-27 20:43:14 +08:00
|
|
|
|
|
|
|
INVALID_TOKEN = 'invalid_token'
|
|
|
|
VALID_ADDRESS = 'Calle Siempreviva 3, Valladolid'
|
|
|
|
WELL_KNOWN_LONGITUDE = -4.730947
|
|
|
|
WELL_KNOWN_LATITUDE = 41.668654
|
|
|
|
|
|
|
|
|
|
|
|
class MapboxGeocoderTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2018-02-14 01:23:36 +08:00
|
|
|
self.geocoder = MapboxGeocoder(token=mapbox_api_key(), logger=Mock())
|
2017-12-27 20:43:14 +08:00
|
|
|
|
|
|
|
def test_invalid_token(self):
|
2017-12-28 18:36:02 +08:00
|
|
|
invalid_geocoder = MapboxGeocoder(token=INVALID_TOKEN, logger=Mock())
|
2017-12-27 20:43:14 +08:00
|
|
|
with self.assertRaises(ServiceException):
|
|
|
|
invalid_geocoder.geocode(VALID_ADDRESS)
|
|
|
|
|
|
|
|
def test_valid_request(self):
|
|
|
|
place = self.geocoder.geocode(VALID_ADDRESS)
|
|
|
|
|
2018-01-30 16:13:43 +08:00
|
|
|
self.assertEqual('%.3f' % place[0], '%.3f' % WELL_KNOWN_LONGITUDE)
|
|
|
|
self.assertEqual('%.3f' % place[1], '%.3f' % WELL_KNOWN_LATITUDE)
|
2018-01-03 21:14:25 +08:00
|
|
|
|
|
|
|
def test_valid_request_namedplace(self):
|
|
|
|
place = self.geocoder.geocode(searchtext='Barcelona')
|
|
|
|
|
|
|
|
assert place
|
2018-01-16 23:36:24 +08:00
|
|
|
|
|
|
|
def test_valid_request_namedplace2(self):
|
|
|
|
place = self.geocoder.geocode(searchtext='New York', country='us')
|
|
|
|
|
|
|
|
assert place
|
2018-01-31 19:05:27 +08:00
|
|
|
|
2018-02-13 01:02:37 +08:00
|
|
|
def test_odd_characters(self):
|
|
|
|
place = self.geocoder.geocode(searchtext='Barcelona; "Spain"')
|
|
|
|
|
|
|
|
assert place
|
|
|
|
|
2018-01-31 19:05:27 +08:00
|
|
|
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 == []
|