API - get workouts ordered by distance, duration or average speed

This commit is contained in:
Sam 2021-11-02 08:55:21 +01:00
parent 8b74138447
commit 73a0b53dd7
2 changed files with 308 additions and 5 deletions

View File

@ -53,6 +53,13 @@ class TestGetWorkouts(ApiTestCaseMixin):
assert 1 == data['data']['workouts'][1]['sport_id'] assert 1 == data['data']['workouts'][1]['sport_id']
assert 10.0 == data['data']['workouts'][1]['distance'] assert 10.0 == data['data']['workouts'][1]['distance']
assert '1:00:00' == data['data']['workouts'][1]['duration'] assert '1:00:00' == data['data']['workouts'][1]['duration']
assert data['pagination'] == {
'has_next': False,
'has_prev': False,
'page': 1,
'pages': 1,
'total': 2,
}
def test_it_gets_no_workouts_for_authenticated_user_with_no_workouts( def test_it_gets_no_workouts_for_authenticated_user_with_no_workouts(
self, self,
@ -83,6 +90,13 @@ class TestGetWorkouts(ApiTestCaseMixin):
assert response.status_code == 200 assert response.status_code == 200
assert 'success' in data['status'] assert 'success' in data['status']
assert len(data['data']['workouts']) == 0 assert len(data['data']['workouts']) == 0
assert data['pagination'] == {
'has_next': False,
'has_prev': False,
'page': 1,
'pages': 0,
'total': 0,
}
def test_it_returns_401_if_user_is_not_authenticated( def test_it_returns_401_if_user_is_not_authenticated(
self, app: Flask self, app: Flask
@ -128,6 +142,13 @@ class TestGetWorkoutsWithPagination(ApiTestCaseMixin):
== data['data']['workouts'][4]['workout_date'] == data['data']['workouts'][4]['workout_date']
) )
assert '0:17:04' == data['data']['workouts'][4]['duration'] assert '0:17:04' == data['data']['workouts'][4]['duration']
assert data['pagination'] == {
'has_next': True,
'has_prev': False,
'page': 1,
'pages': 2,
'total': 7,
}
def test_it_gets_first_page( def test_it_gets_first_page(
self, self,
@ -159,6 +180,13 @@ class TestGetWorkoutsWithPagination(ApiTestCaseMixin):
== data['data']['workouts'][4]['workout_date'] == data['data']['workouts'][4]['workout_date']
) )
assert '0:17:04' == data['data']['workouts'][4]['duration'] assert '0:17:04' == data['data']['workouts'][4]['duration']
assert data['pagination'] == {
'has_next': True,
'has_prev': False,
'page': 1,
'pages': 2,
'total': 7,
}
def test_it_gets_second_page( def test_it_gets_second_page(
self, self,
@ -190,6 +218,13 @@ class TestGetWorkoutsWithPagination(ApiTestCaseMixin):
== data['data']['workouts'][1]['workout_date'] == data['data']['workouts'][1]['workout_date']
) )
assert '0:17:04' == data['data']['workouts'][1]['duration'] assert '0:17:04' == data['data']['workouts'][1]['duration']
assert data['pagination'] == {
'has_next': False,
'has_prev': True,
'page': 2,
'pages': 2,
'total': 7,
}
def test_it_gets_empty_third_page( def test_it_gets_empty_third_page(
self, self,
@ -209,6 +244,13 @@ class TestGetWorkoutsWithPagination(ApiTestCaseMixin):
assert response.status_code == 200 assert response.status_code == 200
assert 'success' in data['status'] assert 'success' in data['status']
assert len(data['data']['workouts']) == 0 assert len(data['data']['workouts']) == 0
assert data['pagination'] == {
'has_next': False,
'has_prev': True,
'page': 3,
'pages': 2,
'total': 7,
}
def test_it_returns_error_on_invalid_page_value( def test_it_returns_error_on_invalid_page_value(
self, self,
@ -259,6 +301,13 @@ class TestGetWorkoutsWithPagination(ApiTestCaseMixin):
'Thu, 01 Jun 2017 00:00:00 GMT' 'Thu, 01 Jun 2017 00:00:00 GMT'
== data['data']['workouts'][5]['workout_date'] == data['data']['workouts'][5]['workout_date']
) )
assert data['pagination'] == {
'has_next': True,
'has_prev': False,
'page': 1,
'pages': 2,
'total': 7,
}
@patch('fittrackee.workouts.workouts.MAX_WORKOUTS_PER_PAGE', 6) @patch('fittrackee.workouts.workouts.MAX_WORKOUTS_PER_PAGE', 6)
def test_it_gets_given_number_of_workouts_per_page( def test_it_gets_given_number_of_workouts_per_page(
@ -287,6 +336,13 @@ class TestGetWorkoutsWithPagination(ApiTestCaseMixin):
'Fri, 23 Feb 2018 00:00:00 GMT' 'Fri, 23 Feb 2018 00:00:00 GMT'
== data['data']['workouts'][2]['workout_date'] == data['data']['workouts'][2]['workout_date']
) )
assert data['pagination'] == {
'has_next': True,
'has_prev': False,
'page': 1,
'pages': 3,
'total': 7,
}
class TestGetWorkoutsWithOrder(ApiTestCaseMixin): class TestGetWorkoutsWithOrder(ApiTestCaseMixin):
@ -316,6 +372,13 @@ class TestGetWorkoutsWithOrder(ApiTestCaseMixin):
'Mon, 01 Jan 2018 00:00:00 GMT' 'Mon, 01 Jan 2018 00:00:00 GMT'
== data['data']['workouts'][4]['workout_date'] == data['data']['workouts'][4]['workout_date']
) )
assert data['pagination'] == {
'has_next': True,
'has_prev': False,
'page': 1,
'pages': 2,
'total': 7,
}
def test_it_gets_workouts_with_ascending_order( def test_it_gets_workouts_with_ascending_order(
self, self,
@ -343,6 +406,13 @@ class TestGetWorkoutsWithOrder(ApiTestCaseMixin):
'Fri, 23 Feb 2018 00:00:00 GMT' 'Fri, 23 Feb 2018 00:00:00 GMT'
== data['data']['workouts'][4]['workout_date'] == data['data']['workouts'][4]['workout_date']
) )
assert data['pagination'] == {
'has_next': True,
'has_prev': False,
'page': 1,
'pages': 2,
'total': 7,
}
def test_it_gets_workouts_with_descending_order( def test_it_gets_workouts_with_descending_order(
self, self,
@ -370,6 +440,133 @@ class TestGetWorkoutsWithOrder(ApiTestCaseMixin):
'Mon, 01 Jan 2018 00:00:00 GMT' 'Mon, 01 Jan 2018 00:00:00 GMT'
== data['data']['workouts'][4]['workout_date'] == data['data']['workouts'][4]['workout_date']
) )
assert data['pagination'] == {
'has_next': True,
'has_prev': False,
'page': 1,
'pages': 2,
'total': 7,
}
class TestGetWorkoutsWithOrderBy(ApiTestCaseMixin):
def test_it_gets_workouts_ordered_by_workout_date(
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
seven_workouts_user_1: Workout,
) -> None:
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts?order_by=workout_date',
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['workouts']) == 5
assert (
'Wed, 09 May 2018 00:00:00 GMT'
== data['data']['workouts'][0]['workout_date']
)
assert (
'Mon, 01 Jan 2018 00:00:00 GMT'
== data['data']['workouts'][4]['workout_date']
)
assert data['pagination'] == {
'has_next': True,
'has_prev': False,
'page': 1,
'pages': 2,
'total': 7,
}
def test_it_gets_workouts_ordered_by_distance(
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
seven_workouts_user_1: Workout,
) -> None:
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts?order_by=distance',
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['workouts']) == 5
assert 10 == data['data']['workouts'][0]['distance']
assert 8 == data['data']['workouts'][4]['distance']
assert data['pagination'] == {
'has_next': True,
'has_prev': False,
'page': 1,
'pages': 2,
'total': 7,
}
def test_it_gets_workouts_ordered_by_duration(
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
seven_workouts_user_1: Workout,
) -> None:
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts?order_by=duration',
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['workouts']) == 5
assert '1:40:00' == data['data']['workouts'][0]['duration']
assert '0:17:04' == data['data']['workouts'][4]['duration']
assert data['pagination'] == {
'has_next': True,
'has_prev': False,
'page': 1,
'pages': 2,
'total': 7,
}
def test_it_gets_workouts_ordered_by_average_speed(
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
seven_workouts_user_1: Workout,
) -> None:
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts?order_by=ave_speed',
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['workouts']) == 5
assert 36 == data['data']['workouts'][0]['ave_speed']
assert 10.42 == data['data']['workouts'][4]['ave_speed']
assert data['pagination'] == {
'has_next': True,
'has_prev': False,
'page': 1,
'pages': 2,
'total': 7,
}
class TestGetWorkoutsWithFilters(ApiTestCaseMixin): class TestGetWorkoutsWithFilters(ApiTestCaseMixin):
@ -403,6 +600,13 @@ class TestGetWorkoutsWithFilters(ApiTestCaseMixin):
== data['data']['workouts'][1]['workout_date'] == data['data']['workouts'][1]['workout_date']
) )
assert '0:16:40' == data['data']['workouts'][1]['duration'] assert '0:16:40' == data['data']['workouts'][1]['duration']
assert data['pagination'] == {
'has_next': False,
'has_prev': False,
'page': 1,
'pages': 1,
'total': 2,
}
def test_it_gets_no_workouts_with_date_filter( def test_it_gets_no_workouts_with_date_filter(
self, self,
@ -422,6 +626,13 @@ class TestGetWorkoutsWithFilters(ApiTestCaseMixin):
assert response.status_code == 200 assert response.status_code == 200
assert 'success' in data['status'] assert 'success' in data['status']
assert len(data['data']['workouts']) == 0 assert len(data['data']['workouts']) == 0
assert data['pagination'] == {
'has_next': False,
'has_prev': False,
'page': 1,
'pages': 0,
'total': 0,
}
def test_if_gets_workouts_with_date_filter_from( def test_if_gets_workouts_with_date_filter_from(
self, self,
@ -450,6 +661,13 @@ class TestGetWorkoutsWithFilters(ApiTestCaseMixin):
'Sun, 01 Apr 2018 00:00:00 GMT' 'Sun, 01 Apr 2018 00:00:00 GMT'
== data['data']['workouts'][1]['workout_date'] == data['data']['workouts'][1]['workout_date']
) )
assert data['pagination'] == {
'has_next': False,
'has_prev': False,
'page': 1,
'pages': 1,
'total': 2,
}
def test_it_gets_workouts_with_date_filter_to( def test_it_gets_workouts_with_date_filter_to(
self, self,
@ -477,6 +695,13 @@ class TestGetWorkoutsWithFilters(ApiTestCaseMixin):
'Mon, 20 Mar 2017 00:00:00 GMT' 'Mon, 20 Mar 2017 00:00:00 GMT'
== data['data']['workouts'][1]['workout_date'] == data['data']['workouts'][1]['workout_date']
) )
assert data['pagination'] == {
'has_next': False,
'has_prev': False,
'page': 1,
'pages': 1,
'total': 2,
}
def test_it_gets_workouts_with_distance_filter( def test_it_gets_workouts_with_distance_filter(
self, self,
@ -504,6 +729,13 @@ class TestGetWorkoutsWithFilters(ApiTestCaseMixin):
'Mon, 20 Mar 2017 00:00:00 GMT' 'Mon, 20 Mar 2017 00:00:00 GMT'
== data['data']['workouts'][1]['workout_date'] == data['data']['workouts'][1]['workout_date']
) )
assert data['pagination'] == {
'has_next': False,
'has_prev': False,
'page': 1,
'pages': 1,
'total': 2,
}
def test_it_gets_workouts_with_duration_filter( def test_it_gets_workouts_with_duration_filter(
self, self,
@ -527,6 +759,13 @@ class TestGetWorkoutsWithFilters(ApiTestCaseMixin):
'Thu, 01 Jun 2017 00:00:00 GMT' 'Thu, 01 Jun 2017 00:00:00 GMT'
== data['data']['workouts'][0]['workout_date'] == data['data']['workouts'][0]['workout_date']
) )
assert data['pagination'] == {
'has_next': False,
'has_prev': False,
'page': 1,
'pages': 1,
'total': 1,
}
def test_it_gets_workouts_with_average_speed_filter( def test_it_gets_workouts_with_average_speed_filter(
self, self,
@ -550,6 +789,13 @@ class TestGetWorkoutsWithFilters(ApiTestCaseMixin):
'Fri, 23 Feb 2018 00:00:00 GMT' 'Fri, 23 Feb 2018 00:00:00 GMT'
== data['data']['workouts'][0]['workout_date'] == data['data']['workouts'][0]['workout_date']
) )
assert data['pagination'] == {
'has_next': False,
'has_prev': False,
'page': 1,
'pages': 1,
'total': 1,
}
def test_it_gets_workouts_with_max_speed_filter( def test_it_gets_workouts_with_max_speed_filter(
self, self,
@ -577,6 +823,13 @@ class TestGetWorkoutsWithFilters(ApiTestCaseMixin):
'Sun, 01 Apr 2018 00:00:00 GMT' 'Sun, 01 Apr 2018 00:00:00 GMT'
== data['data']['workouts'][0]['workout_date'] == data['data']['workouts'][0]['workout_date']
) )
assert data['pagination'] == {
'has_next': False,
'has_prev': False,
'page': 1,
'pages': 1,
'total': 1,
}
def test_it_gets_workouts_with_sport_filter( def test_it_gets_workouts_with_sport_filter(
self, self,
@ -602,6 +855,13 @@ class TestGetWorkoutsWithFilters(ApiTestCaseMixin):
'Sun, 01 Apr 2018 00:00:00 GMT' 'Sun, 01 Apr 2018 00:00:00 GMT'
== data['data']['workouts'][0]['workout_date'] == data['data']['workouts'][0]['workout_date']
) )
assert data['pagination'] == {
'has_next': False,
'has_prev': False,
'page': 1,
'pages': 1,
'total': 1,
}
class TestGetWorkoutsWithFiltersAndPagination(ApiTestCaseMixin): class TestGetWorkoutsWithFiltersAndPagination(ApiTestCaseMixin):
@ -631,6 +891,13 @@ class TestGetWorkoutsWithFiltersAndPagination(ApiTestCaseMixin):
'Mon, 20 Mar 2017 00:00:00 GMT' 'Mon, 20 Mar 2017 00:00:00 GMT'
== data['data']['workouts'][1]['workout_date'] == data['data']['workouts'][1]['workout_date']
) )
assert data['pagination'] == {
'has_next': False,
'has_prev': True,
'page': 2,
'pages': 2,
'total': 7,
}
def test_it_get_page_2_with_date_filter_and_ascending_order( def test_it_get_page_2_with_date_filter_and_ascending_order(
self, self,
@ -658,6 +925,13 @@ class TestGetWorkoutsWithFiltersAndPagination(ApiTestCaseMixin):
'Wed, 09 May 2018 00:00:00 GMT' 'Wed, 09 May 2018 00:00:00 GMT'
== data['data']['workouts'][1]['workout_date'] == data['data']['workouts'][1]['workout_date']
) )
assert data['pagination'] == {
'has_next': False,
'has_prev': True,
'page': 2,
'pages': 2,
'total': 7,
}
class TestGetWorkout(ApiTestCaseMixin): class TestGetWorkout(ApiTestCaseMixin):

