API - fix workout serializer when speed value is 0

This commit is contained in:
Sam 2023-07-26 17:52:28 +02:00
parent c385d8066a
commit 4af294cd4a
2 changed files with 39 additions and 2 deletions

View File

@ -1132,6 +1132,39 @@ class TestPostWorkoutWithoutGpx(ApiTestCaseMixin):
assert data['data']['workouts'][0]['ascent'] == input_ascent assert data['data']['workouts'][0]['ascent'] == input_ascent
assert data['data']['workouts'][0]['descent'] == input_descent assert data['data']['workouts'][0]['descent'] == input_descent
def test_it_adds_workout_with_low_value_for_distance(
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
) -> None:
client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post(
'/api/workouts/no_gpx',
content_type='application/json',
data=json.dumps(
dict(
sport_id=1,
duration=1200,
workout_date='2023-07-26 12:00',
distance=0.001,
)
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 201
assert 'created' in data['status']
assert len(data['data']['workouts']) == 1
assert data['data']['workouts'][0]['ave_speed'] == 0
assert data['data']['workouts'][0]['distance'] == 0.001
assert data['data']['workouts'][0]['duration'] == '0:20:00'
assert data['data']['workouts'][0]['max_speed'] == 0
@pytest.mark.parametrize( @pytest.mark.parametrize(
'description,input_data', 'description,input_data',
[ [

View File

@ -210,8 +210,12 @@ class Workout(BaseModel):
if self.descent is not None if self.descent is not None
else None, else None,
'ascent': float(self.ascent) if self.ascent is not None else None, 'ascent': float(self.ascent) if self.ascent is not None else None,
'max_speed': float(self.max_speed) if self.max_speed else None, 'max_speed': (
'ave_speed': float(self.ave_speed) if self.ave_speed else None, float(self.max_speed) if self.max_speed is not None else None
),
'ave_speed': (
float(self.ave_speed) if self.ave_speed is not None else None
),
'records': [record.serialize() for record in self.records], 'records': [record.serialize() for record in self.records],
'segments': [segment.serialize() for segment in self.segments], 'segments': [segment.serialize() for segment in self.segments],
'weather_start': self.weather_start, 'weather_start': self.weather_start,