def create(system, source_is_error_module=False): """ return a dict of modules: the conditional with a single source and a single child. Keys are 'cond_module', 'source_module', and 'child_module'. if the source_is_error_module flag is set, create a real ErrorModule for the source. """ descriptor_system = get_test_descriptor_system() # construct source descriptor and module: source_location = Location("edX", "conditional_test", "test_run", "problem", "SampleProblem", None) if source_is_error_module: # Make an error descriptor and module source_descriptor = NonStaffErrorDescriptor.from_xml( "some random xml data", system, id_generator=CourseLocationManager(source_location.course_key), error_msg="random error message", ) else: source_descriptor = Mock(name="source_descriptor") source_descriptor.location = source_location source_descriptor.runtime = descriptor_system source_descriptor.render = lambda view, context=None: descriptor_system.render(source_descriptor, view, context) # construct other descriptors: child_descriptor = Mock(name="child_descriptor") child_descriptor._xmodule.student_view.return_value.content = u"<p>This is a secret</p>" child_descriptor.student_view = child_descriptor._xmodule.student_view child_descriptor.displayable_items.return_value = [child_descriptor] child_descriptor.runtime = descriptor_system child_descriptor.xmodule_runtime = get_test_system() child_descriptor.render = lambda view, context=None: descriptor_system.render(child_descriptor, view, context) child_descriptor.location = source_location.replace(category="html", name="child") descriptor_system.load_item = { child_descriptor.location: child_descriptor, source_location: source_descriptor, }.get system.descriptor_runtime = descriptor_system # construct conditional module: cond_location = Location("edX", "conditional_test", "test_run", "conditional", "SampleConditional", None) field_data = DictFieldData( {"data": "<conditional/>", "xml_attributes": {"attempted": "true"}, "children": [child_descriptor.location]} ) cond_descriptor = ConditionalDescriptor( descriptor_system, field_data, ScopeIds(None, None, cond_location, cond_location) ) cond_descriptor.xmodule_runtime = system system.get_module = lambda desc: desc cond_descriptor.get_required_module_descriptors = Mock(return_value=[source_descriptor]) # return dict: return {"cond_module": cond_descriptor, "source_module": source_descriptor, "child_module": child_descriptor}
def test_name_collision(self): """ Test dwim translation when the old name was not unique """ org = "myorg" course = "another_course" name = "running_again" course_location = Location(org, course, name, "course", name) course_xlate = loc_mapper().translate_location(course_location, add_entry_if_missing=True) self.assertEqual(course_location, loc_mapper().translate_locator_to_location(course_xlate)) eponymous_block = course_location.replace(category="chapter") chapter_xlate = loc_mapper().translate_location(eponymous_block, add_entry_if_missing=True) self.assertEqual(course_location, loc_mapper().translate_locator_to_location(course_xlate)) self.assertEqual(eponymous_block, loc_mapper().translate_locator_to_location(chapter_xlate)) # and a non-existent one w/o add eponymous_block = course_location.replace(category="problem") with self.assertRaises(ItemNotFoundError): chapter_xlate = loc_mapper().translate_location(eponymous_block, add_entry_if_missing=False)
def test_name_collision(self): """ Test dwim translation when the old name was not unique """ org = "myorg" course = "another_course" name = "running_again" course_location = Location(org, course, name, 'course', name) course_xlate = loc_mapper().translate_location(course_location, add_entry_if_missing=True) self.assertEqual(course_location, loc_mapper().translate_locator_to_location(course_xlate)) eponymous_block = course_location.replace(category='chapter') chapter_xlate = loc_mapper().translate_location(eponymous_block, add_entry_if_missing=True) self.assertEqual(course_location, loc_mapper().translate_locator_to_location(course_xlate)) self.assertEqual(eponymous_block, loc_mapper().translate_locator_to_location(chapter_xlate)) # and a non-existent one w/o add eponymous_block = course_location.replace(category='problem') with self.assertRaises(ItemNotFoundError): chapter_xlate = loc_mapper().translate_location(eponymous_block, add_entry_if_missing=False)
def test_translate_location_dwim(self): """ Test the location translation mechanisms which try to do-what-i-mean by creating new entries for never seen queries. """ org = "foo_org" course = "bar_course" run = "baz_run" problem_name = "abc123abc123abc123abc123abc123f9" location = Location(org, course, run, "problem", problem_name) new_offering = "{}.{}".format(course, run) self.translate_n_check(location, org, new_offering, "problemabc", ModuleStoreEnum.BranchName.published, True) # create an entry w/o a guid name other_location = Location(org, course, run, "chapter", "intro") self.translate_n_check(other_location, org, new_offering, "intro", ModuleStoreEnum.BranchName.published, True) # add a distractor course delta_new_org = "{}.geek_dept".format(org) run = "delta_run" delta_new_offering = "{}.{}".format(course, run) delta_course_locn = SlashSeparatedCourseKey(org, course, run) loc_mapper().create_map_entry( delta_course_locn, delta_new_org, delta_new_offering, block_map={problem_name: {"problem": "problem3"}} ) self.translate_n_check(location, org, new_offering, "problemabc", ModuleStoreEnum.BranchName.published, True) # add a new one to both courses (ensure name doesn't have same beginning) new_prob_name = uuid.uuid4().hex while new_prob_name.startswith("abc"): new_prob_name = uuid.uuid4().hex new_prob_locn = location.replace(name=new_prob_name) new_usage_id = "problem{}".format(new_prob_name[:3]) self.translate_n_check( new_prob_locn, org, new_offering, new_usage_id, ModuleStoreEnum.BranchName.published, True ) new_prob_locn = new_prob_locn.replace(run=run) self.translate_n_check( new_prob_locn, delta_new_org, delta_new_offering, new_usage_id, ModuleStoreEnum.BranchName.published, True )
def test_translate_location_dwim(self): """ Test the location translation mechanisms which try to do-what-i-mean by creating new entries for never seen queries. """ org = 'foo_org' course = 'bar_course' run = 'baz_run' problem_name = 'abc123abc123abc123abc123abc123f9' location = Location(org, course, run, 'problem', problem_name) new_offering = '{}.{}'.format(course, run) self.translate_n_check(location, org, new_offering, 'problemabc', BRANCH_NAME_PUBLISHED, True) # create an entry w/o a guid name other_location = Location(org, course, run, 'chapter', 'intro') self.translate_n_check(other_location, org, new_offering, 'intro', BRANCH_NAME_PUBLISHED, True) # add a distractor course delta_new_org = '{}.geek_dept'.format(org) run = 'delta_run' delta_new_offering = '{}.{}'.format(course, run) delta_course_locn = SlashSeparatedCourseKey(org, course, run) loc_mapper().create_map_entry( delta_course_locn, delta_new_org, delta_new_offering, block_map={problem_name: {'problem': 'problem3'}} ) self.translate_n_check(location, org, new_offering, 'problemabc', BRANCH_NAME_PUBLISHED, True) # add a new one to both courses (ensure name doesn't have same beginning) new_prob_name = uuid.uuid4().hex while new_prob_name.startswith('abc'): new_prob_name = uuid.uuid4().hex new_prob_locn = location.replace(name=new_prob_name) new_usage_id = 'problem{}'.format(new_prob_name[:3]) self.translate_n_check(new_prob_locn, org, new_offering, new_usage_id, BRANCH_NAME_PUBLISHED, True) new_prob_locn = new_prob_locn.replace(run=run) self.translate_n_check( new_prob_locn, delta_new_org, delta_new_offering, new_usage_id, BRANCH_NAME_PUBLISHED, True )
def test_translate_location_dwim(self): """ Test the location translation mechanisms which try to do-what-i-mean by creating new entries for never seen queries. """ org = 'foo_org' course = 'bar_course' run = 'baz_run' problem_name = 'abc123abc123abc123abc123abc123f9' location = Location(org, course, run, 'problem', problem_name) new_offering = '{}.{}'.format(course, run) self.translate_n_check(location, org, new_offering, 'problemabc', 'published', True) # create an entry w/o a guid name other_location = Location(org, course, run, 'chapter', 'intro') self.translate_n_check(other_location, org, new_offering, 'intro', 'published', True) # add a distractor course delta_new_org = '{}.geek_dept'.format(org) run = 'delta_run' delta_new_offering = '{}.{}'.format(course, run) delta_course_locn = SlashSeparatedCourseKey(org, course, run) loc_mapper().create_map_entry( delta_course_locn, delta_new_org, delta_new_offering, block_map={problem_name: {'problem': 'problem3'}} ) self.translate_n_check(location, org, new_offering, 'problemabc', 'published', True) # add a new one to both courses (ensure name doesn't have same beginning) new_prob_name = uuid.uuid4().hex while new_prob_name.startswith('abc'): new_prob_name = uuid.uuid4().hex new_prob_locn = location.replace(name=new_prob_name) new_usage_id = 'problem{}'.format(new_prob_name[:3]) self.translate_n_check(new_prob_locn, org, new_offering, new_usage_id, 'published', True) new_prob_locn = new_prob_locn.replace(run=run) self.translate_n_check( new_prob_locn, delta_new_org, delta_new_offering, new_usage_id, 'published', True )
def create(system, source_is_error_module=False): """ return a dict of modules: the conditional with a single source and a single child. Keys are 'cond_module', 'source_module', and 'child_module'. if the source_is_error_module flag is set, create a real ErrorModule for the source. """ descriptor_system = get_test_descriptor_system() # construct source descriptor and module: source_location = Location("edX", "conditional_test", "test_run", "problem", "SampleProblem", None) if source_is_error_module: # Make an error descriptor and module source_descriptor = NonStaffErrorDescriptor.from_xml( 'some random xml data', system, id_generator=CourseLocationGenerator(SlashSeparatedCourseKey('edX', 'conditional_test', 'test_run')), error_msg='random error message' ) else: source_descriptor = Mock() source_descriptor.location = source_location source_descriptor.runtime = descriptor_system source_descriptor.render = lambda view, context=None: descriptor_system.render(source_descriptor, view, context) # construct other descriptors: child_descriptor = Mock() child_descriptor._xmodule.student_view.return_value.content = u'<p>This is a secret</p>' child_descriptor.student_view = child_descriptor._xmodule.student_view child_descriptor.displayable_items.return_value = [child_descriptor] child_descriptor.runtime = descriptor_system child_descriptor.xmodule_runtime = get_test_system() child_descriptor.render = lambda view, context=None: descriptor_system.render(child_descriptor, view, context) child_descriptor.location = source_location.replace(category='html', name='child') descriptor_system.load_item = { child_descriptor.location: child_descriptor, source_location: source_descriptor }.get # construct conditional module: cond_location = Location("edX", "conditional_test", "test_run", "conditional", "SampleConditional", None) field_data = DictFieldData({ 'data': '<conditional/>', 'xml_attributes': {'attempted': 'true'}, 'children': [child_descriptor.location], }) cond_descriptor = ConditionalDescriptor( descriptor_system, field_data, ScopeIds(None, None, cond_location, cond_location) ) cond_descriptor.xmodule_runtime = system system.get_module = lambda desc: desc cond_descriptor.get_required_module_descriptors = Mock(return_value=[source_descriptor]) # return dict: return {'cond_module': cond_descriptor, 'source_module': source_descriptor, 'child_module': child_descriptor}
def test_conditional_module(self): """Make sure that conditional module works""" print "Starting import" course = self.get_course('conditional_and_poll') print "Course: ", course print "id: ", course.id def inner_get_module(descriptor): if isinstance(descriptor, Location): location = descriptor descriptor = self.modulestore.get_item(location, depth=None) descriptor.xmodule_runtime = get_test_system() descriptor.xmodule_runtime.get_module = inner_get_module return descriptor # edx - HarvardX # cond_test - ER22x location = Location("HarvardX", "ER22x", "2013_Spring", "conditional", "condone") def replace_urls(text, staticfiles_prefix=None, replace_prefix='/static/', course_namespace=None): return text self.test_system.replace_urls = replace_urls self.test_system.get_module = inner_get_module module = inner_get_module(location) print "module: ", module print "module children: ", module.get_children() print "module display items (children): ", module.get_display_items() html = module.render('student_view').content print "html type: ", type(html) print "html: ", html html_expect = module.xmodule_runtime.render_template( 'conditional_ajax.html', { # Test ajax url is just usage-id / handler_name 'ajax_url': '{}/xmodule_handler'.format(location.to_deprecated_string()), 'element_id': u'i4x-HarvardX-ER22x-conditional-condone', 'depends': u'i4x-HarvardX-ER22x-problem-choiceprob' } ) self.assertEqual(html, html_expect) gdi = module.get_display_items() print "gdi=", gdi ajax = json.loads(module.handle_ajax('', '')) module.save() print "ajax: ", ajax html = ajax['html'] self.assertFalse(any(['This is a secret' in item for item in html])) # Now change state of the capa problem to make it completed inner_module = inner_get_module(location.replace(category="problem", name='choiceprob')) inner_module.attempts = 1 # Save our modifications to the underlying KeyValueStore so they can be persisted inner_module.save() ajax = json.loads(module.handle_ajax('', '')) module.save() print "post-attempt ajax: ", ajax html = ajax['html'] self.assertTrue(any(['This is a secret' in item for item in html]))
def create(system, source_is_error_module=False): """ return a dict of modules: the conditional with a single source and a single child. Keys are 'cond_module', 'source_module', and 'child_module'. if the source_is_error_module flag is set, create a real ErrorModule for the source. """ descriptor_system = get_test_descriptor_system() # construct source descriptor and module: source_location = Location("edX", "conditional_test", "test_run", "problem", "SampleProblem", None) if source_is_error_module: # Make an error descriptor and module source_descriptor = NonStaffErrorDescriptor.from_xml( 'some random xml data', system, id_generator=CourseLocationManager(source_location.course_key), error_msg='random error message') else: source_descriptor = Mock(name='source_descriptor') source_descriptor.location = source_location source_descriptor.runtime = descriptor_system source_descriptor.render = lambda view, context=None: descriptor_system.render( source_descriptor, view, context) # construct other descriptors: child_descriptor = Mock(name='child_descriptor') child_descriptor._xmodule.student_view.return_value.content = u'<p>This is a secret</p>' child_descriptor.student_view = child_descriptor._xmodule.student_view child_descriptor.displayable_items.return_value = [child_descriptor] child_descriptor.runtime = descriptor_system child_descriptor.xmodule_runtime = get_test_system() child_descriptor.render = lambda view, context=None: descriptor_system.render( child_descriptor, view, context) child_descriptor.location = source_location.replace(category='html', name='child') descriptor_system.load_item = { child_descriptor.location: child_descriptor, source_location: source_descriptor }.get system.descriptor_runtime = descriptor_system # construct conditional module: cond_location = Location("edX", "conditional_test", "test_run", "conditional", "SampleConditional", None) field_data = DictFieldData({ 'data': '<conditional/>', 'xml_attributes': { 'attempted': 'true' }, 'children': [child_descriptor.location], }) cond_descriptor = ConditionalDescriptor( descriptor_system, field_data, ScopeIds(None, None, cond_location, cond_location)) cond_descriptor.xmodule_runtime = system system.get_module = lambda desc: desc cond_descriptor.get_required_module_descriptors = Mock( return_value=[source_descriptor]) # return dict: return { 'cond_module': cond_descriptor, 'source_module': source_descriptor, 'child_module': child_descriptor }
def test_conditional_module(self): """Make sure that conditional module works""" print "Starting import" course = self.get_course('conditional_and_poll') print "Course: ", course print "id: ", course.id def inner_get_module(descriptor): if isinstance(descriptor, Location): location = descriptor descriptor = self.modulestore.get_item(location, depth=None) descriptor.xmodule_runtime = get_test_system() descriptor.xmodule_runtime.descriptor_runtime = descriptor._runtime # pylint: disable=protected-access descriptor.xmodule_runtime.get_module = inner_get_module return descriptor # edx - HarvardX # cond_test - ER22x location = Location("HarvardX", "ER22x", "2013_Spring", "conditional", "condone") def replace_urls(text, staticfiles_prefix=None, replace_prefix='/static/', course_namespace=None): return text self.test_system.replace_urls = replace_urls self.test_system.get_module = inner_get_module module = inner_get_module(location) print "module: ", module print "module children: ", module.get_children() print "module display items (children): ", module.get_display_items() html = module.render(STUDENT_VIEW).content print "html type: ", type(html) print "html: ", html html_expect = module.xmodule_runtime.render_template( 'conditional_ajax.html', { # Test ajax url is just usage-id / handler_name 'ajax_url': '{}/xmodule_handler'.format(location.to_deprecated_string()), 'element_id': u'i4x-HarvardX-ER22x-conditional-condone', 'depends': u'i4x-HarvardX-ER22x-problem-choiceprob' }) self.assertEqual(html, html_expect) gdi = module.get_display_items() print "gdi=", gdi ajax = json.loads(module.handle_ajax('', '')) module.save() print "ajax: ", ajax html = ajax['html'] self.assertFalse(any(['This is a secret' in item for item in html])) # Now change state of the capa problem to make it completed inner_module = inner_get_module( location.replace(category="problem", name='choiceprob')) inner_module.attempts = 1 # Save our modifications to the underlying KeyValueStore so they can be persisted inner_module.save() ajax = json.loads(module.handle_ajax('', '')) module.save() print "post-attempt ajax: ", ajax html = ajax['html'] self.assertTrue(any(['This is a secret' in item for item in html]))
def create(system, source_is_error_module=False, source_visible_to_staff_only=False): """ return a dict of modules: the conditional with a single source and a single child. Keys are 'cond_module', 'source_module', and 'child_module'. if the source_is_error_module flag is set, create a real ErrorModule for the source. """ descriptor_system = get_test_descriptor_system() # construct source descriptor and module: source_location = Location("edX", "conditional_test", "test_run", "problem", "SampleProblem", None) if source_is_error_module: # Make an error descriptor and module source_descriptor = NonStaffErrorDescriptor.from_xml( 'some random xml data', system, id_generator=CourseLocationManager(source_location.course_key), error_msg='random error message') else: source_descriptor = Mock(name='source_descriptor') source_descriptor.location = source_location source_descriptor.visible_to_staff_only = source_visible_to_staff_only source_descriptor.runtime = descriptor_system source_descriptor.render = lambda view, context=None: descriptor_system.render( source_descriptor, view, context) # construct other descriptors: child_descriptor = Mock(name='child_descriptor') child_descriptor.visible_to_staff_only = False child_descriptor._xmodule.student_view.return_value.content = u'<p>This is a secret</p>' child_descriptor.student_view = child_descriptor._xmodule.student_view child_descriptor.displayable_items.return_value = [child_descriptor] child_descriptor.runtime = descriptor_system child_descriptor.xmodule_runtime = get_test_system() child_descriptor.render = lambda view, context=None: descriptor_system.render( child_descriptor, view, context) child_descriptor.location = source_location.replace(category='html', name='child') def visible_to_nonstaff_users(desc): """ Returns if the object is visible to nonstaff users. """ return not desc.visible_to_staff_only def load_item(usage_id, for_parent=None): # pylint: disable=unused-argument """Test-only implementation of load_item that simply returns static xblocks.""" return { child_descriptor.location: child_descriptor, source_location: source_descriptor }.get(usage_id) descriptor_system.load_item = load_item system.descriptor_runtime = descriptor_system # construct conditional module: cond_location = Location("edX", "conditional_test", "test_run", "conditional", "SampleConditional", None) field_data = DictFieldData({ 'data': '<conditional/>', 'conditional_attr': 'attempted', 'conditional_value': 'true', 'xml_attributes': { 'attempted': 'true' }, 'children': [child_descriptor.location], }) cond_descriptor = ConditionalDescriptor( descriptor_system, field_data, ScopeIds(None, None, cond_location, cond_location)) cond_descriptor.xmodule_runtime = system system.get_module = lambda desc: desc if visible_to_nonstaff_users( desc) else None cond_descriptor.get_required_module_descriptors = Mock( return_value=[source_descriptor]) cond_descriptor.required_modules = [ system.get_module(descriptor) for descriptor in cond_descriptor.get_required_module_descriptors() ] # return dict: return { 'cond_module': cond_descriptor, 'source_module': source_descriptor, 'child_module': child_descriptor }
def test_translate_location_read_only(self): """ Test the variants of translate_location which don't create entries, just decode """ # lookup before there are any maps org = "foo_org" course = "bar_course" run = "baz_run" slash_course_key = SlashSeparatedCourseKey(org, course, run) with self.assertRaises(ItemNotFoundError): _ = loc_mapper().translate_location( Location(org, course, run, "problem", "abc123"), add_entry_if_missing=False ) new_style_org = "{}.geek_dept".format(org) new_style_offering = ".{}.{}".format(course, run) block_map = { "abc123": {"problem": "problem2", "vertical": "vertical2"}, "def456": {"problem": "problem4"}, "ghi789": {"problem": "problem7"}, } loc_mapper().create_map_entry(slash_course_key, new_style_org, new_style_offering, block_map=block_map) test_problem_locn = Location(org, course, run, "problem", "abc123") self.translate_n_check( test_problem_locn, new_style_org, new_style_offering, "problem2", ModuleStoreEnum.BranchName.published ) # look for non-existent problem with self.assertRaises(ItemNotFoundError): loc_mapper().translate_location(Location(org, course, run, "problem", "1def23"), add_entry_if_missing=False) test_no_cat_locn = test_problem_locn.replace(category=None) with self.assertRaises(InvalidLocationError): loc_mapper().translate_location( slash_course_key.make_usage_key(None, "abc123"), test_no_cat_locn, False, False ) test_no_cat_locn = test_no_cat_locn.replace(name="def456") self.translate_n_check( test_no_cat_locn, new_style_org, new_style_offering, "problem4", ModuleStoreEnum.BranchName.published ) # add a distractor course (note that abc123 has a different translation in this one) distractor_block_map = { "abc123": {"problem": "problem3"}, "def456": {"problem": "problem4"}, "ghi789": {"problem": "problem7"}, } run = "delta_run" test_delta_new_org = "{}.geek_dept".format(org) test_delta_new_offering = "{}.{}".format(course, run) loc_mapper().create_map_entry( SlashSeparatedCourseKey(org, course, run), test_delta_new_org, test_delta_new_offering, block_map=distractor_block_map, ) # test that old translation still works self.translate_n_check( test_problem_locn, new_style_org, new_style_offering, "problem2", ModuleStoreEnum.BranchName.published ) # and new returns new id self.translate_n_check( test_problem_locn.replace(run=run), test_delta_new_org, test_delta_new_offering, "problem3", ModuleStoreEnum.BranchName.published, )
def test_translate_location_read_only(self): """ Test the variants of translate_location which don't create entries, just decode """ # lookup before there are any maps org = 'foo_org' course = 'bar_course' run = 'baz_run' slash_course_key = SlashSeparatedCourseKey(org, course, run) with self.assertRaises(ItemNotFoundError): _ = loc_mapper().translate_location( Location(org, course, run, 'problem', 'abc123'), add_entry_if_missing=False ) new_style_org = '{}.geek_dept'.format(org) new_style_offering = '.{}.{}'.format(course, run) block_map = { 'abc123': {'problem': 'problem2', 'vertical': 'vertical2'}, 'def456': {'problem': 'problem4'}, 'ghi789': {'problem': 'problem7'}, } loc_mapper().create_map_entry( slash_course_key, new_style_org, new_style_offering, block_map=block_map ) test_problem_locn = Location(org, course, run, 'problem', 'abc123') self.translate_n_check(test_problem_locn, new_style_org, new_style_offering, 'problem2', BRANCH_NAME_PUBLISHED) # look for non-existent problem with self.assertRaises(ItemNotFoundError): loc_mapper().translate_location( Location(org, course, run, 'problem', '1def23'), add_entry_if_missing=False ) test_no_cat_locn = test_problem_locn.replace(category=None) with self.assertRaises(InvalidLocationError): loc_mapper().translate_location( slash_course_key.make_usage_key(None, 'abc123'), test_no_cat_locn, False, False ) test_no_cat_locn = test_no_cat_locn.replace(name='def456') self.translate_n_check( test_no_cat_locn, new_style_org, new_style_offering, 'problem4', BRANCH_NAME_PUBLISHED ) # add a distractor course (note that abc123 has a different translation in this one) distractor_block_map = { 'abc123': {'problem': 'problem3'}, 'def456': {'problem': 'problem4'}, 'ghi789': {'problem': 'problem7'}, } run = 'delta_run' test_delta_new_org = '{}.geek_dept'.format(org) test_delta_new_offering = '{}.{}'.format(course, run) loc_mapper().create_map_entry( SlashSeparatedCourseKey(org, course, run), test_delta_new_org, test_delta_new_offering, block_map=distractor_block_map ) # test that old translation still works self.translate_n_check( test_problem_locn, new_style_org, new_style_offering, 'problem2', BRANCH_NAME_PUBLISHED ) # and new returns new id self.translate_n_check( test_problem_locn.replace(run=run), test_delta_new_org, test_delta_new_offering, 'problem3', BRANCH_NAME_PUBLISHED )
def create(system, source_is_error_module=False, source_visible_to_staff_only=False): """ return a dict of modules: the conditional with a single source and a single child. Keys are 'cond_module', 'source_module', and 'child_module'. if the source_is_error_module flag is set, create a real ErrorModule for the source. """ descriptor_system = get_test_descriptor_system() # construct source descriptor and module: source_location = Location("edX", "conditional_test", "test_run", "problem", "SampleProblem", None) if source_is_error_module: # Make an error descriptor and module source_descriptor = NonStaffErrorDescriptor.from_xml( 'some random xml data', system, id_generator=CourseLocationManager(source_location.course_key), error_msg='random error message' ) else: source_descriptor = Mock(name='source_descriptor') source_descriptor.location = source_location source_descriptor.visible_to_staff_only = source_visible_to_staff_only source_descriptor.runtime = descriptor_system source_descriptor.render = lambda view, context=None: descriptor_system.render(source_descriptor, view, context) # construct other descriptors: child_descriptor = Mock(name='child_descriptor') child_descriptor.visible_to_staff_only = False child_descriptor._xmodule.student_view.return_value.content = u'<p>This is a secret</p>' child_descriptor.student_view = child_descriptor._xmodule.student_view child_descriptor.displayable_items.return_value = [child_descriptor] child_descriptor.runtime = descriptor_system child_descriptor.xmodule_runtime = get_test_system() child_descriptor.render = lambda view, context=None: descriptor_system.render(child_descriptor, view, context) child_descriptor.location = source_location.replace(category='html', name='child') def visible_to_nonstaff_users(desc): """ Returns if the object is visible to nonstaff users. """ return not desc.visible_to_staff_only def load_item(usage_id, for_parent=None): # pylint: disable=unused-argument """Test-only implementation of load_item that simply returns static xblocks.""" return { child_descriptor.location: child_descriptor, source_location: source_descriptor }.get(usage_id) descriptor_system.load_item = load_item system.descriptor_runtime = descriptor_system # construct conditional module: cond_location = Location("edX", "conditional_test", "test_run", "conditional", "SampleConditional", None) field_data = DictFieldData({ 'data': '<conditional/>', 'conditional_attr': 'attempted', 'conditional_value': 'true', 'xml_attributes': {'attempted': 'true'}, 'children': [child_descriptor.location], }) cond_descriptor = ConditionalDescriptor( descriptor_system, field_data, ScopeIds(None, None, cond_location, cond_location) ) cond_descriptor.xmodule_runtime = system system.get_module = lambda desc: desc if visible_to_nonstaff_users(desc) else None cond_descriptor.get_required_module_descriptors = Mock(return_value=[source_descriptor]) cond_descriptor.required_modules = [ system.get_module(descriptor) for descriptor in cond_descriptor.get_required_module_descriptors() ] # return dict: return {'cond_module': cond_descriptor, 'source_module': source_descriptor, 'child_module': child_descriptor}
def test_translate_location_read_only(self): """ Test the variants of translate_location which don't create entries, just decode """ # lookup before there are any maps org = 'foo_org' course = 'bar_course' run = 'baz_run' slash_course_key = SlashSeparatedCourseKey(org, course, run) with self.assertRaises(ItemNotFoundError): _ = loc_mapper().translate_location( Location(org, course, run, 'problem', 'abc123'), add_entry_if_missing=False ) new_style_org = '{}.geek_dept'.format(org) new_style_offering = '.{}.{}'.format(course, run) block_map = { 'abc123': {'problem': 'problem2', 'vertical': 'vertical2'}, 'def456': {'problem': 'problem4'}, 'ghi789': {'problem': 'problem7'}, } loc_mapper().create_map_entry( slash_course_key, new_style_org, new_style_offering, block_map=block_map ) test_problem_locn = Location(org, course, run, 'problem', 'abc123') self.translate_n_check(test_problem_locn, new_style_org, new_style_offering, 'problem2', 'published') # look for non-existent problem with self.assertRaises(ItemNotFoundError): loc_mapper().translate_location( Location(org, course, run, 'problem', '1def23'), add_entry_if_missing=False ) test_no_cat_locn = test_problem_locn.replace(category=None) with self.assertRaises(InvalidLocationError): loc_mapper().translate_location( slash_course_key.make_usage_key(None, 'abc123'), test_no_cat_locn, False, False ) test_no_cat_locn = test_no_cat_locn.replace(name='def456') self.translate_n_check( test_no_cat_locn, new_style_org, new_style_offering, 'problem4', 'published' ) # add a distractor course (note that abc123 has a different translation in this one) distractor_block_map = { 'abc123': {'problem': 'problem3'}, 'def456': {'problem': 'problem4'}, 'ghi789': {'problem': 'problem7'}, } run = 'delta_run' test_delta_new_org = '{}.geek_dept'.format(org) test_delta_new_offering = '{}.{}'.format(course, run) loc_mapper().create_map_entry( SlashSeparatedCourseKey(org, course, run), test_delta_new_org, test_delta_new_offering, block_map=distractor_block_map ) # test that old translation still works self.translate_n_check( test_problem_locn, new_style_org, new_style_offering, 'problem2', 'published' ) # and new returns new id self.translate_n_check( test_problem_locn.replace(run=run), test_delta_new_org, test_delta_new_offering, 'problem3', 'published' )