def test_raises_geocoder_error_when_postcode_lookup_fails_with_timeout( self, lookup_mock): lookup_mock.side_effect = requests.exceptions.ConnectTimeout() with self.assertRaisesRegexp(geocoder.GeocoderError, "Caused by ConnectTimeout"): geocoder.geocode(self.postcode)
def test_raises_postcode_not_found_error_when_postcode_lookup_cannot_find_the_postcode( self, lookup_mock): lookup_mock.return_value = {"status": 200, "result": None} with self.assertRaises(geocoder.PostcodeNotFound) as not_found_error: geocoder.geocode(self.postcode) self.assertEqual(not_found_error.exception.postcode, "sw1a1aa")
def test_raises_geocoder_error_when_postcode_lookup_fails_with_client_error( self, lookup_mock): lookup_mock.return_value = { "status": 400, "message": "Invalid request" } with self.assertRaisesRegexp(geocoder.GeocoderError, "Invalid request"): geocoder.geocode(self.postcode)
def test_raises_geocoder_error_when_postcode_lookup_fails_with_server_error( self, lookup_mock): lookup_mock.return_value = { "status": 500, "message": "Bad things happened" } with self.assertRaisesRegexp(geocoder.GeocoderError, "Bad things happened"): geocoder.geocode(self.postcode)
def test_geocode_strips_spaces_before_calling_external_api(self): responses.add( responses.GET, "https://api.postcodes.io/postcodes/?q=sw1a1aa&limit=1", match_querystring=True, json=self.good_result, status=200, ) geocoder.geocode("SW1A 1AA") self.assertEqual(len(responses.calls), 1)
def test_raises_postcode_not_found_error_when_postcode_lookup_returns_404( self, lookup_mock): lookup_mock.return_value = { "status": 404, "error": "Postcode not found" } with self.assertRaises(geocoder.PostcodeNotFound) as not_found_error: geocoder.geocode(self.postcode) self.assertEqual(not_found_error.exception.postcode, "sw1a1aa")
def test_geocode_calls_postcode_lookup(self, lookup_mock): lookup_mock.return_value = self.good_result result = geocoder.geocode(self.postcode) self.assertEqual(result.postcode, "sw1a1aa") self.assertEqual(result.latitude, 51.501009) self.assertEqual(result.longitude, -0.141588)