API - do not return elevation in chart data when no elevation in file

This commit is contained in:
Sam 2023-07-18 18:57:38 +02:00
parent 7d30fba00b
commit c409f2cbab
2 changed files with 87 additions and 25 deletions

View File

@ -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("</trkpt>")
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("</trkpt>")
# 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("</trkpt>")
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,

View File

@ -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 (<ele> element)
# gpx file contains elevation data (<ele> 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