def test_dict_collection_magic_get_item(self): """Ensure LocationCollectionDict[key] performs a key-based lookup, raising a KeyError on any missing key""" locations = LocationCollectionDict(self.batch_dict_response["results"]) self.assertEqual( locations["3101 patterson ave, richmond, va"].coords, (37.560890255102, -77.477400571429), ) # Ensure 'get' is able to look up by dictionary key self.assertEqual( locations["1"].coords, (37.560890255102, -77.477400571429), ) # Case sensitive on the specific query self.assertRaises(KeyError, locations.__getitem__, "3101 Patterson Ave, richmond, va") locations = LocationCollectionDict( self.batch_dict_components_response["results"]) self.assertEqual( locations[{ "street": "1109 N Highland St", "city": "Arlington", "state": "VA" }].coords, (38.886672, -77.094735)) # Requires all fields used for lookup self.assertRaises(KeyError, locations.__getitem__, { "street": "1109 N Highland St", "city": "Arlington" })
def test_dict_collection_addresses(self): """Ensure that formatted addresses are returned""" locations = LocationCollectionDict(self.batch_dict_response["results"]) self.assertEqual( locations.formatted_addresses, { "1": "3101 Patterson Ave, Richmond VA, 23221", "2": "1657 W Broad St, Richmond VA, 23220", "3": "", }, )
def test_dict_collection_coords(self): """Ensure the coords property returns a list of suitable tuples""" locations = LocationCollectionDict(self.batch_dict_response["results"]) self.assertEqual( locations.coords, { "1": (37.560890255102, -77.477400571429), "2": (37.554895702703, -77.457561054054), "3": None }, ) # Do the same with the order changed locations = LocationCollectionDict(self.batch_dict_response["results"], order="lng") self.assertEqual( locations.coords, { "1": (-77.477400571429, 37.560890255102), "2": (-77.457561054054, 37.554895702703), "3": None }, )
def test_dict_collection_magic_contains(self): """Ensure "key in LocationDict(...)" checks if key would return a result with LocationDict(...)[key]""" locations = LocationCollectionDict(self.batch_dict_response["results"]) self.assertTrue("3101 patterson ave, richmond, va" in locations) # Ensure it also works with look up by dictionary key self.assertTrue("1" in locations) # Case sensitive on the specific query self.assertFalse("3101 Patterson Ave, richmond, va" in locations) locations = LocationCollectionDict( self.batch_dict_components_response["results"]) self.assertTrue({ "street": "1109 N Highland St", "city": "Arlington", "state": "VA" } in locations) # Requires all fields used for lookup self.assertFalse({ "street": "1109 N Highland St", "city": "Arlington" } in locations)
def test_dict_collection_get_default(self): """Ensure 'get' returns default if key-based lookup fails without an error""" locations = LocationCollectionDict(self.batch_dict_response["results"]) self.assertEqual(locations.get("wrong original query lookup"), None) self.assertEqual(locations.get("25"), None) locations = LocationCollectionDict( self.batch_dict_components_response["results"]) self.assertEqual( locations.get( { "street": "wrong street", "city": "Arlington", "state": "VA" }, "test"), "test")
def batch_reverse(self, points, **kwargs): """ Method for identifying the addresses from a list of lat/lng tuples or dict mapping of arbitrary keys to lat/lng tuples """ fields = ",".join(kwargs.pop("fields", [])) response = self._req("post", verb="reverse", params={"fields": fields}, data=json_points(points)) if response.status_code != 200: return error_response(response) results = response.json()["results"] if isinstance(results, list): return LocationCollection(results) elif isinstance(results, dict): return LocationCollectionDict(results) else: raise Exception("Error: Unknown API change")
def batch_geocode(self, addresses, **kwargs): """ Returns an Address dictionary with the components of the queried address. Accepts either a list or dictionary of addresses """ fields = ",".join(kwargs.pop("fields", [])) response = self._req( "post", verb="geocode", params={"fields": fields}, data=json.dumps(addresses), ) if response.status_code != 200: return error_response(response) results = response.json()["results"] if isinstance(results, list): return LocationCollection(results) elif isinstance(results, dict): return LocationCollectionDict(results) else: raise Exception("Error: Unknown API change")
def test_dict_collection_get(self): """Ensure 'get' performs a key based lookup""" locations = LocationCollectionDict(self.batch_dict_response["results"]) self.assertEqual( locations.get("3101 patterson ave, richmond, va").coords, (37.560890255102, -77.477400571429), ) # Ensure 'get' is able to look up by dictionary key self.assertEqual( locations.get("1").coords, (37.560890255102, -77.477400571429), ) locations = LocationCollectionDict( self.batch_dict_components_response["results"]) self.assertEqual( locations.get({ "street": "1109 N Highland St", "city": "Arlington", "state": "VA" }).coords, (38.886672, -77.094735))
def test_dict_collection(self): """Ensure that the LocationCollectionDict stores as a dict of Locations""" self.assertTrue(isinstance(self.batch_dict_response, dict)) locations = LocationCollectionDict(self.batch_dict_response["results"]) self.assertTrue(isinstance(locations["1"], Location))