View File

@ -206,12 +206,13 @@ def get_workouts(auth_user_id: int) -> Union[Dict, HttpResponse]:
ave_speed_to = params.get('ave_speed_to') ave_speed_to = params.get('ave_speed_to')
max_speed_from = params.get('max_speed_from') max_speed_from = params.get('max_speed_from')
max_speed_to = params.get('max_speed_to') max_speed_to = params.get('max_speed_to')
order = params.get('order') order_by = params.get('order_by', 'workout_date')
order = params.get('order', 'desc')
sport_id = params.get('sport_id') sport_id = params.get('sport_id')
per_page = int(params.get('per_page', DEFAULT_WORKOUTS_PER_PAGE)) per_page = int(params.get('per_page', DEFAULT_WORKOUTS_PER_PAGE))
if per_page > MAX_WORKOUTS_PER_PAGE: if per_page > MAX_WORKOUTS_PER_PAGE:
per_page = MAX_WORKOUTS_PER_PAGE per_page = MAX_WORKOUTS_PER_PAGE
workouts = ( workouts_pagination = (
Workout.query.filter( Workout.query.filter(
Workout.user_id == auth_user_id, Workout.user_id == auth_user_id,
Workout.sport_id == sport_id if sport_id else True, Workout.sport_id == sport_id if sport_id else True,
@ -243,18 +244,46 @@ def get_workouts(auth_user_id: int) -> Union[Dict, HttpResponse]:
else True, else True,
) )
.order_by( .order_by(
Workout.ave_speed.asc()
if order_by == 'ave_speed' and order == 'asc'
else True,
Workout.ave_speed.desc()
if order_by == 'ave_speed' and order == 'desc'
else True,
Workout.distance.asc()
if order_by == 'distance' and order == 'asc'
else True,
Workout.distance.desc()
if order_by == 'distance' and order == 'desc'
else True,
Workout.duration.asc()
if order_by == 'duration' and order == 'asc'
else True,
Workout.duration.desc()
if order_by == 'duration' and order == 'desc'
else True,
Workout.workout_date.asc() Workout.workout_date.asc()
if order == 'asc' if order_by == 'workout_date' and order == 'asc'
else Workout.workout_date.desc() else True,
Workout.workout_date.desc()
if order_by == 'workout_date' and order == 'desc'
else True,
) )
.paginate(page, per_page, False) .paginate(page, per_page, False)
.items
) )
workouts = workouts_pagination.items
return { return {
'status': 'success', 'status': 'success',
'data': { 'data': {
'workouts': [workout.serialize(params) for workout in workouts] 'workouts': [workout.serialize(params) for workout in workouts]
}, },
'pagination': {
'has_next': workouts_pagination.has_next,
'has_prev': workouts_pagination.has_prev,
'page': workouts_pagination.page,
'pages': workouts_pagination.pages,
'total': workouts_pagination.total,
},
} }
except Exception as e: except Exception as e:
return handle_error_and_return_response(e) return handle_error_and_return_response(e)