From 184fd01ae9530a441ae7d0a37ed5be77ff5cbc8c Mon Sep 17 00:00:00 2001 From: "E. Kolpakov" Date: Thu, 8 Jan 2015 13:15:10 +0300 Subject: [PATCH] Created dedicated LibraryRoot test file and moved rendering tests from modulestore/tests/test_libraries --- .../modulestore/tests/test_libraries.py | 72 +----------------- .../xmodule/tests/test_library_root.py | 76 +++++++++++++++++++ 2 files changed, 78 insertions(+), 70 deletions(-) create mode 100644 common/lib/xmodule/xmodule/tests/test_library_root.py diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_libraries.py b/common/lib/xmodule/xmodule/modulestore/tests/test_libraries.py index 6038e7ca81..03ee84ae35 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_libraries.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_libraries.py @@ -6,14 +6,12 @@ Higher-level tests are in `cms/djangoapps/contentstore`. """ from bson.objectid import ObjectId import ddt -from mock import patch from opaque_keys.edx.locator import LibraryLocator -from xblock.fragment import Fragment -from xblock.runtime import Runtime as VanillaRuntime + from xmodule.modulestore.exceptions import DuplicateCourseError from xmodule.modulestore.tests.factories import LibraryFactory, ItemFactory, check_mongo_calls from xmodule.modulestore.tests.utils import MixedSplitTestCase -from xmodule.x_module import AUTHOR_VIEW + @ddt.ddt @@ -179,72 +177,6 @@ class TestLibraries(MixedSplitTestCase): version = lib.location.library_key.version_guid self.assertIsInstance(version, ObjectId) - @patch('xmodule.modulestore.split_mongo.caching_descriptor_system.CachingDescriptorSystem.render', VanillaRuntime.render) - 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. - """ - 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, - ) - library = self.store.get_library(library.location.library_key) - - context = {'reorderable_items': set(), } - # 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) - 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): - """ - 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): - 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: - 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(1, 3) - render_and_check_contents(0, 2) - render_and_check_contents(1, 2) - def test_xblock_in_lib_have_published_version_returns_false(self): library = LibraryFactory.create(modulestore=self.store) block = ItemFactory.create( diff --git a/common/lib/xmodule/xmodule/tests/test_library_root.py b/common/lib/xmodule/xmodule/tests/test_library_root.py new file mode 100644 index 0000000000..e17dafef85 --- /dev/null +++ b/common/lib/xmodule/xmodule/tests/test_library_root.py @@ -0,0 +1,76 @@ +from mock import patch + +from xblock.fragment import Fragment +from xblock.runtime import Runtime as VanillaRuntime +from xmodule.x_module import AUTHOR_VIEW + +from xmodule.modulestore.tests.factories import LibraryFactory, CourseFactory, ItemFactory +from xmodule.modulestore.tests.utils import MixedSplitTestCase + + +class TestLibraryRoot(MixedSplitTestCase): + @patch('xmodule.modulestore.split_mongo.caching_descriptor_system.CachingDescriptorSystem.render', VanillaRuntime.render) + 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. + """ + 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, + ) + library = self.store.get_library(library.location.library_key) + + context = {'reorderable_items': set(), } + # 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) + 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): + """ + 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): + 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: + 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(1, 3) + render_and_check_contents(0, 2) + render_and_check_contents(1, 2) \ No newline at end of file