API - fix total ascent when user has workous without gpx

This commit is contained in:
Sam 2022-07-19 16:33:05 +02:00
parent 0cb4a26cf2
commit ee67b74d06
2 changed files with 43 additions and 2 deletions

View File

@ -38,6 +38,7 @@ class UserModelAssertMixin:
assert 'nb_workouts' in serialized_user assert 'nb_workouts' in serialized_user
assert 'records' in serialized_user assert 'records' in serialized_user
assert 'sports_list' in serialized_user assert 'sports_list' in serialized_user
assert 'total_ascent' in serialized_user
assert 'total_distance' in serialized_user assert 'total_distance' in serialized_user
assert 'total_duration' in serialized_user assert 'total_duration' in serialized_user
@ -168,6 +169,46 @@ class TestUserRecords(UserModelAssertMixin):
) )
assert serialized_user['records'][0]['workout_date'] 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): class TestUserWorkouts(UserModelAssertMixin):
def test_it_returns_infos_when_no_workouts( def test_it_returns_infos_when_no_workouts(

View File

@ -127,7 +127,7 @@ class User(BaseModel):
raise UserNotFoundException() raise UserNotFoundException()
sports = [] sports = []
total = (0, '0:00:00') total = (0, '0:00:00', 0)
if self.workouts_count > 0: # type: ignore if self.workouts_count > 0: # type: ignore
sports = ( sports = (
db.session.query(Workout.sport_id) db.session.query(Workout.sport_id)
@ -164,9 +164,9 @@ class User(BaseModel):
'sports_list': [ 'sports_list': [
sport for sportslist in sports for sport in sportslist 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_distance': float(total[0]),
'total_duration': str(total[1]), 'total_duration': str(total[1]),
'total_ascent': float(total[2]),
'username': self.username, 'username': self.username,
} }
if role == UserRole.AUTH_USER: if role == UserRole.AUTH_USER: