test_split_module.py 10.4 KB
Newer Older
1 2 3 4
"""
Test for split test XModule
"""
from django.core.urlresolvers import reverse
5
from mock import MagicMock
6
from nose.plugins.attrib import attr
7

8 9
from courseware.module_render import get_module_for_descriptor
from courseware.model_data import FieldDataCache
10
from student.tests.factories import UserFactory, CourseEnrollmentFactory
11
from xmodule.modulestore.tests.factories import ItemFactory, CourseFactory
Toby Lawrence committed
12
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
13
from xmodule.partitions.partitions import Group, UserPartition
14
from openedx.core.djangoapps.user_api.tests.factories import UserCourseTagFactory
15 16


17
@attr('shard_1')
18
class SplitTestBase(SharedModuleStoreTestCase):
19 20 21 22
    """
    Sets up a basic course and user for split test testing.
    Also provides tests of rendered HTML for two user_tag conditions, 0 and 1.
    """
23
    __test__ = False
24 25 26 27 28
    COURSE_NUMBER = 'split-test-base'
    ICON_CLASSES = None
    TOOLTIPS = None
    HIDDEN_CONTENT = None
    VISIBLE_CONTENT = None
29

30 31 32 33
    @classmethod
    def setUpClass(cls):
        super(SplitTestBase, cls).setUpClass()
        cls.partition = UserPartition(
34 35 36 37 38 39 40 41 42
            0,
            'first_partition',
            'First Partition',
            [
                Group(0, 'alpha'),
                Group(1, 'beta')
            ]
        )

43 44 45
        cls.course = CourseFactory.create(
            number=cls.COURSE_NUMBER,
            user_partitions=[cls.partition]
46 47
        )

48 49
        cls.chapter = ItemFactory.create(
            parent_location=cls.course.location,
50 51 52
            category="chapter",
            display_name="test chapter",
        )
53 54
        cls.sequential = ItemFactory.create(
            parent_location=cls.chapter.location,
55 56 57 58
            category="sequential",
            display_name="Split Test Tests",
        )

59 60 61
    def setUp(self):
        super(SplitTestBase, self).setUp()

62 63 64 65 66
        self.student = UserFactory.create()
        CourseEnrollmentFactory.create(user=self.student, course_id=self.course.id)
        self.client.login(username=self.student.username, password='test')

    def _video(self, parent, group):
67 68 69 70
        """
        Returns a video component with parent ``parent``
        that is intended to be displayed to group ``group``.
        """
71 72 73 74 75 76 77
        return ItemFactory.create(
            parent_location=parent.location,
            category="video",
            display_name="Group {} Sees This Video".format(group),
        )

    def _problem(self, parent, group):
78 79 80 81
        """
        Returns a problem component with parent ``parent``
        that is intended to be displayed to group ``group``.
        """
82 83 84 85 86 87 88 89
        return ItemFactory.create(
            parent_location=parent.location,
            category="problem",
            display_name="Group {} Sees This Problem".format(group),
            data="<h1>No Problem Defined Yet!</h1>",
        )

    def _html(self, parent, group):
90 91 92 93
        """
        Returns an html component with parent ``parent``
        that is intended to be displayed to group ``group``.
        """
94 95 96 97 98 99 100 101 102 103 104 105 106 107
        return ItemFactory.create(
            parent_location=parent.location,
            category="html",
            display_name="Group {} Sees This HTML".format(group),
            data="Some HTML for group {}".format(group),
        )

    def test_split_test_0(self):
        self._check_split_test(0)

    def test_split_test_1(self):
        self._check_split_test(1)

    def _check_split_test(self, user_tag):
108 109 110
        """Checks that the right compentents are rendered for user with ``user_tag``"""
        # This explicitly sets the user_tag for self.student to ``user_tag``
        UserCourseTagFactory(
111 112 113 114 115 116
            user=self.student,
            course_id=self.course.id,
            key='xblock.partition_service.partition_{0}'.format(self.partition.id),
            value=str(user_tag)
        )

117 118
        resp = self.client.get(reverse(
            'courseware_section',
119
            kwargs={'course_id': self.course.id.to_deprecated_string(),
120 121
                    'chapter': self.chapter.url_name,
                    'section': self.sequential.url_name}
122 123 124 125
        ))
        content = resp.content

        # Assert we see the proper icon in the top display
126
        self.assertIn('<a class="{} inactive progress-0 nav-item"'.format(self.ICON_CLASSES[user_tag]), content)
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        # And proper tooltips
        for tooltip in self.TOOLTIPS[user_tag]:
            self.assertIn(tooltip, content)

        for hidden in self.HIDDEN_CONTENT[user_tag]:
            self.assertNotIn(hidden, content)

        # Assert that we can see the data from the appropriate test condition
        for visible in self.VISIBLE_CONTENT[user_tag]:
            self.assertIn(visible, content)


class TestVertSplitTestVert(SplitTestBase):
    """
    Tests related to xmodule/split_test_module
    """
    __test__ = True

145
    COURSE_NUMBER = 'vert-split-vert'
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

    ICON_CLASSES = [
        'seq_problem',
        'seq_video',
    ]
    TOOLTIPS = [
        ['Group 0 Sees This Video', "Group 0 Sees This Problem"],
        ['Group 1 Sees This Video', 'Group 1 Sees This HTML'],
    ]
    HIDDEN_CONTENT = [
        ['Condition 0 vertical'],
        ['Condition 1 vertical'],
    ]

    # Data is html encoded, because it's inactive inside the
    # sequence until javascript is executed
    VISIBLE_CONTENT = [
        ['class=&#34;problems-wrapper'],
        ['Some HTML for group 1']
    ]

    def setUp(self):
