From 2bfbd57ec18cd1a1c47f2c75d699bcba59b0a7d6 Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Wed, 29 Apr 2015 11:39:48 -0400 Subject: [PATCH] Add a test case for an XBlock that has no student state fields, but sets a score --- .../courseware/tests/test_module_render.py | 51 ++++++++++++++++++- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/lms/djangoapps/courseware/tests/test_module_render.py b/lms/djangoapps/courseware/tests/test_module_render.py index bdac1694d6..fff12aae4d 100644 --- a/lms/djangoapps/courseware/tests/test_module_render.py +++ b/lms/djangoapps/courseware/tests/test_module_render.py @@ -78,6 +78,27 @@ class EmptyXModuleDescriptor(XModuleDescriptor): # pylint: disable=abstract-met module_class = EmptyXModule +class GradedStatelessXBlock(XBlock): + """ + This XBlock exists to test grade storage for blocks that don't store + student state in a scoped field. + """ + + @XBlock.json_handler + def set_score(self, json_data, suffix): # pylint: disable=unused-argument + """ + Set the score for this testing XBlock. + """ + self.runtime.publish( + self, + 'grade', + { + 'value': json_data['grade'], + 'max_value': 1 + } + ) + + @attr('shard_1') @ddt.ddt class ModuleRenderTestCase(ModuleStoreTestCase, LoginEnrollmentTestCase): @@ -335,8 +356,7 @@ class TestHandleXBlockCallback(ModuleStoreTestCase, LoginEnrollmentTestCase): self.course_key = self.create_toy_course() self.location = self.course_key.make_usage_key('chapter', 'Overview') self.toy_course = modulestore().get_course(self.course_key) - self.mock_user = UserFactory() - self.mock_user.id = 1 + self.mock_user = UserFactory.create() self.request_factory = RequestFactory() # Construct a mock module for the modulestore to return @@ -476,6 +496,33 @@ class TestHandleXBlockCallback(ModuleStoreTestCase, LoginEnrollmentTestCase): 'bad_dispatch', ) + @XBlock.register_temp_plugin(GradedStatelessXBlock, identifier='stateless_scorer') + def test_score_without_student_state(self): + course = CourseFactory.create() + block = ItemFactory.create(category='stateless_scorer', parent=course) + + request = self.request_factory.post( + 'dummy_url', + data=json.dumps({"grade": 0.75}), + content_type='application/json' + ) + request.user = self.mock_user + + response = render.handle_xblock_callback( + request, + unicode(course.id), + quote_slashes(unicode(block.scope_ids.usage_id)), + 'set_score', + '', + ) + self.assertEquals(response.status_code, 200) + student_module = StudentModule.objects.get( + student=self.mock_user, + module_state_key=block.scope_ids.usage_id, + ) + self.assertEquals(student_module.grade, 0.75) + self.assertEquals(student_module.max_grade, 1) + @patch.dict('django.conf.settings.FEATURES', {'ENABLE_XBLOCK_VIEW_ENDPOINT': True}) def test_xblock_view_handler(self): args = [