From 7d30fba00b19eacb106047b79b3eecfbadd7155c Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 18 Jul 2023 18:48:53 +0200 Subject: [PATCH] API - return ascent/descent to null when no elevation data in gpx file --- .../tests/fixtures/fixtures_workouts.py | 90 +++++++++++++++++++ .../workouts/test_workouts_api_1_post.py | 39 ++++++++ fittrackee/workouts/utils/gpx.py | 11 ++- 3 files changed, 137 insertions(+), 3 deletions(-) diff --git a/fittrackee/tests/fixtures/fixtures_workouts.py b/fittrackee/tests/fixtures/fixtures_workouts.py index 23d6c04f..7a848512 100644 --- a/fittrackee/tests/fixtures/fixtures_workouts.py +++ b/fittrackee/tests/fixtures/fixtures_workouts.py @@ -575,6 +575,96 @@ def gpx_file_with_offset() -> str: ) +@pytest.fixture() +def gpx_file_without_elevation() -> str: + return ( + '' + '' # noqa + ' ' + ' ' + ' just a workout' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + '' + ) + + @pytest.fixture() def gpx_file_wo_track() -> str: return ( diff --git a/fittrackee/tests/workouts/test_workouts_api_1_post.py b/fittrackee/tests/workouts/test_workouts_api_1_post.py index 8d81ff60..ff360e5f 100644 --- a/fittrackee/tests/workouts/test_workouts_api_1_post.py +++ b/fittrackee/tests/workouts/test_workouts_api_1_post.py @@ -499,6 +499,45 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin): assert len(data['data']['workouts']) == 1 assert_workout_data_with_gpx(data) + def test_it_adds_a_workout_without_elevation( + self, + app: Flask, + user_1: User, + sport_1_cycling: Sport, + gpx_file_without_elevation: str, + ) -> None: + client, auth_token = self.get_test_client_and_auth_token( + app, user_1.email + ) + + response = client.post( + '/api/workouts', + data=dict( + file=( + BytesIO(str.encode(gpx_file_without_elevation)), + 'example.gpx', + ), + data='{"sport_id": 1}', + ), + headers=dict( + content_type='multipart/form-data', + Authorization=f'Bearer {auth_token}', + ), + ) + + data = json.loads(response.data.decode()) + assert response.status_code == 201 + workout = data['data']['workouts'][0] + assert workout['duration'] == '0:04:10' + assert workout['ascent'] is None + assert workout['ave_speed'] == 4.57 + assert workout['descent'] is None + assert workout['distance'] == 0.317 + assert workout['max_alt'] is None + assert workout['max_speed'] == 5.1 + assert workout['min_alt'] is None + assert workout['with_gpx'] is True + def test_it_returns_400_when_quotes_are_not_escaped_in_notes( self, app: Flask, diff --git a/fittrackee/workouts/utils/gpx.py b/fittrackee/workouts/utils/gpx.py index 8cbc7666..409e1aa1 100644 --- a/fittrackee/workouts/utils/gpx.py +++ b/fittrackee/workouts/utils/gpx.py @@ -42,9 +42,14 @@ def get_gpx_data( gpx_data['elevation_max'] = ele.maximum gpx_data['elevation_min'] = ele.minimum - hill = parsed_gpx.get_uphill_downhill() - gpx_data['uphill'] = hill.uphill - gpx_data['downhill'] = hill.downhill + # gpx file contains elevation datat ( element) + if ele.maximum is not None: + hill = parsed_gpx.get_uphill_downhill() + gpx_data['uphill'] = hill.uphill + gpx_data['downhill'] = hill.downhill + else: + gpx_data['uphill'] = None + gpx_data['downhill'] = None moving_data = parsed_gpx.get_moving_data( stopped_speed_threshold=stopped_speed_threshold