From 835f18795afe56f3fd4e6b96935f705d103f0ad2 Mon Sep 17 00:00:00 2001 From: Victor Shnayder Date: Fri, 30 Nov 2012 10:27:34 -0500 Subject: [PATCH] Make tests pass again --- .../instructor/staff_grading_service.py | 30 +++++++++++++++---- lms/djangoapps/instructor/tests.py | 16 +++++++--- lms/envs/common.py | 3 ++ lms/envs/test.py | 6 +++- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/lms/djangoapps/instructor/staff_grading_service.py b/lms/djangoapps/instructor/staff_grading_service.py index 05c131ed56..c070bd6835 100644 --- a/lms/djangoapps/instructor/staff_grading_service.py +++ b/lms/djangoapps/instructor/staff_grading_service.py @@ -38,7 +38,7 @@ class MockStaffGradingService(object): 'rubric': 'A rubric'}) def save_grade(self, course_id, grader_id, submission_id, score, feedback): - return self.get_next(course_id) + return self.get_next(course_id, grader_id) class StaffGradingService(object): @@ -140,8 +140,28 @@ class StaffGradingService(object): return r.text -_service = StaffGradingService(settings.STAFF_GRADING_INTERFACE) -#_service = MockStaffGradingService() +# don't initialize until grading_service() is called--means that just +# importing this file doesn't create objects that may not have the right config +_service = None + +def grading_service(): + """ + Return a staff grading service instance--if settings.MOCK_STAFF_GRADING is True, + returns a mock one, otherwise a real one. + + Caches the result, so changing the setting after the first call to this + function will have no effect. + """ + global _service + if _service is not None: + return _service + + if settings.MOCK_STAFF_GRADING: + _service = MockStaffGradingService() + else: + _service = StaffGradingService(settings.STAFF_GRADING_INTERFACE) + + return _service def _err_response(msg): """ @@ -194,7 +214,7 @@ def _get_next(course_id, grader_id): """ try: - return _service.get_next(course_id, grader_id) + return grading_service().get_next(course_id, grader_id) except GradingServiceError: log.exception("Error from grading service") return json.dumps({'success': False, 'error': 'Could not connect to grading service'}) @@ -228,7 +248,7 @@ def save_grade(request, course_id): p = request.POST try: - result_json = _service.save_grade(course_id, + result_json = grading_service().save_grade(course_id, grader_id, p['submission_id'], p['score'], diff --git a/lms/djangoapps/instructor/tests.py b/lms/djangoapps/instructor/tests.py index 87be93128c..c47eb170fc 100644 --- a/lms/djangoapps/instructor/tests.py +++ b/lms/djangoapps/instructor/tests.py @@ -233,6 +233,14 @@ class TestStaffGradingService(ct.PageLoader): def setUp(self): xmodule.modulestore.django._MODULESTORES = {} + self.student = 'view@test.com' + self.instructor = 'view2@test.com' + self.password = 'foo' + self.create_account('u1', self.student, self.password) + self.create_account('u2', self.instructor, self.password) + self.activate_user(self.student) + self.activate_user(self.instructor) + self.course_id = "edX/toy/2012_Fall" self.toy = modulestore().get_course(self.course_id) def make_instructor(course): @@ -242,6 +250,8 @@ class TestStaffGradingService(ct.PageLoader): make_instructor(self.toy) + self.mock_service = staff_grading_service.grading_service() + self.logout() def test_access(self): @@ -257,7 +267,6 @@ class TestStaffGradingService(ct.PageLoader): self.check_for_post_code(404, url) - @patch.object(staff_grading_service, '_service', _mock_service) def test_get_next(self): self.login(self.instructor, self.password) @@ -266,10 +275,9 @@ class TestStaffGradingService(ct.PageLoader): r = self.check_for_get_code(200, url) d = json.loads(r.content) self.assertTrue(d['success']) - self.assertEquals(d['submission_id'], _mock_service.cnt) + self.assertEquals(d['submission_id'], self.mock_service.cnt) - @patch.object(staff_grading_service, '_service', _mock_service) def test_save_grade(self): self.login(self.instructor, self.password) @@ -281,6 +289,6 @@ class TestStaffGradingService(ct.PageLoader): r = self.check_for_post_code(200, url, data) d = json.loads(r.content) self.assertTrue(d['success'], str(d)) - self.assertEquals(d['submission_id'], _mock_service.cnt) + self.assertEquals(d['submission_id'], self.mock_service.cnt) diff --git a/lms/envs/common.py b/lms/envs/common.py index 3d26cb54c9..79d0bb78f9 100644 --- a/lms/envs/common.py +++ b/lms/envs/common.py @@ -325,6 +325,9 @@ WIKI_LINK_DEFAULT_LEVEL = 2 ################################# Staff grading config ##################### STAFF_GRADING_INTERFACE = None +# Used for testing, debugging +MOCK_STAFF_GRADING = False + ################################# Jasmine ################################### JASMINE_TEST_DIRECTORY = PROJECT_ROOT + '/static/coffee' diff --git a/lms/envs/test.py b/lms/envs/test.py index e815efdf4e..ef2a343db4 100644 --- a/lms/envs/test.py +++ b/lms/envs/test.py @@ -65,6 +65,10 @@ XQUEUE_INTERFACE = { } XQUEUE_WAITTIME_BETWEEN_REQUESTS = 5 # seconds + +# Don't rely on a real staff grading backend +MOCK_STAFF_GRADING = True + # TODO (cpennington): We need to figure out how envs/test.py can inject things # into common.py so that we don't have to repeat this sort of thing STATICFILES_DIRS = [ @@ -99,7 +103,7 @@ DATABASES = { } CACHES = { - # This is the cache used for most things. + # This is the cache used for most things. # In staging/prod envs, the sessions also live here. 'default': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',