Exemplo n.º 1
0
    def test_azavea_office_geocodes_correctly(self):
        res = geocode(MockGeocodeRequest('340 n 12th st philadelphia'))

        self.assertNotIn('error', res,
                         'The response should not have an error property')
        self.assertIn('candidates', res,
                      'The reponse should have a "candidates" property')
        self.assertTrue(len(res['candidates']) > 0,
                        'There should be one or more candidates')

        first_candidate = res['candidates'][0]

        for prop in ['x', 'y', 'srid', 'score']:
            self.assertIn(prop, first_candidate,
                          'Candidates should include "%s"' % prop)

        self.assertEqual(3857, first_candidate['srid'],
                         'The default response srid should be '
                         'spherical mercator')

        self.assertTrue(abs(first_candidate['x'] - (-8366592.924405822)) < 1,
                        'The actual x coordinate was more than 1 meter '
                        'away from the expected value')
        self.assertTrue(abs(first_candidate['y'] - 4859953.672488515) < 1,
                        'The actual y coordinate was more than 1 meter '
                        'away from the expected value')
Exemplo n.º 2
0
    def test_azavea_office_geocodes_correctly(self):
        res = geocode(MockGeocodeRequest('340 n 12th st philadelphia'))

        self.assertNotIn('error', res,
                         'The response should not have an error property')
        self.assertIn('candidates', res,
                      'The reponse should have a "candidates" property')
        self.assertTrue(
            len(res['candidates']) > 0,
            'There should be one or more candidates')

        first_candidate = res['candidates'][0]

        for prop in ['x', 'y', 'srid', 'score']:
            self.assertIn(prop, first_candidate,
                          'Candidates should include "%s"' % prop)

        self.assertEqual(
            3857, first_candidate['srid'],
            'The default response srid should be '
            'spherical mercator')

        self.assertTrue(
            abs(first_candidate['x'] - (-8366592.924405822)) < 1,
            'The actual x coordinate was more than 1 meter '
            'away from the expected value')
        self.assertTrue(
            abs(first_candidate['y'] - 4859953.672488515) < 1,
            'The actual y coordinate was more than 1 meter '
            'away from the expected value')
Exemplo n.º 3
0
    def test_geocoding_gibberish_returns_404(self):
        res = geocode(MockGeocodeRequest('n0 w@y th1s will g30code'))

        self.assertTrue(isinstance(res, HttpResponse),
                        'View should return HttpResponse on 404')
        self.assertEqual(404, res.status_code,
                         'Status code should be 404 Not Found')

        response_json = loads(res.content)
        self.assertIn('error', response_json,
                      'The response body should have an "error" property')
Exemplo n.º 4
0
    def test_geocoding_without_magic_key_returns_404(self):
        res = geocode(MockGeocodeRequest('', ''))

        self.assertTrue(isinstance(res, HttpResponse),
                        'View should return HttpResponse on 404')
        self.assertEqual(404, res.status_code,
                         'Status code should be 404 Not Found')

        response_json = json.loads(res.content)
        self.assertIn('error', response_json,
                      'The response body should have an "error" property')
Exemplo n.º 5
0
    def test_geocoding_gibberish_returns_404(self):
        res = geocode(MockGeocodeRequest('n0 w@y th1s will g30code'))

        self.assertTrue(isinstance(res, HttpResponse),
                        'View should return HttpResponse on 404')
        self.assertEqual(404, res.status_code,
                         'Status code should be 404 Not Found')

        response_json = loads(res.content)
        self.assertIn('error', response_json,
                      'The response body should have an "error" property')
Exemplo n.º 6
0
    def test_geocoding_without_magic_key_returns_404(self):
        res = geocode(MockGeocodeRequest('', ''))

        self.assertTrue(isinstance(res, HttpResponse),
                        'View should return HttpResponse on 404')
        self.assertEqual(404, res.status_code,
                         'Status code should be 404 Not Found')

        response_json = json.loads(res.content)
        self.assertIn('error', response_json,
                      'The response body should have an "error" property')
