예제 #1
0
 def test_collection_get(self):
     """Ensure 'get' performs a key based lookup"""
     locations = LocationCollection(self.batch_response)
     self.assertEqual(locations.get("3101 patterson ave, richmond, va").coords,
             (-77.477400571429, 37.560890255102))
     # Case sensitive on the specific query
     self.assertRaises(KeyError, locations.get,
             "3101 Patterson Ave, richmond, va")
예제 #2
0
    def test_collection(self):
        """Ensure that the LocationCollection stores as a list of Locations"""
        self.assertTrue(isinstance(self.batch_response, dict))
        locations = LocationCollection(self.batch_response["results"])

        self.assertTrue(isinstance(locations[0], Location))

        locations = LocationCollection(self.batch_reverse_response["results"])
        self.assertTrue(isinstance(locations[0], Location))
예제 #3
0
    def test_collection_get(self):
        """Ensure 'get' performs a key based lookup"""
        locations = LocationCollection(self.batch_response["results"])
        self.assertEqual(
            locations.get("3101 patterson ave, richmond, va").coords,
            (37.560890255102, -77.477400571429),
        )

        # Case sensitive on the specific query
        self.assertRaises(KeyError, locations.get,
                          "3101 Patterson Ave, richmond, va")

        locations = LocationCollection(self.batch_reverse_response["results"])

        # The rendred query string value is acceptable
        self.assertEqual(
            locations.get("37.538758,-77.433594").coords,
            (37.538758, -77.433594))
        # A tuple of floats is acceptable
        self.assertEqual(
            locations.get((37.538758, -77.433594)).coords,
            (37.538758, -77.433594))
        # If it can be coerced to a float it is acceptable
        self.assertEqual(
            locations.get(("37.538758", "-77.433594")).coords,
            (37.538758, -77.433594))

        # This is unacceptable
        self.assertRaises(ValueError, locations.get,
                          ("37.538758 N", "-77.433594 W"))
