transcript util

EDUCATOR-2131
This commit is contained in:
muhammad-ammar
2018-02-26 20:40:50 +05:00
parent b57b813f1c
commit a01ba7621e
2 changed files with 356 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
""" Tests for transcripts_utils. """
import copy
import tempfile
import ddt
import json
import textwrap
@@ -19,7 +20,8 @@ from xmodule.contentstore.content import StaticContent
from xmodule.contentstore.django import contentstore
from xmodule.exceptions import NotFoundError
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory
from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory
from student.tests.factories import UserFactory
from xmodule.video_module import transcripts_utils
TEST_DATA_CONTENTSTORE = copy.deepcopy(settings.CONTENTSTORE)
@@ -721,3 +723,220 @@ class TestVideoIdsInfo(unittest.TestCase):
"""
actual_result = transcripts_utils.get_video_ids_info(edx_video_id, youtube_id_1_0, html5_sources)
self.assertEqual(actual_result, expected_result)
@ddt.ddt
class TestGetTranscript(SharedModuleStoreTestCase):
"""Tests for `get_transcript` function."""
def setUp(self):
super(TestGetTranscript, self).setUp()
self.course = CourseFactory.create()
self.subs_id = 'video_101'
self.subs_sjson = {
'start': [100, 200, 240, 390, 1000],
'end': [200, 240, 380, 1000, 1500],
'text': [
'subs #1',
'subs #2',
'subs #3',
'subs #4',
'subs #5'
]
}
self.subs_srt = transcripts_utils.Transcript.convert(json.dumps(self.subs_sjson), 'sjson', 'srt')
self.subs = {
u'en': self.subs_srt,
u'ur': transcripts_utils.Transcript.convert(json.dumps(self.subs_sjson), 'sjson', 'srt'),
}
self.srt_mime_type = transcripts_utils.Transcript.mime_types[transcripts_utils.Transcript.SRT]
self.sjson_mime_type = transcripts_utils.Transcript.mime_types[transcripts_utils.Transcript.SJSON]
self.user = UserFactory.create()
self.vertical = ItemFactory.create(category='vertical', parent_location=self.course.location)
self.video = ItemFactory.create(category='video', parent_location=self.vertical.location)
def create_transcript(self, subs_id, language=u'en', filename='video.srt'):
"""
create transcript.
"""
transcripts = {}
if language != u'en':
transcripts = {language: filename}
self.video = ItemFactory.create(
category='video',
parent_location=self.vertical.location,
sub=subs_id,
transcripts=transcripts
)
if subs_id:
transcripts_utils.save_subs_to_store(
self.subs_sjson,
subs_id,
self.video,
language=language,
)
def create_srt_file(self, content):
"""
Create srt file.
"""
srt_file = tempfile.NamedTemporaryFile(suffix=".srt")
srt_file.content_type = transcripts_utils.Transcript.SRT
srt_file.write(content)
srt_file.seek(0)
return srt_file
def upload_file(self, subs_file, location, filename):
"""
Upload a file in content store.
Arguments:
subs_file (File): pointer to file to be uploaded
location (Locator): Item location
filename (unicode): Name of file to be uploaded
"""
mime_type = subs_file.content_type
content_location = StaticContent.compute_location(
location.course_key, filename
)
content = StaticContent(content_location, filename, mime_type, subs_file.read())
contentstore().save(content)
@ddt.data(
# en lang does not exist so NotFoundError will be raised
(u'en',),
# ur lang does not exist so KeyError and then NotFoundError will be raised
(u'ur',),
)
@ddt.unpack
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.course.id, self.video.location.block_id, lang=lang)
@ddt.data(
{
'language': u'en',
'subs_id': 'video_101',
'filename': 'en_video_101.srt',
},
{
'language': u'ur',
'subs_id': '',
'filename': 'ur_video_101.srt',
},
)
@ddt.unpack
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.course.id,
self.video.location.block_id,
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.course.id,
self.video.location.block_id,
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)
@patch(
'openedx.core.djangoapps.video_config.models.VideoTranscriptEnabledFlag.feature_enabled',
Mock(return_value=True),
)
@patch('xmodule.video_module.transcripts_utils.get_video_transcript_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.course.id,
self.video.location.block_id,
)
self.assertEqual(content, self.subs_srt)
self.assertEqual(filename, 'edx.srt')
self.assertEqual(mimetype, self.srt_mime_type)
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.course.id,
self.video.location.block_id,
'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_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.course.id,
self.video.location.block_id,
'ur'
)
exception_message = text_type(no_content_exception.exception)
self.assertEqual(exception_message, 'No transcript content')
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.course.id,
self.video.location.block_id,
'en'
)
exception_message = text_type(no_en_transcript_exception.exception)
self.assertEqual(exception_message, 'No transcript for `en` language')