Commit c9afa056 by Alexander Kryklia

Move video descriptor instantiation in tests to single place.

parent 54399533
...@@ -12,14 +12,13 @@ You can then use the CourseFactory and XModuleItemFactory as defined ...@@ -12,14 +12,13 @@ You can then use the CourseFactory and XModuleItemFactory as defined
in common/lib/xmodule/xmodule/modulestore/tests/factories.py to create in common/lib/xmodule/xmodule/modulestore/tests/factories.py to create
the course, section, subsection, unit, etc. the course, section, subsection, unit, etc.
""" """
import unittest import unittest
import datetime import datetime
from mock import Mock from mock import Mock
from . import LogicTest from . import LogicTest
from lxml import etree from lxml import etree
from opaque_keys.edx.locations import Location from opaque_keys.edx.locations import SlashSeparatedCourseKey
from xmodule.video_module import VideoDescriptor, create_youtube_string from xmodule.video_module import VideoDescriptor, create_youtube_string
from .test_import import DummySystem from .test_import import DummySystem
from xblock.field_data import DictFieldData from xblock.field_data import DictFieldData
...@@ -28,6 +27,20 @@ from xblock.fields import ScopeIds ...@@ -28,6 +27,20 @@ from xblock.fields import ScopeIds
from xmodule.tests import get_test_descriptor_system from xmodule.tests import get_test_descriptor_system
def instantiate_descriptor(**field_data):
"""
Instantiate descriptor with most properties.
"""
system = get_test_descriptor_system()
course_key = SlashSeparatedCourseKey('org', 'course', 'run')
usage_key = course_key.make_usage_key('video', 'SampleProblem')
return system.construct_xblock_from_class(
VideoDescriptor,
scope_ids=ScopeIds(None, None, usage_key, usage_key),
field_data=DictFieldData(field_data),
)
class VideoModuleTest(LogicTest): class VideoModuleTest(LogicTest):
"""Logic tests for Video Xmodule.""" """Logic tests for Video Xmodule."""
descriptor_class = VideoDescriptor descriptor_class = VideoDescriptor
...@@ -108,59 +121,48 @@ class VideoModuleTest(LogicTest): ...@@ -108,59 +121,48 @@ class VideoModuleTest(LogicTest):
) )
class VideoDescriptorTest(unittest.TestCase): class VideoDescriptorTestBase(unittest.TestCase):
"""Test for VideoDescriptor""" """
Base class for tests for VideoDescriptor
"""
def setUp(self): def setUp(self):
system = get_test_descriptor_system() self.descriptor = instantiate_descriptor()
location = Location('org', 'course', 'run', 'video', 'name', None)
self.descriptor = system.construct_xblock_from_class(
VideoDescriptor,
scope_ids=ScopeIds(None, None, location, location),
field_data=DictFieldData({}),
)
class TestCreateYoutubeString(VideoDescriptorTestBase):
"""
Checks that create_youtube_string correcty extracts information from Video descriptor.
"""
def test_create_youtube_string(self): def test_create_youtube_string(self):
""" """
Test that Youtube ID strings are correctly created when writing Test that Youtube ID strings are correctly created when writing back out to XML.
back out to XML. """
""" self.descriptor.youtube_id_0_75 = 'izygArpw-Qo'
system = DummySystem(load_error_modules=True) self.descriptor.youtube_id_1_0 = 'p2Q6BrNhdh8'
location = Location("edX", 'course', 'run', "video", 'SampleProblem1', None) self.descriptor.youtube_id_1_25 = '1EeWXzPdhSA'
field_data = DictFieldData({'location': location}) self.descriptor.youtube_id_1_5 = 'rABDYkeK0x8'
descriptor = VideoDescriptor(system, field_data, Mock())
descriptor.youtube_id_0_75 = 'izygArpw-Qo'
descriptor.youtube_id_1_0 = 'p2Q6BrNhdh8'
descriptor.youtube_id_1_25 = '1EeWXzPdhSA'
descriptor.youtube_id_1_5 = 'rABDYkeK0x8'
expected = "0.75:izygArpw-Qo,1.00:p2Q6BrNhdh8,1.25:1EeWXzPdhSA,1.50:rABDYkeK0x8" expected = "0.75:izygArpw-Qo,1.00:p2Q6BrNhdh8,1.25:1EeWXzPdhSA,1.50:rABDYkeK0x8"
self.assertEqual(create_youtube_string(descriptor), expected) self.assertEqual(create_youtube_string(self.descriptor), expected)
def test_create_youtube_string_missing(self): def test_create_youtube_string_missing(self):
""" """
Test that Youtube IDs which aren't explicitly set aren't included Test that Youtube IDs which aren't explicitly set aren't included in the output string.
in the output string.
""" """
system = DummySystem(load_error_modules=True) self.descriptor.youtube_id_0_75 = 'izygArpw-Qo'
location = Location("edX", 'course', 'run', "video", "SampleProblem1", None) self.descriptor.youtube_id_1_0 = 'p2Q6BrNhdh8'
field_data = DictFieldData({'location': location}) self.descriptor.youtube_id_1_25 = '1EeWXzPdhSA'
descriptor = VideoDescriptor(system, field_data, Mock())
descriptor.youtube_id_0_75 = 'izygArpw-Qo'
descriptor.youtube_id_1_0 = 'p2Q6BrNhdh8'
descriptor.youtube_id_1_25 = '1EeWXzPdhSA'
expected = "0.75:izygArpw-Qo,1.00:p2Q6BrNhdh8,1.25:1EeWXzPdhSA" expected = "0.75:izygArpw-Qo,1.00:p2Q6BrNhdh8,1.25:1EeWXzPdhSA"
self.assertEqual(create_youtube_string(descriptor), expected) self.assertEqual(create_youtube_string(self.descriptor), expected)
class VideoDescriptorImportTestCase(unittest.TestCase): class VideoDescriptorImportTestCase(unittest.TestCase):
""" """
Make sure that VideoDescriptor can import an old XML-based video correctly. Make sure that VideoDescriptor can import an old XML-based video correctly.
""" """
def assert_attributes_equal(self, video, attrs): def assert_attributes_equal(self, video, attrs):
""" """
Assert that `video` has the correct attributes. `attrs` is a map Assert that `video` has the correct attributes. `attrs` is a map of {metadata_field: value}.
of {metadata_field: value}.
""" """
for key, value in attrs.items(): for key, value in attrs.items():
self.assertEquals(getattr(video, key), value) self.assertEquals(getattr(video, key), value)
...@@ -182,13 +184,7 @@ class VideoDescriptorImportTestCase(unittest.TestCase): ...@@ -182,13 +184,7 @@ class VideoDescriptorImportTestCase(unittest.TestCase):
<transcript language="ge" src="german_translation.srt" /> <transcript language="ge" src="german_translation.srt" />
</video> </video>
''' '''
location = Location("edX", 'course', 'run', "video", "SampleProblem1", None) descriptor = instantiate_descriptor(data=sample_xml)
field_data = DictFieldData({
'data': sample_xml,
'location': location
})
system = DummySystem(load_error_modules=True)
descriptor = VideoDescriptor(system, field_data, Mock())
self.assert_attributes_equal(descriptor, { self.assert_attributes_equal(descriptor, {
'youtube_id_0_75': 'izygArpw-Qo', 'youtube_id_0_75': 'izygArpw-Qo',
'youtube_id_1_0': 'p2Q6BrNhdh8', 'youtube_id_1_0': 'p2Q6BrNhdh8',
...@@ -480,14 +476,10 @@ class VideoDescriptorImportTestCase(unittest.TestCase): ...@@ -480,14 +476,10 @@ class VideoDescriptorImportTestCase(unittest.TestCase):
}) })
class VideoExportTestCase(unittest.TestCase): class VideoExportTestCase(VideoDescriptorTestBase):
""" """
Make sure that VideoDescriptor can export itself to XML Make sure that VideoDescriptor can export itself to XML correctly.
correctly.
""" """
def setUp(self):
self.location = Location("edX", 'course', 'run', "video", "SampleProblem1", None)
def assertXmlEqual(self, expected, xml): def assertXmlEqual(self, expected, xml):
for attr in ['tag', 'attrib', 'text', 'tail']: for attr in ['tag', 'attrib', 'text', 'tail']:
self.assertEqual(getattr(expected, attr), getattr(xml, attr)) self.assertEqual(getattr(expected, attr), getattr(xml, attr))
...@@ -495,27 +487,26 @@ class VideoExportTestCase(unittest.TestCase): ...@@ -495,27 +487,26 @@ class VideoExportTestCase(unittest.TestCase):
self.assertXmlEqual(left, right) self.assertXmlEqual(left, right)
def test_export_to_xml(self): def test_export_to_xml(self):
"""Test that we write the correct XML on export.""" """
module_system = DummySystem(load_error_modules=True) Test that we write the correct XML on export.
desc = VideoDescriptor(module_system, DictFieldData({}), ScopeIds(None, None, self.location, self.location)) """
self.descriptor.youtube_id_0_75 = 'izygArpw-Qo'
desc.youtube_id_0_75 = 'izygArpw-Qo' self.descriptor.youtube_id_1_0 = 'p2Q6BrNhdh8'
desc.youtube_id_1_0 = 'p2Q6BrNhdh8' self.descriptor.youtube_id_1_25 = '1EeWXzPdhSA'
desc.youtube_id_1_25 = '1EeWXzPdhSA' self.descriptor.youtube_id_1_5 = 'rABDYkeK0x8'
desc.youtube_id_1_5 = 'rABDYkeK0x8' self.descriptor.show_captions = False
desc.show_captions = False self.descriptor.start_time = datetime.timedelta(seconds=1.0)
desc.start_time = datetime.timedelta(seconds=1.0) self.descriptor.end_time = datetime.timedelta(seconds=60)
desc.end_time = datetime.timedelta(seconds=60) self.descriptor.track = 'http://www.example.com/track'
desc.track = 'http://www.example.com/track' self.descriptor.handout = 'http://www.example.com/handout'
desc.handout = 'http://www.example.com/handout' self.descriptor.download_track = True
desc.download_track = True self.descriptor.html5_sources = ['http://www.example.com/source.mp4', 'http://www.example.com/source.ogg']
desc.html5_sources = ['http://www.example.com/source.mp4', 'http://www.example.com/source.ogg'] self.descriptor.download_video = True
desc.download_video = True self.descriptor.transcripts = {'ua': 'ukrainian_translation.srt', 'ge': 'german_translation.srt'}
desc.transcripts = {'ua': 'ukrainian_translation.srt', 'ge': 'german_translation.srt'}
xml = self.descriptor.definition_to_xml(None) # We don't use the `resource_fs` parameter
xml = desc.definition_to_xml(None) # We don't use the `resource_fs` parameter
expected = etree.fromstring('''\ expected = etree.fromstring('''\
<video url_name="SampleProblem1" start_time="0:00:01" youtube="0.75:izygArpw-Qo,1.00:p2Q6BrNhdh8,1.25:1EeWXzPdhSA,1.50:rABDYkeK0x8" show_captions="false" end_time="0:01:00" download_video="true" download_track="true"> <video url_name="SampleProblem" start_time="0:00:01" youtube="0.75:izygArpw-Qo,1.00:p2Q6BrNhdh8,1.25:1EeWXzPdhSA,1.50:rABDYkeK0x8" show_captions="false" end_time="0:01:00" download_video="true" download_track="true">
<source src="http://www.example.com/source.mp4"/> <source src="http://www.example.com/source.mp4"/>
<source src="http://www.example.com/source.ogg"/> <source src="http://www.example.com/source.ogg"/>
<track src="http://www.example.com/track"/> <track src="http://www.example.com/track"/>
...@@ -527,25 +518,24 @@ class VideoExportTestCase(unittest.TestCase): ...@@ -527,25 +518,24 @@ class VideoExportTestCase(unittest.TestCase):
self.assertXmlEqual(expected, xml) self.assertXmlEqual(expected, xml)
def test_export_to_xml_empty_end_time(self): def test_export_to_xml_empty_end_time(self):
"""Test that we write the correct XML on export.""" """
module_system = DummySystem(load_error_modules=True) Test that we write the correct XML on export.
desc = VideoDescriptor(module_system, DictFieldData({}), ScopeIds(None, None, self.location, self.location)) """
self.descriptor.youtube_id_0_75 = 'izygArpw-Qo'
desc.youtube_id_0_75 = 'izygArpw-Qo' self.descriptor.youtube_id_1_0 = 'p2Q6BrNhdh8'
desc.youtube_id_1_0 = 'p2Q6BrNhdh8' self.descriptor.youtube_id_1_25 = '1EeWXzPdhSA'
desc.youtube_id_1_25 = '1EeWXzPdhSA' self.descriptor.youtube_id_1_5 = 'rABDYkeK0x8'
desc.youtube_id_1_5 = 'rABDYkeK0x8' self.descriptor.show_captions = False
desc.show_captions = False self.descriptor.start_time = datetime.timedelta(seconds=5.0)
desc.start_time = datetime.timedelta(seconds=5.0) self.descriptor.end_time = datetime.timedelta(seconds=0.0)
desc.end_time = datetime.timedelta(seconds=0.0) self.descriptor.track = 'http://www.example.com/track'
desc.track = 'http://www.example.com/track' self.descriptor.download_track = True
desc.download_track = True self.descriptor.html5_sources = ['http://www.example.com/source.mp4', 'http://www.example.com/source.ogg']
desc.html5_sources = ['http://www.example.com/source.mp4', 'http://www.example.com/source.ogg'] self.descriptor.download_video = True
desc.download_video = True
xml = self.descriptor.definition_to_xml(None) # We don't use the `resource_fs` parameter
xml = desc.definition_to_xml(None) # We don't use the `resource_fs` parameter
expected = etree.fromstring('''\ expected = etree.fromstring('''\
<video url_name="SampleProblem1" start_time="0:00:05" youtube="0.75:izygArpw-Qo,1.00:p2Q6BrNhdh8,1.25:1EeWXzPdhSA,1.50:rABDYkeK0x8" show_captions="false" download_video="true" download_track="true"> <video url_name="SampleProblem" start_time="0:00:05" youtube="0.75:izygArpw-Qo,1.00:p2Q6BrNhdh8,1.25:1EeWXzPdhSA,1.50:rABDYkeK0x8" show_captions="false" download_video="true" download_track="true">
<source src="http://www.example.com/source.mp4"/> <source src="http://www.example.com/source.mp4"/>
<source src="http://www.example.com/source.ogg"/> <source src="http://www.example.com/source.ogg"/>
<track src="http://www.example.com/track"/> <track src="http://www.example.com/track"/>
...@@ -555,11 +545,9 @@ class VideoExportTestCase(unittest.TestCase): ...@@ -555,11 +545,9 @@ class VideoExportTestCase(unittest.TestCase):
self.assertXmlEqual(expected, xml) self.assertXmlEqual(expected, xml)
def test_export_to_xml_empty_parameters(self): def test_export_to_xml_empty_parameters(self):
"""Test XML export with defaults.""" """
module_system = DummySystem(load_error_modules=True) Test XML export with defaults.
desc = VideoDescriptor(module_system, DictFieldData({}), ScopeIds(None, None, self.location, self.location)) """
xml = self.descriptor.definition_to_xml(None)
xml = desc.definition_to_xml(None) expected = '<video url_name="SampleProblem"/>\n'
expected = '<video url_name="SampleProblem1"/>\n'
self.assertEquals(expected, etree.tostring(xml, pretty_print=True)) self.assertEquals(expected, etree.tostring(xml, pretty_print=True))
...@@ -10,11 +10,11 @@ from django.conf import settings ...@@ -10,11 +10,11 @@ from django.conf import settings
from xblock.fields import ScopeIds from xblock.fields import ScopeIds
from xblock.field_data import DictFieldData from xblock.field_data import DictFieldData
from xmodule.video_module import create_youtube_string from xmodule.video_module import create_youtube_string, VideoDescriptor
from xmodule.tests import get_test_descriptor_system
from xmodule.video_module import VideoDescriptor
from xmodule.x_module import STUDENT_VIEW from xmodule.x_module import STUDENT_VIEW
from opaque_keys.edx.locations import SlashSeparatedCourseKey from xmodule.tests import get_test_descriptor_system
from xmodule.tests.test_video import VideoDescriptorTestBase
from . import BaseTestXmodule from . import BaseTestXmodule
from .test_video_xml import SOURCE_XML from .test_video_xml import SOURCE_XML
...@@ -494,20 +494,12 @@ class TestVideoDescriptorInitialization(BaseTestXmodule): ...@@ -494,20 +494,12 @@ class TestVideoDescriptorInitialization(BaseTestXmodule):
self.assertFalse(self.item_descriptor.download_video) self.assertFalse(self.item_descriptor.download_video)
class VideoDescriptorTest(unittest.TestCase): class VideoDescriptorTest(VideoDescriptorTestBase):
""" """
Tests for video descriptor that requires access to django settings. Tests for video descriptor that requires access to django settings.
""" """
def setUp(self): def setUp(self):
system = get_test_descriptor_system() super(VideoDescriptorTest, self).setUp()
course_key = SlashSeparatedCourseKey('org', 'course', 'run')
usage_key = course_key.make_usage_key('video', 'name')
self.descriptor = system.construct_xblock_from_class(
VideoDescriptor,
scope_ids=ScopeIds(None, None, usage_key, usage_key),
field_data=DictFieldData({}),
)
self.descriptor.runtime.handler_url = MagicMock() self.descriptor.runtime.handler_url = MagicMock()
def test_get_context(self): def test_get_context(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment