From c409f2cbabbe928b783ade12039d68365a2a1ec0 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 18 Jul 2023 18:57:38 +0200 Subject: [PATCH] API - do not return elevation in chart data when no elevation in file --- .../workouts/test_workouts_api_1_post.py | 70 ++++++++++++++++++- fittrackee/workouts/utils/gpx.py | 42 +++++------ 2 files changed, 87 insertions(+), 25 deletions(-) diff --git a/fittrackee/tests/workouts/test_workouts_api_1_post.py b/fittrackee/tests/workouts/test_workouts_api_1_post.py index ff360e5f..e349c63f 100644 --- a/fittrackee/tests/workouts/test_workouts_api_1_post.py +++ b/fittrackee/tests/workouts/test_workouts_api_1_post.py @@ -1721,7 +1721,65 @@ class TestPostAndGetWorkoutWithGpx(ApiTestCaseMixin): assert response.status_code == 200 assert 'success' in data['status'] assert data['message'] == '' - assert data['data']['chart_data'] != '' + assert len(data['data']['chart_data']) == gpx_file.count("") + assert data['data']['chart_data'][0] == { + 'distance': 0.0, + 'duration': 0, + 'elevation': 998.0, + 'latitude': 44.68095, + 'longitude': 6.07367, + 'speed': 3.21, + 'time': 'Tue, 13 Mar 2018 12:44:45 GMT', + } + + def test_it_gets_chart_data_for_a_workout_created_with_gpx_without_elevation( # noqa + 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()) + workout_short_id = data['data']['workouts'][0]['id'] + response = client.get( + f'/api/workouts/{workout_short_id}/chart_data', + headers=dict(Authorization=f'Bearer {auth_token}'), + ) + + data = json.loads(response.data.decode()) + assert response.status_code == 200 + assert 'success' in data['status'] + assert data['message'] == '' + assert len( + data['data']['chart_data'] + ) == gpx_file_without_elevation.count("") + # no 'elevation' key in data + assert data['data']['chart_data'][0] == { + 'distance': 0.0, + 'duration': 0, + 'latitude': 44.68095, + 'longitude': 6.07367, + 'speed': 3.21, + 'time': 'Tue, 13 Mar 2018 12:44:45 GMT', + } def test_it_gets_segment_chart_data_for_a_workout_created_with_gpx( self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str @@ -1753,6 +1811,16 @@ class TestPostAndGetWorkoutWithGpx(ApiTestCaseMixin): assert 'success' in data['status'] assert data['message'] == '' assert data['data']['chart_data'] != '' + assert len(data['data']['chart_data']) == gpx_file.count("") + assert data['data']['chart_data'][0] == { + 'distance': 0.0, + 'duration': 0, + 'elevation': 998.0, + 'latitude': 44.68095, + 'longitude': 6.07367, + 'speed': 3.21, + 'time': 'Tue, 13 Mar 2018 12:44:45 GMT', + } def test_it_returns_403_on_getting_chart_data_if_workout_belongs_to_another_user( # noqa self, diff --git a/fittrackee/workouts/utils/gpx.py b/fittrackee/workouts/utils/gpx.py index 409e1aa1..37cf9c6e 100644 --- a/fittrackee/workouts/utils/gpx.py +++ b/fittrackee/workouts/utils/gpx.py @@ -42,7 +42,7 @@ def get_gpx_data( gpx_data['elevation_max'] = ele.maximum gpx_data['elevation_min'] = ele.minimum - # gpx file contains elevation datat ( element) + # gpx file contains elevation data ( element) if ele.maximum is not None: hill = parsed_gpx.get_uphill_downhill() gpx_data['uphill'] = hill.uphill @@ -242,29 +242,23 @@ def get_chart_data( if segment.get_speed(point_idx) is not None else 0 ) - chart_data.append( - { - 'distance': ( - round(distance / 1000, 2) - if distance is not None - else 0 - ), - 'duration': point.time_difference(first_point), - 'elevation': ( - round(point.elevation, 1) - if point.elevation is not None - else 0 - ), - 'latitude': point.latitude, - 'longitude': point.longitude, - 'speed': speed, - # workaround - # https://github.com/tkrajina/gpxpy/issues/209 - 'time': point.time.replace( - tzinfo=timezone(point.time.utcoffset()) - ), - } - ) + data = { + 'distance': ( + round(distance / 1000, 2) if distance is not None else 0 + ), + 'duration': point.time_difference(first_point), + 'latitude': point.latitude, + 'longitude': point.longitude, + 'speed': speed, + # workaround + # https://github.com/tkrajina/gpxpy/issues/209 + 'time': point.time.replace( + tzinfo=timezone(point.time.utcoffset()) + ), + } + if point.elevation: + data['elevation'] = round(point.elevation, 1) + chart_data.append(data) previous_point = point previous_distance = distance