From ee67b74d061b28de0eae898cd7c13db0f5e1ce2c Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 19 Jul 2022 16:33:05 +0200 Subject: [PATCH] API - fix total ascent when user has workous without gpx --- fittrackee/tests/users/test_users_model.py | 41 ++++++++++++++++++++++ fittrackee/users/models.py | 4 +-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/fittrackee/tests/users/test_users_model.py b/fittrackee/tests/users/test_users_model.py index 93a5e0c1..cd8867f0 100644 --- a/fittrackee/tests/users/test_users_model.py +++ b/fittrackee/tests/users/test_users_model.py @@ -38,6 +38,7 @@ class UserModelAssertMixin: assert 'nb_workouts' in serialized_user assert 'records' in serialized_user assert 'sports_list' in serialized_user + assert 'total_ascent' in serialized_user assert 'total_distance' in serialized_user assert 'total_duration' in serialized_user @@ -168,6 +169,46 @@ class TestUserRecords(UserModelAssertMixin): ) assert serialized_user['records'][0]['workout_date'] + def test_it_returns_totals_when_user_has_workout_without_ascent( + self, + app: Flask, + user_1: User, + sport_1_cycling: Sport, + workout_cycling_user_1: Workout, + ) -> None: + serialized_user = user_1.serialize(user_1) + assert serialized_user['total_ascent'] == 0 + assert serialized_user['total_distance'] == 10 + assert serialized_user['total_duration'] == '1:00:00' + + def test_it_returns_totals_when_user_has_workout_with_ascent( + self, + app: Flask, + user_1: User, + sport_1_cycling: Sport, + workout_cycling_user_1: Workout, + ) -> None: + workout_cycling_user_1.ascent = 100 + serialized_user = user_1.serialize(user_1) + assert serialized_user['total_ascent'] == 100 + assert serialized_user['total_distance'] == 10 + assert serialized_user['total_duration'] == '1:00:00' + + def test_it_returns_totals_when_user_has_mutiple_workouts( + self, + app: Flask, + user_1: User, + sport_1_cycling: Sport, + sport_2_running: Sport, + workout_cycling_user_1: Workout, + workout_running_user_1: Workout, + ) -> None: + workout_cycling_user_1.ascent = 100 + serialized_user = user_1.serialize(user_1) + assert serialized_user['total_ascent'] == 100 + assert serialized_user['total_distance'] == 22 + assert serialized_user['total_duration'] == '2:40:00' + class TestUserWorkouts(UserModelAssertMixin): def test_it_returns_infos_when_no_workouts( diff --git a/fittrackee/users/models.py b/fittrackee/users/models.py index be614ba0..a240541c 100644 --- a/fittrackee/users/models.py +++ b/fittrackee/users/models.py @@ -127,7 +127,7 @@ class User(BaseModel): raise UserNotFoundException() sports = [] - total = (0, '0:00:00') + total = (0, '0:00:00', 0) if self.workouts_count > 0: # type: ignore sports = ( db.session.query(Workout.sport_id) @@ -164,9 +164,9 @@ class User(BaseModel): 'sports_list': [ sport for sportslist in sports for sport in sportslist ], + 'total_ascent': float(total[2]) if total[2] else 0.0, 'total_distance': float(total[0]), 'total_duration': str(total[1]), - 'total_ascent': float(total[2]), 'username': self.username, } if role == UserRole.AUTH_USER: