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.
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user