168 169
        # We define problem compenents that we need but don't explicitly call elsewhere.
        # pylint: disable=unused-variable
170 171 172 173 174 175 176 177 178 179
        super(TestVertSplitTestVert, self).setUp()

        # vert <- split_test
        # split_test cond 0 = vert <- {video, problem}
        # split_test cond 1 = vert <- {video, html}
        vert1 = ItemFactory.create(
            parent_location=self.sequential.location,
            category="vertical",
            display_name="Split test vertical",
        )
180 181
        c0_url = self.course.id.make_usage_key("vertical", "split_test_cond0")
        c1_url = self.course.id.make_usage_key("vertical", "split_test_cond1")
182 183 184 185 186 187

        split_test = ItemFactory.create(
            parent_location=vert1.location,
            category="split_test",
            display_name="Split test",
            user_partition_id='0',
188
            group_id_to_child={"0": c0_url, "1": c1_url},
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
        )

        cond0vert = ItemFactory.create(
            parent_location=split_test.location,
            category="vertical",
            display_name="Condition 0 vertical",
            location=c0_url,
        )
        video0 = self._video(cond0vert, 0)
        problem0 = self._problem(cond0vert, 0)

        cond1vert = ItemFactory.create(
            parent_location=split_test.location,
            category="vertical",
            display_name="Condition 1 vertical",
            location=c1_url,
        )
        video1 = self._video(cond1vert, 1)
        html1 = self._html(cond1vert, 1)


class TestSplitTestVert(SplitTestBase):
    """
    Tests related to xmodule/split_test_module
    """
    __test__ = True

    COURSE_NUMBER = 'split-vert'

    ICON_CLASSES = [
        'seq_problem',
        'seq_video',
    ]
    TOOLTIPS = [
        ['Group 0 Sees This Video', "Group 0 Sees This Problem"],
        ['Group 1 Sees This Video', 'Group 1 Sees This HTML'],
    ]
    HIDDEN_CONTENT = [
        ['Condition 0 vertical'],
        ['Condition 1 vertical'],
    ]

    # Data is html encoded, because it's inactive inside the
    # sequence until javascript is executed
    VISIBLE_CONTENT = [
        ['class=&#34;problems-wrapper'],
        ['Some HTML for group 1']
    ]

    def setUp(self):
239 240
        # We define problem compenents that we need but don't explicitly call elsewhere.
        # pylint: disable=unused-variable
241 242 243 244
        super(TestSplitTestVert, self).setUp()

        # split_test cond 0 = vert <- {video, problem}
        # split_test cond 1 = vert <- {video, html}
245 246
        c0_url = self.course.id.make_usage_key("vertical", "split_test_cond0")
        c1_url = self.course.id.make_usage_key("vertical", "split_test_cond1")
247 248 249 250 251 252

        split_test = ItemFactory.create(
            parent_location=self.sequential.location,
            category="split_test",
            display_name="Split test",
            user_partition_id='0',
253
            group_id_to_child={"0": c0_url, "1": c1_url},
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
        )

        cond0vert = ItemFactory.create(
            parent_location=split_test.location,
            category="vertical",
            display_name="Condition 0 vertical",
            location=c0_url,
        )
        video0 = self._video(cond0vert, 0)
        problem0 = self._problem(cond0vert, 0)

        cond1vert = ItemFactory.create(
            parent_location=split_test.location,
            category="vertical",
            display_name="Condition 1 vertical",
            location=c1_url,
        )
        video1 = self._video(cond1vert, 1)
        html1 = self._html(cond1vert, 1)
273 274


275
@attr('shard_1')
276
class SplitTestPosition(SharedModuleStoreTestCase):
277 278 279
    """
    Check that we can change positions in a course with partitions defined
    """
280 281 282 283
    @classmethod
    def setUpClass(cls):
        super(SplitTestPosition, cls).setUpClass()
        cls.partition = UserPartition(
284 285 286 287 288 289 290 291 292
            0,
            'first_partition',
            'First Partition',
            [
                Group(0, 'alpha'),
                Group(1, 'beta')
            ]
        )

293 294
        cls.course = CourseFactory.create(
            user_partitions=[cls.partition]
295 296
        )

297 298
        cls.chapter = ItemFactory.create(
            parent_location=cls.course.location,
299 300 301 302
            category="chapter",
            display_name="test chapter",
        )

303 304 305
    def setUp(self):
        super(SplitTestPosition, self).setUp()

306 307 308 309 310 311 312 313 314 315 316 317
        self.student = UserFactory.create()
        CourseEnrollmentFactory.create(user=self.student, course_id=self.course.id)
        self.client.login(username=self.student.username, password='test')

    def test_changing_position_works(self):
        # Make a mock FieldDataCache for this course, so we can get the course module
        mock_field_data_cache = FieldDataCache([self.course], self.course.id, self.student)
        course = get_module_for_descriptor(
            self.student,
            MagicMock(name='request'),
            self.course,
            mock_field_data_cache,
318 319
            self.course.id,
            course=self.course
320 321 322 323 324
        )

        # Now that we have the course, change the position and save, nothing should explode!
        course.position = 2
        course.save()