Commit 1a387d82 by E. Kolpakov

Test for paging in items.py

parent f5132903
...@@ -441,6 +441,7 @@ class TestLibraries(LibraryTestCase): ...@@ -441,6 +441,7 @@ class TestLibraries(LibraryTestCase):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
self._refresh_children(lc_block, status_code_expected=400) self._refresh_children(lc_block, status_code_expected=400)
@ddt.ddt @ddt.ddt
class TestLibraryAccess(LibraryTestCase): class TestLibraryAccess(LibraryTestCase):
""" """
......
...@@ -3,7 +3,7 @@ import json ...@@ -3,7 +3,7 @@ import json
from datetime import datetime, timedelta from datetime import datetime, timedelta
import ddt import ddt
from mock import patch from mock import patch, Mock, PropertyMock
from pytz import UTC from pytz import UTC
from webob import Response from webob import Response
...@@ -18,6 +18,7 @@ from contentstore.views.component import ( ...@@ -18,6 +18,7 @@ from contentstore.views.component import (
component_handler, get_component_templates component_handler, get_component_templates
) )
from contentstore.views.item import create_xblock_info, ALWAYS, VisibilityState, _xblock_type_and_display_name from contentstore.views.item import create_xblock_info, ALWAYS, VisibilityState, _xblock_type_and_display_name
from contentstore.tests.utils import CourseTestCase from contentstore.tests.utils import CourseTestCase
from student.tests.factories import UserFactory from student.tests.factories import UserFactory
...@@ -86,12 +87,18 @@ class ItemTest(CourseTestCase): ...@@ -86,12 +87,18 @@ class ItemTest(CourseTestCase):
class GetItemTest(ItemTest): class GetItemTest(ItemTest):
"""Tests for '/xblock' GET url.""" """Tests for '/xblock' GET url."""
def _get_container_preview(self, usage_key): def _get_preview(self, usage_key, data=None):
""" Makes a request to xblock preview handler """
preview_url = reverse_usage_url("xblock_view_handler", usage_key, {'view_name': 'container_preview'})
data = data if data else {}
resp = self.client.get(preview_url, data, HTTP_ACCEPT='application/json')
return resp
def _get_container_preview(self, usage_key, data=None):
""" """
Returns the HTML and resources required for the xblock at the specified UsageKey Returns the HTML and resources required for the xblock at the specified UsageKey
""" """
preview_url = reverse_usage_url("xblock_view_handler", usage_key, {'view_name': 'container_preview'}) resp = self._get_preview(usage_key, data)
resp = self.client.get(preview_url, HTTP_ACCEPT='application/json')
self.assertEqual(resp.status_code, 200) self.assertEqual(resp.status_code, 200)
resp_content = json.loads(resp.content) resp_content = json.loads(resp.content)
html = resp_content['html'] html = resp_content['html']
...@@ -100,6 +107,14 @@ class GetItemTest(ItemTest): ...@@ -100,6 +107,14 @@ class GetItemTest(ItemTest):
self.assertIsNotNone(resources) self.assertIsNotNone(resources)
return html, resources return html, resources
def _get_container_preview_with_error(self, usage_key, expected_code, data=None, content_contains=None):
""" Make request and asserts on response code and response contents """
resp = self._get_preview(usage_key, data)
self.assertEqual(resp.status_code, expected_code)
if content_contains:
self.assertIn(content_contains, resp.content)
return resp
@ddt.data( @ddt.data(
(1, 21, 23, 35, 37), (1, 21, 23, 35, 37),
(2, 22, 24, 38, 39), (2, 22, 24, 38, 39),
...@@ -247,6 +262,40 @@ class GetItemTest(ItemTest): ...@@ -247,6 +262,40 @@ class GetItemTest(ItemTest):
self.assertIn('New_NAME_A', html) self.assertIn('New_NAME_A', html)
self.assertIn('New_NAME_B', html) self.assertIn('New_NAME_B', html)
def test_valid_paging(self):
"""
Tests that valid paging is passed along to underlying block
"""
with patch('contentstore.views.item.get_preview_fragment') as patched_get_preview_fragment:
retval = Mock()
type(retval).content = PropertyMock(return_value="Some content")
type(retval).resources = PropertyMock(return_value=[])
patched_get_preview_fragment.return_value = retval
root_usage_key = self._create_vertical()
_, _ = self._get_container_preview(
root_usage_key,
{'enable_paging': 'true', 'page_number': 0, 'page_size': 2}
)
call_args = patched_get_preview_fragment.call_args[0]
_, _, context = call_args
self.assertIn('paging', context)
self.assertEqual({'page_number': 0, 'page_size': 2}, context['paging'])
@ddt.data([1, 'invalid'], ['invalid', 2])
@ddt.unpack
def test_invalid_paging(self, page_number, page_size):
"""
Tests that valid paging is passed along to underlying block
"""
root_usage_key = self._create_vertical()
self._get_container_preview_with_error(
root_usage_key,
400,
data={'enable_paging': 'true', 'page_number': page_number, 'page_size': page_size},
content_contains="Couldn't parse paging parameters"
)
class DeleteItem(ItemTest): class DeleteItem(ItemTest):
"""Tests for '/xblock' DELETE url.""" """Tests for '/xblock' DELETE url."""
......
...@@ -14,15 +14,17 @@ from opaque_keys.edx.locator import LibraryLocator ...@@ -14,15 +14,17 @@ from opaque_keys.edx.locator import LibraryLocator
from xblock.fragment import Fragment from xblock.fragment import Fragment
from xblock.runtime import Runtime as VanillaRuntime from xblock.runtime import Runtime as VanillaRuntime
from xmodule.x_module import AUTHOR_VIEW, STUDENT_VIEW from xmodule.x_module import AUTHOR_VIEW
from xmodule.library_content_module import LibraryVersionReference, LibraryList, ANY_CAPA_TYPE_VALUE, LibraryContentDescriptor from xmodule.library_content_module import (
LibraryVersionReference, LibraryList, 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
from xmodule.tests import get_test_system from xmodule.tests import get_test_system
from xmodule.validation import StudioValidationMessage from xmodule.validation import StudioValidationMessage
_dummy_render = lambda block, _: Fragment(block.data) dummy_render = lambda block, _: Fragment(block.data)
class BaseTestLibraryContainer(MixedSplitTestCase): class BaseTestLibraryContainer(MixedSplitTestCase):
...@@ -74,7 +76,7 @@ class BaseTestLibraryContainer(MixedSplitTestCase): ...@@ -74,7 +76,7 @@ class BaseTestLibraryContainer(MixedSplitTestCase):
} }
) )
def _bind_course_module(self, module, render=None): def _bind_course_module(self, module):
""" """
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.
""" """
...@@ -120,6 +122,7 @@ class BaseTestLibraryContainer(MixedSplitTestCase): ...@@ -120,6 +122,7 @@ class BaseTestLibraryContainer(MixedSplitTestCase):
modulestore=self.store, modulestore=self.store,
) )
@ddt.ddt @ddt.ddt
class TestLibraryContainer(BaseTestLibraryContainer): class TestLibraryContainer(BaseTestLibraryContainer):
""" """
...@@ -270,8 +273,7 @@ class TestLibraryContainer(BaseTestLibraryContainer): ...@@ -270,8 +273,7 @@ class TestLibraryContainer(BaseTestLibraryContainer):
@patch('xmodule.modulestore.split_mongo.caching_descriptor_system.CachingDescriptorSystem.render', VanillaRuntime.render) @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.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: []) @patch('xmodule.x_module.DescriptorSystem.applicable_aside_types', lambda self, block: [])
class TestLibraryContentRender(BaseTestLibraryContainer): class TestLibraryContentRender(BaseTestLibraryContainer):
""" """
...@@ -321,7 +323,7 @@ class TestLibraryList(TestCase): ...@@ -321,7 +323,7 @@ class TestLibraryList(TestCase):
lib_list = LibraryList() lib_list = LibraryList()
lib1_key, lib1_version = u'library-v1:Org1+Lib1', '5436ffec56c02c13806a4c1b' lib1_key, lib1_version = u'library-v1:Org1+Lib1', '5436ffec56c02c13806a4c1b'
lib2_key, lib2_version = u'library-v1:Org2+Lib2', '112dbaf312c0daa019ce9992' lib2_key, lib2_version = u'library-v1:Org2+Lib2', '112dbaf312c0daa019ce9992'
raw = [lib1_key+','+lib1_version, lib2_key+','+lib2_version] raw = [lib1_key + ',' + lib1_version, lib2_key + ',' + lib2_version]
parsed = lib_list.from_json(raw) parsed = lib_list.from_json(raw)
self.assertEqual(len(parsed), 2) self.assertEqual(len(parsed), 2)
self.assertEquals(parsed[0].library_id, LibraryLocator.from_string(lib1_key)) self.assertEquals(parsed[0].library_id, LibraryLocator.from_string(lib1_key))
...@@ -335,4 +337,4 @@ class TestLibraryList(TestCase): ...@@ -335,4 +337,4 @@ class TestLibraryList(TestCase):
""" """
lib_list = LibraryList() lib_list = LibraryList()
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
lib_list.from_json(["Not-a-library-key,whatever"]) lib_list.from_json(["Not-a-library-key,whatever"])
\ No newline at end of file
# -*- coding: utf-8 -*-
"""
Basic unit tests for LibraryRoot
"""
from mock import patch from mock import patch
from xblock.fragment import Fragment from xblock.fragment import Fragment
from xblock.runtime import Runtime as VanillaRuntime from xblock.runtime import Runtime as VanillaRuntime
from xmodule.x_module import AUTHOR_VIEW from xmodule.x_module import AUTHOR_VIEW
from xmodule.modulestore.tests.factories import LibraryFactory, CourseFactory, ItemFactory from xmodule.modulestore.tests.factories import LibraryFactory, ItemFactory
from xmodule.modulestore.tests.utils import MixedSplitTestCase from xmodule.modulestore.tests.utils import MixedSplitTestCase
_dummy_render = lambda block, _: Fragment(block.data) dummy_render = lambda block, _: Fragment(block.data)
@patch('xmodule.modulestore.split_mongo.caching_descriptor_system.CachingDescriptorSystem.render', VanillaRuntime.render) @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.html_module.HtmlDescriptor.author_view', dummy_render, create=True)
@patch('xmodule.x_module.DescriptorSystem.applicable_aside_types', lambda self, block: []) @patch('xmodule.x_module.DescriptorSystem.applicable_aside_types', lambda self, block: [])
class TestLibraryRoot(MixedSplitTestCase): class TestLibraryRoot(MixedSplitTestCase):
"""
Basic unit tests for LibraryRoot (library_root_xblock.py)
"""
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
...@@ -62,8 +69,9 @@ class TestLibraryRoot(MixedSplitTestCase): ...@@ -62,8 +69,9 @@ class TestLibraryRoot(MixedSplitTestCase):
library = self.store.get_library(library.location.library_key) library = self.store.get_library(library.location.library_key)
def render_and_check_contents(page, page_size): 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}} context = {'reorderable_items': set(), 'paging': {'page_number': page, 'page_size': page_size}}
expected_blocks = blocks[page_size*page:page_size*(page+1)] expected_blocks = blocks[page_size * page:page_size * (page + 1)]
result = library.render(AUTHOR_VIEW, context) result = library.render(AUTHOR_VIEW, context)
for expected_block in expected_blocks: for expected_block in expected_blocks:
...@@ -72,4 +80,4 @@ class TestLibraryRoot(MixedSplitTestCase): ...@@ -72,4 +80,4 @@ class TestLibraryRoot(MixedSplitTestCase):
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)
render_and_check_contents(1, 2) render_and_check_contents(1, 2)
\ No newline at end of file
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