Commit 84edced1 by E. Kolpakov

Render tests for library content block preview and author views

parent d0919c93
...@@ -5,6 +5,12 @@ Basic unit tests for LibraryContentModule ...@@ -5,6 +5,12 @@ Basic unit tests for LibraryContentModule
Higher-level tests are in `cms/djangoapps/contentstore/tests/test_libraries.py`. Higher-level tests are in `cms/djangoapps/contentstore/tests/test_libraries.py`.
""" """
import ddt import ddt
from mock import patch
from xblock.fragment import Fragment
from xblock.runtime import Runtime as VanillaRuntime
from xmodule.x_module import AUTHOR_VIEW, STUDENT_VIEW
from xmodule.library_content_module import LibraryVersionReference, ANY_CAPA_TYPE_VALUE, LibraryContentDescriptor from xmodule.library_content_module import LibraryVersionReference, ANY_CAPA_TYPE_VALUE, LibraryContentDescriptor
from xmodule.modulestore.tests.factories import LibraryFactory, CourseFactory, ItemFactory from xmodule.modulestore.tests.factories import LibraryFactory, CourseFactory, ItemFactory
from xmodule.modulestore.tests.utils import MixedSplitTestCase from xmodule.modulestore.tests.utils import MixedSplitTestCase
...@@ -12,13 +18,15 @@ from xmodule.tests import get_test_system ...@@ -12,13 +18,15 @@ from xmodule.tests import get_test_system
from xmodule.validation import StudioValidationMessage from xmodule.validation import StudioValidationMessage
@ddt.ddt _dummy_render = lambda block, _: Fragment(block.data)
class TestLibraries(MixedSplitTestCase):
class BaseTestLibraryContainer(MixedSplitTestCase):
""" """
Basic unit tests for LibraryContentModule (library_content_module.py) Base class for TestLibraryContainer and TestLibraryContainerRender
""" """
def setUp(self): def setUp(self):
super(TestLibraries, self).setUp() super(BaseTestLibraryContainer, self).setUp()
self.library = LibraryFactory.create(modulestore=self.store) self.library = LibraryFactory.create(modulestore=self.store)
self.lib_blocks = [ self.lib_blocks = [
...@@ -62,7 +70,7 @@ class TestLibraries(MixedSplitTestCase): ...@@ -62,7 +70,7 @@ class TestLibraries(MixedSplitTestCase):
} }
) )
def _bind_course_module(self, module): def _bind_course_module(self, module, render=None):
""" """
Bind a module (part of self.course) so we can access student-specific data. Bind a module (part of self.course) so we can access student-specific data.
""" """
...@@ -108,6 +116,11 @@ class TestLibraries(MixedSplitTestCase): ...@@ -108,6 +116,11 @@ class TestLibraries(MixedSplitTestCase):
modulestore=self.store, modulestore=self.store,
) )
@ddt.ddt
class TestLibraryContainer(BaseTestLibraryContainer):
"""
Basic unit tests for LibraryContentModule (library_content_module.py)
"""
def test_lib_content_block(self): def test_lib_content_block(self):
""" """
Test that blocks from a library are copied and added as children Test that blocks from a library are copied and added as children
...@@ -250,3 +263,31 @@ class TestLibraries(MixedSplitTestCase): ...@@ -250,3 +263,31 @@ class TestLibraries(MixedSplitTestCase):
non_editable_metadata_fields = self.lc_block.non_editable_metadata_fields non_editable_metadata_fields = self.lc_block.non_editable_metadata_fields
self.assertIn(LibraryContentDescriptor.mode, non_editable_metadata_fields) self.assertIn(LibraryContentDescriptor.mode, non_editable_metadata_fields)
self.assertNotIn(LibraryContentDescriptor.display_name, non_editable_metadata_fields) self.assertNotIn(LibraryContentDescriptor.display_name, non_editable_metadata_fields)
@patch('xmodule.modulestore.split_mongo.caching_descriptor_system.CachingDescriptorSystem.render', VanillaRuntime.render)
@patch('xmodule.html_module.HtmlModule.author_view', _dummy_render, create=True)
@patch('xmodule.html_module.HtmlModule.student_view', _dummy_render, create=True)
@patch('xmodule.x_module.DescriptorSystem.applicable_aside_types', lambda self, block: [])
class TestLibraryContentRender(BaseTestLibraryContainer):
"""
Rendering unit tests for LibraryContentModule (library_content_module.py)
"""
def test_preivew_view(self):
""" Test preview view rendering """
self.lc_block.refresh_children()
self.lc_block = self.store.get_item(self.lc_block.location)
self.assertEqual(len(self.lc_block.children), len(self.lib_blocks))
self._bind_course_module(self.lc_block)
rendered = self.lc_block.render(AUTHOR_VIEW, {'root_xblock': self.lc_block})
self.assertIn("Hello world from block 1", rendered.content)
def test_author_view(self):
""" Test author view rendering """
self.lc_block.refresh_children()
self.lc_block = self.store.get_item(self.lc_block.location)
self.assertEqual(len(self.lc_block.children), len(self.lib_blocks))
self._bind_course_module(self.lc_block)
rendered = self.lc_block.render(AUTHOR_VIEW, {})
self.assertEqual("", rendered.content) # content should be empty
self.assertEqual("LibraryContentAuthorView", rendered.js_init_fn) # but some js initialization should happen
\ No newline at end of file
...@@ -7,9 +7,13 @@ from xmodule.x_module import AUTHOR_VIEW ...@@ -7,9 +7,13 @@ from xmodule.x_module import AUTHOR_VIEW
from xmodule.modulestore.tests.factories import LibraryFactory, CourseFactory, ItemFactory from xmodule.modulestore.tests.factories import LibraryFactory, CourseFactory, ItemFactory
from xmodule.modulestore.tests.utils import MixedSplitTestCase from xmodule.modulestore.tests.utils import MixedSplitTestCase
_dummy_render = lambda block, _: Fragment(block.data)
@patch('xmodule.modulestore.split_mongo.caching_descriptor_system.CachingDescriptorSystem.render', VanillaRuntime.render)
@patch('xmodule.html_module.HtmlDescriptor.author_view', _dummy_render, create=True)
@patch('xmodule.x_module.DescriptorSystem.applicable_aside_types', lambda self, block: [])
class TestLibraryRoot(MixedSplitTestCase): class TestLibraryRoot(MixedSplitTestCase):
@patch('xmodule.modulestore.split_mongo.caching_descriptor_system.CachingDescriptorSystem.render', VanillaRuntime.render)
def test_library_author_view(self): def test_library_author_view(self):
""" """
Test that LibraryRoot.author_view can run and includes content from its Test that LibraryRoot.author_view can run and includes content from its
...@@ -17,6 +21,7 @@ class TestLibraryRoot(MixedSplitTestCase): ...@@ -17,6 +21,7 @@ class TestLibraryRoot(MixedSplitTestCase):
We have to patch the runtime (module system) in order to be able to We have to patch the runtime (module system) in order to be able to
render blocks in our test environment. render blocks in our test environment.
""" """
message = u"Hello world"
library = LibraryFactory.create(modulestore=self.store) library = LibraryFactory.create(modulestore=self.store)
# Add one HTML block to the library: # Add one HTML block to the library:
ItemFactory.create( ItemFactory.create(
...@@ -25,19 +30,16 @@ class TestLibraryRoot(MixedSplitTestCase): ...@@ -25,19 +30,16 @@ class TestLibraryRoot(MixedSplitTestCase):
user_id=self.user_id, user_id=self.user_id,
publish_item=False, publish_item=False,
modulestore=self.store, modulestore=self.store,
data=message
) )
library = self.store.get_library(library.location.library_key) library = self.store.get_library(library.location.library_key)
context = {'reorderable_items': set(), } context = {'reorderable_items': set(), }
# Patch the HTML block to always render "Hello world" # Patch the HTML block to always render "Hello world"
message = u"Hello world"
hello_render = lambda _, context: Fragment(message)
with patch('xmodule.html_module.HtmlDescriptor.author_view', hello_render, create=True):
with patch('xmodule.x_module.DescriptorSystem.applicable_aside_types', lambda self, block: []):
result = library.render(AUTHOR_VIEW, context) result = library.render(AUTHOR_VIEW, context)
self.assertIn(message, result.content) self.assertIn(message, result.content)
@patch('xmodule.modulestore.split_mongo.caching_descriptor_system.CachingDescriptorSystem.render', VanillaRuntime.render)
def test_library_author_view_with_paging(self): def test_library_author_view_with_paging(self):
""" """
Test that LibraryRoot.author_view can apply paging Test that LibraryRoot.author_view can apply paging
...@@ -67,9 +69,6 @@ class TestLibraryRoot(MixedSplitTestCase): ...@@ -67,9 +69,6 @@ class TestLibraryRoot(MixedSplitTestCase):
for expected_block in expected_blocks: for expected_block in expected_blocks:
self.assertIn(expected_block.data, result.content) self.assertIn(expected_block.data, result.content)
hello_render = lambda block, _: Fragment(block.data)
with patch('xmodule.html_module.HtmlDescriptor.author_view', hello_render, create=True):
with patch('xmodule.x_module.DescriptorSystem.applicable_aside_types', lambda self, block: []):
render_and_check_contents(0, 3) render_and_check_contents(0, 3)
render_and_check_contents(1, 3) render_and_check_contents(1, 3)
render_and_check_contents(0, 2) render_and_check_contents(0, 2)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment