def test_get_coordinates_bad_http_status(self, mocked_request): a = MagicMock(status=300) mocked_request.return_value.__enter__.return_value = a coords = get_coordinates('test', 'test') self.assertEqual(coords, (0, 0))
def test_get_coordinates_bad_json_response(self, mocked_request): a = MagicMock(status=200) b = MagicMock() b.decode.return_value = '{"test":1}' a.read.return_value = b mocked_request.return_value.__enter__.return_value = a coords = get_coordinates('test', 'test') self.assertEqual(coords, (0, 0))
def test_get_coordinates_missing_lon(self, mocked_request): response_json = '[{"place_id": "94242929", "licence": "Data © OpenStreetMap contributors, ODbL 1.0. ' \ 'https://osm.org/copyright", "osm_type": "way", "osm_id": "114823817", "boundingbox": ' \ '["51.1525635", "51.1614997", "-1.4508447", "-1.4408037"], "lat": 51.1576661, ' \ '"display_name": "Test, Test Valley, Hampshire, South East, England, SO20 6BD, UK", "class": ' \ '"waterway", "type": "river", "importance": 0.46204844474975}]' a = MagicMock(status=200) b = MagicMock() b.decode.return_value = response_json a.read.return_value = b mocked_request.return_value.__enter__.return_value = a coords = get_coordinates('test', 'test') self.assertEqual(coords, (0, 0))
geocoded = {} # Geocode all unique countries in the list countrycoords = helpers.geocode_list(data.Country.unique(), args.referer) # Attempt to geocode each wine based on it's region for i in data[['Region', 'Country']].itertuples(): if isinstance(i.Region, str): region = i.Region + ' ' + i.Country # Check the cache first before querying Nominatim if region not in regions: coords = helpers.get_coordinates(region, args.referer) # Comply with https://operations.osmfoundation.org/policies/nominatim/ time.sleep(1) regions[region] = coords else: coords = regions[region] # Fall back to using the country coordinates if the geocoder fails geocoded[i.Index] = coords if coords != ( 0, 0) else countrycoords[i.Country] else: