def test_order_by_start_date(self): with self._app.app_context(): # Setup. trips = [] # Should be excluded because of start date. _add_trip(trips=trips, trip_id=1, title='Tahoe', activity_name='Hike', start=datetime.date(2016, 1, 1), end=datetime.date(2016, 1, 10)) # Should be excluded because of end date. _add_trip(trips=trips, trip_id=2, title='Tahoe', activity_name='Hike', start=datetime.date(2016, 2, 11), end=datetime.date(2016, 2, 22)) # Should be included and be second in the results because of start date. _add_trip(trips=trips, trip_id=3, title='Tahoe', activity_name='Hike', start=datetime.date(2016, 2, 14), end=datetime.date(2016, 2, 16)) # Should be included and be first in the results because of start date. _add_trip(trips=trips, trip_id=4, title='Tahoe', activity_name='Hike', start=datetime.date(2016, 2, 12), end=datetime.date(2016, 2, 18)) _insert_trips(trips) # Exercise. (count, trips) = trips_models.search(from_date=datetime.date(2016, 2, 10), to_date=datetime.date(2016, 2, 20)) # Verify. self.assertEqual(2, count) self.assertEqual(2, len(trips)) self.assertEqual(4, trips[0].trip_id) self.assertEqual(3, trips[1].trip_id)
def test_find_by_search_text(self): with self._app.app_context(): # Setup. trips = [] _add_trip(trips=trips, trip_id=1, title='Tahoe', activity_name='Hike', start=datetime.date(2016, 1, 1), end=datetime.date(2016, 1, 10)) _insert_trips(trips) # Exercise. (count, trips) = trips_models.search(search_text='Tahoe') # Verify. self.assertEqual(1, count) self.assertEqual(1, len(trips))
def search_trips(): '''Gets a list of trips with the given parameters.''' title = request.args.get('title', '') from_date = _parse_date(request.args.get('fromDate', '')) to_date = _parse_date(request.args.get('toDate', '')) page_str = request.args.get('page', '') offset = _get_offset(page_str) min_price = _get_int(request.args.get('minPrice', '')) max_price = _get_int(request.args.get('maxPrice', '')) # TODO(Janet): change it to ngrams.corrections(title) when suggestions are better. suggested_text = '' (count, trips) = trips_models.search(search_text=title, from_date=from_date, to_date=to_date, min_price=min_price, max_price=max_price, offset=offset, limit=PAGE_SIZE) count_pages = int(math.ceil(count / float(PAGE_SIZE))) list_trips = [trip.as_dict() for trip in trips] return jsonify(trips=list_trips, count_pages=count_pages, count=count, suggested=suggested_text)
def test_find_with_limit(self): with self._app.app_context(): # Setup. trips = [] _add_trip(trips=trips, trip_id=1, title='Tahoe 1', activity_name='Hike', start=datetime.date(2016, 1, 1), end=datetime.date(2016, 1, 10)) _add_trip(trips=trips, trip_id=2, title='Tahoe 2', activity_name='Hike', start=datetime.date(2016, 1, 1), end=datetime.date(2016, 1, 10)) _add_trip(trips=trips, trip_id=3, title='Tahoe 3', activity_name='Hike', start=datetime.date(2016, 1, 1), end=datetime.date(2016, 1, 10)) _add_trip(trips=trips, trip_id=4, title='Tahoe 4', activity_name='Hike', start=datetime.date(2016, 1, 1), end=datetime.date(2016, 1, 10)) _insert_trips(trips) # Exercise. (count, trips) = trips_models.search(search_text='Tahoe', offset=1, limit=2) # Verify. self.assertEqual(4, count) self.assertEqual(2, len(trips)) self.assertEqual(2, trips[0].trip_id)
def test_find_by_from_to_dates(self): with self._app.app_context(): # Setup. trips = [] _add_trip(trips=trips, trip_id=1, title='Tahoe', activity_name='Hike', start=datetime.date(2016, 1, 1), end=datetime.date(2016, 1, 10)) _add_trip(trips=trips, trip_id=2, title='Tahoe', activity_name='Hike', start=datetime.date(2016, 2, 20), end=datetime.date(2016, 3, 29)) _add_trip(trips=trips, trip_id=3, title='Tahoe', activity_name='Hike', start=datetime.date(2016, 2, 21), end=datetime.date(2016, 4, 10)) _insert_trips(trips) # Exercise. (count, trips) = trips_models.search(from_date=datetime.date(2016, 2, 10), to_date=datetime.date(2016, 3, 30)) # Verify. self.assertEqual(1, count) self.assertEqual(1, len(trips)) self.assertEqual(2, trips[0].trip_id)
def test_search_by_price(self): with self._app.app_context(): # Setup. trips = [] _add_trip(trips=trips, trip_id=1, title='Tahoe', activity_name='Hike', start=datetime.date(2016, 1, 1), end=datetime.date(2016, 1, 10), from_price=100) _add_trip(trips=trips, trip_id=2, title='Yosemitee', activity_name='Hike', start=datetime.date(2016, 2, 20), end=datetime.date(2016, 3, 29), from_price=200) _add_trip(trips=trips, trip_id=3, title='Kings Canyon', activity_name='Hike', start=datetime.date(2016, 2, 21), end=datetime.date(2016, 4, 10), from_price=300) _insert_trips(trips) # Exercise. (count, trips) = trips_models.search(min_price=150, max_price=250) # Verify. self.assertEqual(1, count) self.assertEqual(1, len(trips)) self.assertEqual(2, trips[0].trip_id)