Commit 1a387d82 by E. Kolpakov

Test for paging in items.py

parent f5132903
......@@ -441,6 +441,7 @@ class TestLibraries(LibraryTestCase):
with self.assertRaises(ValueError):
self._refresh_children(lc_block, status_code_expected=400)
@ddt.ddt
class TestLibraryAccess(LibraryTestCase):
"""
......
......@@ -3,7 +3,7 @@ import json
from datetime import datetime, timedelta
import ddt
from mock import patch
from mock import patch, Mock, PropertyMock
from pytz import UTC
from webob import Response
......@@ -18,6 +18,7 @@ from contentstore.views.component import (
component_handler, get_component_templates
)
from contentstore.views.item import create_xblock_info, ALWAYS, VisibilityState, _xblock_type_and_display_name
from contentstore.tests.utils import CourseTestCase
from student.tests.factories import UserFactory
......@@ -86,12 +87,18 @@ class ItemTest(CourseTestCase):
class GetItemTest(ItemTest):
"""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
"""
preview_url = reverse_usage_url("xblock_view_handler", usage_key, {'view_name': 'container_preview'})
resp = self.client.get(preview_url, HTTP_ACCEPT='application/json')
resp = self._get_preview(usage_key, data)
self.assertEqual(resp.status_code, 200)
resp_content = json.loads(resp.content)
html = resp_content['html']
......@@ -100,6 +107,14 @@ class GetItemTest(ItemTest):
self.assertIsNotNone(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(
(1, 21, 23, 35, 37),
(2, 22, 24, 38, 39),
......@@ -247,6 +262,40 @@ class GetItemTest(ItemTest):
self.assertIn('New_NAME_A', 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):
"""Tests for '/xblock' DELETE url."""
......
......@@ -14,15 +14,17 @@ from opaque_keys.edx.locator import LibraryLocator
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, LibraryList, ANY_CAPA_TYPE_VALUE, LibraryContentDescriptor
from xmodule.x_module import AUTHOR_VIEW
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.utils import MixedSplitTestCase
from xmodule.tests import get_test_system
from xmodule.validation import StudioValidationMessage
_dummy_render = lambda block, _: Fragment(block.data)
dummy_render = lambda block, _: Fragment(block.data)
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.
"""
......@@ -120,6 +122,7 @@ class BaseTestLibraryContainer(MixedSplitTestCase):
modulestore=self.store,
)
@ddt.ddt
class TestLibraryContainer(BaseTestLibraryContainer):
"""
......@@ -270,8 +273,7 @@ class TestLibraryContainer(BaseTestLibraryContainer):
@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.html_module.HtmlModule.author_view', dummy_render, create=True)
@patch('xmodule.x_module.DescriptorSystem.applicable_aside_types', lambda self, block: [])
class TestLibraryContentRender(BaseTestLibraryContainer):
"""
......@@ -321,7 +323,7 @@ class TestLibraryList(TestCase):
lib_list = LibraryList()
lib1_key, lib1_version = u'library-v1:Org1+Lib1', '5436ffec56c02c13806a4c1b'
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)
self.assertEqual(len(parsed), 2)
self.assertEquals(parsed[0].library_id, LibraryLocator.from_string(lib1_key))
......@@ -335,4 +337,4 @@ class TestLibraryList(TestCase):
"""
lib_list = LibraryList()
with self.assertRaises(ValueError):
lib_list.from_json(["Not-a-library-key,whatever"])
\ No newline at end of file
lib_list.from_json(["Not-a-library-key,whatever"])
# -*- coding: utf-8 -*-
"""
Basic unit tests for LibraryRoot
"""
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.factories import LibraryFactory, ItemFactory
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.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: [])
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
......@@ -62,8 +69,9 @@ class TestLibraryRoot(MixedSplitTestCase):
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)]
expected_blocks = blocks[page_size * page:page_size * (page + 1)]
result = library.render(AUTHOR_VIEW, context)
for expected_block in expected_blocks:
......@@ -72,4 +80,4 @@ class TestLibraryRoot(MixedSplitTestCase):
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
render_and_check_contents(1, 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