2016-03-23 02:33:55 +08:00
|
|
|
#!/usr/local/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
2016-08-02 01:04:56 +08:00
|
|
|
import mock
|
2016-03-23 02:33:55 +08:00
|
|
|
import unittest
|
|
|
|
import requests_mock
|
2016-08-02 01:04:56 +08:00
|
|
|
from mock import Mock
|
2016-03-23 02:33:55 +08:00
|
|
|
|
|
|
|
from cartodb_services.mapzen import MapzenGeocoder
|
2016-09-16 19:24:14 +08:00
|
|
|
from cartodb_services.mapzen.exceptions import MalformedResult, TimeoutException
|
2016-03-23 02:33:55 +08:00
|
|
|
|
|
|
|
requests_mock.Mocker.TEST_PREFIX = 'test_'
|
|
|
|
|
|
|
|
|
|
|
|
@requests_mock.Mocker()
|
2016-07-06 18:18:52 +08:00
|
|
|
class MapzenGeocoderTestCase(unittest.TestCase):
|
2016-03-23 02:33:55 +08:00
|
|
|
MAPZEN_GEOCODER_URL = 'https://search.mapzen.com/v1/search'
|
|
|
|
|
|
|
|
EMPTY_RESPONSE = """{
|
|
|
|
"results" : [],
|
|
|
|
"status" : "ZERO_RESULTS"
|
|
|
|
}"""
|
|
|
|
|
|
|
|
GOOD_RESPONSE = """{
|
|
|
|
"geocoding": {
|
|
|
|
"version": "0.1",
|
|
|
|
"attribution": "https://search.mapzen.com/v1/attribution",
|
|
|
|
"query": {
|
|
|
|
"text": "Calle siempreviva 3, Valladolid",
|
|
|
|
"parsed_text": {
|
|
|
|
"name": "Calle siempreviva 3",
|
|
|
|
"regions": [
|
|
|
|
"Calle siempreviva 3",
|
|
|
|
"Valladolid"
|
|
|
|
],
|
|
|
|
"admin_parts": "Valladolid"
|
|
|
|
},
|
|
|
|
"types": {
|
|
|
|
"from_layers": [
|
|
|
|
"osmaddress",
|
|
|
|
"openaddresses"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"size": 10,
|
|
|
|
"private": false,
|
|
|
|
"type": [
|
|
|
|
"osmaddress",
|
|
|
|
"openaddresses"
|
|
|
|
],
|
|
|
|
"querySize": 20
|
|
|
|
},
|
|
|
|
"engine": {
|
|
|
|
"name": "Pelias",
|
|
|
|
"author": "Mapzen",
|
|
|
|
"version": "1.0"
|
|
|
|
},
|
|
|
|
"timestamp": 1458661873749
|
|
|
|
},
|
|
|
|
"type": "FeatureCollection",
|
|
|
|
"features": [
|
|
|
|
{
|
|
|
|
"type": "Feature",
|
|
|
|
"properties": {
|
|
|
|
"id": "df7428b955ae44a39dc40d52578f61e3",
|
|
|
|
"gid": "oa:address:df7428b955ae44a39dc40d52578f61e3",
|
|
|
|
"layer": "address",
|
|
|
|
"source": "oa",
|
|
|
|
"name": "5 Close Siempreviva",
|
|
|
|
"housenumber": "5",
|
|
|
|
"street": "Close Siempreviva",
|
|
|
|
"country_a": "ESP",
|
|
|
|
"country": "Spain",
|
|
|
|
"region": "Valladolid",
|
|
|
|
"localadmin": "Valladolid",
|
|
|
|
"locality": "Valladolid",
|
|
|
|
"confidence": 0.887,
|
|
|
|
"label": "5 Close Siempreviva, Valladolid, Spain"
|
|
|
|
},
|
|
|
|
"geometry": {
|
|
|
|
"type": "Point",
|
|
|
|
"coordinates": [
|
|
|
|
-4.730928,
|
|
|
|
41.669034
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}"""
|
|
|
|
|
|
|
|
MALFORMED_RESPONSE = """{"manolo": "escobar"}"""
|
|
|
|
|
|
|
|
def setUp(self):
|
2016-08-02 01:04:56 +08:00
|
|
|
logger = Mock()
|
|
|
|
self.geocoder = MapzenGeocoder('search-XXXXXXX', logger)
|
2016-03-23 02:33:55 +08:00
|
|
|
|
|
|
|
def test_geocode_address_with_valid_params(self, req_mock):
|
|
|
|
req_mock.register_uri('GET', self.MAPZEN_GEOCODER_URL,
|
|
|
|
text=self.GOOD_RESPONSE)
|
|
|
|
response = self.geocoder.geocode(
|
|
|
|
searchtext='Calle Siempreviva 3, Valldolid',
|
|
|
|
country='ESP')
|
|
|
|
|
|
|
|
self.assertEqual(response[0], -4.730928)
|
|
|
|
self.assertEqual(response[1], 41.669034)
|
|
|
|
|
|
|
|
def test_geocode_with_malformed_result(self, req_mock):
|
|
|
|
req_mock.register_uri('GET', self.MAPZEN_GEOCODER_URL,
|
|
|
|
text=self.MALFORMED_RESPONSE)
|
|
|
|
with self.assertRaises(MalformedResult):
|
|
|
|
self.geocoder.geocode(
|
|
|
|
searchtext='Calle Siempreviva 3, Valladolid',
|
|
|
|
country='ESP')
|