test_split_module.py 10.3 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 12 13
from xmodule.modulestore.tests.factories import ItemFactory, CourseFactory
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
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(ModuleStoreTestCase):
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

    def setUp(self):
31
        super(SplitTestBase, self).setUp()
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
        self.partition = UserPartition(
            0,
            'first_partition',
            'First Partition',
            [
                Group(0, 'alpha'),
                Group(1, 'beta')
            ]
        )

        self.course = CourseFactory.create(
            number=self.COURSE_NUMBER,
            user_partitions=[self.partition]
        )

        self.chapter = ItemFactory.create(
            parent_location=self.course.location,
            category="chapter",
            display_name="test chapter",
        )
        self.sequential = ItemFactory.create(
            parent_location=self.chapter.location,
            category="sequential",
            display_name="Split Test Tests",
        )

        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):
63 64 65 66
        """
        Returns a video component with parent ``parent``
        that is intended to be displayed to group ``group``.
        """
67 68 69 70 71 72 73
        return ItemFactory.create(
            parent_location=parent.location,
            category="video",
            display_name="Group {} Sees This Video".format(group),
        )

    def _problem(self, parent, group):
74 75 76 77
        """
        Returns a problem component with parent ``parent``
        that is intended to be displayed to group ``group``.
        """
78 79 80 81 82 83 84 85
        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):
86 87 88 89
        """
        Returns an html component with parent ``parent``
        that is intended to be displayed to group ``group``.
        """
90 91 92 93 94 95 96 97 98 99 100 101 102 103
        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):
104 105 106
        """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(
107 108 109 110 111 112
            user=self.student,
            course_id=self.course.id,
            key='xblock.partition_service.partition_{0}'.format(self.partition.id),
            value=str(user_tag)
        )

113 114
        resp = self.client.get(reverse(
            'courseware_section',
115
            kwargs={'course_id': self.course.id.to_deprecated_string(),
116 117
                    'chapter': self.chapter.url_name,
                    'section': self.sequential.url_name}
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        ))
        content = resp.content

        # Assert we see the proper icon in the top display
        self.assertIn('<a class="{} inactive progress-0"'.format(self.ICON_CLASSES[user_tag]), content)
        # 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

141
    COURSE_NUMBER = 'vert-split-vert'
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

    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):
164 165
        # We define problem compenents that we need but don't explicitly call elsewhere.
        # pylint: disable=unused-variable
166 167 168 169 170 171 172 173 174 175
        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",
        )
176
        # pylint: disable=protected-access
177 178
        c0_url = self.course.id.make_usage_key("vertical", "split_test_cond0")
        c1_url = self.course.id.make_usage_key("vertical", "split_test_cond1")
179 180 181 182 183 184

        split_test = ItemFactory.create(
            parent_location=vert1.location,
            category="split_test",
            display_name="Split test",
            user_partition_id='0',
185
            group_id_to_child={"0": c0_url, "1": c1_url},
186 187 188 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
        )

        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):
236 237
        # We define problem compenents that we need but don't explicitly call elsewhere.
        # pylint: disable=unused-variable
238 239 240 241
        super(TestSplitTestVert, self).setUp()

        # split_test cond 0 = vert <- {video, problem}
        # split_test cond 1 = vert <- {video, html}
242
        # pylint: disable=protected-access
243 244
        c0_url = self.course.id.make_usage_key("vertical", "split_test_cond0")
        c1_url = self.course.id.make_usage_key("vertical", "split_test_cond1")
245 246 247 248 249 250

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

        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)
271 272


273
@attr('shard_1')
274 275 276 277 278
class SplitTestPosition(ModuleStoreTestCase):
    """
    Check that we can change positions in a course with partitions defined
    """
    def setUp(self):
279 280
        super(SplitTestPosition, self).setUp()

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
        self.partition = UserPartition(
            0,
            'first_partition',
            'First Partition',
            [
                Group(0, 'alpha'),
                Group(1, 'beta')
            ]
        )

        self.course = CourseFactory.create(
            user_partitions=[self.partition]
        )

        self.chapter = ItemFactory.create(
            parent_location=self.course.location,
            category="chapter",
            display_name="test chapter",
        )

        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,
313 314
            self.course.id,
            course=self.course
315 316 317 318 319
        )

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