From 9164e924652387e6044ce6e0c0500660323a7ca4 Mon Sep 17 00:00:00 2001 From: Feanil Patel Date: Fri, 26 Jul 2024 15:03:24 -0400 Subject: [PATCH] test: Update a BlockStructureFactory test mixin. Previously, we were not caching BlockStructures to the database when we were adding them to the store by default. Now that we are doing that, the BlockStructureFactory test failed because it was taking a shortcut that would no longer work. It was just creating a blockstructure that had relations but not any block information. Now that we're always persisting updates to the database, this broke because to persist the structure to the database, we have to look up the block information from the block_structure which now fails. This change updates the test mixin to add more data so that the content can be persisted to the database successfully as a part of this test. --- .../content/block_structure/tests/helpers.py | 6 +++++- .../block_structure/tests/test_factory.py | 18 +++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/openedx/core/djangoapps/content/block_structure/tests/helpers.py b/openedx/core/djangoapps/content/block_structure/tests/helpers.py index 93655c79e1..f0a20554e6 100644 --- a/openedx/core/djangoapps/content/block_structure/tests/helpers.py +++ b/openedx/core/djangoapps/content/block_structure/tests/helpers.py @@ -274,10 +274,14 @@ class ChildrenMapTestMixin: # 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 b9fb8c5e10..00efa393d8 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 @@ Tests for block_structure_factory.py 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 @@ class TestBlockStructureFactory(TestCase, ChildrenMapTestMixin): 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,