def test_get_results_returns_processed_response(self, process_response, get_url, urlopen): params = dict(address="New York", sensor="false") g = Geocoder(client_id=self.client_id, private_key=self.private_key) results = g._get_results(params) self.assertEqual(process_response.return_value, results) process_response.assert_called_once_with(urlopen.return_value, get_url.return_value)
def test_get_results_opens_url_with_request_url_and_timeout(self, get_url, urlopen): params = dict(address="New York", sensor="false") g = Geocoder(client_id=self.client_id, private_key=self.private_key) g._get_results(params) get_url.assert_called_once_with(params) urlopen.assert_called_once_with(get_url.return_value, timeout=g.TIMEOUT_SECONDS)
def test_process_response_raises_geocoder_error_when_status_not_ok(self): g = Geocoder() response = StringIO(no_results_response) url = "http://www.example.com" with self.assertRaises(GeocodeError) as ctx: g._process_response(response, url) self.assertEqual(GeocodeError.G_GEO_ZERO_RESULTS, ctx.exception.status) self.assertEqual(url, ctx.exception.url)
def test_get_request_url_returns_url_with_params_when_not_premier(self, urlencode): params = dict(address="New York", sensor="false") urlencode.return_value = "address=New+York&sensor=false" g = Geocoder() request_url = g._get_request_url(params) self.assertEqual(g.GOOGLE_API_URL + urlencode.return_value, request_url) urlencode.assert_called_once_with(params)
def test_gets_premier_url_when_supplied_credentials(self, get_premier_url, urlencode): params = dict(address="New York", sensor="false") urlencode.return_value = "address=New+York&sensor=false" g = Geocoder(client_id=self.client_id, private_key=self.private_key) request_url = g._get_request_url(params) self.assertEqual(get_premier_url.return_value, request_url) expected_url_to_pass = g.GOOGLE_API_URL + urlencode.return_value get_premier_url.assert_called_once_with(expected_url_to_pass)
def test_process_response_returns_results_when_status_is_ok(self): success_json = """{ "results" : [ {"one": "the one"}, {"two": "the two"} ], "status" : "OK" }""" response = StringIO(success_json) url = "http://www.example.com" expected_result = [dict(one="the one"), dict(two="the two")] g = Geocoder() self.assertEqual(expected_result, g._process_response(response, url))
def test_get_premier_url_adds_client_and_signature_to_query_string(self): g = Geocoder(client_id=self.client_id, private_key=self.private_key) premier_url = g._get_premier_url(self.base_url) expected_url = self.base_url + "&client={0}&signature={1}".format(self.client_id, self.known_signature) self.assertEqual(expected_url, premier_url)
def test_generate_signature_generates_proper_signature(self): # this is using a known signature from Google Documentation. # http://code.google.com/apis/maps/documentation/webservices/index.html#SignatureExamples g = Geocoder(client_id=self.client_id, private_key=self.private_key) base_url = self.base_url + "&client=clientID" self.assertEqual(self.known_signature, g._generate_signature(base_url, self.private_key))
def test(): address = "" ggeocoder = Geocoder() for d in ggeocoder.geocode(address).data: pprint(dict(orig=address, result=d.data))
def geocodeit(address, fw): ggeocoder = Geocoder() for d in ggeocoder.geocode(address).data: print json.dumps(dict(orig=address, result=d.data)) fw.write(json.dumps(dict(orig=address, result=d.data)) + '\n')