Skip to content

Commit

Permalink
test: Update a BlockStructureFactory test mixin.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
feanil committed Jul 29, 2024
1 parent 3f05c06 commit 318f55a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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,
Expand Down

0 comments on commit 318f55a

Please sign in to comment.