def test_get_transcript_not_found(self, lang): """ Verify that `NotFoundError` exception is raised when transcript is not found in both the content store and val. """ with self.assertRaises(NotFoundError): transcripts_utils.get_transcript( self.video, lang=lang )
def test_get_transcript_no_en_transcript(self): """ Verify that `get_transcript` function returns correct exception when no transcript exists for `en`. """ self.video.youtube_id_1_0 = '' self.store.update_item(self.video, self.user.id) with self.assertRaises(NotFoundError) as no_en_transcript_exception: transcripts_utils.get_transcript(self.video, 'en') exception_message = str(no_en_transcript_exception.exception) self.assertEqual(exception_message, 'No transcript for `en` language')
def test_get_transcript_invalid_format(self): """ Verify that `get_transcript` raises correct exception if transcript format is invalid. """ with self.assertRaises(NotFoundError) as invalid_format_exception: transcripts_utils.get_transcript(self.video, 'ur', output_format='mpeg') exception_message = str(invalid_format_exception.exception) self.assertEqual(exception_message, 'Invalid transcript format `mpeg`')
def test_get_transcript_no_content(self): """ Verify that `get_transcript` function returns correct exception when transcript content is empty. """ self.upload_file(self.create_srt_file(b''), self.video.location, 'ur_video_101.srt') self.create_transcript('', 'ur', 'ur_video_101.srt') with self.assertRaises(NotFoundError) as no_content_exception: transcripts_utils.get_transcript(self.video, 'ur') exception_message = str(no_content_exception.exception) self.assertEqual(exception_message, 'No transcript content')
def test_get_transcript_invalid_format(self): """ Verify that `get_transcript` raises correct exception if transcript format is invalid. """ with self.assertRaises(NotFoundError) as invalid_format_exception: transcripts_utils.get_transcript( self.video, 'ur', output_format='mpeg' ) exception_message = text_type(invalid_format_exception.exception) self.assertEqual(exception_message, 'Invalid transcript format `mpeg`')
def test_get_transcript_no_en_transcript(self): """ Verify that `get_transcript` function returns correct exception when no transcript exists for `en`. """ self.video.youtube_id_1_0 = '' self.store.update_item(self.video, self.user.id) with self.assertRaises(NotFoundError) as no_en_transcript_exception: transcripts_utils.get_transcript( self.video, 'en' ) exception_message = text_type(no_en_transcript_exception.exception) self.assertEqual(exception_message, 'No transcript for `en` language')
def test_get_transcript_no_content(self): """ Verify that `get_transcript` function returns correct exception when transcript content is empty. """ self.upload_file(self.create_srt_file(''), self.video.location, 'ur_video_101.srt') self.create_transcript('', 'ur', 'ur_video_101.srt') with self.assertRaises(NotFoundError) as no_content_exception: transcripts_utils.get_transcript( self.video, 'ur' ) exception_message = text_type(no_content_exception.exception) self.assertEqual(exception_message, 'No transcript content')
def test_get_transcript_incorrect_json_(self, mock_get_video_transcript_data): """ Verify that `get transcript` function returns a working json file if the original throws an error """ error_transcript = { "start": [1], "end": [2], "text": ["An error occured obtaining the transcript."] } mock_get_video_transcript_data.side_effect = ValueError content, _, _ = transcripts_utils.get_transcript(self.video, 'zh') assert error_transcript["text"][0] in content
def test_get_transcript_from_val(self, mock_get_video_transcript_content): """ Verify that `get_transcript` function returns correct data when transcript is in val. """ mock_get_video_transcript_content.return_value = { 'content': json.dumps(self.subs_sjson), 'file_name': 'edx.sjson' } content, filename, mimetype = transcripts_utils.get_transcript( self.video, ) self.assertEqual(content, self.subs_srt) self.assertEqual(filename, 'edx.srt') self.assertEqual(mimetype, self.srt_mime_type)
def test_get_transcript_from_content_store_for_ur(self): """ Verify that `get_transcript` function returns correct data for non-english when transcript is in content store. """ language = 'ur' self.create_transcript(self.subs_id, language) content, filename, mimetype = transcripts_utils.get_transcript( self.video, language, output_format=transcripts_utils.Transcript.SJSON) self.assertEqual(json.loads(content), self.subs_sjson) self.assertEqual(filename, 'ur_video_101.sjson') self.assertEqual(mimetype, self.sjson_mime_type)
def test_get_transcript_from_content_store(self, language, subs_id, filename): """ Verify that `get_transcript` function returns correct data when transcript is in content store. """ self.upload_file(self.create_srt_file(self.subs_srt), self.video.location, filename) self.create_transcript(subs_id, language, filename) content, filename, mimetype = transcripts_utils.get_transcript( self.video, language) self.assertEqual(content, self.subs[language]) self.assertEqual(filename, filename) self.assertEqual(mimetype, self.srt_mime_type)
def test_get_transcript_from_content_store(self, language, subs_id, filename): """ Verify that `get_transcript` function returns correct data when transcript is in content store. """ self.upload_file(self.create_srt_file(self.subs_srt), self.video.location, filename) self.create_transcript(subs_id, language, filename) content, filename, mimetype = transcripts_utils.get_transcript( self.video, language ) self.assertEqual(content, self.subs[language]) self.assertEqual(filename, filename) self.assertEqual(mimetype, self.srt_mime_type)
def test_get_transcript_from_content_store_for_ur(self): """ Verify that `get_transcript` function returns correct data for non-english when transcript is in content store. """ language = u'ur' self.create_transcript(self.subs_id, language) content, filename, mimetype = transcripts_utils.get_transcript( self.video, language, output_format=transcripts_utils.Transcript.SJSON ) self.assertEqual(json.loads(content), self.subs_sjson) self.assertEqual(filename, 'ur_video_101.sjson') self.assertEqual(mimetype, self.sjson_mime_type)
def get(self, request, course, *args, **kwargs): block_id = kwargs['block_id'] lang = kwargs['lang'] usage_key = BlockUsageLocator(course.id, block_type='video', block_id=block_id) video_descriptor = modulestore().get_item(usage_key) try: content, filename, mimetype = get_transcript(video_descriptor, lang=lang) except NotFoundError: raise Http404(u'Transcript not found for {}, lang: {}'.format(block_id, lang)) response = HttpResponse(content, content_type=mimetype) response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename.encode('utf-8')) return response
def test_get_transcript_from_contentstore(self, language, subs_id, youtube_id_1_0, html5_sources, expected_filename): """ Verify that `get_transcript` function returns correct data when transcript is in content store. """ base_filename = 'video_101.srt' self.upload_file(self.create_srt_file(self.subs_srt.encode('utf-8')), self.video.location, base_filename) self.create_transcript(subs_id, language, base_filename, youtube_id_1_0, html5_sources) content, file_name, mimetype = transcripts_utils.get_transcript( self.video, language) self.assertEqual(content, self.subs[language]) self.assertEqual(file_name, expected_filename) self.assertEqual(mimetype, self.srt_mime_type)
def download_transcripts(request): """ Passes to user requested transcripts file. Raises Http404 if unsuccessful. """ error, video = validate_video_module(request, locator=request.GET.get('locator')) if error: raise Http404 try: content, filename, mimetype = get_transcript(video, lang=u'en') except NotFoundError: raise Http404 # Construct an HTTP response response = HttpResponse(content, content_type=mimetype) response['Content-Disposition'] = u'attachment; filename="{filename}"'.format(filename=filename.encode('utf-8')) return response
def download_transcripts(request): """ Passes to user requested transcripts file. Raises Http404 if unsuccessful. """ error, video = validate_video_module(request, locator=request.GET.get('locator')) if error: raise Http404 try: content, filename, mimetype = get_transcript(video, lang='en') except NotFoundError: raise Http404 # lint-amnesty, pylint: disable=raise-missing-from # Construct an HTTP response response = HttpResponse(content, content_type=mimetype) response['Content-Disposition'] = f'attachment; filename="{filename}"' return response
def test_get_transcript_from_contentstore( self, language, subs_id, youtube_id_1_0, html5_sources, expected_filename ): """ Verify that `get_transcript` function returns correct data when transcript is in content store. """ base_filename = 'video_101.srt' self.upload_file(self.create_srt_file(self.subs_srt), self.video.location, base_filename) self.create_transcript(subs_id, language, base_filename, youtube_id_1_0, html5_sources) content, file_name, mimetype = transcripts_utils.get_transcript( self.video, language ) self.assertEqual(content, self.subs[language]) self.assertEqual(file_name, expected_filename) self.assertEqual(mimetype, self.srt_mime_type)