def test_build_flight_search_queries_with_return_arrival_day(self): f = FlightSearchBuilder(from_location="SFO", to_location="MCO", start_from=date(2020, 1, 1), return_arrival_day=Day.MONDAY) query = build_flight_search_queries(f) self.assertEqual(len(query), 1) self.assertEqual( query[0], "https://api.skypicker.com/flights?partner=picky&curr=USD&max_stopovers=0&fly_from=SFO&fly_to=MCO&date_from=01/01/2020&date_to=01/03/2020" )
def test_build_flight_search_queries_with_return_arrival_time(self): f = FlightSearchBuilder(from_location="SFO", to_location="MCO", start_from=date(2020, 1, 1), return_arrival_time=(time(hour=10, minute=0), time(hour=12, minute=0))) query = build_flight_search_queries(f) self.assertEqual(len(query), 1) self.assertEqual( query[0], "https://api.skypicker.com/flights?partner=picky&curr=USD&max_stopovers=0&fly_from=SFO&fly_to=MCO&ret_atime_from=10:00&ret_atime_to=12:00&date_from=01/01/2020&date_to=01/03/2020" )
def test_build_flight_search_queries_with_departure_day_and_return_arrival_day( self): f = FlightSearchBuilder(from_location="SFO", to_location="MCO", start_from=date(2020, 1, 1), departure_day=Day.SATURDAY, return_arrival_day=Day.MONDAY) query = build_flight_search_queries(f) self.assertEqual(len(query), 9) self.assertEqual( query[0], "https://api.skypicker.com/flights?partner=picky&curr=USD&max_stopovers=0&fly_from=SFO&fly_to=MCO&date_from=04/01/2020&date_to=04/01/2020&return_from=06/01/2020&return_to=06/01/2020&nights_in_dst_from=1&nights_in_dst_to=1" ) self.assertEqual( query[1], "https://api.skypicker.com/flights?partner=picky&curr=USD&max_stopovers=0&fly_from=SFO&fly_to=MCO&date_from=11/01/2020&date_to=11/01/2020&return_from=13/01/2020&return_to=13/01/2020&nights_in_dst_from=1&nights_in_dst_to=1" ) self.assertEqual( query[2], "https://api.skypicker.com/flights?partner=picky&curr=USD&max_stopovers=0&fly_from=SFO&fly_to=MCO&date_from=18/01/2020&date_to=18/01/2020&return_from=20/01/2020&return_to=20/01/2020&nights_in_dst_from=1&nights_in_dst_to=1" ) self.assertEqual( query[3], "https://api.skypicker.com/flights?partner=picky&curr=USD&max_stopovers=0&fly_from=SFO&fly_to=MCO&date_from=25/01/2020&date_to=25/01/2020&return_from=27/01/2020&return_to=27/01/2020&nights_in_dst_from=1&nights_in_dst_to=1" ) self.assertEqual( query[4], "https://api.skypicker.com/flights?partner=picky&curr=USD&max_stopovers=0&fly_from=SFO&fly_to=MCO&date_from=01/02/2020&date_to=01/02/2020&return_from=03/02/2020&return_to=03/02/2020&nights_in_dst_from=1&nights_in_dst_to=1" ) self.assertEqual( query[5], "https://api.skypicker.com/flights?partner=picky&curr=USD&max_stopovers=0&fly_from=SFO&fly_to=MCO&date_from=08/02/2020&date_to=08/02/2020&return_from=10/02/2020&return_to=10/02/2020&nights_in_dst_from=1&nights_in_dst_to=1" ) self.assertEqual( query[6], "https://api.skypicker.com/flights?partner=picky&curr=USD&max_stopovers=0&fly_from=SFO&fly_to=MCO&date_from=15/02/2020&date_to=15/02/2020&return_from=17/02/2020&return_to=17/02/2020&nights_in_dst_from=1&nights_in_dst_to=1" ) self.assertEqual( query[7], "https://api.skypicker.com/flights?partner=picky&curr=USD&max_stopovers=0&fly_from=SFO&fly_to=MCO&date_from=22/02/2020&date_to=22/02/2020&return_from=24/02/2020&return_to=24/02/2020&nights_in_dst_from=1&nights_in_dst_to=1" ) self.assertEqual( query[8], "https://api.skypicker.com/flights?partner=picky&curr=USD&max_stopovers=0&fly_from=SFO&fly_to=MCO&date_from=29/02/2020&date_to=29/02/2020&return_from=02/03/2020&return_to=02/03/2020&nights_in_dst_from=1&nights_in_dst_to=1" )
def search(self): # validate request if not self.request_is_valid(): return ConfigurationError("Invalid request") # build requests flight_queries = build_flight_search_queries(self) # make request trips = [] for flight_query in flight_queries: trip = parse_flight_response(make_api_request(flight_query)) if trip: trips.extend(trip) sleep(API_BACKOFF_SECONDS) # return trip object trips.sort(key=lambda trip: trip.price) return trips
def test_make_api_request_bad_response_body(self): f = FlightSearchBuilder(from_location="SFO", to_location="MCO", start_from=date(2020, 1, 1)) query = build_flight_search_queries(f) self.assertEqual(len(query), 1) self.assertEqual( query[0], "https://api.skypicker.com/flights?partner=picky&curr=USD&max_stopovers=0&fly_from=SFO&fly_to=MCO&date_from=01/01/2020&date_to=01/03/2020" ) responses.add( responses.GET, 'https://api.skypicker.com/flights?partner=picky&curr=USD&max_stopovers=0&fly_from=SFO&fly_to=MCO&date_from=01/01/2020&date_to=01/03/2020', json={'error': 'bad_response'}, status=200) r = make_api_request(query[0]) self.assertIsNone(r)