API - get stopped speed threshold depending on sport

This commit is contained in:
Sam
2021-11-11 16:36:34 +01:00
parent 7682ccc6aa
commit b6f89d7182
6 changed files with 160 additions and 14 deletions

View File

@ -2,9 +2,11 @@ import datetime
from io import BytesIO
from typing import Generator
from unittest.mock import Mock, patch
from uuid import uuid4
import pytest
from PIL import Image
from werkzeug.datastructures import FileStorage
from fittrackee import db
from fittrackee.workouts.models import Sport, Workout, WorkoutSegment
@ -43,6 +45,7 @@ def sport_1_cycling_inactive() -> Sport:
@pytest.fixture()
def sport_2_running() -> Sport:
sport = Sport(label='Running')
sport.stopped_speed_threshold = 0.1
db.session.add(sport)
db.session.commit()
return sport
@ -575,3 +578,10 @@ def gpx_file_with_segments() -> str:
' </trk>'
'</gpx>'
)
@pytest.fixture()
def gpx_file_storage(gpx_file: str) -> FileStorage:
return FileStorage(
filename=f'{uuid4().hex}.gpx', stream=BytesIO(str.encode(gpx_file))
)

View File

@ -0,0 +1,58 @@
from unittest.mock import call, patch
import pytest
from flask import Flask
from gpxpy.gpx import MovingData
from werkzeug.datastructures import FileStorage
from fittrackee.users.models import User
from fittrackee.workouts.models import Sport
from fittrackee.workouts.utils import process_files
folders = {
'extract_dir': '/tmp/fitTrackee/uploads',
'tmp_dir': '/tmp/fitTrackee/uploads/tmp',
}
moving_data = MovingData(
moving_time=1,
stopped_time=1,
moving_distance=1,
stopped_distance=1,
max_speed=1,
)
class TestStoppedSpeedThreshold:
@pytest.mark.parametrize(
'sport_id, expected_threshold',
[(1, 1.0), (2, 0.1)],
)
def test_it_calls_get_moving_data_with_threshold_depending_on_sport(
self,
app: Flask,
user_1: User,
gpx_file_storage: FileStorage,
sport_1_cycling: Sport,
sport_2_running: Sport,
sport_id: int,
expected_threshold: float,
) -> None:
with patch(
'fittrackee.workouts.utils.get_new_file_path',
return_value='/tmp/fitTrackee/uploads/test.png',
), patch(
'gpxpy.gpx.GPXTrackSegment.get_moving_data',
return_value=moving_data,
) as gpx_track_segment_mock:
process_files(
auth_user_id=user_1.id,
folders=folders,
workout_data={'sport_id': sport_id},
workout_file=gpx_file_storage,
)
assert gpx_track_segment_mock.call_args_list[0] == call(
stopped_speed_threshold=expected_threshold
)
gpx_track_segment_mock.assert_called_with(expected_threshold)