Exemplo n.º 7
0
    def test_azavea_office_geocodes_correctly(self):
        extent = {
            'xmin': -8475485,
            'xmax': -8280250,
            'ymin': 4643135,
            'ymax': 4954810,
        }

        # Fetch suggestions from ESRI suggestion engine (as the front end does)
        url = ('https://geocode.arcgis.com/arcgis/rest/services/'
               'World/GeocodeServer/suggest?' + urlencode({
                   'f':
                   'json',
                   'searchExtent':
                   json.dumps(
                       dict({'spatialReference': {
                           'wkid': 102100
                       }}, **extent)),
                   'text':
                   '340 n 12th st philadelphia'
               }))
        result = json.loads(requests.get(url).content)

        self.assertIn('suggestions', result,
                      'The reponse should have a "suggestions" property')
        self.assertTrue(
            len(result['suggestions']) > 0,
            'There should be one or more suggestions')
        suggestion = result['suggestions'][0]
        self.assertIn('text', suggestion,
                      'The suggestion should have a "text" property')
        self.assertIn('magicKey', suggestion,
                      'The suggestion should have a "magicKey" property')

        # Geocode first suggestion
        res = geocode(
            MockGeocodeRequest(suggestion['text'], suggestion['magicKey']))

        self.assertNotIn('error', res,
                         'The response should not have an error property')
        self.assertIn('lat', res, 'The reponse should have a "lat" property')
        self.assertIn('lng', res, 'The reponse should have a "lng" property')
        self.assertTrue(
            abs(res['lat'] - 39.958750) < .00001, 'Latitude not as expected')
        self.assertTrue(
            abs(res['lng'] - (-75.158416)) < .00001,
            'Longitude not as expected')
Exemplo n.º 8
0
    def test_simple_address(self):
        addr = "300%20N%2012th%20St%20Philadelphia%20PA"
        req = self.factory.get('/geocode?address=%s' %
                               addr)

        response = geocode(req)

        self.assertEqual(response.status_code, 200)
        rslt = json.loads(response.content)

        x = rslt['x']
        y = rslt['y']

        tgty = 39.957688
        tgtx = -75.158653

        self.assertTrue(abs(x - tgtx) < 0.00001)
        self.assertTrue(abs(y - tgty) < 0.00001)
Exemplo n.º 9
0
    def test_azavea_office_geocodes_correctly(self):
        extent = {
            'xmin': -8475485,
            'xmax': -8280250,
            'ymin': 4643135,
            'ymax': 4954810,
        }

        # Fetch suggestions from ESRI suggestion engine (as the front end does)
        url = (
            'https://geocode.arcgis.com/arcgis/rest/services/'
            'World/GeocodeServer/suggest?' +
            urlencode({
                'f': 'json',
                'searchExtent': json.dumps(dict({
                    'spatialReference': {'wkid': 102100}
                }, **extent)),
                'text': '340 n 12th st philadelphia'
            }))
        result = json.loads(requests.get(url).content)

        self.assertIn('suggestions', result,
                      'The reponse should have a "suggestions" property')
        self.assertTrue(len(result['suggestions']) > 0,
                        'There should be one or more suggestions')
        suggestion = result['suggestions'][0]
        self.assertIn('text', suggestion,
                      'The suggestion should have a "text" property')
        self.assertIn('magicKey', suggestion,
                      'The suggestion should have a "magicKey" property')

        # Geocode first suggestion
        res = geocode(MockGeocodeRequest(
            suggestion['text'], suggestion['magicKey']))

        self.assertNotIn('error', res,
                         'The response should not have an error property')
        self.assertIn('lat', res, 'The reponse should have a "lat" property')
        self.assertIn('lng', res, 'The reponse should have a "lng" property')
        self.assertTrue(abs(res['lat'] - 39.958750) < .00001,
                        'Latitude not as expected')
        self.assertTrue(abs(res['lng'] - (-75.158416)) < .00001,
                        'Longitude not as expected')