def _assert_import(self, course_dir, expected_field_val, has_draft=False): """ Import a course from XML, then verify that the XBlock was loaded with the correct field value. Args: course_dir (str): The name of the course directory (relative to the test data directory) expected_xblock_loc (str): The location of the XBlock in the course. expected_field_val (str): The expected value of the XBlock's test field. Kwargs: has_draft (bool): If true, check that a draft of the XBlock exists with the expected field value set. """ courses = import_from_xml(self.store, self.user.id, 'common/test/data', [course_dir]) xblock_location = courses[0].id.make_usage_key('stubxblock', 'xblock_test') if has_draft: xblock_location = as_draft(xblock_location) xblock = self.store.get_item(xblock_location) self.assertTrue(isinstance(xblock, StubXBlock)) self.assertEqual(xblock.test_field, expected_field_val) if has_draft: draft_xblock = self.store.get_item(xblock_location) self.assertTrue(getattr(draft_xblock, 'is_draft', False)) self.assertTrue(isinstance(draft_xblock, StubXBlock)) self.assertEqual(draft_xblock.test_field, expected_field_val)
def test_migrate_published_info(self): """ Tests that blocks that were storing published_date and published_by through CMSBlockMixin are loaded correctly """ # Insert the test block directly into the module store location = Location('edX', 'migration', '2012_Fall', 'html', 'test_html') published_date = datetime(1970, 1, 1, tzinfo=UTC) published_by = 123 self.store._update_single_item( as_draft(location), { 'definition.data': {}, 'metadata': { # published_date was previously stored as a list of time components, not a datetime 'published_date': list(published_date.timetuple()), 'published_by': published_by, }, }, ) # Retrieve the block and verify its fields component = self.draft_store.get_item(location) self.assertEqual(component.published_date, published_date) self.assertEqual(component.published_by, published_by)
def _assert_import(self, course_dir, expected_field_val, has_draft=False): """ Import a course from XML, then verify that the XBlock was loaded with the correct field value. Args: course_dir (str): The name of the course directory (relative to the test data directory) expected_xblock_loc (str): The location of the XBlock in the course. expected_field_val (str): The expected value of the XBlock's test field. Kwargs: has_draft (bool): If true, check that a draft of the XBlock exists with the expected field value set. """ courses = import_from_xml( self.store, self.user.id, 'common/test/data', [course_dir] ) xblock_location = courses[0].id.make_usage_key('stubxblock', 'xblock_test') if has_draft: xblock_location = as_draft(xblock_location) xblock = self.store.get_item(xblock_location) self.assertTrue(isinstance(xblock, StubXBlock)) self.assertEqual(xblock.test_field, expected_field_val) if has_draft: draft_xblock = self.store.get_item(xblock_location) self.assertTrue(getattr(draft_xblock, 'is_draft', False)) self.assertTrue(isinstance(draft_xblock, StubXBlock)) self.assertEqual(draft_xblock.test_field, expected_field_val)
def compare_dags(self, presplit, presplit_dag_root, split_dag_root, published): # check that locations match self.assertEqual( presplit_dag_root.location, self.loc_mapper.translate_locator_to_location(split_dag_root.location).replace(revision=None), ) # compare all fields but children for name in presplit_dag_root.fields.iterkeys(): if name != "children": self.assertEqual( getattr(presplit_dag_root, name), getattr(split_dag_root, name), "{}/{}: {} != {}".format( split_dag_root.location, name, getattr(presplit_dag_root, name), getattr(split_dag_root, name) ), ) # test split get_item using old Location: old draft store didn't set revision for things above vertical # but split does distinguish these; so, set revision if not published if not published: location = draft.as_draft(presplit_dag_root.location) else: location = presplit_dag_root.location refetched = self.split_mongo.get_item(location) self.assertEqual( refetched.location, split_dag_root.location, "Fetch from split via old Location {} not same as new {}".format( refetched.location, split_dag_root.location ), ) # compare children if presplit_dag_root.has_children: self.assertEqual( len(presplit_dag_root.get_children()), len(split_dag_root.get_children()), "{0.category} '{0.display_name}': children count {1} != {2}".format( presplit_dag_root, len(presplit_dag_root.get_children()), split_dag_root.children ), ) for pre_child, split_child in zip(presplit_dag_root.get_children(), split_dag_root.get_children()): self.compare_dags(presplit, pre_child, split_child, published)
def compare_dags(self, presplit, presplit_dag_root, split_dag_root, published): # check that locations match self.assertEqual( presplit_dag_root.location, self.loc_mapper.translate_locator_to_location( split_dag_root.location).replace(revision=None)) # compare all fields but children for name in presplit_dag_root.fields.iterkeys(): if name != 'children': self.assertEqual( getattr(presplit_dag_root, name), getattr(split_dag_root, name), "{}/{}: {} != {}".format(split_dag_root.location, name, getattr(presplit_dag_root, name), getattr(split_dag_root, name))) # test split get_item using old Location: old draft store didn't set revision for things above vertical # but split does distinguish these; so, set revision if not published if not published: location = draft.as_draft(presplit_dag_root.location) else: location = presplit_dag_root.location refetched = self.split_mongo.get_item(location) self.assertEqual( refetched.location, split_dag_root.location, "Fetch from split via old Location {} not same as new {}".format( refetched.location, split_dag_root.location)) # compare children if presplit_dag_root.has_children: self.assertEqual( len(presplit_dag_root.get_children()), len(split_dag_root.get_children()), "{0.category} '{0.display_name}': children count {1} != {2}". format(presplit_dag_root, len(presplit_dag_root.get_children()), split_dag_root.children)) for pre_child, split_child in zip(presplit_dag_root.get_children(), split_dag_root.get_children()): self.compare_dags(presplit, pre_child, split_child, published)
def _assert_import(self, course_dir, expected_field_val, has_draft=False): """ Import a course from XML, then verify that the XBlock was loaded with the correct field value. Args: course_dir (str): The name of the course directory (relative to the test data directory) expected_xblock_loc (str): The location of the XBlock in the course. expected_field_val (str): The expected value of the XBlock's test field. Kwargs: has_draft (bool): If true, check that a draft of the XBlock exists with the expected field value set. """ # It is necessary to use the "old mongo" modulestore because split doesn't work # with the "has_draft" logic below. store = modulestore()._get_modulestore_by_type( ModuleStoreEnum.Type.mongo) # pylint: disable=protected-access courses = import_from_xml(store, self.user.id, TEST_DATA_DIR, [course_dir], create_course_if_not_present=True) xblock_location = courses[0].id.make_usage_key('stubxblock', 'xblock_test') if has_draft: xblock_location = as_draft(xblock_location) xblock = store.get_item(xblock_location) self.assertTrue(isinstance(xblock, StubXBlock)) self.assertEqual(xblock.test_field, expected_field_val) if has_draft: draft_xblock = store.get_item(xblock_location) self.assertTrue(getattr(draft_xblock, 'is_draft', False)) self.assertTrue(isinstance(draft_xblock, StubXBlock)) self.assertEqual(draft_xblock.test_field, expected_field_val)
def _assert_import(self, course_dir, expected_field_val, has_draft=False): """ Import a course from XML, then verify that the XBlock was loaded with the correct field value. Args: course_dir (str): The name of the course directory (relative to the test data directory) expected_xblock_loc (str): The location of the XBlock in the course. expected_field_val (str): The expected value of the XBlock's test field. Kwargs: has_draft (bool): If true, check that a draft of the XBlock exists with the expected field value set. """ # It is necessary to use the "old mongo" modulestore because split doesn't work # with the "has_draft" logic below. store = modulestore()._get_modulestore_by_type(ModuleStoreEnum.Type.mongo) # pylint: disable=protected-access courses = import_course_from_xml( store, self.user.id, TEST_DATA_DIR, [course_dir], create_if_not_present=True ) xblock_location = courses[0].id.make_usage_key('stubxblock', 'xblock_test') if has_draft: xblock_location = as_draft(xblock_location) xblock = store.get_item(xblock_location) self.assertTrue(isinstance(xblock, StubXBlock)) self.assertEqual(xblock.test_field, expected_field_val) if has_draft: draft_xblock = store.get_item(xblock_location) self.assertTrue(getattr(draft_xblock, 'is_draft', False)) self.assertTrue(isinstance(draft_xblock, StubXBlock)) self.assertEqual(draft_xblock.test_field, expected_field_val)