""" Test for split test XModule """ from unittest.mock import MagicMock from django.urls import reverse from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase, SharedModuleStoreTestCase from xmodule.modulestore.tests.factories import CourseFactory, BlockFactory from xmodule.partitions.partitions import Group, UserPartition from lms.djangoapps.courseware.model_data import FieldDataCache from lms.djangoapps.courseware.block_render import get_block_for_descriptor from openedx.core.djangoapps.user_api.tests.factories import UserCourseTagFactory from common.djangoapps.student.tests.factories import CourseEnrollmentFactory, UserFactory class SplitTestBase(ModuleStoreTestCase): """ 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. """ __test__ = False COURSE_NUMBER = 'split-test-base' ICON_CLASSES = None TOOLTIPS = None VISIBLE_CONTENT = None @classmethod def setUpClass(cls): super().setUpClass() cls.partition = UserPartition( 0, 'first_partition', 'First Partition', [ Group(0, 'alpha'), Group(1, 'beta') ] ) def setUp(self): super().setUp() self.course = CourseFactory.create( number=self.COURSE_NUMBER, user_partitions=[self.partition] ) self.chapter = BlockFactory.create( parent_location=self.course.location, category="chapter", display_name="test chapter", ) self.sequential = BlockFactory.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') self.included_usage_keys = None self.excluded_usage_keys = None def _video(self, parent, group): """ Returns a video component with parent ``parent`` that is intended to be displayed to group ``group``. """ return BlockFactory.create( parent_location=parent.location, category="video", display_name=f"Group {group} Sees This Video", ) def _problem(self, parent, group): """ Returns a problem component with parent ``parent`` that is intended to be displayed to group ``group``. """ return BlockFactory.create( parent_location=parent.location, category="problem", display_name=f"Group {group} Sees This Problem", data="

No Problem Defined Yet!

", ) def _html(self, parent, group): """ Returns an html component with parent ``parent`` that is intended to be displayed to group ``group``. """ return BlockFactory.create( parent_location=parent.location, category="html", display_name=f"Group {group} Sees This HTML", data=f"Some HTML for group {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): """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( user=self.student, course_id=self.course.id, key=f'xblock.partition_service.partition_{self.partition.id}', value=str(user_tag) ) resp = self.client.get(reverse('render_xblock', args=[str(self.sequential.location)])) unicode_content = resp.content.decode(resp.charset) # Assert we see the proper icon in the top display assert f'