def test_reverse_geo_lookup_bad_data(self, req_mock): req_mock.get( 'http://service-location-service:1555/v1/location/reversegeo', text='{"addressComponents": {}}') loc = Location(coordinates=(50.0, 8.0)) result = loc._reverse_geo_lookup() self.assertIsNone(loc._text)
def test_forward_geo_lookup_ok(self, req_mock): req_mock.get('http://service-location-service:1555/v1/location/geo', text='{"lat": 50.0, "lng": 8.0}') loc = Location('some place') result = loc._forward_geo_lookup() self.assertEqual(result, None) self.assertEqual(loc._coordinates, (50.0, 8.0))
def test_reverse_geo_lookup_ok(self, req_mock): req_mock.get( 'http://service-location-service:1555/v1/location/reversegeo', text='{"addressComponents": {"city": "some place"}}') loc = Location(coordinates=(50.0, 8.0)) result = loc._reverse_geo_lookup() self.assertEqual(result, None) self.assertEqual(loc._text, 'some place')
def test_timezone_with_data_from_location_service(self, req_mock): req_mock.get( 'http://service-location-service:1555/v1/location/geo', text= '{"lat": 50.0, "lng": 8.0, "timeZone": "Europe/Berlin", "address":{"addressComponents": {"city": "some place"}}}' ) loc = Location('some place') loc._forward_geo_lookup() self.assertEqual(loc.timezone, 'Europe/Berlin') self.assertEqual(loc.text, 'some place')
def test_auth_header(self, req_mock, session_mock, config_mock, log_mock): config_mock['service-location'] = {'auth_header': 'true'} req_mock.json = { "context": { "attributes": { 'location': 'Berlin' }, "intent": "TELEKOM_Demo_Intent", "locale": "de", "tokens": { "cvi": "eyJ123" }, "clientAttributes": {} } } loc = Location('some place') coordinates = loc.coordinates log_mock.assert_called_with( 'Adding CVI token to authorization header.') session_mock.__enter__().get.assert_called_once_with( 'http://service-location-service:1555/v1/location/geo', headers={ 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': 'Bearer eyJ123' }, params={'text': 'some place'})
def test_coordinates_text(self, req_mock): req_mock.get('http://service-location-service:1555/v1/location/geo', text='''{ "lat": 50.0, "lng": 8.0, "timeZone": "Europe/Berlin", "location":{ "addressComponents": {"city": "some place"} }}''') self.assertEqual(Location('some place').coordinates, (50.0, 8.0))
def test_reverse_geo_lookup_server_error(self, req_mock, log_mock): req_mock.get( 'http://service-location-service:1555/v1/location/reversegeo', text='{}', status_code=500) loc = Location(coordinates=(50.0, 8.0)) with self.assertRaises(BadHttpResponseCodeException): loc._reverse_geo_lookup() req_mock.get( 'http://service-location-service:1555/v1/location/reversegeo', text='not a json') loc = Location('some place', coordinates=(50.0, 8.0)) loc._reverse_geo_lookup() self.assertEqual(loc._text, 'some place')
def test_forward_geo_lookup_bad_data(self, req_mock): req_mock.get('http://service-location-service:1555/v1/location/geo', text='{}') loc = Location('some place') self.assertEqual(loc._text, 'some place') loc = Location(language='de') with self.assertRaises(ValueError): loc._forward_geo_lookup()
def test_text_present(self): self.assertEqual(Location('some place').text, 'some place')
def test_text_lookup_failed(self): loc = Location(coordinates=(50.0, 8.0)) loc._reverse_lookup_failed = True from skill_sdk.services.location import GeoLookupError with self.assertRaises(GeoLookupError): loc.text
def test_coordinates_present(self): self.assertEqual( Location(coordinates=(50.0, 8.0)).coordinates, (50.0, 8.0))
def test_reverse_geo_lookup_no_coordinates(self): loc = Location('some place') with self.assertRaises(ValueError): loc._reverse_geo_lookup()
def test_reverse_geo_lookup_failed_before(self): loc = Location(coordinates=(50.0, 8.0)) loc._reverse_lookup_failed = True result = loc._reverse_geo_lookup() self.assertEqual(result, None) self.assertEqual(loc._text, None)
def test_forward_geo_lookup_server_error(self, req_mock): req_mock.get('http://service-location-service:1555/v1/location/geo', status_code=500) loc = Location('some place') with self.assertRaises(BadHttpResponseCodeException): loc._forward_geo_lookup()
def test_forward_geo_lookup_not_valid_json(self, req_mock): req_mock.get('http://service-location-service:1555/v1/location/geo', text='not a json') loc = Location('some place') loc._forward_geo_lookup() self.assertEqual(loc._text, 'some place')
def test_text_coordinates(self, req_mock): req_mock.get( 'http://service-location-service:1555/v1/location/reversegeo', text='{"addressComponents": {"city": "some place"}}') self.assertEqual(Location(coordinates=(50.0, 8.0)).text, 'some place')
def test_coordinates_lookup_failed(self, req_mock): loc = Location('some place') loc._forward_lookup_failed = True self.assertIsNone(loc.coordinates)
def test_timezone_with_no_data_from_location_service(self, req_mock): req_mock.get('http://service-location-service:1555/v1/location/geo', text='{"lat": 50.0, "lng": 8.0}') loc = Location('some place') loc._forward_geo_lookup() self.assertEqual(loc.timezone, 'Europe/Berlin')