Commit 72411a17 by Will Daly

Added jnater's unit tests, cleaned up broken tests, ran pep8fix

parent f8adfc62
from django.test import TestCase
from courseware import grades
from mock import MagicMock
# from __future__ import division
# import random
# import logging
from collections import defaultdict
from django.conf import settings
from django.contrib.auth.models import User
# from models import StudentModuleCache
from courseware.module_render import get_module as get_module
from courseware.module_render import get_instance_module as get_instance_module
# from xmodule import graders
from xmodule.capa_module import CapaModule
# from xmodule.course_module import CourseDescriptor
# from xmodule.graders import Score
# from models import StudentModule
class test_grades(TestCase):
def test_yield_module_descendents(self):
mock_module = MagicMock()
a = MagicMock()
b = MagicMock()
c = MagicMock()
z = MagicMock()
y = MagicMock()
mock_module.get_display_items.return_value = [a, b, c]
a.get_display_items.return_value = [y, z]
b.get_display_items.return_value = []
c.get_display_items.return_value = []
z.get_display_items.return_value = []
y.get_display_items.return_value = []
dummy = list(grades.yield_module_descendents(mock_module))
self.assertEqual(dummy, [a,z,y,b, c])
def test_yield_dynamic_descriptor_descendents(self):
descriptor_true_mock = MagicMock()
a = MagicMock()
b = MagicMock()
b.has_dynamic_children.return_value = False
b.get_children.return_value = 'b'
c = MagicMock()
c.has_dynamic_children.return_value = False
c.get_children.return_value = 'c'
e = MagicMock()
e.has_dynamic_children.return_value = False
e.get_children.return_value = None
descriptor_true_mock.return_value = a
descriptor_true_mock.has_dynamic_children.return_value = True
module_creator_mock = MagicMock()
module_mock = MagicMock()
module_creator_mock(descriptor_true_mock).return_value = module_mock
child_locations_mock = MagicMock()
module_mock.get_children_locations.__iter__.return_value = [b, c]
print descriptor_true_mock.system.load_item(b)
descriptor_true_mock.system.load_item(b).return_value = b
descriptor_true_mock.system.load_item(c).return_value = c
descriptor_false_mock = MagicMock()
descriptor_false_mock.has_dynamic_children.return_value = False
descriptor_false_mock.get_children.return_value = e
true_descriptor_children_list = [descriptor_true_mock]
self.assertEqual(list(grades.yield_dynamic_descriptor_descendents(descriptor_true_mock, module_creator_mock)),true_descriptor_children_list)
self.assertEqual(list(grades.yield_dynamic_descriptor_descendents(descriptor_false_mock, module_creator_mock)),[descriptor_false_mock])
def test_yield_problems(self):
course_mock = MagicMock()
# course = course_mock
grading_context_mock = MagicMock()
# mock for grading context
course_mock.grading_context.return_value = grading_context_mock
# mock for course.id
course_id_mock = MagicMock()
course_mock.id.return_value = course_id_mock
# mock for student
student_mock = MagicMock()
student = student_mock()
grading_context_mock['all_descriptors'] = MagicMock()
sec_form1 = MagicMock()
sec_form2 = MagicMock()
sec1 = MagicMock()
sec1['section_descriptor'].return_value = "sec1 descriptor"
sec2 = MagicMock()
sec2['section_descriptor'].return_value = "sec2 descriptor"
sec3 = MagicMock()
sec3['section_descriptor'].return_value = "sec3 descriptor"
sec4 = MagicMock()
sec4['section_descriptor'].return_value = "sec4 descriptor"
grading_context_mock['all_descriptors'].__iter__.return_value = [(sec_form1, [sec1, sec2]), (sec_form2, [sec3, sec4])]
StudentModuleCache_mock = MagicMock()
student_module_cache_mock = MagicMock()
StudentModuleCache_mock(course_id_mock, student_mock, grading_context_mock['all_descriptors']).return_value = student_module_cache_mock
sec1_xmod = MagicMock()
sec2_xmod = MagicMock()
sec3_xmod = MagicMock()
sec4_xmod = MagicMock()
sec1['xmoduledescriptors'].return_value = [sec1_xmod]
sec2['xmoduledescriptors'].return_value = [sec2_xmod]
sec3['xmoduledescriptors'].return_value = [sec3_xmod]
sec4['xmoduledescriptors'].return_value = [sec4_xmod]
sec1_xmod_category = MagicMock()
sec2_xmod_category = MagicMock()
sec3_xmod_category = MagicMock()
sec4_xmod_category = MagicMock()
sec1_xmod.category.return_value = sec1_xmod_category
sec2_xmod.category.return_value = sec2_xmod_category
sec3_xmod.category.return_value = sec3_xmod_category
sec4_xmod.category.return_value = sec4_xmod_category
sec1_xmod_location_url = MagicMock()
sec2_xmod_location_url = MagicMock()
sec3_xmod_location_url = MagicMock()
sec4_xmod_location_url = MagicMock()
sec1_xmod.location.url.return_value = sec1_xmod_location_url
sec2_xmod.location.url.return_value = sec2_xmod_location_url
sec3_xmod.location.url.return_value = sec3_xmod_location_url
sec4_xmod.location.url.return_value = sec4_xmod_location_url
student_module_cache_mock.lookup(course_id_mock, sec1_xmod, sec1_xmod.location.url()).return_value = True
student_module_cache_mock.lookup(course_id_mock, sec2_xmod, sec2_xmod.location.url()).return_value = True
student_module_cache_mock.lookup(course_id_mock, sec3_xmod, sec3_xmod.location.url()).return_value = False
student_module_cache_mock.lookup(course_id_mock, sec4_xmod, sec4_xmod.location.url()).return_value = False
student_mock = MagicMock()
request_mock = MagicMock()
sec1_module_mock = MagicMock()
sec2_module_mock = MagicMock()
sec3_module_mock = MagicMock()
sec4_module_mock = MagicMock()
get_module_mock = MagicMock()
get_module_mock(student_mock, request_mock, sec1_xmod.location, student_module_cache_mock, course_id_mock).return_value = sec1_module_mock
get_module_mock(student_mock, request_mock, sec2_xmod.location, student_module_cache_mock, course_id_mock).return_value = sec2_module_mock
get_module_mock(student_mock, request_mock, sec3_xmod.location, student_module_cache_mock, course_id_mock).return_value = sec3_module_mock
get_module_mock(student_mock, request_mock, sec4_xmod.location, student_module_cache_mock, course_id_mock).return_value = sec4_module_mock
prob1 = MagicMock()
prob2 = MagicMock()
prob3 = MagicMock()
prob4 = MagicMock()
prob5 = MagicMock()
prob6 = MagicMock()
prob7 = MagicMock()
prob8 = MagicMock()
yield_module_descendents_mock = MagicMock()
yield_module_descendents_mock(sec1_module_mock).return_value = [prob1, prob2]
yield_module_descendents_mock(sec2_module_mock).return_value = [prob3, prob4]
yield_module_descendents_mock(sec3_module_mock).return_value = [prob5, prob6]
yield_module_descendents_mock(sec4_module_mock).return_value = [prob7, prob8]
self.assertEqual(list(grades.yield_problems(request_mock, course_mock, student_mock)), [])
import django_comment_client.models as models
import django_comment_client.permissions as permissions
from django.test import TestCase
class RoleClassTestCase(TestCase):
def setUp(self):
# For course ID, syntax edx/classname/classdate is important
# because xmodel.course_module.id_to_location looks for a string to split
self.course_id = "edX/toy/2012_Fall"
self.student_role = models.Role.objects.get_or_create(name="Student", \
course_id=self.course_id)[0]
self.student_role.add_permission("delete_thread")
self.student_2_role = models.Role.objects.get_or_create(name="Student", \
course_id=self.course_id)[0]
self.TA_role = models.Role.objects.get_or_create(name="Community TA",\
course_id=self.course_id)[0]
self.course_id_2 = "edx/6.002x/2012_Fall"
self.TA_role_2 = models.Role.objects.get_or_create(name="Community TA",\
course_id=self.course_id_2)[0]
class Dummy():
def render_template():
pass
d = {"data": {
"textbooks": [],
'wiki_slug': True,
}
}
def testHasPermission(self):
# Whenever you add a permission to student_role,
# Roles with the same FORUM_ROLE in same class also receives the same
# permission.
# Is this desirable behavior?
self.assertTrue(self.student_role.has_permission("delete_thread"))
self.assertTrue(self.student_2_role.has_permission("delete_thread"))
self.assertFalse(self.TA_role.has_permission("delete_thread"))
def testInheritPermissions(self):
self.TA_role.inherit_permissions(self.student_role)
self.assertTrue(self.TA_role.has_permission("delete_thread"))
# Despite being from 2 different courses, TA_role_2 can still inherit
# permissions from TA_role without error
self.TA_role_2.inherit_permissions(self.TA_role)
class PermissionClassTestCase(TestCase):
def setUp(self):
self.permission = permissions.Permission.objects.get_or_create(name="test")[0]
def testUnicode(self):
self.assertEqual(str(self.permission), "test")
......@@ -3,26 +3,40 @@ import random
import collections
from django.test import TestCase
from mock import MagicMock
from django.test.utils import override_settings
import django.core.urlresolvers as urlresolvers
import django_comment_client.mustache_helpers as mustache_helpers
#########################################################################################
class PluralizeTestCase(TestCase):
def test_pluralize(self):
self.text1 = '0 goat'
self.text2 = '1 goat'
self.text3 = '7 goat'
self.content = 'unused argument'
self.assertEqual(mustache_helpers.pluralize(self.content, self.text1), 'goats')
self.assertEqual(mustache_helpers.pluralize(self.content, self.text2), 'goat')
self.assertEqual(mustache_helpers.pluralize(self.content, self.text3), 'goats')
class PluralizeTest(TestCase):
def setUp(self):
self.text1 = '0 goat'
self.text2 = '1 goat'
self.text3 = '7 goat'
self.content = 'unused argument'
class CloseThreadTextTestCase(TestCase):
def test_pluralize(self):
self.assertEqual(mustache_helpers.pluralize(self.content, self.text1), 'goats')
self.assertEqual(mustache_helpers.pluralize(self.content, self.text2), 'goat')
self.assertEqual(mustache_helpers.pluralize(self.content, self.text3), 'goats')
#########################################################################################
class CloseThreadTextTest(TestCase):
def setUp(self):
self.contentClosed = {'closed': True}
self.contentOpen = {'closed': False}
def test_close_thread_text(self):
self.assertEqual(mustache_helpers.close_thread_text(self.contentClosed), 'Re-open thread')
self.assertEqual(mustache_helpers.close_thread_text(self.contentOpen), 'Close thread')
#########################################################################################
def test_close_thread_text(self):
self.contentClosed = {'closed': True}
self.contentOpen = {'closed': False}
self.assertEqual(mustache_helpers.close_thread_text(self.contentClosed), 'Re-open thread')
self.assertEqual(mustache_helpers.close_thread_text(self.contentOpen), 'Close thread')
import string
import random
import collections
from django.contrib.auth.models import User
from django.test import TestCase
import django_comment_client.models as models
import student.models
import django_comment_client.permissions as permissions
###############################################################################
class PermissionsTestCase(TestCase):
def random_str(self, length=15, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for x in range(length))
def setUp(self):
self.course_id = "edX/toy/2012_Fall"
self.moderator_role = models.Role.objects.get_or_create(name="Moderator",
course_id=self.course_id)[0]
self.student_role = models.Role.objects.get_or_create(name="Student",
course_id=self.course_id)[0]
self.student = User.objects.create(username=self.random_str(),
password="123456", email="john@yahoo.com")
self.moderator = User.objects.create(username=self.random_str(),
password="123456", email="staff@edx.org")
self.moderator.is_staff = True
self.moderator.save()
self.student_enrollment = student.models.CourseEnrollment.objects.create(user=self.student,
course_id=self.course_id)
self.moderator_enrollment = student.models.CourseEnrollment.objects.create(user=self.moderator,
course_id=self.course_id)
#Fake json files
self.empty_data = {"content": {
}
}
self.open_data = {"content": {
"closed": False,
"user_id": str(self.student.id)
}
}
self.closed_data = {"content": {
"closed": True,
"user_id": str(self.student.id)
}
}
def tearDown(self):
self.student_enrollment.delete()
self.moderator_enrollment.delete()
# Do we need to have this? We shouldn't be deleting students, ever
# self.student.delete()
# self.moderator.delete()
def testDefaultRoles(self):
self.assertTrue(self.student_role in self.student.roles.all())
self.assertTrue(self.moderator_role in self.moderator.roles.all())
def testPermission(self):
name = self.random_str()
self.moderator_role.add_permission(name)
self.assertTrue(permissions.has_permission(self.moderator, name, self.course_id))
# Moderators do not have student priveleges unless explicitly added
self.student_role.add_permission(name)
self.assertTrue(permissions.has_permission(self.student, name, self.course_id))
# Students don't have moderator priveleges
name2 = self.random_str()
self.student_role.add_permission(name2)
self.assertFalse(permissions.has_permission(self.moderator, name2, self.course_id))
def testCachedPermission(self):
# Cache miss returns None
# Don't really understand how this works? What's in Cache?
self.assertFalse(permissions.cached_has_permission(self.student, self.moderator,
course_id=None))
self.assertFalse(permissions.cached_has_permission(self.student, "update_thread",
course_id=None))
def testCheckCondition(self):
# Checks whether something? is open, or whether the author is user
self.assertFalse(permissions.check_condition(self.student, 'is_open',
self.course_id, self.empty_data))
self.assertFalse(permissions.check_condition(self.student, 'is_author',
self.course_id, self.empty_data))
self.assertTrue(permissions.check_condition(self.student, 'is_open',
self.course_id, self.open_data))
self.assertTrue(permissions.check_condition(self.student, 'is_author',
self.course_id, self.open_data))
self.assertFalse(permissions.check_condition(self.student,'is_open',
self.course_id, self.closed_data))
def testCheckConditionsPermissions(self):
#Function does not seem to return True
self.assertFalse(permissions.check_conditions_permissions(self.student, 'is_open',
self.course_id,
data=self.open_data))
self.assertFalse(permissions.check_conditions_permissions(self.student, 'is_open',
self.course_id,
data=self.empty_data))
self.assertFalse(permissions.check_conditions_permissions(self.student,
['is_open', 'is_author'],
self.course_id,
data=self.open_data))
self.assertFalse(permissions.check_conditions_permissions(self.student,
['is_open', 'is_author'],
self.course_id,
data=self.open_data,
operator='and'))
self.assertFalse(permissions.check_conditions_permissions(self.student, 'update_thread',
self.course_id, data=self.open_data))
def testCheckPermissionsByView(self):
# kwargs is the data entered in check_condition, which is json?
self.assertRaises(UnboundLocalError, permissions.check_permissions_by_view,
self.student, self.course_id, self.empty_data,
"nonexistant")
self.assertFalse(permissions.check_permissions_by_view(self.student,self.course_id,
self.empty_data, 'update_thread'))
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