API - fix max workouts per page in order to display up to 100 workouts

This commit is contained in:
Sam 2021-02-17 08:37:32 +01:00
parent b72959338a
commit d27385b24d
4 changed files with 16 additions and 12 deletions

View File

@ -249,7 +249,7 @@
<dd class="field-even"><ul class="simple">
<li><p><strong>page</strong> (<em>integer</em>) page if using pagination (default: 1)</p></li>
<li><p><strong>per_page</strong> (<em>integer</em>) number of workouts per page
(default: 5, max: 50)</p></li>
(default: 5, max: 100)</p></li>
<li><p><strong>sport_id</strong> (<em>integer</em>) sport id</p></li>
<li><p><strong>from</strong> (<em>string</em>) start date (format: <code class="docutils literal notranslate"><span class="pre">%Y-%m-%d</span></code>)</p></li>
<li><p><strong>to</strong> (<em>string</em>) end date (format: <code class="docutils literal notranslate"><span class="pre">%Y-%m-%d</span></code>)</p></li>

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,5 @@
import json
from unittest.mock import patch
from uuid import uuid4
from flask import Flask
@ -278,7 +279,8 @@ class TestGetWorkoutsWithPagination:
in data['message']
)
def test_it_gets_5_workouts_per_page(
@patch('fittrackee.workouts.workouts.MAX_WORKOUTS_PER_PAGE', 6)
def test_it_gets_max_workouts_per_page_if_per_page_exceeds_max(
self,
app: Flask,
user_1: User,
@ -303,17 +305,18 @@ class TestGetWorkoutsWithPagination:
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['workouts']) == 7
assert len(data['data']['workouts']) == 6
assert (
'Wed, 09 May 2018 00:00:00 GMT'
== data['data']['workouts'][0]['workout_date']
)
assert (
'Mon, 20 Mar 2017 00:00:00 GMT'
== data['data']['workouts'][6]['workout_date']
'Thu, 01 Jun 2017 00:00:00 GMT'
== data['data']['workouts'][5]['workout_date']
)
def test_it_gets_3_workouts_per_page(
@patch('fittrackee.workouts.workouts.MAX_WORKOUTS_PER_PAGE', 6)
def test_it_gets_given_number_of_workouts_per_page(
self,
app: Flask,
user_1: User,

View File

@ -44,7 +44,8 @@ from .utils_id import decode_short_id
workouts_blueprint = Blueprint('workouts', __name__)
WORKOUTS_PER_PAGE = 5
DEFAULT_WORKOUTS_PER_PAGE = 5
MAX_WORKOUTS_PER_PAGE = 100
@workouts_blueprint.route('/workouts', methods=['GET'])
@ -168,7 +169,7 @@ def get_workouts(auth_user_id: int) -> Union[Dict, HttpResponse]:
:query integer page: page if using pagination (default: 1)
:query integer per_page: number of workouts per page
(default: 5, max: 50)
(default: 5, max: 100)
:query integer sport_id: sport id
:query string from: start date (format: ``%Y-%m-%d``)
:query string to: end date (format: ``%Y-%m-%d``)
@ -219,10 +220,10 @@ def get_workouts(auth_user_id: int) -> Union[Dict, HttpResponse]:
per_page = (
int(params.get('per_page'))
if params.get('per_page')
else WORKOUTS_PER_PAGE
else DEFAULT_WORKOUTS_PER_PAGE
)
if per_page > 50:
per_page = 50
if per_page > MAX_WORKOUTS_PER_PAGE:
per_page = MAX_WORKOUTS_PER_PAGE
workouts = (
Workout.query.filter(
Workout.user_id == auth_user_id,