From 70469c16b2f9cd72bd403adcff20e552a20027da Mon Sep 17 00:00:00 2001 From: Nimisha Asthagiri Date: Mon, 22 Feb 2016 11:24:16 -0500 Subject: [PATCH] Block Structure API: Replace has_block with __contains__ --- .../blocks/transformers/tests/test_navigation.py | 4 ++-- lms/djangoapps/course_blocks/transformers/tests/helpers.py | 2 +- openedx/core/lib/block_structure/block_structure.py | 7 ++++--- openedx/core/lib/block_structure/manager.py | 2 +- openedx/core/lib/block_structure/tests/helpers.py | 2 +- .../core/lib/block_structure/tests/test_block_structure.py | 6 +++--- 6 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lms/djangoapps/course_api/blocks/transformers/tests/test_navigation.py b/lms/djangoapps/course_api/blocks/transformers/tests/test_navigation.py index 2de954edc0..973303ca85 100644 --- a/lms/djangoapps/course_api/blocks/transformers/tests/test_navigation.py +++ b/lms/djangoapps/course_api/blocks/transformers/tests/test_navigation.py @@ -88,14 +88,14 @@ class BlockNavigationTransformerCourseTestCase(ModuleStoreTestCase): BlockNavigationTransformer.collect(block_structure) block_structure._collect_requested_xblock_fields() - self.assertTrue(block_structure.has_block(chapter_x_key)) + self.assertIn(chapter_x_key, block_structure) # transform phase BlockDepthTransformer().transform(usage_info=None, block_structure=block_structure) BlockNavigationTransformer(0).transform(usage_info=None, block_structure=block_structure) 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_usage_key, diff --git a/lms/djangoapps/course_blocks/transformers/tests/helpers.py b/lms/djangoapps/course_blocks/transformers/tests/helpers.py index cc6e9ffaab..af8b5afa77 100644 --- a/lms/djangoapps/course_blocks/transformers/tests/helpers.py +++ b/lms/djangoapps/course_blocks/transformers/tests/helpers.py @@ -311,7 +311,7 @@ class BlockParentsMapTestCase(TransformerRegistryTestMixin, ModuleStoreTestCase) for i, xblock_key in enumerate(self.xblock_keys): # 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)) # compare with expected value diff --git a/openedx/core/lib/block_structure/block_structure.py b/openedx/core/lib/block_structure/block_structure.py index 8fb118d650..a526612d78 100644 --- a/openedx/core/lib/block_structure/block_structure.py +++ b/openedx/core/lib/block_structure/block_structure.py @@ -79,6 +79,7 @@ class BlockStructure(object): Returns the parents of the block identified by the given usage_key. + Arguments: usage_key - The usage key of the block whose parents are to be returned. @@ -86,7 +87,7 @@ class BlockStructure(object): Returns: [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): """ @@ -100,7 +101,7 @@ class BlockStructure(object): Returns: [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): """ @@ -117,7 +118,7 @@ class BlockStructure(object): self.root_block_usage_key = usage_key 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 block structure. diff --git a/openedx/core/lib/block_structure/manager.py b/openedx/core/lib/block_structure/manager.py index 7df65d66b5..1c8cf3be15 100644 --- a/openedx/core/lib/block_structure/manager.py +++ b/openedx/core/lib/block_structure/manager.py @@ -57,7 +57,7 @@ class BlockStructureManager(object): # Override the root_block_usage_key so traversals start at the # requested location. The rest of the structure will be pruned # 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( "The requested usage_key '{0}' is not found in the block_structure with root '{1}'", unicode(starting_block_usage_key), diff --git a/openedx/core/lib/block_structure/tests/helpers.py b/openedx/core/lib/block_structure/tests/helpers.py index c37001774e..3d232ca50e 100644 --- a/openedx/core/lib/block_structure/tests/helpers.py +++ b/openedx/core/lib/block_structure/tests/helpers.py @@ -219,7 +219,7 @@ class ChildrenMapTestMixin(object): for block_key, children in enumerate(children_map): # Verify presence self.assertEquals( - block_structure.has_block(block_key), + block_key in block_structure, block_key not in missing_blocks, 'Expected presence in block_structure for block_key {} to match absence in missing_blocks.'.format( unicode(block_key) diff --git a/openedx/core/lib/block_structure/tests/test_block_structure.py b/openedx/core/lib/block_structure/tests/test_block_structure.py index 78f47b2327..d1362dabbf 100644 --- a/openedx/core/lib/block_structure/tests/test_block_structure.py +++ b/openedx/core/lib/block_structure/tests/test_block_structure.py @@ -37,10 +37,10 @@ class TestBlockStructure(TestCase, ChildrenMapTestMixin): for child, parents in enumerate(self.get_parents_map(children_map)): self.assertSetEqual(set(block_structure.get_parents(child)), set(parents)) - # has_block + # __contains__ for node in range(len(children_map)): - self.assertTrue(block_structure.has_block(node)) - self.assertFalse(block_structure.has_block(len(children_map) + 1)) + self.assertIn(node, block_structure) + self.assertNotIn(len(children_map) + 1, block_structure) @ddt.ddt