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

133 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
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
GOOD_RESPONSE = """{
2015-11-05 01:12:54 +08:00
"Response": {
"MetaInfo": {
2016-01-22 01:07:07 +08:00
"Timestamp": "2015-11-04T16:30:32.187+0000"
2015-11-05 01:12:54 +08:00
},
2016-01-22 01:07:07 +08:00
"View": [{
"_type": "SearchResultsViewType",
"ViewId": 0,
"Result": {
2016-01-22 01:07:07 +08:00
"Relevance": 0.89,
"MatchLevel": "street",
"MatchQuality": {
"City": 1.0,
"Street": [1.0]
2015-11-05 01:12:54 +08:00
},
2016-01-22 01:07:07 +08:00
"Location": {
"LocationId": "NT_yyKB4r3mCWAX4voWgxPcuA",
"LocationType": "address",
"DisplayPosition": {
"Latitude": 40.43433,
"Longitude": -3.70126
2015-11-05 18:12:10 +08:00
},
2016-01-22 01:07:07 +08:00
"NavigationPosition": [{
"Latitude": 40.43433,
"Longitude": -3.70126
2015-11-05 01:12:54 +08:00
}],
2016-01-22 01:07:07 +08:00
"MapView": {
"TopLeft": {
"Latitude": 40.43493,
"Longitude": -3.70404
2015-11-05 01:12:54 +08:00
},
2016-01-22 01:07:07 +08:00
"BottomRight": {
"Latitude": 40.43373,
"Longitude": -3.69873
2015-11-05 01:12:54 +08:00
}
},
2016-01-22 01:07:07 +08:00
"Address": {
"Label": "Calle de Eloy Gonzalo, Madrid, Espana",
2016-01-22 01:07:07 +08:00
"Country": "ESP",
"State": "Comunidad de Madrid",
"County": "Madrid",
"City": "Madrid",
"District": "Trafalgar",
"Street": "Calle de Eloy Gonzalo",
"AdditionalData": [{
"value": "Espana",
2016-01-22 01:07:07 +08:00
"key": "CountryName"
2015-11-05 01:12:54 +08:00
},
{
2016-01-22 01:07:07 +08:00
"value": "Comunidad de Madrid",
"key": "StateName"
2015-11-05 01:12:54 +08:00
},
{
2016-01-22 01:07:07 +08:00
"value": "Madrid",
"key": "CountyName"
2015-11-05 01:12:54 +08:00
}]
}
}
}
2015-11-05 01:12:54 +08:00
}]
}
}"""
MALFORMED_RESPONSE = """{"manolo": "escobar"}"""
2015-11-05 01:12:54 +08:00
def setUp(self):
self.geocoder = HereMapsGeocoder(None, None)
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(
2015-11-05 01:12:54 +08:00
searchtext='Calle Eloy Gonzalo 27',
city='Madrid',
country='España')
self.assertEqual(response[0], -3.70126)
self.assertEqual(response[1], 40.43433)
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(
searchtext='Calle Eloy Gonzalo 27',
city='Madrid',
country='España')