diff --git a/openedx/core/djangoapps/content/block_structure/tests/helpers.py b/openedx/core/djangoapps/content/block_structure/tests/helpers.py index 93655c79e16e..406fde512671 100644 --- a/openedx/core/djangoapps/content/block_structure/tests/helpers.py +++ b/openedx/core/djangoapps/content/block_structure/tests/helpers.py @@ -7,6 +7,7 @@ from uuid import uuid4 from unittest.mock import patch +from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.locator import BlockUsageLocator, CourseLocator from xmodule.modulestore.exceptions import ItemNotFoundError @@ -274,10 +275,14 @@ def create_block_structure(self, children_map, block_structure_cls=BlockStructur # create empty block structure block_structure = block_structure_cls(root_block_usage_key=self.block_key_factory(0)) - # _add_relation + # _add_relation and blocks for parent, children in enumerate(children_map): + if isinstance(block_structure, BlockStructureBlockData): + block_structure._get_or_create_block(self.block_key_factory(parent)) # pylint: disable=protected-access for child in children: block_structure._add_relation(self.block_key_factory(parent), self.block_key_factory(child)) # pylint: disable=protected-access + if isinstance(block_structure, BlockStructureBlockData): + block_structure._get_or_create_block(self.block_key_factory(child)) # pylint: disable=protected-access return block_structure def get_parents_map(self, children_map): diff --git a/openedx/core/djangoapps/content/block_structure/tests/test_factory.py b/openedx/core/djangoapps/content/block_structure/tests/test_factory.py index b9fb8c5e1077..00efa393d8fa 100644 --- a/openedx/core/djangoapps/content/block_structure/tests/test_factory.py +++ b/openedx/core/djangoapps/content/block_structure/tests/test_factory.py @@ -5,6 +5,7 @@ import pytest from django.test import TestCase +from opaque_keys.edx.keys import CourseKey from xmodule.modulestore.exceptions import ItemNotFoundError from ..exceptions import BlockStructureNotFound @@ -18,14 +19,22 @@ class TestBlockStructureFactory(TestCase, ChildrenMapTestMixin): Tests for BlockStructureFactory """ + def block_key_factory(self, block_id): + """ + Returns a usage_key object for the given block_id. + This overrides the method in the ChildrenMapTestMixin. + """ + return CourseKey.from_string("course-v1:org+course+run").make_usage_key("html", str(block_id)) + def setUp(self): super().setUp() self.children_map = self.SIMPLE_CHILDREN_MAP self.modulestore = MockModulestoreFactory.create(self.children_map, self.block_key_factory) def test_from_modulestore(self): + usage_key = CourseKey.from_string("course-v1:org+course+run").make_usage_key("html", "0") block_structure = BlockStructureFactory.create_from_modulestore( - root_block_usage_key=0, modulestore=self.modulestore + root_block_usage_key=usage_key, modulestore=self.modulestore ) self.assert_block_structure(block_structure, self.children_map) @@ -48,15 +57,18 @@ def test_from_cache(self): def test_from_cache_none(self): store = BlockStructureStore(MockCache()) + # Non-existent usage key + usage_key = CourseKey.from_string("course-v1:org+course+run").make_usage_key("html", "0") with pytest.raises(BlockStructureNotFound): BlockStructureFactory.create_from_store( - root_block_usage_key=0, + root_block_usage_key=usage_key, block_structure_store=store, ) def test_new(self): + usage_key = CourseKey.from_string("course-v1:org+course+run").make_usage_key("html", "0") block_structure = BlockStructureFactory.create_from_modulestore( - root_block_usage_key=0, modulestore=self.modulestore + root_block_usage_key=usage_key, modulestore=self.modulestore ) new_structure = BlockStructureFactory.create_new( block_structure.root_block_usage_key,