Last active
August 29, 2015 14:00
-
-
Save mhl/11188804 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pygeocoder import Geocoder | |
def our_geocoder(country, query): | |
print "### in our_geocoder" | |
components = 'country:' + country | |
response = Geocoder.geocode(query, components=components) | |
return [ | |
{ | |
"address": entry['formatted_address'], | |
"latitude": entry['geometry']['location']['lat'], | |
"longitude": entry['geometry']['location']['lng'], | |
} for entry in response.raw] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mock==1.0.1 | |
pygeocoder==1.2.2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import unittest | |
import pygeocoder | |
from geocoder import our_geocoder | |
from mock import patch, Mock | |
def fake_geocode(q, **kwargs): | |
print "### in fake_geocode" | |
if q == "Place that does not exist": | |
raw_result = [] | |
elif q == "Cape Town": | |
raw_result = [ | |
{'geometry': | |
{'location': { | |
"lat": -33.924741, | |
"lng": 18.4241074 | |
}}, | |
'address_components': [], | |
'formatted_address': "The Real Cape Town"} | |
] | |
else: | |
raise Exception, "Not yet faked..." | |
return Mock(raw=raw_result) | |
class GeocoderTests(unittest.TestCase): | |
@patch.object(pygeocoder.Geocoder, 'geocode', side_effect=fake_geocode) | |
def test_no_match(self, mocked_class_method): | |
results = our_geocoder(country='za', | |
query="Cape Town") | |
self.assertEqual(results[0]['address'], | |
'The Real Cape Town') | |
mocked_class_method.assert_called_once_with( | |
'Cape Town', components='country:za') | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment