API - fix HA record when workout w/o gpx already exists

This commit is contained in:
Sam 2022-07-19 10:42:37 +02:00
parent 4488c44297
commit 4322b01966
2 changed files with 43 additions and 1 deletions

View File

@ -70,6 +70,7 @@ def assert_workout_data_with_gpx(data: Dict) -> None:
assert records[2]['sport_id'] == 1 assert records[2]['sport_id'] == 1
assert records[2]['workout_id'] == data['data']['workouts'][0]['id'] assert records[2]['workout_id'] == data['data']['workouts'][0]['id']
assert records[2]['record_type'] == 'HA' assert records[2]['record_type'] == 'HA'
assert records[2]['value'] == 0.4
assert records[2]['workout_date'] == 'Tue, 13 Mar 2018 12:44:45 GMT' assert records[2]['workout_date'] == 'Tue, 13 Mar 2018 12:44:45 GMT'
assert records[3]['sport_id'] == 1 assert records[3]['sport_id'] == 1
assert records[3]['workout_id'] == data['data']['workouts'][0]['id'] assert records[3]['workout_id'] == data['data']['workouts'][0]['id']
@ -260,6 +261,39 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
assert 'just a workout' == data['data']['workouts'][0]['title'] assert 'just a workout' == data['data']['workouts'][0]['title']
assert_workout_data_with_gpx(data) assert_workout_data_with_gpx(data)
def test_it_returns_ha_record_when_a_workout_without_gpx_exists(
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
gpx_file: str,
workout_cycling_user_1: Workout,
) -> 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)), 'example.gpx'),
data='{"sport_id": 1}',
),
headers=dict(
content_type='multipart/form-data',
Authorization=f'Bearer {auth_token}',
),
)
data = json.loads(response.data.decode())
records = data['data']['workouts'][0]['records']
assert len(records) == 1
assert records[0]['sport_id'] == 1
assert records[0]['workout_id'] == data['data']['workouts'][0]['id']
assert records[0]['record_type'] == 'HA'
assert records[0]['value'] == 0.4
assert records[0]['workout_date'] == 'Tue, 13 Mar 2018 12:44:45 GMT'
def test_it_creates_workout_with_expecting_gpx_path( def test_it_creates_workout_with_expecting_gpx_path(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
) -> None: ) -> None:

View File

@ -320,6 +320,10 @@ class Workout(BaseModel):
def get_user_workout_records( def get_user_workout_records(
cls, user_id: int, sport_id: int, as_integer: Optional[bool] = False cls, user_id: int, sport_id: int, as_integer: Optional[bool] = False
) -> Dict: ) -> Dict:
"""
Note:
Values for ascent are null for workouts without gpx
"""
record_types_columns = { record_types_columns = {
'AS': 'ave_speed', # 'Average speed' 'AS': 'ave_speed', # 'Average speed'
'FD': 'distance', # 'Farthest Distance' 'FD': 'distance', # 'Farthest Distance'
@ -331,7 +335,11 @@ class Workout(BaseModel):
for record_type, column in record_types_columns.items(): for record_type, column in record_types_columns.items():
column_sorted = getattr(getattr(Workout, column), 'desc')() column_sorted = getattr(getattr(Workout, column), 'desc')()
record_workout = ( record_workout = (
Workout.query.filter_by(user_id=user_id, sport_id=sport_id) Workout.query.filter(
Workout.user_id == user_id,
Workout.sport_id == sport_id,
getattr(Workout, column) != None, # noqa
)
.order_by(column_sorted, Workout.workout_date) .order_by(column_sorted, Workout.workout_date)
.first() .first()
) )