Skip to content

Commit

Permalink
fixup! first implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kernicPanel committed Oct 17, 2024
1 parent 2348be6 commit e9b8476
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 45 deletions.
56 changes: 15 additions & 41 deletions src/backend/marsha/core/tests/utils/test_transcode.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Tests for the `core.utils.transcode` module."""

from unittest.mock import patch
from uuid import uuid4

from django.test import TestCase

from django_peertube_runner_connector.factories import (
VideoFactory as TranscodedVideoFactory,
VideoJobInfoFactory,
)
from django_peertube_runner_connector.models import (
Video as TranscodedVideo,
Expand All @@ -24,8 +24,8 @@
)


class TranscodeTestCase(TestCase):
"""Test the `transcode` functions."""
class TranscodingEndedCallbackTestCase(TestCase):
"""Test the `transcoding_ended_callback` function."""

def setUp(self):
# Create a test video
Expand Down Expand Up @@ -90,55 +90,29 @@ def test_transcoding_ended_callback_with_error(self, mock_delete_temp_file):
self.transcoded_video, f"tmp/{self.video.pk}/video/1698941501"
)


class DeleteTranscodingTempFilesTestCase(TestCase):
"""Test the `delete_transcoding_temp_files` function."""

@patch("marsha.core.utils.transcode.delete_temp_file")
def test_transcoding_delete_transcoding_temp_files(self, mock_delete_temp_file):
"""The temp files should be deleted."""
VideoJobInfoFactory(pendingTranscode=0, video=self.transcoded_video)
temp_video_file = TranscodedVideoFactory(
directory=self.transcoded_video.directory.replace(
defaults.VOD_VIDEOS_STORAGE_BASE_DIRECTORY,
defaults.TMP_VIDEOS_STORAGE_BASE_DIRECTORY,
)
video_id = uuid4()
video_file = TranscodedVideoFactory(
directory=f"vod/{video_id}/video/1698941501"
)

delete_transcoding_temp_files()

mock_delete_temp_file.assert_called_once_with(
self.transcoded_video, temp_video_file.directory
)

@patch("marsha.core.utils.transcode.delete_temp_file")
def test_transcoding_delete_transcoding_temp_files_pending_transcode(
self, mock_delete_temp_file
):
"""The temp files should not be deleted if there is a pending transcode."""
VideoJobInfoFactory(pendingTranscode=1, video=self.transcoded_video)
TranscodedVideoFactory(
directory=self.transcoded_video.directory.replace(
defaults.VOD_VIDEOS_STORAGE_BASE_DIRECTORY,
defaults.TMP_VIDEOS_STORAGE_BASE_DIRECTORY,
)
video_file, f"tmp/{video_id}/video/1698941501"
)

delete_transcoding_temp_files()

mock_delete_temp_file.assert_not_called()

@patch("marsha.core.utils.transcode.delete_temp_file")
def test_transcoding_delete_transcoding_temp_files_not_published(
self, mock_delete_temp_file
):
"""The temp files should be deleted."""
self.transcoded_video.state = VideoState.TO_TRANSCODE
self.transcoded_video.save()
VideoJobInfoFactory(pendingTranscode=0, video=self.transcoded_video)
TranscodedVideoFactory(
directory=self.transcoded_video.directory.replace(
defaults.VOD_VIDEOS_STORAGE_BASE_DIRECTORY,
defaults.TMP_VIDEOS_STORAGE_BASE_DIRECTORY,
)
)
def test_transcoding_delete_transcoding_temp_files_all(self, mock_delete_temp_file):
"""All video files should be processed."""
TranscodedVideoFactory.create_batch(15)

delete_transcoding_temp_files()

mock_delete_temp_file.assert_not_called()
self.assertEqual(mock_delete_temp_file.call_count, 15)
5 changes: 1 addition & 4 deletions src/backend/marsha/core/utils/transcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ def transcoding_ended_callback(transcoded_video: TranscodedVideo):

def delete_transcoding_temp_files():
"""Delete all transcoding temp files."""
transcoded_videos = TranscodedVideo.objects.filter(
state=VideoState.PUBLISHED, jobInfo__pendingTranscode=0
)
for transcoded_video in transcoded_videos:
for transcoded_video in TranscodedVideo.objects.all():
tmp_filename = transcoded_video.directory.replace(
VOD_VIDEOS_STORAGE_BASE_DIRECTORY, TMP_VIDEOS_STORAGE_BASE_DIRECTORY
)
Expand Down

0 comments on commit e9b8476

Please sign in to comment.