Files
edx-platform/xmodule/tests/test_library_root.py
M Umar Khan a91df0c40f refactor: move xmodule folder to root
- Moving xmodule folder to root as we're dissolving sub-projects of common folder in edx-platform
    - More info: https://openedx.atlassian.net/browse/BOM-2579
- -e common/lib/xmodule has been removed from the requirements as xmodule has itself become the part of edx-platform and not being installed through requirements
- The test files common/lib/xmodule/test_files/ have been removed as they are not being used anymore
2022-06-20 14:33:45 +05:00

88 lines
3.2 KiB
Python

"""
Basic unit tests for LibraryRoot
"""
from unittest.mock import patch
from web_fragments.fragment import Fragment
from xblock.runtime import Runtime as VanillaRuntime
from xmodule.modulestore.tests.factories import ItemFactory, LibraryFactory
from xmodule.modulestore.tests.utils import MixedSplitTestCase
from xmodule.x_module import AUTHOR_VIEW
dummy_render = lambda block, _: Fragment(block.data) # pylint: disable=invalid-name
@patch(
'xmodule.modulestore.split_mongo.caching_descriptor_system.CachingDescriptorSystem.render', VanillaRuntime.render
)
@patch('xmodule.html_module.HtmlBlock.author_view', dummy_render, create=True)
@patch('xmodule.html_module.HtmlBlock.has_author_view', True, create=True)
@patch('xmodule.x_module.DescriptorSystem.applicable_aside_types', lambda self, block: [])
class TestLibraryRoot(MixedSplitTestCase):
"""
Basic unit tests for LibraryRoot (library_root_xblock.py)
"""
def test_library_author_view(self):
"""
Test that LibraryRoot.author_view can run and includes content from its
children.
We have to patch the runtime (module system) in order to be able to
render blocks in our test environment.
"""
message = "Hello world"
library = LibraryFactory.create(modulestore=self.store)
# Add one HTML block to the library:
ItemFactory.create(
category="html",
parent_location=library.location,
user_id=self.user_id,
publish_item=False,
modulestore=self.store,
data=message
)
library = self.store.get_library(library.location.library_key)
context = {'reorderable_items': set(), }
# Patch the HTML block to always render "Hello world"
result = library.render(AUTHOR_VIEW, context)
assert message in result.content
def test_library_author_view_with_paging(self):
"""
Test that LibraryRoot.author_view can apply paging
We have to patch the runtime (module system) in order to be able to
render blocks in our test environment.
"""
library = LibraryFactory.create(modulestore=self.store)
# Add five HTML blocks to the library:
blocks = [
ItemFactory.create(
category="html",
parent_location=library.location,
user_id=self.user_id,
publish_item=False,
modulestore=self.store,
data="HtmlBlock" + str(i)
)
for i in range(5)
]
library = self.store.get_library(library.location.library_key)
def render_and_check_contents(page, page_size):
""" Renders block and asserts on returned content """
context = {'reorderable_items': set(), 'paging': {'page_number': page, 'page_size': page_size}}
expected_blocks = blocks[page_size * page:page_size * (page + 1)]
result = library.render(AUTHOR_VIEW, context)
for expected_block in expected_blocks:
assert expected_block.data in result.content
render_and_check_contents(0, 3)
render_and_check_contents(1, 3)
render_and_check_contents(0, 2)
render_and_check_contents(1, 2)