Commit abc3e5b0 by Deena Wang

more tests in test_views.py

parent 40ddaa99
...@@ -23,6 +23,7 @@ from contentstore.tests.factories import CourseFactory, ItemFactory ...@@ -23,6 +23,7 @@ from contentstore.tests.factories import CourseFactory, ItemFactory
from xmodule.modulestore import Location from xmodule.modulestore import Location
from xmodule.x_module import ModuleSystem from xmodule.x_module import ModuleSystem
from xmodule.error_module import ErrorModule from xmodule.error_module import ErrorModule
from contentstore.utils import get_course_for_item
class Stub(): class Stub():
pass pass
...@@ -49,6 +50,7 @@ TEST_DATA_XML_MODULESTORE = xml_store_config(TEST_DATA_DIR) ...@@ -49,6 +50,7 @@ TEST_DATA_XML_MODULESTORE = xml_store_config(TEST_DATA_DIR)
class ViewsTestCase(TestCase): class ViewsTestCase(TestCase):
def setUp(self): def setUp(self):
#modulestore().collection.drop()
self.location = ['i4x', 'edX', 'toy', 'chapter', 'Overview'] self.location = ['i4x', 'edX', 'toy', 'chapter', 'Overview']
self.location_2 = ['i4x', 'edX', 'full', 'course', '6.002_Spring_2012'] self.location_2 = ['i4x', 'edX', 'full', 'course', '6.002_Spring_2012']
self.location_3 = ['i4x', 'MITx', '999', 'course', 'Robot_Super_Course'] self.location_3 = ['i4x', 'MITx', '999', 'course', 'Robot_Super_Course']
...@@ -56,8 +58,6 @@ class ViewsTestCase(TestCase): ...@@ -56,8 +58,6 @@ class ViewsTestCase(TestCase):
self._MODULESTORES = {} self._MODULESTORES = {}
self.course_id = 'edX/toy/2012_Fall' self.course_id = 'edX/toy/2012_Fall'
self.course_id_2 = 'edx/full/6.002_Spring_2012' self.course_id_2 = 'edx/full/6.002_Spring_2012'
#self.toy_course = modulestore().get_course(self.course_id)
# Problem: Classes persist, need to delete stuff from modulestore
# is a CourseDescriptor object? # is a CourseDescriptor object?
self.course = CourseFactory.create() self.course = CourseFactory.create()
# is a sequence descriptor # is a sequence descriptor
...@@ -66,7 +66,7 @@ class ViewsTestCase(TestCase): ...@@ -66,7 +66,7 @@ class ViewsTestCase(TestCase):
def tearDown(self): def tearDown(self):
_MODULESTORES = {} _MODULESTORES = {}
modulestore().collection.drop() modulestore().collection.drop()
assert False #assert False
def test_has_access(self): def test_has_access(self):
user = MagicMock(is_staff = True, is_active = True, is_authenticated = True) user = MagicMock(is_staff = True, is_active = True, is_authenticated = True)
...@@ -196,18 +196,121 @@ class ViewsTestCase(TestCase): ...@@ -196,18 +196,121 @@ class ViewsTestCase(TestCase):
print self.course.xmodule_constructor(system) print self.course.xmodule_constructor(system)
print self.course.xmodule_constructor(system).func print self.course.xmodule_constructor(system).func
print self.course.xmodule_constructor(system).keywords print self.course.xmodule_constructor(system).keywords
print dir(self.course.xmodule_constructor(system).func)
def test__xmodule_recurse(self): def test__xmodule_recurse(self):
raise SkipTest #There shouldn't be a difference, but the code works with defined
## mock_item = MagicMock() # function f but not with lambda functions
## mock_item.get_children.return_value = [] mock_item = MagicMock()
mock_item.get_children.return_value = []
s = Stub() s = Stub()
s.children.append(Stub()) s.children.append(Stub())
views._xmodule_recurse(s, lambda x: return) views._xmodule_recurse(s, f)
#views._xmodule_recurse(s, lambda x: x.n += 1)
self.assertEquals(s.n, 1) self.assertEquals(s.n, 1)
self.assertEquals(s.children[0].n, 1) self.assertEquals(s.children[0].n, 1)
def test_get_module_previews(self):
# needs a working render_to_string
raise SkipTest
self.request = RequestFactory().get('foo')
self.request.user = UserFactory()
self.request.session = {}
print views.get_module_previews(self.request, self.course)
def test_delete_item(self):
# If user doesn't have permission, redirect
self.no_permit_user = MagicMock(is_staff = False, is_active = False)
self.no_permit_user.is_authenticated.return_value = True
self.request = RequestFactory().post('i4x://MITx/999/course/Robot_Super_Course')
self.request.POST = self.request.POST.copy()
self.request.POST.update({'id':'i4x://MITx/999/course/Robot_Super_Course'})
self.request.user = self.no_permit_user
self.assertRaises(PermissionDenied, views.delete_item, self.request)
# Should return an HttpResponse
self.permit_user =MagicMock(is_staff = True, is_active = True)
self.permit_user.is_authenticated.return_value = True
self.request_2 = RequestFactory().post(self.item.location.url())
self.request_2.POST = self.request_2.POST.copy()
self.request_2.POST.update({'id':self.item.location.url()})
self.request_2.user = self.permit_user
response = views.delete_item(self.request_2)
self.assertIsInstance(response, HttpResponse)
self.assertEquals(modulestore().get_items(self.item.location.url()), [])
# Set delete_children to True to delete all children
# Create children
self.item_2 = ItemFactory.create()
child_item = ItemFactory.create()
## print type(self.item_2)
## print self.item_2.__dict__
# Is there better way of adding children? What format are children in?
self.item_2.definition['children'] = [child_item.location.url()]
self.request_3 = RequestFactory().post(self.item_2.location.url())
self.request_3.POST = self.request_3.POST.copy()
self.request_3.POST.update({'id':self.item_2.location.url(),
'delete_children':True,
'delete_all_versions':True})
self.request_3.user = self.permit_user
print self.item_2.get_children()
self.assertIsInstance(views.delete_item(self.request_3), HttpResponse)
self.assertEquals(modulestore().get_items(self.item_2.location.url()), [])
# Problem: Function doesn't delete child item?
# child_item can be manually deleted, but can't delete it using function
# Not sure if problem with _xmodule_recurse and lambda functions
#store = views.get_modulestore(child_item.location.url())
#store.delete_item(child_item.location)
self.assertEquals(modulestore().get_items(child_item.location.url()), [])
# Check delete_item on 'vertical'
self.item_3 = ItemFactory.create(template = 'i4x://edx/templates/vertical/Empty')
self.request_4 = RequestFactory().post(self.item_3.location.url())
self.request_4.POST = self.request_4.POST.copy()
self.request_4.POST.update({'id':self.item_3.location.url(),
'delete_children':True,
'delete_all_versions':True})
self.request_4.user = self.permit_user
self.assertIsInstance(views.delete_item(self.request_4), HttpResponse)
self.assertEquals(modulestore().get_items(self.item_3.location.url()), [])
def test_save_item(self):
# Test that user with no permissions gets redirected
self.no_permit_user = MagicMock(is_staff = False, is_active = False)
self.no_permit_user.is_authenticated.return_value = True
self.request = RequestFactory().post(self.item.location.url())
self.request.POST = self.request.POST.copy()
self.request.POST.update({'id':self.item.location.url()})
self.request.user = self.no_permit_user
self.assertRaises(PermissionDenied, views.save_item, self.request)
# Test user with permissions but nothing in request.POST
self.item_2 = ItemFactory.create()
self.permit_user =MagicMock(is_staff = True, is_active = True)
self.permit_user.is_authenticated.return_value = True
self.request_2 = RequestFactory().post(self.item_2.location.url())
self.request_2.POST = self.request.POST.copy()
self.request_2.POST.update({'id':self.item_2.location.url()})
self.request_2.user = self.permit_user
self.assertIsInstance(views.save_item(self.request_2), HttpResponse)
# Test updating data
self.request_3 = RequestFactory().post(self.item_2.location.url())
self.request_3.POST = self.request.POST.copy()
self.request_3.POST.update({'id':self.item_2.location.url(),
'data':{'foo':'bar'}})
self.request_3.user = self.permit_user
self.assertIsInstance(views.save_item(self.request_3), HttpResponse)
self.assertEquals(modulestore().get_item(self.item_2.location.dict()).definition['data'],
{u'foo': u'bar'})
# Test metadata, which is a dictionary?
self.request_4 = RequestFactory().post(self.item_2.location.url())
self.request_4.POST = self.request.POST.copy()
self.request_4.POST.update({'id':self.item_2.location.url(),
'metadata':{'foo':'bar'}})
self.request_4.user = self.permit_user
self.assertIsInstance(views.save_item(self.request_4), HttpResponse)
self.assertEquals(modulestore().get_item(self.item_2.location.dict()).metadata['foo'],
'bar')
def f(x):
x.n += 1
class Stub(): class Stub():
def __init__(self): def __init__(self):
self.n = 0 self.n = 0
......
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