From 430ee4cd2fcb576dc1bb903a1c3834ad8df459e9 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 8 Mar 2023 11:29:15 +0100 Subject: [PATCH] API - add missing test --- .../workouts/test_workouts_api_1_post.py | 46 ++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/fittrackee/tests/workouts/test_workouts_api_1_post.py b/fittrackee/tests/workouts/test_workouts_api_1_post.py index 103549e5..304ec429 100644 --- a/fittrackee/tests/workouts/test_workouts_api_1_post.py +++ b/fittrackee/tests/workouts/test_workouts_api_1_post.py @@ -217,7 +217,9 @@ def assert_workout_data_wo_gpx(data: Dict) -> None: assert records[3]['value'] == 10.0 -def assert_files_are_deleted(app: Flask, user: User) -> None: +def assert_files_are_deleted( + app: Flask, user: User, expected_count: Optional[int] = 0 +) -> None: upload_directory = os.path.join( app.config["UPLOAD_FOLDER"], f"workouts/{user.id}" ) @@ -229,7 +231,7 @@ def assert_files_are_deleted(app: Flask, user: User) -> None: if os.path.isfile(os.path.join(upload_directory, name)) ] ) - == 0 + == expected_count ) @@ -841,6 +843,46 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin): assert_files_are_deleted(app, user_1) + def test_it_deletes_only_errored_file( + self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str + ) -> None: + client, auth_token = self.get_test_client_and_auth_token( + app, user_1.email + ) + 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}', + ), + ) + + with patch( + 'fittrackee.workouts.utils.workouts.generate_map', + side_effect=Exception(), + ): + client.post( + '/api/workouts', + data=dict( + file=(BytesIO(str.encode(gpx_file)), 'example.gpx'), + data='{"sport_id": 2}', + ), + headers=dict( + content_type='multipart/form-data', + Authorization=f'Bearer {auth_token}', + ), + ) + + assert_files_are_deleted(app, user_1, expected_count=2) + upload_directory = os.path.join(app.config["UPLOAD_FOLDER"]) + workout = Workout.query.first() + os.path.exists(os.path.join(upload_directory, workout.gpx)) + os.path.exists(os.path.join(upload_directory, workout.map)) + def test_it_cleans_uploaded_file_and_static_map_on_segments_creation_error( self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str ) -> None: