def test_should_estimate_wroclaw_pricing(self): wroclaw_location_a = Location(51.11801979999999, 17.04196479999996) wroclaw_location_b = Location(51.0987672, 17.036518600000022) actual_response = self.uber_client.estimate_price( wroclaw_location_a, wroclaw_location_b) self.assertIsNotNone(actual_response) self.assertEqual(len(actual_response), 2)
def import_from_file(csv_file_name=CITIES_CSV): logger.info("Opening file: {} for reading cities...".format(csv_file_name)) cities = [] with open(csv_file_name, 'rb') as csv_file: places_csv_reader = csv.reader(csv_file, delimiter=',', quotechar='|') for row in places_csv_reader: if row: try: lat = float(row[LATITUDE_COLUMN_INDEX]) lon = float(row[LONGITUDE_COLUMN_INDEX]) location = Location(latitude=lat, longitude=lon) country = unicode(row[COUNTRY_COLUMN_INDEX]) city_name = unicode(row[CITY_COLUMN_INDEX]) population = int(row[POPULATION_COLUMN_INDEX]) city_code = str(row[SKY_SCANNER_CITY_CODE_COLUMN_INDEX]) city = City(city_name=city_name, population=population, location=location, country=country, city_code=city_code) cities.append(city) except Exception as e: logger.warning( "Could not create city from data: <{}>".format(row)) else: logger.warning( "Could not create city from data: {}".format(row)) logger.info("Ended reading cities, read: {} cities.".format(len(cities))) return cities
def test_listing_object_building(self): actual_object = build_air_bnb_listing_object(self.api_response) expected_price = Price(value=67, currency="USD") expected_location = Location(latitude=40.71919290408344, longitude=17.430601156522822) self.assertIsNotNone(actual_object) self.assertEqual(actual_object.location, expected_location) self.assertEqual(actual_object.price, expected_price)
def retrieve_uber_3km_pricing(location_dict): """ :type location_dict: dict :rtype: list[dict] """ location = Location.from_serializable(location_dict) uber_client = build_uber_client_using_env_variables() uber_pricing_list = uber_client.estimate_3km_price(location) return [p.to_serializable() for p in uber_pricing_list]
def retrieve_weather(location_dict): """ :type location_dict: dict :rtype: list[dict] """ location = Location.from_serializable(location_dict) client = build_open_weather_client_using_env_variables() forecasts = client.get_5_days_forecast(location) return [f.to_serializable() for f in forecasts]
def retrieve_weather(event, context): """ :type event: dict[str, str] :rtype: list[dict] """ location = Location.from_serializable(read_payload(event)) print("Resolving weather for: {}...".format(location)) client = build_open_weather_client_using_env_variables() forecasts = client.get_5_days_forecast(location) print("Resolved {} forecasts for: {}!".format(len(forecasts), location)) return build_response(payload=[f.to_serializable() for f in forecasts])
def retrieve_uber_3km_pricing(event, context): """ :type event: dict :rtype: list[dict] """ location = Location.from_serializable(read_payload(event)) print("Resolving Uber pricing for: {}...".format(location)) uber_client = build_uber_client_using_env_variables() uber_pricing_list = uber_client.estimate_3km_price(location) print("Resolving {} Uber pricing for: {}!".format(len(uber_pricing_list), location)) return build_response([p.to_serializable() for p in uber_pricing_list])
class SerializationTest(unittest.TestCase): def setUp(self): self.location = Location(10.01, -31.16) def test_should_serialize_location_object(self): serialized_location = to_json(self.location) self.assertIsNotNone(serialized_location) def test_should_serialize_and_deserialize_location_object(self): serialized_location = to_json(self.location) deserialized_location = from_json(serialized_location, Location) self.assertIsNotNone(deserialized_location) self.assertEqual(self.location, deserialized_location) def test_model_should_return_serializable(self): serializable = self.location.to_serializable() self.assertIsNotNone(serializable) self.assertEqual(serializable, {"latitude": 10.01, "longitude": -31.16}) def test_model_should_recreate_object_from_serializable(self): serializable = self.location.to_serializable() recreated = Location.from_serializable(serializable) self.assertIsNotNone(recreated) self.assertEqual(self.location, recreated)
def estimate_3km_price(self, location_a): """ :type location_a: commons.model.Location :rtype: list[commons.uber.UberPricing] """ location_b = Location( latitude=location_a.latitude + DELTA_COORDINATES_FOR_3_KM, longitude=location_a.longitude + DELTA_COORDINATES_FOR_3_KM) response = self.client.get_price_estimates(location_a.latitude, location_a.longitude, location_b.latitude, location_b.longitude) if response.status_code == 200: return map(lambda pricing: build_uber_pricing_object(pricing), response.json.get("prices")) else: raise self._build_error_from_response(response)
def build_air_bnb_listing_object(air_bnb_response_dict): """ :type air_bnb_response_dict: dict[str | dict] :rtype: commons.air_bnb.AirBnBListing """ air_bnb_listing = air_bnb_response_dict.get("listing") id = int(air_bnb_listing.get("id")) city = air_bnb_listing.get("city") lat = float(air_bnb_listing.get("lat")) lng = float(air_bnb_listing.get("lng")) name = air_bnb_listing.get("name") person_capacity = int(air_bnb_listing.get("person_capacity")) primary_host_name = air_bnb_listing.get("primary_host").get("first_name") primary_host_picture_url = air_bnb_listing.get("primary_host").get( "picture_url") property_type = air_bnb_listing.get("property_type") public_address = air_bnb_listing.get("public_address") star_rating = air_bnb_listing.get("star_rating") bedroom_count = air_bnb_listing.get("bedrooms") bed_count = air_bnb_listing.get("beds") picture_urls = air_bnb_listing.get("picture_urls") air_bnb_pricing = air_bnb_response_dict.get("pricing_quote") price_value = float(air_bnb_pricing.get("localized_nightly_price")) price_currency = air_bnb_pricing.get("localized_currency") return AirBnBListing(id=id, name=name, type=property_type, city=city, public_address=public_address, location=Location(latitude=lat, longitude=lng), star_rating=star_rating, person_capacity=person_capacity, host_person_name=primary_host_name, host_person_picture=primary_host_picture_url, bedroom_count=bedroom_count, bed_count=bed_count, picture_urls=picture_urls, price=Price(value=price_value, currency=price_currency))
def test_model_should_recreate_object_from_serializable(self): serializable = self.location.to_serializable() recreated = Location.from_serializable(serializable) self.assertIsNotNone(recreated) self.assertEqual(self.location, recreated)
from commons.model import City, Location DEFAULT_CITIES = [ City(city_name="Berlin", population=3671000, location=Location(52.52000659999999, 13.404953999999975), country="Germany", city_code="BERL-sky"), City(city_name="London", population=8673713, location=Location(51.5073509, -0.12775829999998223), country="United Kingdom", city_code="LOND-sky"), City(city_name="Paris", population=2244000, location=Location(48.85661400000001, 2.3522219000000177), country="France", city_code="PARI-sky") ]
def test_wroclaw_products_should_be_available(self): wroclaw_location = Location(51.11, 17.022222) actual_response = self.uber_client.get_available_products( wroclaw_location) self.assertIsNotNone(actual_response) self.assertEqual(len(actual_response), 2)
def setUp(self): self.client = build_open_weather_client_using_env_variables() self.wroclaw_location = Location(51.11, 17.022222)
def test_should_return_200_code_on_location_request(self): response = self.client.photos_from(location=Location(51.11, 17.022222)) actual_response_code = int(response.get("meta").get("code")) actual_response_data = response.get("data") self.assertEqual(actual_response_code, 200) self.assertIsNotNone(actual_response_data)
def estimate_3km_price(self, location_a): """ :type location_a: commons.model.Location :rtype: list[commons.uber.UberPricing] """ location_b = Location( latitude=location_a.latitude + DELTA_COORDINATES_FOR_3_KM, longitude=location_a.longitude + DELTA_COORDINATES_FOR_3_KM) response = self.client.get_price_estimates(location_a.latitude, location_a.longitude, location_b.latitude, location_b.longitude) if response.status_code == 200: return map(lambda pricing: build_uber_pricing_object(pricing), response.json.get("prices")) else: raise self._build_error_from_response(response) @staticmethod def _build_error_from_response(response): return IOError( "Could not retrieve information from Uber. Code: {} error: {}". format(response.status_code, response.json)) print( distance( Location(1.0, 1.0), Location(1.0 + DELTA_COORDINATES_FOR_3_KM, 1.0 + DELTA_COORDINATES_FOR_3_KM)))
def setUp(self): self.location = Location(10.01, -31.16)
def to_meta(self): """ :rtype: commons.model.Location """ return Location(self.latitude, self.longitude)