Refactors and adds unit tests
This commit is contained in:
parent
4a55223ec7
commit
cf99077276
@ -1,3 +1,6 @@
|
|||||||
|
#!/usr/local/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
class BadGeocodingParams(Exception):
|
class BadGeocodingParams(Exception):
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
#!/usr/local/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import urllib
|
import urllib
|
||||||
|
|
||||||
@ -6,8 +9,8 @@ from heremapsexceptions import BadGeocodingParams, EmptyGeocoderResponse, NoGeoc
|
|||||||
class Geocoder:
|
class Geocoder:
|
||||||
'A Here Maps Geocoder wrapper for python'
|
'A Here Maps Geocoder wrapper for python'
|
||||||
|
|
||||||
URL_GEOCODE_JSON = 'http://geocoder.cit.api.here.com/6.2/geocode.json'
|
URL_GEOCODE_JSON = 'http://geocoder.api.here.com/6.2/geocode.json'
|
||||||
MAX_RESULTS = 1
|
DEFAULT_MAXRESULTS = 1
|
||||||
|
|
||||||
ADDRESS_PARAMS = [
|
ADDRESS_PARAMS = [
|
||||||
'city',
|
'city',
|
||||||
@ -43,19 +46,31 @@ class Geocoder:
|
|||||||
|
|
||||||
app_id = ''
|
app_id = ''
|
||||||
app_code = ''
|
app_code = ''
|
||||||
|
maxresults = ''
|
||||||
|
|
||||||
def __init__(self, app_id, app_code):
|
def __init__(self, app_id, app_code, maxresults=DEFAULT_MAXRESULTS):
|
||||||
self.app_id = app_id
|
self.app_id = app_id
|
||||||
self.app_code = app_code
|
self.app_code = app_code
|
||||||
|
self.maxresults = maxresults
|
||||||
|
|
||||||
def geocode(self, params):
|
def geocode(self, params):
|
||||||
if not set(params.keys()).issubset(set(self.ADDRESS_PARAMS)):
|
if not set(params.keys()).issubset(set(self.ADDRESS_PARAMS)):
|
||||||
raise BadGeocodingParams(params)
|
raise BadGeocodingParams(params)
|
||||||
|
|
||||||
|
response = self.performRequest(params)
|
||||||
|
|
||||||
|
try:
|
||||||
|
results = response['Response']['View'][0]['Result']
|
||||||
|
except IndexError:
|
||||||
|
raise EmptyGeocoderResponse()
|
||||||
|
|
||||||
|
return results
|
||||||
|
|
||||||
|
def performRequest(self, params):
|
||||||
request_params = {
|
request_params = {
|
||||||
'app_id' : self.app_id,
|
'app_id' : self.app_id,
|
||||||
'app_code' : self.app_code,
|
'app_code' : self.app_code,
|
||||||
'maxresults' : self.MAX_RESULTS,
|
'maxresults' : self.maxresults,
|
||||||
'gen' : '9'
|
'gen' : '9'
|
||||||
}
|
}
|
||||||
request_params.update(params)
|
request_params.update(params)
|
||||||
@ -78,12 +93,8 @@ class Geocoder:
|
|||||||
|
|
||||||
return self.geocode(params)
|
return self.geocode(params)
|
||||||
|
|
||||||
def extractLngLatFromResponse(self, response):
|
def extractLngLatFromResult(self, result):
|
||||||
view = response['Response']['View']
|
location = result['Location']
|
||||||
|
|
||||||
if len(view) is 0: raise EmptyGeocoderResponse()
|
|
||||||
|
|
||||||
location = view[0]['Result'][0]['Location']
|
|
||||||
|
|
||||||
longitude = location['DisplayPosition']['Longitude']
|
longitude = location['DisplayPosition']['Longitude']
|
||||||
latitude = location['DisplayPosition']['Latitude']
|
latitude = location['DisplayPosition']['Latitude']
|
||||||
|
0
lib/python/heremaps/tests/__init__.py
Normal file
0
lib/python/heremaps/tests/__init__.py
Normal file
110
lib/python/heremaps/tests/heremapsgeocoder_tests.py
Normal file
110
lib/python/heremaps/tests/heremapsgeocoder_tests.py
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
#!/usr/local/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from heremaps import heremapsgeocoder
|
||||||
|
from heremaps.heremapsexceptions import BadGeocodingParams, EmptyGeocoderResponse, NoGeocodingParams
|
||||||
|
|
||||||
|
from secrets import *
|
||||||
|
|
||||||
|
class GeocoderTestCase(unittest.TestCase):
|
||||||
|
EMPTY_RESPONSE = {
|
||||||
|
"Response":{
|
||||||
|
"MetaInfo":{
|
||||||
|
"Timestamp":"2015-11-04T16:31:57.273+0000"
|
||||||
|
},
|
||||||
|
"View":[]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
GOOD_RESPONSE = {
|
||||||
|
"Response": {
|
||||||
|
"MetaInfo": {
|
||||||
|
"Timestamp":"2015-11-04T16:30:32.187+0000"
|
||||||
|
},
|
||||||
|
"View":[{
|
||||||
|
"_type":"SearchResultsViewType",
|
||||||
|
"ViewId":0,
|
||||||
|
"Result":[{
|
||||||
|
"Relevance":0.89,
|
||||||
|
"MatchLevel":"street",
|
||||||
|
"MatchQuality":{
|
||||||
|
"City":1.0,
|
||||||
|
"Street":[1.0]
|
||||||
|
},
|
||||||
|
"Location":{
|
||||||
|
"LocationId":"NT_yyKB4r3mCWAX4voWgxPcuA",
|
||||||
|
"LocationType":"address",
|
||||||
|
"DisplayPosition":{
|
||||||
|
"Latitude":40.43433,
|
||||||
|
"Longitude":-3.70126
|
||||||
|
},
|
||||||
|
"NavigationPosition":[{
|
||||||
|
"Latitude":40.43433,
|
||||||
|
"Longitude":-3.70126
|
||||||
|
}],
|
||||||
|
"MapView":{
|
||||||
|
"TopLeft":{
|
||||||
|
"Latitude":40.43493,
|
||||||
|
"Longitude":-3.70404
|
||||||
|
},
|
||||||
|
"BottomRight":{
|
||||||
|
"Latitude":40.43373,
|
||||||
|
"Longitude":-3.69873
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Address":{
|
||||||
|
"Label":"Calle de Eloy Gonzalo, Madrid (Madrid), España",
|
||||||
|
"Country":"ESP",
|
||||||
|
"State":"Comunidad de Madrid",
|
||||||
|
"County":"Madrid",
|
||||||
|
"City":"Madrid",
|
||||||
|
"District":"Trafalgar",
|
||||||
|
"Street":"Calle de Eloy Gonzalo",
|
||||||
|
"AdditionalData":[{
|
||||||
|
"value":"España",
|
||||||
|
"key":"CountryName"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"value":"Comunidad de Madrid",
|
||||||
|
"key":"StateName"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"value":"Madrid",
|
||||||
|
"key":"CountyName"
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.geocoder = heremapsgeocoder.Geocoder(None, None)
|
||||||
|
|
||||||
|
def test_geocodeAddress_with_valid_params(self):
|
||||||
|
self.geocoder.performRequest = lambda x: self.GOOD_RESPONSE
|
||||||
|
response = self.geocoder.geocodeAddress(
|
||||||
|
searchtext='Calle Eloy Gonzalo 27',
|
||||||
|
city='Madrid',
|
||||||
|
country='España')
|
||||||
|
|
||||||
|
def test_geocodeAddress_with_invalid_params(self):
|
||||||
|
with self.assertRaises(BadGeocodingParams):
|
||||||
|
self.geocoder.geocodeAddress(
|
||||||
|
searchtext='Calle Eloy Gonzalo 27',
|
||||||
|
manolo='escobar')
|
||||||
|
|
||||||
|
def test_geocodeAddress_with_no_params(self):
|
||||||
|
with self.assertRaises(NoGeocodingParams):
|
||||||
|
self.geocoder.geocodeAddress()
|
||||||
|
|
||||||
|
def test_geocodeAddress_empty_response(self):
|
||||||
|
self.geocoder.performRequest = lambda x: self.EMPTY_RESPONSE
|
||||||
|
with self.assertRaises(EmptyGeocoderResponse):
|
||||||
|
self.geocoder.geocodeAddress(searchtext='lkajfñlasjfñ')
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
Loading…
Reference in New Issue
Block a user