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 14, 2024
1 parent d5ebdc1 commit 8c2e002
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 55 deletions.
49 changes: 10 additions & 39 deletions src/backend/marsha/core/tests/utils/test_transcode.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Tests for the `core.utils.transcode` module."""

from unittest.mock import patch

from django.test import TestCase

from django_peertube_runner_connector.models import (
Video as TranscodedVideo,
VideoFile,
VideoState,
VideoStreamingPlaylist,
VideoResolution,
)

from marsha.core import defaults
Expand All @@ -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
Expand All @@ -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"
)
29 changes: 13 additions & 16 deletions src/backend/marsha/core/utils/transcode.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)
Expand Down

0 comments on commit 8c2e002

Please sign in to comment.