test_video_xml.py 3.88 KB
Newer Older
1
# -*- coding: utf-8 -*-
2 3
# pylint: disable=W0212

4
"""Test for Video Xmodule functional logic.
5
These test data read from xml, not from mongo.
6 7 8 9 10 11 12 13 14 15 16 17

We have a ModuleStoreTestCase class defined in
common/lib/xmodule/xmodule/modulestore/tests/django_utils.py.
You can search for usages of this in the cms and lms tests for examples.
You use this so that it will do things like point the modulestore
setting to mongo, flush the contentstore before and after, load the
templates, etc.
You can then use the CourseFactory and XModuleItemFactory as defined in
common/lib/xmodule/xmodule/modulestore/tests/factories.py to create the
course, section, subsection, unit, etc.
"""

18
from xmodule.video_module import VideoDescriptor
19
from xmodule.modulestore import Location
20
from xmodule.tests import get_test_system, LogicTest, get_test_descriptor_system
Calen Pennington committed
21 22
from xblock.field_data import DictFieldData
from xblock.fields import ScopeIds
23 24 25


SOURCE_XML = """
26
    <video show_captions="true"
27
    display_name="A Name"
28
    youtube="0.75:jNCf2gIqpeE,1.0:ZwkTiUPN0mg,1.25:rsq9auxASqI,1.50:kMyNdzVHHgg"
29
    sub="a_sub_file.srt.sjson"
30
    download_video="true"
31 32
    start_time="01:00:03" end_time="01:00:10"
    >
33 34
        <source src="example.mp4"/>
        <source src="example.webm"/>
35
        <transcript language="uk" src="ukrainian_translation.srt" />
36
    </video>
37 38 39
"""


40 41
class VideoFactory(object):
    """A helper class to create video modules with various parameters
42 43 44 45 46 47 48 49
    for testing.
    """

    # tag that uses youtube videos
    sample_problem_xml_youtube = SOURCE_XML

    @staticmethod
    def create():
50 51
        """Method return Video Xmodule instance."""
        location = Location(["i4x", "edX", "video", "default",
52
                             "SampleProblem1"])
Calen Pennington committed
53
        field_data = {'data': VideoFactory.sample_problem_xml_youtube,
54
                      'location': location}
55

56
        system = get_test_descriptor_system()
57

Calen Pennington committed
58
        descriptor = VideoDescriptor(system, DictFieldData(field_data), ScopeIds(None, None, None, None))
59 60
        descriptor.xmodule_runtime = get_test_system()
        return descriptor
61 62


63 64
class VideoModuleLogicTest(LogicTest):
    """Tests for logic of Video Xmodule."""
65

66
    descriptor_class = VideoDescriptor
67

Calen Pennington committed
68
    raw_field_data = {
69 70 71 72 73 74
        'data': '<video />'
    }

    def test_parse_youtube(self):
        """Test parsing old-style Youtube ID strings into a dict."""
        youtube_str = '0.75:jNCf2gIqpeE,1.00:ZwkTiUPN0mg,1.25:rsq9auxASqI,1.50:kMyNdzVHHgg'
75
        output = VideoDescriptor._parse_youtube(youtube_str)
76 77 78 79 80 81 82 83 84 85 86
        self.assertEqual(output, {'0.75': 'jNCf2gIqpeE',
                                  '1.00': 'ZwkTiUPN0mg',
                                  '1.25': 'rsq9auxASqI',
                                  '1.50': 'kMyNdzVHHgg'})

    def test_parse_youtube_one_video(self):
        """
        Ensure that all keys are present and missing speeds map to the
        empty string.
        """
        youtube_str = '0.75:jNCf2gIqpeE'
87
        output = VideoDescriptor._parse_youtube(youtube_str)
88 89 90 91 92 93 94 95 96 97 98 99
        self.assertEqual(output, {'0.75': 'jNCf2gIqpeE',
                                  '1.00': '',
                                  '1.25': '',
                                  '1.50': ''})

    def test_parse_youtube_key_format(self):
        """
        Make sure that inconsistent speed keys are parsed correctly.
        """
        youtube_str = '1.00:p2Q6BrNhdh8'
        youtube_str_hack = '1.0:p2Q6BrNhdh8'
        self.assertEqual(
100 101
            VideoDescriptor._parse_youtube(youtube_str),
            VideoDescriptor._parse_youtube(youtube_str_hack)
102 103 104 105 106 107 108
        )

    def test_parse_youtube_empty(self):
        """
        Some courses have empty youtube attributes, so we should handle
        that well.
        """
109
        self.assertEqual(VideoDescriptor._parse_youtube(''),
110 111 112 113
                         {'0.75': '',
                          '1.00': '',
                          '1.25': '',
                          '1.50': ''})