def test_video_transcoding_failure_invalidates_cache(self): # Login user = models.User.objects.create(username="******", is_active=True) user.set_password("password") user.save() self.client.login(username="******", password="******") factories.VideoFactory(public_id="videoid", owner=user) mock_backend = Mock(return_value=Mock( start_transcoding=Mock(return_value=["job"]), check_progress=Mock(side_effect=exceptions.TranscodingFailed), )) video_pre_transcoding = self.client.get( reverse("api:v1:video-detail", kwargs={"id": "videoid"})).json() with override_settings(PLUGIN_BACKEND=mock_backend): tasks.transcode_video("videoid") video_post_transcoding = self.client.get( reverse("api:v1:video-detail", kwargs={"id": "videoid"})).json() self.assertEqual("pending", video_pre_transcoding["processing"]["status"]) self.assertEqual("failed", video_post_transcoding["processing"]["status"])
def test_transcode_video_failure(self): factories.VideoFactory(public_id="videoid") def check_progress(job): if job == "job1": # job1 finishes raise exceptions.TranscodingFailed("error message") else: # job2 finishes return 100, True mock_backend = Mock(return_value=Mock( start_transcoding=Mock(return_value=["job1", "job2"]), check_progress=check_progress, iter_formats=Mock(return_value=[]), )) with override_settings(PLUGIN_BACKEND=mock_backend): tasks.transcode_video("videoid") self.assertEqual(1, models.ProcessingState.objects.count()) video_processing_state = models.ProcessingState.objects.get() self.assertEqual(models.ProcessingState.STATUS_FAILED, video_processing_state.status) self.assertEqual("error message", video_processing_state.message) self.assertEqual(50, video_processing_state.progress) mock_backend.return_value.create_thumbnail.assert_not_called()
def test_transcode_video_success(self): factories.VideoFactory(public_id="videoid", public_thumbnail_id="thumbid") mock_backend = Mock(return_value=Mock( start_transcoding=Mock(return_value=["job1"]), check_progress=Mock(return_value=(42, True)), iter_formats=Mock(return_value=[("SD", 128)]), create_thumbnail=Mock(), )) with override_settings(PLUGIN_BACKEND=mock_backend): tasks.transcode_video("videoid") self.assertEqual(1, models.ProcessingState.objects.count()) video_processing_state = models.ProcessingState.objects.get() self.assertEqual(models.ProcessingState.STATUS_SUCCESS, video_processing_state.status) self.assertEqual("", video_processing_state.message) self.assertEqual(42, video_processing_state.progress) mock_backend.return_value.create_thumbnail.assert_called_once_with( "videoid", "thumbid") mock_backend.return_value.check_progress.assert_called_once_with( "job1") self.assertEqual(1, models.VideoFormat.objects.count()) video_format = models.VideoFormat.objects.get() self.assertEqual("videoid", video_format.video.public_id) self.assertEqual("SD", video_format.name) self.assertEqual(128, video_format.bitrate)
def test_transcode_video_thumbnail_create_fails(self): video = factories.VideoFactory(public_id="videoid") mock_backend = Mock(return_value=Mock( start_transcoding=Mock(return_value=[]), iter_formats=Mock(return_value=[]), create_thumbnail=Mock(side_effect=ValueError("description")), )) with override_settings(PLUGIN_BACKEND=mock_backend): tasks.transcode_video("videoid") processing_state = models.ProcessingState.objects.get(video=video) self.assertEqual(models.ProcessingState.STATUS_FAILED, processing_state.status) self.assertEqual("thumbnail creation: description", processing_state.message)
def test_video_is_deleted_during_transcoding(self): factories.VideoFactory(public_id="videoid") def start_transcoding(video_id): models.Video.objects.filter(public_id="videoid").delete() return [] mock_backend = Mock( return_value=Mock(start_transcoding=start_transcoding, iter_formats=Mock(return_value=[]))) with override_settings(PLUGIN_BACKEND=mock_backend): tasks.transcode_video("videoid") self.assertEqual(0, models.Video.objects.count()) self.assertEqual(0, models.ProcessingState.objects.count()) mock_backend.return_value.delete_video.assert_called_once()
def test_transcode_video_twice(self): factories.VideoFactory(public_id="videoid") mock_backend = Mock(return_value=Mock( start_transcoding=Mock(return_value=["job1"]), iter_formats=Mock(return_value=[]), )) # First attempt: failure mock_backend.return_value.check_progress = Mock( side_effect=exceptions.TranscodingFailed) with override_settings(PLUGIN_BACKEND=mock_backend): tasks.transcode_video("videoid") # Second attempt: success mock_backend.return_value.check_progress = Mock(return_value=(100, True)) with override_settings(PLUGIN_BACKEND=mock_backend): tasks.transcode_video("videoid") video_processing_state = models.ProcessingState.objects.get() self.assertEqual(models.ProcessingState.STATUS_SUCCESS, video_processing_state.status) self.assertEqual("", video_processing_state.message) self.assertEqual(100, video_processing_state.progress)