From c1d4d730e0a2298ad62aa4dce390efe65955dd80 Mon Sep 17 00:00:00 2001 From: Nimisha Asthagiri Date: Sat, 10 Sep 2016 13:28:19 -0400 Subject: [PATCH] Fix submissions API test - now that calculated grades are saved --- .../tests/test_submitting_problems.py | 42 ++++++++++++++----- lms/djangoapps/grades/signals/handlers.py | 4 -- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/lms/djangoapps/courseware/tests/test_submitting_problems.py b/lms/djangoapps/courseware/tests/test_submitting_problems.py index 78296d0312..9262e1b6d4 100644 --- a/lms/djangoapps/courseware/tests/test_submitting_problems.py +++ b/lms/djangoapps/courseware/tests/test_submitting_problems.py @@ -25,6 +25,7 @@ from courseware.models import StudentModule, BaseStudentModuleHistory from courseware.tests.helpers import LoginEnrollmentTestCase from lms.djangoapps.lms_xblock.runtime import quote_slashes from student.models import anonymous_id_for_user, CourseEnrollment +from submissions import api as submissions_api from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory from xmodule.partitions.partitions import Group, UserPartition @@ -143,9 +144,22 @@ class TestSubmittingProblems(ModuleStoreTestCase, LoginEnrollmentTestCase, Probl self.student_user = User.objects.get(email=self.student) self.factory = RequestFactory() # Disable the score change signal to prevent other components from being pulled into tests. - signal_patch = patch('courseware.module_render.SCORE_CHANGED.send') - signal_patch.start() - self.addCleanup(signal_patch.stop) + self.score_changed_signal_patch = patch('courseware.module_render.SCORE_CHANGED.send') + self.score_changed_signal_patch.start() + + def tearDown(self): + super(TestSubmittingProblems, self).tearDown() + self._stop_signal_patch() + + def _stop_signal_patch(self): + """ + Stops the signal patch for the SCORE_CHANGED event. + In case a test wants to test with the event actually + firing. + """ + if self.score_changed_signal_patch: + self.score_changed_signal_patch.stop() + self.score_changed_signal_patch = None def add_dropdown_to_section(self, section_location, name, num_inputs=2): """ @@ -540,14 +554,20 @@ class TestCourseGrader(TestSubmittingProblems): self.check_grade_percent(0.67) self.assertEqual(self.get_grade_summary()['grade'], 'B') - # But now we mock out a get_scores call, and watch as it overrides the - # score read from StudentModule and our student gets an A instead. - with patch('submissions.api.get_scores') as mock_get_scores: - mock_get_scores.return_value = { - self.problem_location('p3').to_deprecated_string(): (1, 1) - } - self.check_grade_percent(1.0) - self.assertEqual(self.get_grade_summary()['grade'], 'A') + # But now, set the score with the submissions API and watch + # as it overrides the score read from StudentModule and our + # student gets an A instead. + self._stop_signal_patch() + student_item = { + 'student_id': anonymous_id_for_user(self.student_user, self.course.id), + 'course_id': unicode(self.course.id), + 'item_id': unicode(self.problem_location('p3')), + 'item_type': 'problem' + } + submission = submissions_api.create_submission(student_item, 'any answer') + submissions_api.set_score(submission['uuid'], 1, 1) + self.check_grade_percent(1.0) + self.assertEqual(self.get_grade_summary()['grade'], 'A') def test_submissions_api_anonymous_student_id(self): """ diff --git a/lms/djangoapps/grades/signals/handlers.py b/lms/djangoapps/grades/signals/handlers.py index 64073e88ff..905d62810b 100644 --- a/lms/djangoapps/grades/signals/handlers.py +++ b/lms/djangoapps/grades/signals/handlers.py @@ -43,8 +43,6 @@ def submissions_score_set_handler(sender, **kwargs): # pylint: disable=unused-a usage_id = kwargs['item_id'] user = user_by_anonymous_id(kwargs['anonymous_user_id']) - # If any of the kwargs were missing, at least one of the following values - # will be None. SCORE_CHANGED.send( sender=None, points_possible=points_possible, @@ -73,8 +71,6 @@ def submissions_score_reset_handler(sender, **kwargs): # pylint: disable=unused usage_id = kwargs['item_id'] user = user_by_anonymous_id(kwargs['anonymous_user_id']) - # If any of the kwargs were missing, at least one of the following values - # will be None. SCORE_CHANGED.send( sender=None, points_possible=0,