def test_linked_problem(self): """ Check to see if a peer grading module with a linked problem loads properly. """ # Mock the linked problem descriptor. linked_descriptor = Mock() linked_descriptor.location = self.coe_location # Mock the peer grading descriptor. pg_descriptor = Mock() pg_descriptor.location = self.problem_location pg_descriptor.get_required_module_descriptors = lambda: [linked_descriptor, ] # Setup the proper field data for the peer grading module. field_data = DictFieldData({ 'data': '<peergrading/>', 'location': self.problem_location, 'use_for_single_location': True, 'link_to_location': self.coe_location, }) # Initialize the peer grading module. peer_grading = PeerGradingModule( pg_descriptor, self.test_system, field_data, ScopeIds(None, None, self.problem_location, self.problem_location) ) # Ensure that it is properly setup. self.assertTrue(peer_grading.use_for_single_location)
def test_distance(self): from warlord.path import distance tileA = Mock() tileB = Mock() tileA.location = (0, 0) tileB.location = (0, 1) self.assertEquals(distance(tileA, tileB), 1)
def _create_peer_grading_with_linked_problem(self, location, valid_linked_descriptor=True): """ Create a peer grading problem with a linked location. """ # Mock the linked problem descriptor. linked_descriptor = Mock() linked_descriptor.location = location # Mock the peer grading descriptor. pg_descriptor = Mock() pg_descriptor.location = self.problem_location if valid_linked_descriptor: pg_descriptor.get_required_module_descriptors = lambda: [ linked_descriptor, ] else: pg_descriptor.get_required_module_descriptors = lambda: [] test_system = self.get_module_system(pg_descriptor) # Initialize the peer grading module. peer_grading = PeerGradingModule( pg_descriptor, test_system, self.field_data, self.scope_ids, ) return peer_grading
def setUp(self): self.symbols = [] # columns self.columns_count = -1 for i in range(0, 8, 1): symbol = Mock() symbol.location = ((0 + (7 - i) * 20, 0), ((7 - i) * 20, 10), (10 + (7 - i) * 20, 10), (10 + (7 - i) * 20, 0)) symbol.data = "__" self.symbols.append(symbol) self.columns_count += 1 # cards self.cards_count = 0 for i in range(0, 7, 1): symbol = Mock() symbol.location = ((5 + i * 20, 5), (5 + i * 20, 15), (15 + i * 20, 15), (15 + i * 20, 5)) symbol.data = "T" + str(i) * 5 self.symbols.append(symbol) self.cards_count += 1 # config symbol symbol = Mock() symbol.location = ((50, 50), (50, 60), (60, 60), (60, 50)) self.board_id = 1 symbol.data = "[%d,%d]" % (self.board_id, self.columns_count) self.symbols.append(symbol)
def _create_peer_grading_with_linked_problem(self, location, valid_linked_descriptor=True): """ Create a peer grading problem with a linked location. """ # Mock the linked problem descriptor. linked_descriptor = Mock() linked_descriptor.location = location # Mock the peer grading descriptor. pg_descriptor = Mock() pg_descriptor.location = self.problem_location if valid_linked_descriptor: pg_descriptor.get_required_module_descriptors = lambda: [linked_descriptor, ] else: pg_descriptor.get_required_module_descriptors = lambda: [] test_system = self.get_module_system(pg_descriptor) # Initialize the peer grading module. peer_grading = PeerGradingModule( pg_descriptor, test_system, self.field_data, self.scope_ids, ) return peer_grading
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 mock_descriptor(fields=[], lms_fields=[]): descriptor = Mock() descriptor.stores_state = True descriptor.location = location('def_id') descriptor.module_class.fields = fields descriptor.module_class.lms.fields = lms_fields return descriptor
def mock_descriptor(fields=[], lms_fields=[]): descriptor = Mock() descriptor.location = location('def_id') descriptor.module_class.fields = fields descriptor.module_class.lms.fields = lms_fields descriptor.module_class.__name__ = 'MockProblemModule' return descriptor
def test_buckets_list(self, client_patch): expected_help = re.compile('Usage: hlcloud buckets list') out = subprocess.check_output(['hlcloud', 'buckets', 'list', '--help'], stderr=subprocess.STDOUT) self.assertRegex(str(out), expected_help) client = Mock() client_patch.return_value = client bucket = Mock() bucket.name = "BUCKET" bucket.storage_class = "REGIONAL" bucket.location = "US-CENTRAL1" bucket.labels = { "user": "******", "project": "mgi-project", "pipeline": "mgi_pipeline", } bucket.update = MagicMock(return_value=1) client.list_buckets = MagicMock(return_value=[bucket]) runner = CliRunner() result = runner.invoke(cli_buckets.buckets_list_cmd) self.assertEqual(result.exit_code, 0) expected_output = """NAME LOCATION CLASS USER PROJECT PIPELINE ------ ----------- -------- --------- ----------- ------------ BUCKET US-CENTRAL1 REGIONAL rfranklin mgi-project mgi_pipeline""" self.assertEqual(result.output, expected_output) client.list_buckets.assert_called() bucket.update.assert_called
def mock_descriptor(fields=[]): descriptor = Mock() descriptor.location = location('def_id') descriptor.module_class.fields.values.return_value = fields descriptor.fields.values.return_value = fields descriptor.module_class.__name__ = 'MockProblemModule' return descriptor
def test_retrieve_with_block(self): """ Check if the API creates the model and retrieved correct info. """ block = Mock() block.location = 'block-v1:course+test+2020+type@problem+block@test' block.lti_version = LtiConfiguration.LTI_1P3 LtiConfiguration.objects.create(location=block.location) # Call API launch_info = get_lti_1p3_launch_info(block=block) # Retrieve created config and check full launch info data lti_config = LtiConfiguration.objects.get() self.assertCountEqual( launch_info, { 'client_id': lti_config.lti_1p3_client_id, 'keyset_url': 'https://example.com/api/lti_consumer/v1/public_keysets/{}'. format(lti_config.lti_1p3_client_id), 'deployment_id': '1', 'oidc_callback': 'https://example.com/api/lti_consumer/v1/launch/', 'token_url': 'https://example.com/api/lti_consumer/v1/token/{}'.format( lti_config.lti_1p3_client_id), })
def test_combat_raises_out_of_range_when_not_in_range(self): from warlord.combat import UnitOutOfRangeError, combat unitA = Mock() unitB = Mock() weapon = Mock() weapon.attack_range = [0] unitA.equipped_item = weapon tileA = Mock() tileA.location = (0, 0) tileA.unit = unitA unitA.tile = tileA tileB = Mock() tileB.location = (0, 2) tileB.unit = unitB unitB.tile = tileB self.assertRaises(UnitOutOfRangeError, combat, unitA, unitB)
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. """ # construct source descriptor and module: source_location = Location( ["i4x", "edX", "conditional_test", "problem", "SampleProblem"]) if source_is_error_module: # Make an error descriptor and module source_descriptor = NonStaffErrorDescriptor.from_xml( 'some random xml data', system, org=source_location.org, course=source_location.course, error_msg='random error message') source_module = source_descriptor.xmodule(system) else: source_descriptor = Mock() source_descriptor.location = source_location source_module = Mock() # construct other descriptors: child_descriptor = Mock() cond_descriptor = Mock() cond_descriptor.get_required_module_descriptors = lambda: [ source_descriptor, ] cond_descriptor.get_children = lambda: [ child_descriptor, ] cond_descriptor.xml_attributes = {"attempted": "true"} # create child module: child_module = Mock() child_module.get_html = lambda: '<p>This is a secret</p>' child_module.displayable_items = lambda: [child_module] module_map = { source_descriptor: source_module, child_descriptor: child_module } system.get_module = lambda descriptor: module_map[descriptor] # construct conditional module: cond_location = Location([ "i4x", "edX", "conditional_test", "conditional", "SampleConditional" ]) model_data = {'data': '<conditional/>', 'location': cond_location} cond_module = ConditionalModule(system, cond_descriptor, model_data) # return dict: return { 'cond_module': cond_module, 'source_module': source_module, 'child_module': child_module }
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(["i4x", "edX", "conditional_test", "problem", "SampleProblem"]) if source_is_error_module: # Make an error descriptor and module source_descriptor = NonStaffErrorDescriptor.from_xml( 'some random xml data', system, org=source_location.org, course=source_location.course, 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) descriptor_system.load_item = {'child': child_descriptor, 'source': source_descriptor}.get # construct conditional module: cond_location = Location(["i4x", "edX", "conditional_test", "conditional", "SampleConditional"]) field_data = DictFieldData({ 'data': '<conditional/>', 'xml_attributes': {'attempted': 'true'}, 'children': ['child'], }) 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_scanned_columns_count_is_equal_to_config_column_count(self): symbols = [] symbol = Mock() symbol.location = ((50, 50), (50, 60), (60, 60), (60, 50)) columns_count = 1 symbol.data = "[1,%d]" % (columns_count) symbols.append(symbol) self.assertRaises(IOError, mapper.Mapper, (symbols))
def test_usingCopyAsParent(self): from niprov.commandline import Commandline self.dependencies.config.verbosity = 'info' cmd = Commandline(self.dependencies) cmd.log = Mock() copy = Mock() copy.location = 'moon:/copy/location' cmd.usingCopyAsParent(copy) cmd.log.assert_called_with('warning', 'Used provenance from copy found at '+str(copy.location))
def test_usingCopyAsParent(self): from niprov.commandline import Commandline self.dependencies.config.verbosity = 'info' cmd = Commandline(self.dependencies) cmd.log = Mock() copy = Mock() copy.location = 'moon:/copy/location' cmd.usingCopyAsParent(copy) cmd.log.assert_called_with( 'warning', 'Used provenance from copy found at ' + str(copy.location))
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. """ # construct source descriptor and module: source_location = Location(["i4x", "edX", "conditional_test", "problem", "SampleProblem"]) if source_is_error_module: # Make an error descriptor and module source_descriptor = NonStaffErrorDescriptor.from_xml('some random xml data', system, org=source_location.org, course=source_location.course, error_msg='random error message') source_module = source_descriptor.xmodule(system) else: source_descriptor = Mock() source_descriptor.location = source_location source_module = Mock() # construct other descriptors: child_descriptor = Mock() cond_descriptor = Mock() cond_descriptor.runtime = system cond_descriptor.get_required_module_descriptors = lambda: [source_descriptor, ] cond_descriptor.get_children = lambda: [child_descriptor, ] cond_descriptor.xml_attributes = {"attempted": "true"} # create child module: child_module = Mock() child_module.runtime = system child_module.get_html.return_value = u'<p>This is a secret</p>' child_module.student_view.return_value = Fragment(child_module.get_html.return_value) child_module.displayable_items = lambda: [child_module] module_map = {source_descriptor: source_module, child_descriptor: child_module} system.get_module = lambda descriptor: module_map[descriptor] # construct conditional module: cond_location = Location(["i4x", "edX", "conditional_test", "conditional", "SampleConditional"]) field_data = DictFieldData({'data': '<conditional/>', 'location': cond_location}) cond_module = ConditionalModule( cond_descriptor, system, field_data, ScopeIds(None, None, cond_location, cond_location) ) # return dict: return {'cond_module': cond_module, 'source_module': source_module, 'child_module': child_module}
def test_do_build_oserror(self): """Test running of do_build""" from unuo.docker import Docker_1_1_x with patch.object(Docker_1_1_x, 'run_and_log') as run_and_log: run_and_log.side_effect = OSError(1, 'oh no') logmanager = Mock() build = Mock() build.repo = 'arepo' build.dockertag = 'atag' build.location = 'alocation' docker = Docker_1_1_x(logmanager) self.assertEquals('oh no', docker.do_build(build).next())
def _create_peer_grading_with_linked_problem(self, location, valid_linked_descriptor=True): """ Create a peer grading problem with a linked location. """ # Mock the linked problem descriptor. linked_descriptor = Mock() linked_descriptor.location = location # Mock the peer grading descriptor. pg_descriptor = Mock() pg_descriptor.location = self.problem_location if valid_linked_descriptor: pg_descriptor.get_required_module_descriptors = lambda: [ linked_descriptor, ] else: pg_descriptor.get_required_module_descriptors = lambda: [] # Setup the proper field data for the peer grading module. field_data = DictFieldData({ 'data': '<peergrading/>', 'location': self.problem_location, 'use_for_single_location': True, 'link_to_location': self.coe_location.url(), 'graded': True, }) # Initialize the peer grading module. peer_grading = PeerGradingModule( pg_descriptor, self.test_system, field_data, ScopeIds(None, None, self.problem_location, self.problem_location)) return peer_grading
def _create_peer_grading_with_linked_problem(self, location, valid_linked_descriptor=True): """ Create a peer grading problem with a linked location. """ # Mock the linked problem descriptor. linked_descriptor = Mock() linked_descriptor.location = location # Mock the peer grading descriptor. pg_descriptor = Mock() pg_descriptor.location = self.problem_location if valid_linked_descriptor: pg_descriptor.get_required_module_descriptors = lambda: [linked_descriptor, ] else: pg_descriptor.get_required_module_descriptors = lambda: [] # Setup the proper field data for the peer grading module. field_data = DictFieldData({ 'data': '<peergrading/>', 'location': self.problem_location, 'use_for_single_location': True, 'link_to_location': self.coe_location.url(), 'graded': True, }) # Initialize the peer grading module. peer_grading = PeerGradingModule( pg_descriptor, self.test_system, field_data, ScopeIds(None, None, self.problem_location, self.problem_location) ) return peer_grading
def get_order_form(ready_by, order_method): site = Site.objects.all()[0] location = site.location_set.all()[0] request = Mock() request.site = site request.location = location form = OrderForm({ 'name': 'John Smith', 'phone': '12345', 'ready_by_0': ready_by.strftime('%I'), 'ready_by_1': ready_by.strftime('%M'), 'ready_by_2': ready_by.strftime('%p'), 'method': order_method, }, request=request) form.total = '40.00' return form
def test_do_build(self): """Test running of do_build""" from unuo.docker import Docker_1_1_x with patch.object(Docker_1_1_x, 'run_and_log') as run_and_log: logmanager = Mock() build = Mock() build.repo = 'arepo' build.dockertag = 'atag' build.location = 'alocation' build.push = True run_and_log.return_value = ['line1', 'line2'] with patch('unuo.docker.shutil.rmtree') as rmtree: rmtree.return_value = None docker = Docker_1_1_x(logmanager) for line in docker.do_build(build): pass
def test_retrieve_with_block(self): """ Check if the API creates a model if no object matching properties is found. """ block = Mock() block.location = 'block-v1:course+test+2020+type@problem+block@test' block.lti_version = LtiConfiguration.LTI_1P3 LtiConfiguration.objects.create(location=block.location) # Call API with patch("lti_consumer.models.LtiConfiguration.get_lti_consumer" ) as mock_get_lti_consumer: get_lti_consumer(block=block) mock_get_lti_consumer.assert_called_once() # Check that there's just a single LTI Config in the models self.assertEqual(LtiConfiguration.objects.all().count(), 1)
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. """ # construct source descriptor and module: source_location = Location(["i4x", "edX", "conditional_test", "problem", "SampleProblem"]) if source_is_error_module: # Make an error descriptor and module source_descriptor = NonStaffErrorDescriptor.from_xml( "some random xml data", system, org=source_location.org, course=source_location.course, error_msg="random error message", ) source_module = source_descriptor.xmodule(system) else: source_descriptor = Mock() source_descriptor.location = source_location source_module = Mock() # construct other descriptors: child_descriptor = Mock() cond_descriptor = Mock() cond_descriptor.get_required_module_descriptors = lambda: [source_descriptor] cond_descriptor.get_children = lambda: [child_descriptor] cond_descriptor.xml_attributes = {"attempted": "true"} # create child module: child_module = Mock() child_module.get_html = lambda: "<p>This is a secret</p>" child_module.displayable_items = lambda: [child_module] module_map = {source_descriptor: source_module, child_descriptor: child_module} system.get_module = lambda descriptor: module_map[descriptor] # construct conditional module: cond_location = Location(["i4x", "edX", "conditional_test", "conditional", "SampleConditional"]) model_data = {"data": "<conditional/>", "location": cond_location} cond_module = ConditionalModule(system, cond_descriptor, model_data) # return dict: return {"cond_module": cond_module, "source_module": source_module, "child_module": child_module}
def get_order_form(ready_by, order_method): site = Site.objects.all()[0] location = site.location_set.all()[0] request = Mock() request.site = site request.location = location form = OrderForm( { 'name': 'John Smith', 'phone': '12345', 'ready_by_0': ready_by.strftime('%I'), 'ready_by_1': ready_by.strftime('%M'), 'ready_by_2': ready_by.strftime('%p'), 'method': order_method, }, request=request) form.total = '40.00' return form
def test_do_build_shutil_errror(self): """Test running of do_build""" from unuo.docker import Docker_1_1_x with patch.object(Docker_1_1_x, 'run_and_log') as run_and_log: with patch('unuo.docker.shutil.rmtree') as rmtree: rmtree.side_effect = Exception('noooo') run_and_log.return_value = ['pass'] logmanager = Mock() build = Mock() build.repo = 'arepo' build.dockertag = 'atag' build.location = 'alocation' build.push = False docker = Docker_1_1_x(logmanager) g = docker.do_build(build) self.assertEquals('pass', g.next()) self.assertEquals('pass', g.next()) self.assertEquals( '!! Unable to remove build folder', g.next())
def test_icat_uses_prefix_mapper(self, _): """ Test: The instrument shorthand name is used When: querying ICAT with function get_location_and_rb_from_icat """ icat_client = Mock() data_file = Mock() data_file.location = 'location' data_file.dataset.investigation.name = 'inv_name' # Add return here to ensure we do NOT try fall through cases # and do NOT have to deal with multiple calls to mock icat_client.execute_query.return_value = [data_file] actual_loc, actual_inv_name = ms.get_location_and_rb_from_icat( icat_client, 'MARI', '123', 'nxs') self.assertEqual('location', actual_loc) self.assertEqual('inv_name', actual_inv_name) icat_client.execute_query.assert_called_once_with( "SELECT df FROM Datafile df WHERE" " df.name = 'MAR00123.nxs' INCLUDE" " df.dataset AS ds, ds.investigation")
def test_that_it_correctly_fetches_the_item_geometry(self): mock_item = Mock() mock_item.location = 'location' eq_('location', self.feed.item_geometry(mock_item))
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 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') 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/>', '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 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 = BlockUsageLocator(CourseLocator("edX", "conditional_test", "test_run", deprecated=True), "problem", "SampleProblem", deprecated=True) 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 = Fragment( 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 = BlockUsageLocator(CourseLocator("edX", "conditional_test", "test_run", deprecated=True), "conditional", "SampleConditional", deprecated=True) 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 _mock_team(idx, distance): team = Mock(id=idx) team.location = Mock(center_distance=distance) return team
def fileWithP(self, provenance, protocol=None): mfile = Mock() mfile.location = 'afilename' mfile.getProvenance.return_value = provenance mfile.getProtocolFields.return_value = protocol return mfile