Exemplo n.º 1
0
class TestGmapHelper(TestCase):
    def setUp(self):
        self.G = GMapClient()
        self.client = googlemaps.Client(key=settings.DJANGO_GC_MAP_API_KEY)

    def test_directions(self):
        self.assertEqual(len(gmap_history), 0)
        res = self.G.directions('Paris', 'Amsterdam', mode='driving')
        self.assertEqual(len(gmap_history), 1)
        self.assertEqual(
            res, self.client.directions('Paris', 'Amsterdam', mode='driving'))
        # call again to fetch from cache
        res = self.G.directions('Paris', 'Amsterdam', mode='driving')
        self.assertEqual(len(gmap_history), 1)
        self.assertEqual(
            res, self.client.directions('Paris', 'Amsterdam', mode='driving'))
        #
        big_route = [
            'Malaya Arnautskaya 5, Odessa, Ukraine',
            'Shevchenko 5, Dnipro, Ukraine', 'Khreschatyk 5, Kiev, Ukraine'
        ] * 10
        res = self.G.directions('Paris',
                                'Amsterdam',
                                waypoints=big_route,
                                mode='driving')
        legs = res[0]['legs']

        # module will do 2 requests to google maps
        self.assertEqual(len(gmap_history), 3)
        self.assertIn('Paris', legs[0]['start_address'])
        self.assertIn('Amsterdam', legs[-1]['end_address'])

    def test_directions_statistics(self):
        res = self.G.directions(u'Киев', u'Берлин', mode='driving')
        stat = self.G.direction_statistics_by_country(res[0])
        #print(json.dumps(stat))
        self.assertEqual(len(stat), 3)
        self.assertEqual(
            stat[0]['short_name'],
            'UA'  # ukraine
        )
        self.assertTrue(520000 > stat[0]['distance'] > 505000)
        self.assertEqual(
            stat[1]['short_name'],
            'PL'  # poland
        )
        self.assertTrue(742000 > stat[1]['distance'] > 726000)
        self.assertEqual(
            stat[2]['short_name'],
            'DE'  # germany
        )
        self.assertTrue(103000 > stat[2]['distance'] > 101000)
class TestGmapHelper(TestCase):
    def setUp(self):
        self.G = GMapClient()

    def test_geocode_country_only_fail(self):
        places = ["EMPTY GEOCODE RESULT"]
        # empty result
        self.assertRaisesMessage(
            exceptions.EmptyGeocodeResult,
            ERROR_MSGS['empty_geocode_result'].format(search_string=places[0]),
            self.G.geocode_country_only, places)

    def test_geocode_contry_only(self):
        # fetch single
        places_in_amsterdam = [
            'NEMO Science Museum', 'Fjällräven Flagship Store Amsterdam'
        ]

        for place in places_in_amsterdam:
            country_info = self.G.geocode_country_only(place + ', ' +
                                                       'Amsterdam')
            self.assertDictEqual(country_info, {
                u'long_name': u'Netherlands',
                u'short_name': u'NL'
            })

        history_len_last = len(gmap_history)
        # run geocode again.
        # this time no googlemaps call will be done
        self.G.geocode_city_only(places_in_amsterdam[0] + ', ' + 'Amsterdam')
        self.assertEqual(len(gmap_history), history_len_last)

        country_info = self.G.geocode_country_only(
            u'Одесса, Потемкинская лестница')
        self.assertDictEqual(country_info, {
            u'long_name': u'Ukraine',
            u'short_name': u'UA'
        })
        self.assertEqual(len(gmap_history), history_len_last + 2)
class TestGmapHelper(TestCase):
    def setUp(self):
        self.G = GMapClient()

    def test_geocode_city_only_fail(self):
        places = ["EMPTY GEOCODE RESULT"]
        # empty result
        self.assertRaisesMessage(
            exceptions.EmptyGeocodeResult,
            ERROR_MSGS['empty_geocode_result'].format(search_string=places[0]),
            self.G.geocode_city_only, places)

    def test_geocode_city_only(self):
        # fetch single
        amsterdam = self.G.geocode('Amsterdam')[0]['geometry']['location']
        places_in_amsterdam = [
            'NEMO Science Museum', 'Fjällräven Flagship Store Amsterdam'
        ]

        for place in places_in_amsterdam:
            place_coordinates = self.G.geocode(
                place)[0]['geometry']['location']
            city_coordinates = self.G.geocode_city_only(
                place)[0]['geometry']['location']

            # place in city 'amsterdam'
            self.assertDictEqual(city_coordinates, amsterdam)
            # coordinates is not real place coordinates but city
            self.assertNotEqual(city_coordinates['lat'],
                                place_coordinates['lat'])
            self.assertNotEqual(city_coordinates['lng'],
                                place_coordinates['lng'])

        history_len_last = len(gmap_history)
        # run geocode again.
        # this time no googlemaps call will be done
        self.G.geocode_city_only(places_in_amsterdam[0])
        self.assertEqual(len(gmap_history), history_len_last)
class TestGmapHelper(TestCase):
    def setUp(self):
        self.G = GMapClient()
        self.h_len = len(gmap_history)

    def test_fail_decode(self):
        # invalid input type 'float'
        self.assertRaisesMessage(
            TypeError,
            ERROR_MSGS['geocode_invalid_type'].format(type=type(1.1)),
            self.G.geocode, 1.1)
        # invalid input type 'int' inside list
        self.assertRaisesMessage(
            TypeError, ERROR_MSGS['geocode_invalid_type'].format(type=type(1)),
            self.G.geocode, [1, 'Paris'])

    def test_geocode(self):
        # fetch single
        self.G.geocode('Amsterdam')
        self.assertEqual(len(gmap_history), self.h_len + 1)

        # fetch from cache
        self.G.geocode('Amsterdam')
        self.assertEqual(len(gmap_history), self.h_len + 1)

        # ignore cache
        self.G.geocode('Amsterdam', ignore_cache=True)
        self.assertEqual(len(gmap_history), self.h_len + 2)
        self.assertEqual(gmap_history[self.h_len + 1].action, 'geocode')
        self.assertEqual(gmap_history[self.h_len + 1].args, ('Amsterdam', ))

        # fetch multiple locations
        res = self.G.geocode(['Paris', 'Amsterdam'])
        self.assertEqual(len(gmap_history), self.h_len + 3)
        # only 'Paris', 'Amsterdam' we have in cache
        self.assertEqual(gmap_history[self.h_len + 2].action, 'geocode')
        self.assertEqual(gmap_history[self.h_len + 2].args, ('Paris', ))
        # paris coordinates
        self.assertEqual(res[0][0]['geometry']['location']['lat'], 48.856614)
        self.assertEqual(res[0][0]['geometry']['location']['lng'], 2.3522219)

        # amsterdam coordinates
        self.assertEqual(res[1][0]['geometry']['location']['lat'], 52.3675734)
        self.assertEqual(res[1][0]['geometry']['location']['lng'], 4.9041389)

        # fetch multiple locations from cache
        res = self.G.geocode(['Paris', 'Amsterdam'])
        self.assertEqual(len(gmap_history), self.h_len + 3)
 def setUp(self):
     self.G = GMapClient()
 def setUp(self):
     self.G = GMapClient()
     self.h_len = len(gmap_history)
Exemplo n.º 7
0
 def setUp(self):
     self.G = GMapClient()
     self.client = googlemaps.Client(key=settings.DJANGO_GC_MAP_API_KEY)