dataservices-api/server/lib/python/cartodb_services/test/test_heremapsgeocoder.py

148 lines
4.9 KiB
Python
Raw Normal View History

2015-11-05 01:12:54 +08:00
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import unittest
import requests_mock
2016-08-02 01:04:56 +08:00
from mock import Mock
2015-11-05 01:12:54 +08:00
from cartodb_services.here import HereMapsGeocoder
from cartodb_services.here.exceptions import BadGeocodingParams
from cartodb_services.here.exceptions import NoGeocodingParams
from cartodb_services.here.exceptions import MalformedResult
2015-11-05 01:12:54 +08:00
requests_mock.Mocker.TEST_PREFIX = 'test_'
2015-11-05 01:12:54 +08:00
@requests_mock.Mocker()
class HereMapsGeocoderTestCase(unittest.TestCase):
EMPTY_RESPONSE = """{
2016-01-22 01:07:07 +08:00
"Response": {
"MetaInfo": {
"Timestamp": "2015-11-04T16:31:57.273+0000"
2015-11-05 01:12:54 +08:00
},
2016-01-22 01:07:07 +08:00
"View": []
2015-11-05 01:12:54 +08:00
}
}"""
2015-11-05 01:12:54 +08:00
2016-02-10 22:24:25 +08:00
GOOD_RESPONSE = unicode("""{
"Response": {
"MetaInfo": {
"Timestamp": "2016-02-10T14:17:33.792+0000",
"NextPageInformation": "2"
},
"View": [
{
"_type": "SearchResultsViewType",
"ViewId": 0,
"Result": [
{
"Relevance": 1,
"MatchLevel": "houseNumber",
"MatchQuality": {
"Street": [
1
],
"HouseNumber": 1
},
"MatchType": "pointAddress",
"Location": {
"LocationId": "NT_CKopMSB9JnBYAO11CMOrxB_zUD",
"LocationType": "address",
"DisplayPosition": {
"Latitude": 37.70246,
"Longitude": -5.2794
},
"NavigationPosition": [
{
"Latitude": 37.7024199,
"Longitude": -5.27939
}
],
"MapView": {
"TopLeft": {
"Latitude": 37.7035842,
"Longitude": -5.2808208
2015-11-05 01:12:54 +08:00
},
2016-02-10 22:24:25 +08:00
"BottomRight": {
"Latitude": 37.7013358,
"Longitude": -5.2779792
2015-11-05 01:12:54 +08:00
}
2016-02-10 22:24:25 +08:00
},
"Address": {
"Label": "Calle Amor de Dios, 35, 14700 Palma del Río (Córdoba), España",
"Country": "ESP",
"State": "Andalucía",
"County": "Córdoba",
"City": "Palma del Río",
"Street": "Calle Amor de Dios",
"HouseNumber": "35",
"PostalCode": "14700",
"AdditionalData": [
{
"value": "España",
"key": "CountryName"
},
{
"value": "Andalucía",
"key": "StateName"
},
{
"value": "Córdoba",
"key": "CountyName"
}
]
}
}
2016-02-10 22:24:25 +08:00
}
]
}
]
}
}""", 'utf-8')
MALFORMED_RESPONSE = """{"manolo": "escobar"}"""
2015-11-05 01:12:54 +08:00
def setUp(self):
2016-08-02 01:04:56 +08:00
logger = Mock()
self.geocoder = HereMapsGeocoder(None, None, logger)
2015-11-05 01:12:54 +08:00
def test_geocode_address_with_valid_params(self, req_mock):
req_mock.register_uri('GET', HereMapsGeocoder.PRODUCTION_GEOCODE_JSON_URL,
text=self.GOOD_RESPONSE)
response = self.geocoder.geocode(
2016-02-10 22:24:25 +08:00
searchtext='Calle amor de dios',
city='Cordoba',
2015-11-05 01:12:54 +08:00
country='España')
2016-02-10 22:24:25 +08:00
self.assertEqual(response[0], -5.2794)
self.assertEqual(response[1], 37.70246)
def test_geocode_address_with_invalid_params(self, req_mock):
req_mock.register_uri('GET', HereMapsGeocoder.PRODUCTION_GEOCODE_JSON_URL,
text=self.GOOD_RESPONSE)
2015-11-05 01:12:54 +08:00
with self.assertRaises(BadGeocodingParams):
self.geocoder.geocode(
2015-11-05 01:12:54 +08:00
searchtext='Calle Eloy Gonzalo 27',
manolo='escobar')
def test_geocode_address_with_no_params(self, req_mock):
req_mock.register_uri('GET', HereMapsGeocoder.PRODUCTION_GEOCODE_JSON_URL,
text=self.GOOD_RESPONSE)
2015-11-05 01:12:54 +08:00
with self.assertRaises(NoGeocodingParams):
self.geocoder.geocode()
2015-11-05 18:12:10 +08:00
def test_geocode_address_empty_response(self, req_mock):
req_mock.register_uri('GET', HereMapsGeocoder.PRODUCTION_GEOCODE_JSON_URL,
text=self.EMPTY_RESPONSE)
result = self.geocoder.geocode(searchtext='lkajfñlasjfñ')
self.assertEqual(result, [])
2015-11-05 18:12:10 +08:00
def test_geocode_with_malformed_result(self, req_mock):
req_mock.register_uri('GET', HereMapsGeocoder.PRODUCTION_GEOCODE_JSON_URL,
text=self.MALFORMED_RESPONSE)
2015-11-05 18:12:10 +08:00
with self.assertRaises(MalformedResult):
self.geocoder.geocode(
2016-02-10 22:24:25 +08:00
searchtext='Calle amor de dios',
city='Cordoba',
country='España')