def test_sorted_result_by_rating(self): """Tests returning sorted results by highest rating.""" result = [{"id": "ryokos-san-francisco", "rating": 4.0}, {"id": "saru-sushi-bar-san-francisco", "rating": 4.5}] result_sorted = sorted_result(result, "rating") self.assertEqual(result_sorted[0]["rating"], 4.5)
def test_sorted_result_by_wait_time(self): """Tests returning sorted results by shortest wait time.""" result = [{"id": "saru-sushi-bar-san-francisco", "quoted_wait_time": 15}, {"id": "ryokos-san-francisco", "quoted_wait_time": 1}] result_sorted = sorted_result(result, "wait_time") self.assertEqual(result_sorted[0]["quoted_wait_time"], 1)
def test_sorted_result_by_review_count(self): """Tests returning sorted results by highest review count.""" result = [{"id": "saru-sushi-bar-san-francisco", "review_count": 547}, {"id": "ryokos-san-francisco", "review_count": 1983}] result_sorted = sorted_result(result, "review_count") self.assertEqual(result_sorted[0]["review_count"], 1983)
def test_sorted_result_by_recently_reported(self): """Tests returning sorted results by most recently reported wait time.""" result = [{"id": "ryokos-san-francisco", "timestamp_value": datetime(2016, 3, 1, 1, 27, 53, 200319), "timestamp": "18 hours ago"}, {"id": "saru-sushi-bar-san-francisco", "timestamp_value": datetime(2016, 3, 1, 20, 5, 55, 643065), "timestamp": "2 minutes ago"}] result_sorted = sorted_result(result, "recently_reported") self.assertEqual(result_sorted[0]["timestamp_value"], datetime(2016, 3, 1, 20, 5, 55, 643065))
def display_search_results(): """Display search results.""" search_term = request.args.get("keyword") location_term = request.args.get("location") if not location_term: location_term = "San Francisco" # Yelp API call with user input values search_results = yelp.search_query(term=search_term, location=location_term, category_filter="food,restaurants", limit=10) # result is the list of restaurant dictionaries result = search_results['businesses'] # For each restaurant, add its open status and wait time info to their dictionary for restaurant in result: add_open_status(restaurant) add_wait_info(restaurant) # Handle sorting sort_value = request.args.get("sort_by") result = sorted_result(result, sort_value) # Handle filtering selected_filters = request.args.getlist("filter_by") result = filtered_result(result, selected_filters) # Add result to result_dict, which will be converted to a JSON through Jinja result_dict = {'result': result} return render_template("results.html", result=result, key=BROWSER_KEY, result_dict=result_dict, search_term=search_term, location_term=location_term)