Commit 70469c16 by Nimisha Asthagiri

Block Structure API: Replace has_block with __contains__

parent d969f486
...@@ -88,14 +88,14 @@ class BlockNavigationTransformerCourseTestCase(ModuleStoreTestCase): ...@@ -88,14 +88,14 @@ class BlockNavigationTransformerCourseTestCase(ModuleStoreTestCase):
BlockNavigationTransformer.collect(block_structure) BlockNavigationTransformer.collect(block_structure)
block_structure._collect_requested_xblock_fields() block_structure._collect_requested_xblock_fields()
self.assertTrue(block_structure.has_block(chapter_x_key)) self.assertIn(chapter_x_key, block_structure)
# transform phase # transform phase
BlockDepthTransformer().transform(usage_info=None, block_structure=block_structure) BlockDepthTransformer().transform(usage_info=None, block_structure=block_structure)
BlockNavigationTransformer(0).transform(usage_info=None, block_structure=block_structure) BlockNavigationTransformer(0).transform(usage_info=None, block_structure=block_structure)
block_structure._prune_unreachable() block_structure._prune_unreachable()
self.assertTrue(block_structure.has_block(chapter_x_key)) self.assertIn(chapter_x_key, block_structure)
course_descendants = block_structure.get_transformer_block_field( course_descendants = block_structure.get_transformer_block_field(
course_usage_key, course_usage_key,
......
...@@ -311,7 +311,7 @@ class BlockParentsMapTestCase(TransformerRegistryTestMixin, ModuleStoreTestCase) ...@@ -311,7 +311,7 @@ class BlockParentsMapTestCase(TransformerRegistryTestMixin, ModuleStoreTestCase)
for i, xblock_key in enumerate(self.xblock_keys): for i, xblock_key in enumerate(self.xblock_keys):
# compute access results of the block # compute access results of the block
block_structure_result = block_structure.has_block(xblock_key) block_structure_result = xblock_key in block_structure
has_access_result = bool(has_access(user, 'load', self.get_block(i), course_key=self.course.id)) has_access_result = bool(has_access(user, 'load', self.get_block(i), course_key=self.course.id))
# compare with expected value # compare with expected value
......
...@@ -79,6 +79,7 @@ class BlockStructure(object): ...@@ -79,6 +79,7 @@ class BlockStructure(object):
Returns the parents of the block identified by the given Returns the parents of the block identified by the given
usage_key. usage_key.
Arguments: Arguments:
usage_key - The usage key of the block whose parents usage_key - The usage key of the block whose parents
are to be returned. are to be returned.
...@@ -86,7 +87,7 @@ class BlockStructure(object): ...@@ -86,7 +87,7 @@ class BlockStructure(object):
Returns: Returns:
[UsageKey] - A list of usage keys of the block's parents. [UsageKey] - A list of usage keys of the block's parents.
""" """
return self._block_relations[usage_key].parents if self.has_block(usage_key) else [] return self._block_relations[usage_key].parents if usage_key in self else []
def get_children(self, usage_key): def get_children(self, usage_key):
""" """
...@@ -100,7 +101,7 @@ class BlockStructure(object): ...@@ -100,7 +101,7 @@ class BlockStructure(object):
Returns: Returns:
[UsageKey] - A list of usage keys of the block's children. [UsageKey] - A list of usage keys of the block's children.
""" """
return self._block_relations[usage_key].children if self.has_block(usage_key) else [] return self._block_relations[usage_key].children if usage_key in self else []
def set_root_block(self, usage_key): def set_root_block(self, usage_key):
""" """
...@@ -117,7 +118,7 @@ class BlockStructure(object): ...@@ -117,7 +118,7 @@ class BlockStructure(object):
self.root_block_usage_key = usage_key self.root_block_usage_key = usage_key
self._block_relations[usage_key].parents = [] self._block_relations[usage_key].parents = []
def has_block(self, usage_key): def __contains__(self, usage_key):
""" """
Returns whether a block with the given usage_key is in this Returns whether a block with the given usage_key is in this
block structure. block structure.
......
...@@ -57,7 +57,7 @@ class BlockStructureManager(object): ...@@ -57,7 +57,7 @@ class BlockStructureManager(object):
# Override the root_block_usage_key so traversals start at the # Override the root_block_usage_key so traversals start at the
# requested location. The rest of the structure will be pruned # requested location. The rest of the structure will be pruned
# as part of the transformation. # as part of the transformation.
if not block_structure.has_block(starting_block_usage_key): if starting_block_usage_key not in block_structure:
raise UsageKeyNotInBlockStructure( raise UsageKeyNotInBlockStructure(
"The requested usage_key '{0}' is not found in the block_structure with root '{1}'", "The requested usage_key '{0}' is not found in the block_structure with root '{1}'",
unicode(starting_block_usage_key), unicode(starting_block_usage_key),
......
...@@ -219,7 +219,7 @@ class ChildrenMapTestMixin(object): ...@@ -219,7 +219,7 @@ class ChildrenMapTestMixin(object):
for block_key, children in enumerate(children_map): for block_key, children in enumerate(children_map):
# Verify presence # Verify presence
self.assertEquals( self.assertEquals(
block_structure.has_block(block_key), block_key in block_structure,
block_key not in missing_blocks, block_key not in missing_blocks,
'Expected presence in block_structure for block_key {} to match absence in missing_blocks.'.format( 'Expected presence in block_structure for block_key {} to match absence in missing_blocks.'.format(
unicode(block_key) unicode(block_key)
......
...@@ -37,10 +37,10 @@ class TestBlockStructure(TestCase, ChildrenMapTestMixin): ...@@ -37,10 +37,10 @@ class TestBlockStructure(TestCase, ChildrenMapTestMixin):
for child, parents in enumerate(self.get_parents_map(children_map)): for child, parents in enumerate(self.get_parents_map(children_map)):
self.assertSetEqual(set(block_structure.get_parents(child)), set(parents)) self.assertSetEqual(set(block_structure.get_parents(child)), set(parents))
# has_block # __contains__
for node in range(len(children_map)): for node in range(len(children_map)):
self.assertTrue(block_structure.has_block(node)) self.assertIn(node, block_structure)
self.assertFalse(block_structure.has_block(len(children_map) + 1)) self.assertNotIn(len(children_map) + 1, block_structure)
@ddt.ddt @ddt.ddt
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment