def test_map_into_course_location(self): original_course = CCXLocator(org='org', course='course', run='run', ccx='1') new_course = CCXLocator(org='edX', course='toy', run='2012_Fall', ccx='1') loc = CCXBlockUsageLocator(original_course, 'cat', 'name:more_name') expected = CCXBlockUsageLocator(new_course, 'cat', 'name:more_name') actual = loc.map_into_course(new_course) self.assertEqual(expected, actual)
def test_replacement(self, key, newvalue): course_key = CCXLocator(org='org', course='course', run='run', branch='rev', ccx='1', deprecated=False) kwargs = {key: newvalue} self.assertEqual( getattr(CCXBlockUsageLocator(course_key, 'c', 'n', deprecated=False).replace(**kwargs), key), newvalue ) with self.assertRaises(InvalidKeyError): CCXBlockUsageLocator(course_key, 'c', 'n', deprecated=True).replace(block_id='name\xae')
def restore_ccx(val, ccx_id): """restore references to a CCX to the incoming value returns the value converted to a CCX-aware state, using the provided ccx_id """ if isinstance(val, CourseLocator): return CCXLocator.from_course_locator(val, ccx_id) elif isinstance(val, BlockUsageLocator): ccx_key = restore_ccx(val.course_key, ccx_id) val = CCXBlockUsageLocator(ccx_key, val.block_type, val.block_id) if hasattr(val, 'location'): val.location = restore_ccx(val.location, ccx_id) if hasattr(val, 'children'): val.children = restore_ccx_collection(val.children, ccx_id) return val
def restore_ccx(val, ccx_id): """restore references to a CCX to the incoming value returns the value converted to a CCX-aware state, using the provided ccx_id """ if isinstance(val, CourseLocator): return CCXLocator.from_course_locator(val, ccx_id) elif isinstance(val, BlockUsageLocator): ccx_key = restore_ccx(val.course_key, ccx_id) val = CCXBlockUsageLocator(ccx_key, val.block_type, val.block_id) for field_name in XMODULE_FIELDS_WITH_USAGE_KEYS: if hasattr(val, field_name): setattr(val, field_name, restore_ccx(getattr(val, field_name), ccx_id)) if hasattr(val, 'children'): val.children = restore_ccx_collection(val.children, ccx_id) return val
def create_usage_key(course_id, unit_id): """Create a UsageKey or CCXBlockUsageLocator instance from a course block usage location.""" usage_key = UsageKey.from_string(unit_id) if isinstance(course_id, CCXLocator): return CCXBlockUsageLocator(course_id, usage_key.block_type, usage_key.block_id) return usage_key
def test_valid_locations(self, org, course, run, ccx, category, name, revision): # pylint: disable=unused-argument course_key = CCXLocator(org=org, course=course, run=run, branch=revision, ccx=ccx) locator = CCXBlockUsageLocator(course_key, block_type=category, block_id=name, ) self.assertEqual(org, locator.org) self.assertEqual(course, locator.course) self.assertEqual(run, locator.run) self.assertEqual(ccx, locator.ccx) self.assertEqual(category, locator.block_type) self.assertEqual(name, locator.block_id) self.assertEqual(revision, locator.branch)
def test_ccx_to_deprecated_string(self): """Verify that _from_deprecated_string raises NotImplemented""" ccx_id = CCXLocator(org='org', course='course', run='run', ccx='1') loc = CCXBlockUsageLocator(ccx_id, 'cat', 'name') with self.assertRaises(NotImplementedError): loc._to_deprecated_string() # pylint: disable=protected-access
def test_ccx_from_deprecated_string(self): """Verify that _from_deprecated_string raises NotImplemented""" with self.assertRaises(NotImplementedError): CCXBlockUsageLocator._from_deprecated_string('org/course/run/1') # pylint: disable=protected-access
def test_invalid_locations(self, *args, **kwargs): with self.assertRaises(TypeError): CCXBlockUsageLocator(*args, **kwargs)