API - fix workouts sort on duration (= moving time)

This commit is contained in:
Sam 2022-10-26 22:01:00 +02:00
parent 7ea74d51e7
commit 1dc35c379d
2 changed files with 26 additions and 3 deletions

View File

@ -1,4 +1,5 @@
import json import json
from datetime import timedelta
from typing import List from typing import List
from unittest.mock import patch from unittest.mock import patch
from uuid import uuid4 from uuid import uuid4
@ -6,6 +7,7 @@ from uuid import uuid4
import pytest import pytest
from flask import Flask from flask import Flask
from fittrackee import db
from fittrackee.users.models import User from fittrackee.users.models import User
from fittrackee.workouts.models import Sport, Workout from fittrackee.workouts.models import Sport, Workout
@ -558,7 +560,7 @@ class TestGetWorkoutsWithOrderBy(ApiTestCaseMixin):
'total': 7, 'total': 7,
} }
def test_it_gets_workouts_ordered_by_duration( def test_it_gets_workouts_ordered_by_moving_time(
self, self,
app: Flask, app: Flask,
user_1: User, user_1: User,
@ -568,6 +570,10 @@ class TestGetWorkoutsWithOrderBy(ApiTestCaseMixin):
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email app, user_1.email
) )
seven_workouts_user_1[6].duration = seven_workouts_user_1[
6
].moving + timedelta(seconds=1000)
db.session.commit()
response = client.get( response = client.get(
'/api/workouts?order_by=duration', '/api/workouts?order_by=duration',
@ -579,7 +585,23 @@ class TestGetWorkoutsWithOrderBy(ApiTestCaseMixin):
assert 'success' in data['status'] assert 'success' in data['status']
assert len(data['data']['workouts']) == 5 assert len(data['data']['workouts']) == 5
assert '1:40:00' == data['data']['workouts'][0]['duration'] assert '1:40:00' == data['data']['workouts'][0]['duration']
assert '0:17:04' == data['data']['workouts'][4]['duration'] assert '1:40:00' == data['data']['workouts'][0]['moving']
assert (
'Sun, 01 Apr 2018 00:00:00 GMT'
== data['data']['workouts'][0]['workout_date']
)
assert '0:57:36' == data['data']['workouts'][1]['duration']
assert '0:57:36' == data['data']['workouts'][1]['moving']
assert (
'Thu, 01 Jun 2017 00:00:00 GMT'
== data['data']['workouts'][1]['workout_date']
)
assert '1:06:40' == data['data']['workouts'][2]['duration']
assert '0:50:00' == data['data']['workouts'][2]['moving']
assert (
'Wed, 09 May 2018 00:00:00 GMT'
== data['data']['workouts'][2]['workout_date']
)
assert data['pagination'] == { assert data['pagination'] == {
'has_next': True, 'has_next': True,
'has_prev': False, 'has_prev': False,

View File

@ -224,8 +224,9 @@ def get_workouts(auth_user: User) -> 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_by = params.get('order_by', 'workout_date')
workout_column = getattr( workout_column = getattr(
Workout, params.get('order_by', 'workout_date') Workout, 'moving' if order_by == 'duration' else order_by
) )
order = params.get('order', 'desc') order = params.get('order', 'desc')
sport_id = params.get('sport_id') sport_id = params.get('sport_id')