Esempio n. 1
0
 def test_determine_last_page(self, mock_read_database):
     test_data = [
         # (num_items, per_page, expected_last_page)
         (0, 10, 0),
         (1, 10, 1),
         (10, 10, 1),
         (11, 10, 2),
         (22, 3, 8),
     ]
     for num_items, per_page, expected_last_page in test_data:
         mock_read_database.return_value = [[num_items]]
         last_page = CustomResultData.determine_last_page(db=None, result_uuid='123-456-780', per_page=per_page)
         self.assertEqual(last_page, expected_last_page)
Esempio n. 2
0
def search_custom_results(result_id):
    """
    Search a result for predictions.
    request['maxPredictionSort'] - when true sort by max prediction
    request['all'] - include values in download
    request['page'] - which page of results to show
    request['perPage'] - items per page to show
    :param result_id: str: uuid of the custom_predictions/custom_preferences we want to search
    :return: json response with 'result' property containing an array of predictions
    """
    args = request.args
    search_args = SearchArgs(g_config.binding_max_offset, args)
    format = args.get('format')
    sort_by_max = args.get('maxPredictionSort')
    if sort_by_max == 'false':
        sort_by_max = None
    all_values = args.get('all')
    page = get_optional_int(args, 'page')
    per_page = get_optional_int(args, 'per_page')
    if search_args.is_last_page():
        page = CustomResultData.determine_last_page(get_db(), result_id,
                                                    per_page)
    offset = None
    if page and per_page:
        offset = (page - 1) * per_page

    predictions = CustomResultData.get_predictions(get_db(), result_id,
                                                   sort_by_max, per_page,
                                                   offset)
    if format == 'tsv' or format == 'csv':
        filename = "custom_result.{}".format(format)
        separator = ','
        if format == 'tsv':
            separator = '\t'
        return download_file_response(
            filename,
            make_download_custom_result(separator, all_values, predictions))
    else:
        return make_ok_json_response({'page': page, 'result': predictions})