diff --git a/src/backend/marsha/core/tests/utils/test_transcode.py b/src/backend/marsha/core/tests/utils/test_transcode.py index 9ceaa7d182..189a1e4ae2 100644 --- a/src/backend/marsha/core/tests/utils/test_transcode.py +++ b/src/backend/marsha/core/tests/utils/test_transcode.py @@ -1,5 +1,7 @@ """Tests for the `core.utils.transcode` module.""" +from unittest.mock import patch + from django.test import TestCase from django_peertube_runner_connector.models import ( @@ -7,7 +9,6 @@ VideoFile, VideoState, VideoStreamingPlaylist, - VideoResolution, ) from marsha.core import defaults @@ -32,15 +33,6 @@ def setUp(self): state=VideoState.PUBLISHED, directory=f"vod/{self.video.pk}/video/1698941501", ) - # A temporary source video file should exist on tmp directory - self.transcoded_video_source = VideoFile.objects.create( - video=self.transcoded_video, - streamingPlaylist=None, - resolution=VideoResolution.H_NOVIDEO, - size=123456, - extname="", - filename=f"tmp/{self.video.pk}/video/1698941501", - ) self.video_playlist = VideoStreamingPlaylist.objects.create( video=self.transcoded_video @@ -61,53 +53,32 @@ def setUp(self): extname="mp4", ) - def test_transcoding_ended_callback(self): + @patch("marsha.core.utils.transcode.delete_temp_file") + def test_transcoding_ended_callback(self, mock_delete_temp_file): """The marsha video should correctly be updated.""" transcoding_ended_callback(self.transcoded_video) self.video.refresh_from_db() - self.assertEqual(self.video.uploaded_on, to_datetime("1698941501")) - self.assertEqual(self.video.resolutions, [720, 1080]) - self.assertEqual(self.video.upload_state, defaults.READY) self.assertEqual(self.video.transcode_pipeline, defaults.PEERTUBE_PIPELINE) - - # Temporary source video file should be deleted - with self.assertRaises(VideoFile.DoesNotExist): - VideoFile.objects.get(id=self.transcoded_video_source.id) - - self.assertEqual(self.transcoded_video.files.count(), 2) - self.assertQuerySetEqual( - self.transcoded_video.files.all(), - [self.video_file1, self.video_file2], - ordered=False, + mock_delete_temp_file.assert_called_once_with( + self.transcoded_video, f"tmp/{self.video.pk}/video/1698941501" ) - def test_transcoding_ended_callback_with_error(self): + @patch("marsha.core.utils.transcode.delete_temp_file") + def test_transcoding_ended_callback_with_error(self, mock_delete_temp_file): """The marsha video should be set with state on error and nothing else should be done.""" self.transcoded_video.state = VideoState.TRANSCODING_FAILED transcoding_ended_callback(self.transcoded_video) self.video.refresh_from_db() - self.assertEqual(self.video.upload_state, defaults.ERROR) - self.assertEqual(self.video.uploaded_on, None) - self.assertEqual(self.video.resolutions, []) - self.assertEqual(self.video.transcode_pipeline, None) - - # Temporary source video file should be deleted - with self.assertRaises(VideoFile.DoesNotExist): - VideoFile.objects.get(id=self.transcoded_video_source.id) - - self.assertEqual(self.transcoded_video.files.count(), 2) - self.assertQuerySetEqual( - self.transcoded_video.files.all(), - [self.video_file1, self.video_file2], - ordered=False, + mock_delete_temp_file.assert_called_once_with( + self.transcoded_video, f"tmp/{self.video.pk}/video/1698941501" ) diff --git a/src/backend/marsha/core/utils/transcode.py b/src/backend/marsha/core/utils/transcode.py index fc64bbb4a0..cdd1c7715f 100644 --- a/src/backend/marsha/core/utils/transcode.py +++ b/src/backend/marsha/core/utils/transcode.py @@ -1,13 +1,15 @@ """ Utils related to transcoding """ -from django_peertube_runner_connector.models import ( - Video as TranscodedVideo, - VideoState, - VideoFile, - VideoResolution, +from django_peertube_runner_connector.models import Video as TranscodedVideo, VideoState +from django_peertube_runner_connector.utils.files import delete_temp_file + +from marsha.core.defaults import ( + ERROR, + PEERTUBE_PIPELINE, + READY, + TMP_VIDEOS_STORAGE_BASE_DIRECTORY, + VOD_VIDEOS_STORAGE_BASE_DIRECTORY, ) - -from marsha.core.defaults import ERROR, PEERTUBE_PIPELINE, READY from marsha.core.models.video import Video from marsha.core.utils.time_utils import to_datetime @@ -28,16 +30,11 @@ def transcoding_ended_callback(transcoded_video: TranscodedVideo): uploaded_on = directory[-1] video_id = directory[-3] video = Video.objects.get(pk=video_id) - - temp_video_file = VideoFile.objects.get( - video=transcoded_video, - streamingPlaylist=None, - resolution=VideoResolution.H_NOVIDEO, - extname="", - filename=f"tmp/{video_id}/video/{uploaded_on}", + tmp_filename = transcoded_video.directory.replace( + VOD_VIDEOS_STORAGE_BASE_DIRECTORY, TMP_VIDEOS_STORAGE_BASE_DIRECTORY ) - temp_video_file.remove_web_video_file() - temp_video_file.delete() + + delete_temp_file(transcoded_video, tmp_filename) if transcoded_video.state == VideoState.TRANSCODING_FAILED: video.update_upload_state(ERROR, None)