예제 #4
0
    def test_collection_magic_get_item(self):
        """
        Ensure LocationCollection[key] performs a key based lookup for an index corresponding with key
        or performs an index lookup
        """
        locations = LocationCollection(self.batch_response["results"])
        # Works with normal list indexing
        self.assertEqual(locations[1].coords,
                         (37.554895702703, -77.457561054054))
        self.assertRaises(IndexError, locations.__getitem__, len(locations))

        self.assertEqual(
            locations["3101 patterson ave, richmond, va"].coords,
            (37.560890255102, -77.477400571429),
        )

        # Case sensitive on the specific query
        self.assertRaises(IndexError, locations.__getitem__,
                          "3101 Patterson Ave, richmond, va")

        locations = LocationCollection(
            self.batch_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(IndexError, locations.__getitem__, {
            "street": "1109 N Highland St",
            "city": "Arlington"
        })

        locations = LocationCollection(self.batch_reverse_response["results"])
        # The rendered query string value is acceptable
        self.assertEqual(locations["37.538758,-77.433594"].coords,
                         (37.538758, -77.433594))
        # A tuple of floats is acceptable
        self.assertEqual(locations[(37.538758, -77.433594)].coords,
                         (37.538758, -77.433594))
        # If it can be coerced to a float it is acceptable
        self.assertEqual(locations[("37.538758", "-77.433594")].coords,
                         (37.538758, -77.433594))

        # This is unacceptable
        self.assertRaises(ValueError, locations.__getitem__,
                          ("37.538758 N", "-77.433594 W"))
예제 #5
0
    def test_collection_coords(self):
        """Ensure the coords property returns a list of suitable tuples"""
        locations = LocationCollection(self.batch_response['results'])
        self.assertEqual(locations.coords, [
                (37.560890255102, -77.477400571429),
                (37.554895702703, -77.457561054054),
                None,
        ])

        # Do the same with the order changed
        locations = LocationCollection(self.batch_response['results'], order='lng')
        self.assertEqual(locations.coords, [
                (-77.477400571429, 37.560890255102),
                (-77.457561054054, 37.554895702703),
                None,
        ])
예제 #6
0
 def test_collection_addresses(self):
     """Ensure that formatted addresses are returned"""
     locations = LocationCollection(self.batch_response['results'])
     self.assertEqual(locations.formatted_addresses, [
         "3101 Patterson Ave, Richmond VA, 23221",
         "1657 W Broad St, Richmond VA, 23220", ""
     ])
예제 #7
0
    def test_collection_get(self):
        """Ensure 'get' performs a key based lookup"""
        locations = LocationCollection(self.batch_response["results"])
        self.assertEqual(
            locations.get("3101 patterson ave, richmond, va").coords,
            (37.560890255102, -77.477400571429),
        )

        # Case sensitive on the specific query
        self.assertRaises(KeyError, locations.get, "3101 Patterson Ave, richmond, va")

        locations = LocationCollection(self.batch_reverse_response["results"])

        # The rendred query string value is acceptable
        self.assertEqual(
            locations.get("37.538758,-77.433594").coords, (37.538758, -77.433594)
        )
        # A tuple of floats is acceptable
        self.assertEqual(
            locations.get((37.538758, -77.433594)).coords, (37.538758, -77.433594)
        )
        # If it can be coerced to a float it is acceptable
        self.assertEqual(
            locations.get(("37.538758", "-77.433594")).coords, (37.538758, -77.433594)
        )

        # This is unacceptable
        self.assertRaises(ValueError, locations.get, ("37.538758 N", "-77.433594 W"))
예제 #8
0
 def batch_reverse(self, points, **kwargs):
     """
     Method for identifying the addresses from a list of 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)
     logger.debug(response)
     return LocationCollection(response.json()['results'])
예제 #9
0
 def batch_geocode(self, addresses, **kwargs):
     """
     Returns an Address dictionary with the components of the queried
     address.
     """
     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)
     return LocationCollection(response.json()['results'])
예제 #10
0
    def test_collection_get_default(self):
        """Ensure 'get' returns default if key-based lookup fails without an error"""
        locations = LocationCollection(self.batch_response["results"])
        self.assertEqual(locations.get("wrong original query lookup"), None)

        locations = LocationCollection(
            self.batch_components_response["results"])
        self.assertEqual(
            locations.get(
                {
                    "street": "wrong street",
                    "city": "Arlington",
                    "state": "VA"
                }, "test"), "test")

        locations = LocationCollection(self.batch_reverse_response["results"])

        self.assertEqual(locations.get((5, 5), None), None)
        # If it can be coerced to a float it is acceptable
        self.assertEqual(locations.get(("-4", "-5"), 5), 5)

        # This is still unacceptable
        self.assertRaises(ValueError, locations.get,
                          ("37.538758 N", "-77.433594 W"))
예제 #11
0
    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")
예제 #12
0
    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")
예제 #13
0
    def test_collection_get(self):
        """Ensure 'get' performs a key based lookup"""
        locations = LocationCollection(self.batch_response["results"])
        self.assertEqual(
            locations.get("3101 patterson ave, richmond, va").coords,
            (37.560890255102, -77.477400571429),
        )

        # Case sensitive on the specific query
        self.assertRaises(KeyError, locations.get, "3101 Patterson Ave, richmond, va")

        locations = LocationCollection(self.batch_components_response["results"])
        self.assertEqual(locations.get({
            "street": "1109 N Highland St",
            "city": "Arlington",
            "state": "VA"
        }).coords, (38.886672, -77.094735))

        # Requires all fields used for lookup
        self.assertRaises(KeyError, locations.get,
                          {"street": "1109 N Highland St",
                           "city": "Arlington"})

        locations = LocationCollection(self.batch_reverse_response["results"])

        # The rendred query string value is acceptable
        self.assertEqual(
            locations.get("37.538758,-77.433594").coords, (37.538758, -77.433594)
        )
        # A tuple of floats is acceptable
        self.assertEqual(
            locations.get((37.538758, -77.433594)).coords, (37.538758, -77.433594)
        )
        # If it can be coerced to a float it is acceptable
        self.assertEqual(
            locations.get(("37.538758", "-77.433594")).coords, (37.538758, -77.433594)
        )

        # This is unacceptable
        self.assertRaises(ValueError, locations.get, ("37.538758 N", "-77.433594 W"))