Esempio n. 1
0
 def test_invalid_token_bulk_many(self):
     invalid_geocoder = GeocodioBulkGeocoder(token=INVALID_TOKEN,
                                             logger=Mock())
     with self.assertRaises(ServiceException):
         invalid_geocoder._batch_geocode([
             (SEARCH_ID_1, VALID_ADDRESS_1, None, None, None),
             (SEARCH_ID_2, VALID_ADDRESS_2, None, None, None)
         ])
Esempio n. 2
0
 def setUp(self):
     self.geocoder = GeocodioGeocoder(token=geocodio_api_key(),
                                      logger=Mock())
     self.bulk_geocoder = GeocodioBulkGeocoder(token=geocodio_api_key(),
                                               logger=Mock())
Esempio n. 3
0
class GeocodioGeocoderTestCase(unittest.TestCase):
    def setUp(self):
        self.geocoder = GeocodioGeocoder(token=geocodio_api_key(),
                                         logger=Mock())
        self.bulk_geocoder = GeocodioBulkGeocoder(token=geocodio_api_key(),
                                                  logger=Mock())

    ### NON BULK

    def test_invalid_token(self):
        invalid_geocoder = GeocodioGeocoder(token=INVALID_TOKEN, logger=Mock())
        with self.assertRaises(ServiceException):
            invalid_geocoder.geocode(VALID_ADDRESS_1)

    def test_valid_requests(self):
        place = self.geocoder.geocode(VALID_ADDRESS_1)

        self.assertEqual('%.3f' % place[0], '%.3f' % WELL_KNOWN_LONGITUDE_1)
        self.assertEqual('%.3f' % place[1], '%.3f' % WELL_KNOWN_LATITUDE_1)

        place = self.geocoder.geocode(VALID_ADDRESS_2)

        self.assertEqual('%.3f' % place[0], '%.3f' % WELL_KNOWN_LONGITUDE_2)
        self.assertEqual('%.3f' % place[1], '%.3f' % WELL_KNOWN_LATITUDE_2)

        place = self.geocoder.geocode(VALID_ADDRESS_3)

        self.assertEqual('%.3f' % place[0], '%.3f' % WELL_KNOWN_LONGITUDE_3)
        self.assertEqual('%.3f' % place[1], '%.3f' % WELL_KNOWN_LATITUDE_3)

    def test_valid_request_components(self):
        place = self.geocoder.geocode(searchtext=VALID_SEARCH_TEXT_1,
                                      city=VALID_CITY_1,
                                      state_province=VALID_STATE_PROVINCE_1,
                                      country=VALID_COUNTRY_1)

        self.assertEqual('%.3f' % place[0], '%.3f' % WELL_KNOWN_LONGITUDE_1)
        self.assertEqual('%.3f' % place[1], '%.3f' % WELL_KNOWN_LATITUDE_1)

        place = self.geocoder.geocode(searchtext=VALID_SEARCH_TEXT_2,
                                      city=VALID_CITY_2,
                                      state_province=VALID_STATE_PROVINCE_2,
                                      country=VALID_COUNTRY_2)

        self.assertEqual('%.3f' % place[0], '%.3f' % WELL_KNOWN_LONGITUDE_2)
        self.assertEqual('%.3f' % place[1], '%.3f' % WELL_KNOWN_LATITUDE_2)

        place = self.geocoder.geocode(searchtext=VALID_SEARCH_TEXT_3,
                                      city=VALID_CITY_3,
                                      state_province=VALID_STATE_PROVINCE_3,
                                      country=VALID_COUNTRY_3)

        self.assertEqual('%.3f' % place[0], '%.3f' % WELL_KNOWN_LONGITUDE_3)
        self.assertEqual('%.3f' % place[1], '%.3f' % WELL_KNOWN_LATITUDE_3)

    def test_valid_request_namedplace(self):
        place = self.geocoder.geocode(searchtext='New York')

        assert place

    def test_valid_request_namedplace2(self):
        place = self.geocoder.geocode(searchtext='New York', country='us')

        assert place

    def test_odd_characters(self):
        place = self.geocoder.geocode(searchtext='New York; "USA"')

        assert place

    def test_empty_request(self):
        place = self.geocoder.geocode(searchtext='',
                                      country=None,
                                      city=None,
                                      state_province=None)

        assert place == []

    def test_empty_search_text_request(self):
        place = self.geocoder.geocode(searchtext='     ',
                                      country='us',
                                      city=None,
                                      state_province="")

        assert place == []

    def test_unknown_place_request(self):
        place = self.geocoder.geocode(searchtext='[unknown]',
                                      country='ch',
                                      state_province=None,
                                      city=None)

        assert place == []

    ### BULK ONE

    def test_invalid_token_bulk_one(self):
        invalid_geocoder = GeocodioBulkGeocoder(token=INVALID_TOKEN,
                                                logger=Mock())
        with self.assertRaises(ServiceException):
            invalid_geocoder._batch_geocode([(SEARCH_ID_1, VALID_ADDRESS_1,
                                              None, None, None)])

    def test_valid_request_bulk_one(self):
        place = self.bulk_geocoder._batch_geocode([
            (SEARCH_ID_1, VALID_ADDRESS_1, None, None, None)
        ])

        self.assertEqual(place[0][0], SEARCH_ID_1)
        self.assertEqual('%.3f' % place[0][1], '%.3f' % WELL_KNOWN_LONGITUDE_1)
        self.assertEqual('%.3f' % place[0][2], '%.3f' % WELL_KNOWN_LATITUDE_1)

        place = self.bulk_geocoder._batch_geocode([
            (SEARCH_ID_1, VALID_ADDRESS_2, None, None, None)
        ])

        self.assertEqual(place[0][0], SEARCH_ID_1)
        self.assertEqual('%.3f' % place[0][1], '%.3f' % WELL_KNOWN_LONGITUDE_2)
        self.assertEqual('%.3f' % place[0][2], '%.3f' % WELL_KNOWN_LATITUDE_2)

        place = self.bulk_geocoder._batch_geocode([
            (SEARCH_ID_1, VALID_ADDRESS_3, None, None, None)
        ])

        self.assertEqual(place[0][0], SEARCH_ID_1)
        self.assertEqual('%.3f' % place[0][1], '%.3f' % WELL_KNOWN_LONGITUDE_3)
        self.assertEqual('%.3f' % place[0][2], '%.3f' % WELL_KNOWN_LATITUDE_3)

    def test_valid_request_components_bulk_one(self):
        place = self.bulk_geocoder._batch_geocode([
            (SEARCH_ID_1, VALID_SEARCH_TEXT_1, VALID_CITY_1,
             VALID_STATE_PROVINCE_1, VALID_COUNTRY_1)
        ])

        self.assertEqual(place[0][0], SEARCH_ID_1)
        self.assertEqual('%.3f' % place[0][1], '%.3f' % WELL_KNOWN_LONGITUDE_1)
        self.assertEqual('%.3f' % place[0][2], '%.3f' % WELL_KNOWN_LATITUDE_1)

        place = self.bulk_geocoder._batch_geocode([
            (SEARCH_ID_1, VALID_SEARCH_TEXT_2, VALID_CITY_2,
             VALID_STATE_PROVINCE_2, VALID_COUNTRY_2)
        ])

        self.assertEqual(place[0][0], SEARCH_ID_1)
        self.assertEqual('%.3f' % place[0][1], '%.3f' % WELL_KNOWN_LONGITUDE_2)
        self.assertEqual('%.3f' % place[0][2], '%.3f' % WELL_KNOWN_LATITUDE_2)

        place = self.bulk_geocoder._batch_geocode([
            (SEARCH_ID_1, VALID_SEARCH_TEXT_3, VALID_CITY_3,
             VALID_STATE_PROVINCE_3, VALID_COUNTRY_3)
        ])

        self.assertEqual(place[0][0], SEARCH_ID_1)
        self.assertEqual('%.3f' % place[0][1], '%.3f' % WELL_KNOWN_LONGITUDE_3)
        self.assertEqual('%.3f' % place[0][2], '%.3f' % WELL_KNOWN_LATITUDE_3)

    def test_valid_request_namedplace_bulk_one(self):
        place = self.bulk_geocoder._batch_geocode([(SEARCH_ID_1, 'New York',
                                                    None, None, None)])

        assert place

    def test_valid_request_namedplace2_bulk_one(self):
        place = self.bulk_geocoder._batch_geocode([(SEARCH_ID_1, 'New York',
                                                    'us', None, None)])

        assert place

    def test_odd_characters_bulk_one(self):
        place = self.bulk_geocoder._batch_geocode([
            (SEARCH_ID_1, 'New York; "USA"', None, None, None)
        ])

        assert place

    def test_empty_request_bulk_one(self):
        place = self.bulk_geocoder._batch_geocode([(SEARCH_ID_1, '', None,
                                                    None, None)])

        assert place == [(SEARCH_ID_1, None, None)]

    def test_empty_search_text_request_bulk_one(self):
        place = self.bulk_geocoder._batch_geocode([(SEARCH_ID_1, '     ', 'us',
                                                    None, "")])

        assert place == [(SEARCH_ID_1, None, None)]

    def test_unknown_place_request_bulk_one(self):
        place = self.bulk_geocoder._batch_geocode([(SEARCH_ID_1, '[unknown]',
                                                    'ch', None, None)])

        assert place == [(SEARCH_ID_1, None, None)]

    ### BULK MANY

    def test_invalid_token_bulk_many(self):
        invalid_geocoder = GeocodioBulkGeocoder(token=INVALID_TOKEN,
                                                logger=Mock())
        with self.assertRaises(ServiceException):
            invalid_geocoder._batch_geocode([
                (SEARCH_ID_1, VALID_ADDRESS_1, None, None, None),
                (SEARCH_ID_2, VALID_ADDRESS_2, None, None, None)
            ])

    def test_valid_request_bulk_many(self):
        places = self.bulk_geocoder._batch_geocode([
            (SEARCH_ID_1, VALID_ADDRESS_1, None, None, None),
            (SEARCH_ID_2, VALID_ADDRESS_2, None, None, None)
        ])

        self.assertEqual(places[0][0], SEARCH_ID_1)
        self.assertEqual('%.3f' % places[0][1][0],
                         '%.3f' % WELL_KNOWN_LONGITUDE_1)
        self.assertEqual('%.3f' % places[0][1][1],
                         '%.3f' % WELL_KNOWN_LATITUDE_1)

        self.assertEqual(places[1][0], SEARCH_ID_2)
        self.assertEqual('%.3f' % places[1][1][0],
                         '%.3f' % WELL_KNOWN_LONGITUDE_2)
        self.assertEqual('%.3f' % places[1][1][1],
                         '%.3f' % WELL_KNOWN_LATITUDE_2)

    def test_valid_request_components_bulk_many(self):
        places = self.bulk_geocoder._batch_geocode([
            (SEARCH_ID_1, VALID_SEARCH_TEXT_1, VALID_CITY_1,
             VALID_STATE_PROVINCE_1, VALID_COUNTRY_1),
            (SEARCH_ID_2, VALID_SEARCH_TEXT_2, VALID_CITY_2,
             VALID_STATE_PROVINCE_2, VALID_COUNTRY_2)
        ])

        self.assertEqual(places[0][0], SEARCH_ID_1)
        self.assertEqual(places[1][0], SEARCH_ID_2)

    def test_valid_request_namedplace_bulk_many(self):
        places = self.bulk_geocoder._batch_geocode([
            (SEARCH_ID_1, 'New York', None, None, None),
            (SEARCH_ID_2, 'Los Angeles', None, None, None)
        ])

        assert places

        self.assertEqual(places[0][0], SEARCH_ID_1)
        self.assertEqual(places[1][0], SEARCH_ID_2)

    def test_valid_request_namedplace2_bulk_many(self):
        places = self.bulk_geocoder._batch_geocode([
            (SEARCH_ID_1, 'New York', 'us', None, None),
            (SEARCH_ID_2, 'Los Angeles', None, None, None)
        ])

        assert places

        self.assertEqual(places[0][0], SEARCH_ID_1)
        self.assertEqual(places[1][0], SEARCH_ID_2)

    def test_odd_characters_bulk_many(self):
        places = self.bulk_geocoder._batch_geocode([
            (SEARCH_ID_1, 'New York; "USA"', None, None, None),
            (SEARCH_ID_2, 'Los Angeles', None, None, None)
        ])

        assert places

        self.assertEqual(places[0][0], SEARCH_ID_1)
        self.assertEqual(places[1][0], SEARCH_ID_2)

    def test_empty_request_bulk_many(self):
        places = self.bulk_geocoder._batch_geocode([
            (SEARCH_ID_1, '', None, None, None),
            (SEARCH_ID_2, '', None, None, None)
        ])

        assert places == [(SEARCH_ID_1, [], {}), (SEARCH_ID_2, [], {})]

    def test_empty_search_text_request_bulk_many(self):
        places = self.bulk_geocoder._batch_geocode([
            (SEARCH_ID_1, '     ', 'us', None, ""),
            (SEARCH_ID_2, '     ', 'us', None, "")
        ])

        assert places == [(SEARCH_ID_1, [], {}), (SEARCH_ID_2, [], {})]

    def test_unknown_place_request_bulk_many(self):
        places = self.bulk_geocoder._batch_geocode([
            (SEARCH_ID_1, '[unknown]', 'ch', None, None),
            (SEARCH_ID_2, '[unknown]', 'ch', None, None)
        ])

        assert places == [(SEARCH_ID_1, [], {}), (SEARCH_ID_2, [], {})]