API - add elevation to workout w/o gpx file when provided

This commit is contained in:
Sam
2022-12-14 14:40:29 +01:00
parent fbda86e123
commit 8a0e1d2b40
5 changed files with 186 additions and 5 deletions

View File

@ -886,6 +886,49 @@ class TestPostWorkoutWithoutGpx(ApiTestCaseMixin):
assert len(data['data']['workouts']) == 1
assert_workout_data_wo_gpx(data)
@pytest.mark.parametrize(
'input_ascent, input_descent',
[
(100, 150),
(0, 150),
(100, 0),
],
)
def test_it_adds_workout_with_ascent_and_descent_when_provided(
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
input_ascent: int,
input_descent: int,
) -> 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=3600,
workout_date='2018-05-15 14:05',
distance=10,
ascent=input_ascent,
descent=input_descent,
)
),
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]['ascent'] == input_ascent
assert data['data']['workouts'][0]['descent'] == input_descent
@pytest.mark.parametrize(
'description,input_data',
[
@ -940,6 +983,82 @@ class TestPostWorkoutWithoutGpx(ApiTestCaseMixin):
self.assert_400(response)
@pytest.mark.parametrize(
'description,input_data',
[
("only ascent", {"ascent": 100}),
("only descent", {"descent": 150}),
],
)
def test_it_returns_400_when_ascent_or_descent_are_missing(
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
description: str,
input_data: Dict,
) -> 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(
{
'sport_id': 1,
'duration': 3600,
'workout_date': '2018-05-15 14:05',
'distance': 10,
**input_data,
}
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
self.assert_400(response)
@pytest.mark.parametrize(
'description,input_data',
[
("ascent is below 0", {"ascent": -100, "descent": 100}),
("descent is below 0", {"ascent": 150, "descent": -100}),
("ascent is None", {"ascent": None, "descent": 100}),
("descent is None", {"ascent": 150, "descent": None}),
("ascent is invalid", {"ascent": "a", "descent": 100}),
("descent is invalid", {"ascent": 150, "descent": "b"}),
],
)
def test_it_returns_400_when_ascent_or_descent_are_invalid(
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
description: str,
input_data: Dict,
) -> 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(
{
'sport_id': 1,
'duration': 3600,
'workout_date': '2018-05-15 14:05',
'distance': 10,
**input_data,
}
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
self.assert_400(response)
def test_it_returns_500_if_workout_date_format_is_invalid(
self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None:

View File

@ -75,6 +75,48 @@ class TestWorkoutModel:
assert serialized_workout['with_gpx'] is False
assert str(serialized_workout['workout_date']) == '2018-01-01 00:00:00'
def test_serialize_for_workout_without_gpx_and_with_elevation(
self,
app: Flask,
sport_1_cycling: Sport,
user_1: User,
workout_cycling_user_1: Workout,
) -> None:
workout = workout_cycling_user_1
workout.ascent = 0
workout.descent = 10
serialized_workout = workout.serialize()
assert serialized_workout['ascent'] == workout.ascent
assert serialized_workout['ave_speed'] == float(workout.ave_speed)
assert serialized_workout['bounds'] == []
assert 'creation_date' in serialized_workout
assert serialized_workout['descent'] == workout.descent
assert serialized_workout['distance'] == float(workout.distance)
assert serialized_workout['duration'] == str(workout.duration)
assert serialized_workout['id'] == workout.short_id
assert serialized_workout['map'] is None
assert serialized_workout['max_alt'] is None
assert serialized_workout['max_speed'] == float(workout.max_speed)
assert serialized_workout['min_alt'] is None
assert serialized_workout['modification_date'] is not None
assert serialized_workout['moving'] == str(workout.moving)
assert serialized_workout['next_workout'] is None
assert serialized_workout['notes'] is None
assert serialized_workout['pauses'] is None
assert serialized_workout['previous_workout'] is None
assert serialized_workout['records'] == [
record.serialize() for record in workout.records
]
assert serialized_workout['segments'] == []
assert serialized_workout['sport_id'] == workout.sport_id
assert serialized_workout['title'] == workout.title
assert serialized_workout['user'] == workout.user.username
assert serialized_workout['weather_end'] is None
assert serialized_workout['weather_start'] is None
assert serialized_workout['with_gpx'] is False
assert str(serialized_workout['workout_date']) == '2018-01-01 00:00:00'
def test_serialize_for_workout_with_gpx(
self,
app: Flask,