Commit 7978c581 by Will Daly

Changed test for checking all pages to checking a random page

parent a79a2907
import logging import logging
import json import json
import time import time
import random
from urlparse import urlsplit, urlunsplit from urlparse import urlsplit, urlunsplit
from django.contrib.auth.models import User, Group from django.contrib.auth.models import User, Group
...@@ -242,7 +244,6 @@ class LoginEnrollmentTestCase(TestCase): ...@@ -242,7 +244,6 @@ class LoginEnrollmentTestCase(TestCase):
"got code {0} for url '{1}'. Expected code {2}".format(resp.status_code, url, code)) "got code {0} for url '{1}'. Expected code {2}".format(resp.status_code, url, code))
return resp return resp
class ActivateLoginTest(LoginEnrollmentTestCase): class ActivateLoginTest(LoginEnrollmentTestCase):
'''Test logging in and logging out''' '''Test logging in and logging out'''
def setUp(self): def setUp(self):
...@@ -260,8 +261,10 @@ class ActivateLoginTest(LoginEnrollmentTestCase): ...@@ -260,8 +261,10 @@ class ActivateLoginTest(LoginEnrollmentTestCase):
class PageLoaderTestCase(LoginEnrollmentTestCase): class PageLoaderTestCase(LoginEnrollmentTestCase):
''' Base class that adds a function to load all pages in a modulestore ''' ''' Base class that adds a function to load all pages in a modulestore '''
def check_pages_load(self, module_store): def check_random_page_loads(self, module_store):
"""Make all locations in course load""" '''
Choose a page in the course randomly, and assert that it loads
'''
# enroll in the course before trying to access pages # enroll in the course before trying to access pages
courses = module_store.get_courses() courses = module_store.get_courses()
self.assertEqual(len(courses), 1) self.assertEqual(len(courses), 1)
...@@ -269,77 +272,57 @@ class PageLoaderTestCase(LoginEnrollmentTestCase): ...@@ -269,77 +272,57 @@ class PageLoaderTestCase(LoginEnrollmentTestCase):
self.enroll(course) self.enroll(course)
course_id = course.id course_id = course.id
num = 0 descriptor = random.choice(module_store.get_items(
num_bad = 0 Location(None, None, None, None, None)))
all_ok = True
for descriptor in module_store.get_items( # We have ancillary course information now as modules
Location(None, None, None, None, None)): # and we can't simply use 'jump_to' to view them
if descriptor.location.category == 'about':
num += 1 self._assert_loads('about_course',
print "Checking ", descriptor.location.url() {'course_id': course_id},
descriptor)
# We have ancillary course information now as modules and we can't simply use 'jump_to' to view them
if descriptor.location.category == 'about': elif descriptor.location.category == 'static_tab':
resp = self.client.get(reverse('about_course', kwargs={'course_id': course_id})) kwargs = {'course_id': course_id,
msg = str(resp.status_code) 'tab_slug': descriptor.location.name}
self._assert_loads('static_tab', kwargs, descriptor)
if resp.status_code != 200:
msg = "ERROR " + msg elif descriptor.location.category == 'course_info':
all_ok = False self._assert_loads('info', kwargs={'course_id': course_id},
num_bad += 1 descriptor)
elif descriptor.location.category == 'static_tab':
resp = self.client.get(reverse('static_tab', kwargs={'course_id': course_id, 'tab_slug': descriptor.location.name})) elif descriptor.location.category == 'custom_tag_template':
msg = str(resp.status_code) pass
if resp.status_code != 200: else:
msg = "ERROR " + msg
all_ok = False kwargs = {'course_id': course_id,
num_bad += 1 'location': descriptor.location.url()}
elif descriptor.location.category == 'course_info':
resp = self.client.get(reverse('info', kwargs={'course_id': course_id})) self._assert_loads('jump_to', kwargs, descriptor,
msg = str(resp.status_code) expect_redirect=True,
check_content=True)
if resp.status_code != 200:
msg = "ERROR " + msg
all_ok = False def _assert_loads(self, django_url, kwargs, descriptor,
num_bad += 1 expect_redirect=False,
elif descriptor.location.category == 'custom_tag_template': check_content=False):
pass
else: url = reverse(django_url, kwargs=kwargs)
#print descriptor.__class__, descriptor.location response = self.client.get(url, follow=True)
resp = self.client.get(reverse('jump_to',
kwargs={'course_id': course_id, if response.status_code != 200:
'location': descriptor.location.url()}), follow=True) self.fail('Status %d for page %s' %
msg = str(resp.status_code) (resp.status_code, descriptor.location.url()))
if resp.status_code != 200: if expect_redirect:
msg = "ERROR " + msg + ": " + descriptor.location.url() self.assertEqual(response.redirect_chain[0][1], 302)
all_ok = False
num_bad += 1 if check_content:
elif resp.redirect_chain[0][1] != 302: unavailable_msg = "this module is temporarily unavailable"
msg = "ERROR on redirect from " + descriptor.location.url() self.assertEqual(response.content.find(unavailable_msg), -1)
all_ok = False self.assertFalse(isinstance(descriptor, ErrorDescriptor))
num_bad += 1
# check content to make sure there were no rendering failures
content = resp.content
if content.find("this module is temporarily unavailable") >= 0:
msg = "ERROR unavailable module "
all_ok = False
num_bad += 1
elif isinstance(descriptor, ErrorDescriptor):
msg = "ERROR error descriptor loaded: "
msg = msg + descriptor.error_msg
all_ok = False
num_bad += 1
print msg
self.assertTrue(all_ok) # fail fast
print "{0}/{1} good".format(num - num_bad, num)
log.info("{0}/{1} good".format(num - num_bad, num))
self.assertTrue(all_ok)
@override_settings(MODULESTORE=TEST_DATA_XML_MODULESTORE) @override_settings(MODULESTORE=TEST_DATA_XML_MODULESTORE)
...@@ -357,7 +340,7 @@ class TestCoursesLoadTestCase_XmlModulestore(PageLoaderTestCase): ...@@ -357,7 +340,7 @@ class TestCoursesLoadTestCase_XmlModulestore(PageLoaderTestCase):
load_error_modules=True, load_error_modules=True,
) )
self.check_pages_load(module_store) self.check_random_page_loads(module_store)
def test_full_course_loads(self): def test_full_course_loads(self):
module_store = XMLModuleStore(TEST_DATA_DIR, module_store = XMLModuleStore(TEST_DATA_DIR,
...@@ -365,7 +348,7 @@ class TestCoursesLoadTestCase_XmlModulestore(PageLoaderTestCase): ...@@ -365,7 +348,7 @@ class TestCoursesLoadTestCase_XmlModulestore(PageLoaderTestCase):
course_dirs=['full'], course_dirs=['full'],
load_error_modules=True, load_error_modules=True,
) )
self.check_pages_load(module_store) self.check_random_page_loads(module_store)
@override_settings(MODULESTORE=TEST_DATA_MONGO_MODULESTORE) @override_settings(MODULESTORE=TEST_DATA_MONGO_MODULESTORE)
...@@ -380,12 +363,12 @@ class TestCoursesLoadTestCase_MongoModulestore(PageLoaderTestCase): ...@@ -380,12 +363,12 @@ class TestCoursesLoadTestCase_MongoModulestore(PageLoaderTestCase):
def test_toy_course_loads(self): def test_toy_course_loads(self):
module_store = modulestore() module_store = modulestore()
import_from_xml(module_store, TEST_DATA_DIR, ['toy']) import_from_xml(module_store, TEST_DATA_DIR, ['toy'])
self.check_pages_load(module_store) self.check_random_page_loads(module_store)
def test_full_course_loads(self): def test_full_course_loads(self):
module_store = modulestore() module_store = modulestore()
import_from_xml(module_store, TEST_DATA_DIR, ['full']) import_from_xml(module_store, TEST_DATA_DIR, ['full'])
self.check_pages_load(module_store) self.check_random_page_loads(module_store)
@override_settings(MODULESTORE=TEST_DATA_XML_MODULESTORE) @override_settings(MODULESTORE=TEST_DATA_XML_MODULESTORE)
......
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