def test_gmaps(): city = "Mexico City" test = False try: response = external._gmaps_lookup_city(city) address_types = response['address_components'][0]['types'] if (address_types[0] == 'locality' and address_types[1] == 'political'): test = True city = response['address_components'][0]['long_name'] except: pass assert(test == True and city == "Mexico City") # test_gmaps test 1 city = "asdfdafasd" test = False try: response = external._gmaps_lookup_city(city) address_types = response['address_components'][0]['types'] if (address_types[0] == 'locality' and address_types[1] == 'political'): test = True city = response['address_components'][0]['long_name'] except: pass assert(test == False) # test_gmaps test 2 city = "San Fran" test = False try: response = external._gmaps_lookup_city(city) address_types = response['address_components'][0]['types'] if (address_types[0] == 'locality' and address_types[1] == 'political'): test = True city = response['address_components'][0]['long_name'] except: pass assert(test == True and city == "San Francisco") # test_gmaps test 3 city = "New York City" test = False try: response = external._gmaps_lookup_city(city) address_types = response['address_components'][0]['types'] if (address_types[0] == 'locality' and address_types[1] == 'political'): test = True city = response['address_components'][0]['long_name'] except: pass assert(test == True and city == "New York") # test_gmaps test 4 city = "NYC" test = False try: response = external._gmaps_lookup_city(city) address_types = response['address_components'][0]['types'] if (address_types[0] == 'locality' and address_types[1] == 'political'): test = True city = response['address_components'][0]['long_name'] except: pass assert(test == True and city == "New York") # test_gmaps test 5
def lookup(cls, query): city_json = external._gmaps_lookup_city(query) if city_json is None: return None address_components = city_json['address_components'] city_name = address_components[0]['long_name'] country_name = None for c in address_components: c_type = c['types'][0] if c_type == 'country': country_name = c['long_name'] loc_json = city_json['geometry']['location'] location = Location(loc_json['lat'], loc_json['lng']) return cls(city_name, country_name, location)
def test_gmaps(city): return external._gmaps_lookup_city(city)