From 29a34abd0dd7b6379226c90548569c007761ab4b Mon Sep 17 00:00:00 2001 From: Guido Fioravantti Date: Tue, 3 Nov 2015 16:26:54 +0100 Subject: [PATCH 01/10] Initial commit for heremaps module --- lib/python/heremaps/__init__.py | 0 lib/python/heremaps/heremapsexceptions.py | 11 +++ lib/python/heremaps/heremapsgeocoder.py | 103 ++++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 lib/python/heremaps/__init__.py create mode 100644 lib/python/heremaps/heremapsexceptions.py create mode 100644 lib/python/heremaps/heremapsgeocoder.py diff --git a/lib/python/heremaps/__init__.py b/lib/python/heremaps/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lib/python/heremaps/heremapsexceptions.py b/lib/python/heremaps/heremapsexceptions.py new file mode 100644 index 0000000..cced7a6 --- /dev/null +++ b/lib/python/heremaps/heremapsexceptions.py @@ -0,0 +1,11 @@ +import json + +class BadGeocodingParams(Exception): + def __init__(self, value): + self.value = value + def __str__(self): + return repr('Bad geocoding params: ' + json.dumps(value)) + +class NoGeocodingParams(Exception): + def __str__(self): + return repr('No params for geocoding specified') diff --git a/lib/python/heremaps/heremapsgeocoder.py b/lib/python/heremaps/heremapsgeocoder.py new file mode 100644 index 0000000..eb5c931 --- /dev/null +++ b/lib/python/heremaps/heremapsgeocoder.py @@ -0,0 +1,103 @@ +import inspect +import json +import urllib + +from heremapsexceptions import BadGeocodingParams, NoGeocodingParams + +class Geocoder: + 'A Here Maps Geocoder wrapper for python' + + URL_GEOCODE_JSON = 'http://geocoder.cit.api.here.com/6.2/geocode.json' + MAX_RESULTS = 1 + + ADDRESS_PARAMS = [ + 'city', + 'country', + 'county', + 'district', + 'housenumber', + 'postalcode', + 'searchtext', + 'state', + 'street' + ] + + ADMITTED_PARAMS = [ + 'additionaldata', + 'app_id', + 'app_code', + 'bbox', + 'countryfocus', + 'gen', + 'jsonattributes', + 'jsoncallback', + 'language', + 'locationattributes', + 'locationid', + 'mapview', + 'maxresults', + 'pageinformation', + 'politicalview', + 'prox', + 'strictlanguagemode' + ] + ADDRESS_PARAMS + + app_id = '' + app_code = '' + + def __init__(self, app_id, app_code): + self.app_id = app_id + self.app_code = app_code + + def geocode(self, params): + if not set(params.keys()).issubset(set(self.ADDRESS_PARAMS)): + raise BadGeocodingParams(params) + + request_params = { + 'app_id' : self.app_id, + 'app_code' : self.app_code, + 'maxresults' : self.MAX_RESULTS, + 'gen' : '9' + } + request_params.update(params) + + encoded_request_params = urllib.urlencode(request_params) + + response = json.load( + urllib.urlopen(self.URL_GEOCODE_JSON + + '?' + + encoded_request_params)) + + return response + + def geocodeAddress(self, + searchtext=None, + city=None, + country=None, + county=None, + district=None, + housenumber=None, + postalcode=None, + state=None, + street=None): + frame = inspect.currentframe() + keys, _, _, values = inspect.getargvalues(frame) + + iterableKeys = iter(keys) + next(iterableKeys) + + params = {} + for key in iterableKeys: + if values[key]: params[key] = values[key] + + if not params: raise NoGeocodingParams() + + return self.geocode(params) + + def extractLngLatFromResponse(response): + location = response['Response']['View'][0]['Result'][0]['Location'] + + longitude = location['DisplayPosition']['Longitude'] + latitude = location['DisplayPosition']['Latitude'] + + return [longitude, latitude] From ed560e9e494d3b7dc249809444bd66676fbad8c5 Mon Sep 17 00:00:00 2001 From: Guido Fioravantti Date: Tue, 3 Nov 2015 18:01:19 +0100 Subject: [PATCH 02/10] Fixes indentation --- lib/python/heremaps/heremapsgeocoder.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/python/heremaps/heremapsgeocoder.py b/lib/python/heremaps/heremapsgeocoder.py index eb5c931..aa3876f 100644 --- a/lib/python/heremaps/heremapsgeocoder.py +++ b/lib/python/heremaps/heremapsgeocoder.py @@ -46,8 +46,8 @@ class Geocoder: app_code = '' def __init__(self, app_id, app_code): - self.app_id = app_id - self.app_code = app_code + self.app_id = app_id + self.app_code = app_code def geocode(self, params): if not set(params.keys()).issubset(set(self.ADDRESS_PARAMS)): From 048a5535dc257631bda433b2803dfd1f96899d04 Mon Sep 17 00:00:00 2001 From: Guido Fioravantti Date: Tue, 3 Nov 2015 18:40:47 +0100 Subject: [PATCH 03/10] Corrects Indentation for heremapsexceptions.py --- lib/python/heremaps/heremapsexceptions.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/python/heremaps/heremapsexceptions.py b/lib/python/heremaps/heremapsexceptions.py index cced7a6..cbbab2e 100644 --- a/lib/python/heremaps/heremapsexceptions.py +++ b/lib/python/heremaps/heremapsexceptions.py @@ -1,11 +1,11 @@ import json class BadGeocodingParams(Exception): - def __init__(self, value): - self.value = value - def __str__(self): - return repr('Bad geocoding params: ' + json.dumps(value)) + def __init__(self, value): + self.value = value + def __str__(self): + return repr('Bad geocoding params: ' + json.dumps(value)) class NoGeocodingParams(Exception): - def __str__(self): - return repr('No params for geocoding specified') + def __str__(self): + return repr('No params for geocoding specified') From 1a21dda52a1f47f129ab3ec2241980ba2d8cf897 Mon Sep 17 00:00:00 2001 From: Guido Fioravantti Date: Tue, 3 Nov 2015 19:21:28 +0100 Subject: [PATCH 04/10] Controls empty response from geocoding service --- lib/python/heremaps/heremapsexceptions.py | 4 ++++ lib/python/heremaps/heremapsgeocoder.py | 10 +++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/python/heremaps/heremapsexceptions.py b/lib/python/heremaps/heremapsexceptions.py index cbbab2e..5e89a1b 100644 --- a/lib/python/heremaps/heremapsexceptions.py +++ b/lib/python/heremaps/heremapsexceptions.py @@ -9,3 +9,7 @@ class BadGeocodingParams(Exception): class NoGeocodingParams(Exception): def __str__(self): return repr('No params for geocoding specified') + +class EmptyGeocoderResponse(Exception): + def __str__(self): + return repr('The request could not be geocoded') diff --git a/lib/python/heremaps/heremapsgeocoder.py b/lib/python/heremaps/heremapsgeocoder.py index aa3876f..1b72fed 100644 --- a/lib/python/heremaps/heremapsgeocoder.py +++ b/lib/python/heremaps/heremapsgeocoder.py @@ -2,7 +2,7 @@ import inspect import json import urllib -from heremapsexceptions import BadGeocodingParams, NoGeocodingParams +from heremapsexceptions import BadGeocodingParams, EmptyGeocoderResponse, NoGeocodingParams class Geocoder: 'A Here Maps Geocoder wrapper for python' @@ -94,8 +94,12 @@ class Geocoder: return self.geocode(params) - def extractLngLatFromResponse(response): - location = response['Response']['View'][0]['Result'][0]['Location'] + def extractLngLatFromResponse(self, response): + view = response['Response']['View'] + + if len(view) is 0: raise EmptyGeocoderResponse() + + location = view[0]['Result'][0]['Location'] longitude = location['DisplayPosition']['Longitude'] latitude = location['DisplayPosition']['Latitude'] From 4a55223ec7ce66970c2852abc433317a18c40435 Mon Sep 17 00:00:00 2001 From: Guido Fioravantti Date: Wed, 4 Nov 2015 11:41:06 +0100 Subject: [PATCH 05/10] Uses **kwargs for geocodeAddress --- lib/python/heremaps/heremapsgeocoder.py | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/lib/python/heremaps/heremapsgeocoder.py b/lib/python/heremaps/heremapsgeocoder.py index 1b72fed..96fcb98 100644 --- a/lib/python/heremaps/heremapsgeocoder.py +++ b/lib/python/heremaps/heremapsgeocoder.py @@ -1,4 +1,3 @@ -import inspect import json import urllib @@ -70,25 +69,10 @@ class Geocoder: return response - def geocodeAddress(self, - searchtext=None, - city=None, - country=None, - county=None, - district=None, - housenumber=None, - postalcode=None, - state=None, - street=None): - frame = inspect.currentframe() - keys, _, _, values = inspect.getargvalues(frame) - - iterableKeys = iter(keys) - next(iterableKeys) - + def geocodeAddress(self, **kwargs): params = {} - for key in iterableKeys: - if values[key]: params[key] = values[key] + for key, value in kwargs.iteritems(): + if value: params[key] = value if not params: raise NoGeocodingParams() From cf99077276c0aa40ef32c7eaf3cc72b1cf7fa7c6 Mon Sep 17 00:00:00 2001 From: Guido Fioravantti Date: Wed, 4 Nov 2015 18:12:54 +0100 Subject: [PATCH 06/10] Refactors and adds unit tests --- lib/python/heremaps/heremapsexceptions.py | 3 + lib/python/heremaps/heremapsgeocoder.py | 31 +++-- lib/python/heremaps/tests/__init__.py | 0 .../heremaps/tests/heremapsgeocoder_tests.py | 110 ++++++++++++++++++ 4 files changed, 134 insertions(+), 10 deletions(-) create mode 100644 lib/python/heremaps/tests/__init__.py create mode 100644 lib/python/heremaps/tests/heremapsgeocoder_tests.py diff --git a/lib/python/heremaps/heremapsexceptions.py b/lib/python/heremaps/heremapsexceptions.py index 5e89a1b..8763ce7 100644 --- a/lib/python/heremaps/heremapsexceptions.py +++ b/lib/python/heremaps/heremapsexceptions.py @@ -1,3 +1,6 @@ +#!/usr/local/bin/python +# -*- coding: utf-8 -*- + import json class BadGeocodingParams(Exception): diff --git a/lib/python/heremaps/heremapsgeocoder.py b/lib/python/heremaps/heremapsgeocoder.py index 96fcb98..754f97a 100644 --- a/lib/python/heremaps/heremapsgeocoder.py +++ b/lib/python/heremaps/heremapsgeocoder.py @@ -1,3 +1,6 @@ +#!/usr/local/bin/python +# -*- coding: utf-8 -*- + import json import urllib @@ -6,8 +9,8 @@ from heremapsexceptions import BadGeocodingParams, EmptyGeocoderResponse, NoGeoc class Geocoder: 'A Here Maps Geocoder wrapper for python' - URL_GEOCODE_JSON = 'http://geocoder.cit.api.here.com/6.2/geocode.json' - MAX_RESULTS = 1 + URL_GEOCODE_JSON = 'http://geocoder.api.here.com/6.2/geocode.json' + DEFAULT_MAXRESULTS = 1 ADDRESS_PARAMS = [ 'city', @@ -43,19 +46,31 @@ class Geocoder: app_id = '' 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_code = app_code + self.maxresults = maxresults def geocode(self, params): if not set(params.keys()).issubset(set(self.ADDRESS_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 = { 'app_id' : self.app_id, 'app_code' : self.app_code, - 'maxresults' : self.MAX_RESULTS, + 'maxresults' : self.maxresults, 'gen' : '9' } request_params.update(params) @@ -78,12 +93,8 @@ class Geocoder: return self.geocode(params) - def extractLngLatFromResponse(self, response): - view = response['Response']['View'] - - if len(view) is 0: raise EmptyGeocoderResponse() - - location = view[0]['Result'][0]['Location'] + def extractLngLatFromResult(self, result): + location = result['Location'] longitude = location['DisplayPosition']['Longitude'] latitude = location['DisplayPosition']['Latitude'] diff --git a/lib/python/heremaps/tests/__init__.py b/lib/python/heremaps/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lib/python/heremaps/tests/heremapsgeocoder_tests.py b/lib/python/heremaps/tests/heremapsgeocoder_tests.py new file mode 100644 index 0000000..4819822 --- /dev/null +++ b/lib/python/heremaps/tests/heremapsgeocoder_tests.py @@ -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() From 27fcc15bed1ec02694838eab09c0624d5c202362 Mon Sep 17 00:00:00 2001 From: Guido Fioravantti Date: Wed, 4 Nov 2015 18:20:39 +0100 Subject: [PATCH 07/10] Code refactor --- lib/python/heremaps/tests/heremapsgeocoder_tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/python/heremaps/tests/heremapsgeocoder_tests.py b/lib/python/heremaps/tests/heremapsgeocoder_tests.py index 4819822..f22a84f 100644 --- a/lib/python/heremaps/tests/heremapsgeocoder_tests.py +++ b/lib/python/heremaps/tests/heremapsgeocoder_tests.py @@ -55,7 +55,7 @@ class GeocoderTestCase(unittest.TestCase): } }, "Address":{ - "Label":"Calle de Eloy Gonzalo, Madrid (Madrid), España", + "Label":"Calle de Eloy Gonzalo, Madrid, España", "Country":"ESP", "State":"Comunidad de Madrid", "County":"Madrid", From 1af19f7aacad4a8f44cc14666f90fd1f29c31bf2 Mon Sep 17 00:00:00 2001 From: Guido Fioravantti Date: Wed, 4 Nov 2015 18:22:04 +0100 Subject: [PATCH 08/10] Code refactor --- lib/python/heremaps/tests/heremapsgeocoder_tests.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/python/heremaps/tests/heremapsgeocoder_tests.py b/lib/python/heremaps/tests/heremapsgeocoder_tests.py index f22a84f..58d3ae2 100644 --- a/lib/python/heremaps/tests/heremapsgeocoder_tests.py +++ b/lib/python/heremaps/tests/heremapsgeocoder_tests.py @@ -4,7 +4,9 @@ import unittest from heremaps import heremapsgeocoder -from heremaps.heremapsexceptions import BadGeocodingParams, EmptyGeocoderResponse, NoGeocodingParams +from heremaps.heremapsexceptions import BadGeocodingParams +from heremaps.heremapsexceptions import EmptyGeocoderResponse +from heremaps.heremapsexceptions import NoGeocodingParams from secrets import * From af5dc3836173334795e89ec3b3460ca0f9bdff2b Mon Sep 17 00:00:00 2001 From: Guido Fioravantti Date: Wed, 4 Nov 2015 18:23:08 +0100 Subject: [PATCH 09/10] Code refactor --- lib/python/heremaps/heremapsgeocoder.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/python/heremaps/heremapsgeocoder.py b/lib/python/heremaps/heremapsgeocoder.py index 754f97a..d1b4774 100644 --- a/lib/python/heremaps/heremapsgeocoder.py +++ b/lib/python/heremaps/heremapsgeocoder.py @@ -4,7 +4,9 @@ import json import urllib -from heremapsexceptions import BadGeocodingParams, EmptyGeocoderResponse, NoGeocodingParams +from heremaps.heremapsexceptions import BadGeocodingParams +from heremaps.heremapsexceptions import EmptyGeocoderResponse +from heremaps.heremapsexceptions import NoGeocodingParams class Geocoder: 'A Here Maps Geocoder wrapper for python' From 3552f27de7557ea891846b87d67c43b9ec45ff67 Mon Sep 17 00:00:00 2001 From: Guido Fioravantti Date: Thu, 5 Nov 2015 11:12:10 +0100 Subject: [PATCH 10/10] Code refactor --- lib/python/heremaps/heremapsexceptions.py | 4 ++ lib/python/heremaps/heremapsgeocoder.py | 20 ++++++---- .../heremaps/tests/heremapsgeocoder_tests.py | 38 +++++++++++++------ 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/lib/python/heremaps/heremapsexceptions.py b/lib/python/heremaps/heremapsexceptions.py index 8763ce7..6416872 100644 --- a/lib/python/heremaps/heremapsexceptions.py +++ b/lib/python/heremaps/heremapsexceptions.py @@ -16,3 +16,7 @@ class NoGeocodingParams(Exception): class EmptyGeocoderResponse(Exception): def __str__(self): return repr('The request could not be geocoded') + +class MalformedResult(Exception): + def __str__(self): + return repr('Result structure is malformed') diff --git a/lib/python/heremaps/heremapsgeocoder.py b/lib/python/heremaps/heremapsgeocoder.py index d1b4774..696c79f 100644 --- a/lib/python/heremaps/heremapsgeocoder.py +++ b/lib/python/heremaps/heremapsgeocoder.py @@ -7,12 +7,14 @@ import urllib from heremaps.heremapsexceptions import BadGeocodingParams from heremaps.heremapsexceptions import EmptyGeocoderResponse from heremaps.heremapsexceptions import NoGeocodingParams +from heremaps.heremapsexceptions import MalformedResult class Geocoder: 'A Here Maps Geocoder wrapper for python' URL_GEOCODE_JSON = 'http://geocoder.api.here.com/6.2/geocode.json' DEFAULT_MAXRESULTS = 1 + DEFAULT_GEN = 9 ADDRESS_PARAMS = [ 'city', @@ -50,16 +52,17 @@ class Geocoder: app_code = '' maxresults = '' - def __init__(self, app_id, app_code, maxresults=DEFAULT_MAXRESULTS): + def __init__(self, app_id, app_code, maxresults=DEFAULT_MAXRESULTS, gen=DEFAULT_GEN): self.app_id = app_id self.app_code = app_code self.maxresults = maxresults + self.gen = gen def geocode(self, params): if not set(params.keys()).issubset(set(self.ADDRESS_PARAMS)): raise BadGeocodingParams(params) - response = self.performRequest(params) + response = self.perform_request(params) try: results = response['Response']['View'][0]['Result'] @@ -68,12 +71,12 @@ class Geocoder: return results - def performRequest(self, params): + def perform_request(self, params): request_params = { 'app_id' : self.app_id, 'app_code' : self.app_code, 'maxresults' : self.maxresults, - 'gen' : '9' + 'gen' : self.gen } request_params.update(params) @@ -86,7 +89,7 @@ class Geocoder: return response - def geocodeAddress(self, **kwargs): + def geocode_address(self, **kwargs): params = {} for key, value in kwargs.iteritems(): if value: params[key] = value @@ -95,8 +98,11 @@ class Geocoder: return self.geocode(params) - def extractLngLatFromResult(self, result): - location = result['Location'] + def extract_lng_lat_from_result(self, result): + try: + location = result['Location'] + except KeyError: + raise MalformedResult() longitude = location['DisplayPosition']['Longitude'] latitude = location['DisplayPosition']['Latitude'] diff --git a/lib/python/heremaps/tests/heremapsgeocoder_tests.py b/lib/python/heremaps/tests/heremapsgeocoder_tests.py index 58d3ae2..6fbd1c0 100644 --- a/lib/python/heremaps/tests/heremapsgeocoder_tests.py +++ b/lib/python/heremaps/tests/heremapsgeocoder_tests.py @@ -7,6 +7,7 @@ from heremaps import heremapsgeocoder from heremaps.heremapsexceptions import BadGeocodingParams from heremaps.heremapsexceptions import EmptyGeocoderResponse from heremaps.heremapsexceptions import NoGeocodingParams +from heremaps.heremapsexceptions import MalformedResult from secrets import * @@ -40,8 +41,8 @@ class GeocoderTestCase(unittest.TestCase): "LocationType":"address", "DisplayPosition":{ "Latitude":40.43433, - "Longitude":-3.70126 - }, + "Longitude":-3.70126 + }, "NavigationPosition":[{ "Latitude":40.43433, "Longitude":-3.70126 @@ -86,27 +87,40 @@ class GeocoderTestCase(unittest.TestCase): 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( + def test_geocode_address_with_valid_params(self): + self.geocoder.perform_request = lambda x: self.GOOD_RESPONSE + response = self.geocoder.geocode_address( searchtext='Calle Eloy Gonzalo 27', city='Madrid', country='España') - def test_geocodeAddress_with_invalid_params(self): + def test_geocode_address_with_invalid_params(self): with self.assertRaises(BadGeocodingParams): - self.geocoder.geocodeAddress( + self.geocoder.geocode_address( searchtext='Calle Eloy Gonzalo 27', manolo='escobar') - def test_geocodeAddress_with_no_params(self): + def test_geocode_address_with_no_params(self): with self.assertRaises(NoGeocodingParams): - self.geocoder.geocodeAddress() + self.geocoder.geocode_address() - def test_geocodeAddress_empty_response(self): - self.geocoder.performRequest = lambda x: self.EMPTY_RESPONSE + def test_geocode_address_empty_response(self): + self.geocoder.perform_request = lambda x: self.EMPTY_RESPONSE with self.assertRaises(EmptyGeocoderResponse): - self.geocoder.geocodeAddress(searchtext='lkajfñlasjfñ') + self.geocoder.geocode_address(searchtext='lkajfñlasjfñ') + + def test_extract_lng_lat_from_result(self): + result = self.GOOD_RESPONSE['Response']['View'][0]['Result'][0] + coordinates = self.geocoder.extract_lng_lat_from_result(result) + + self.assertEqual(coordinates[0], -3.70126) + self.assertEqual(coordinates[1], 40.43433) + + def test_extract_lng_lat_from_result_with_malformed_result(self): + result = {'manolo':'escobar'} + + with self.assertRaises(MalformedResult): + self.geocoder.extract_lng_lat_from_result(result) if __name__ == '__main__': main()