feat: handle exam submission and reset (#33323)

handles exam events from event bus that impact the 'instructor' app. These apis deal with learner completion and managing problem attempt state.
This commit is contained in:
Zachary Hancock
2023-10-06 09:10:13 -04:00
committed by GitHub
parent 46776e92e2
commit 9f16b0f8f6
6 changed files with 520 additions and 140 deletions

View File

@@ -5,9 +5,7 @@ import json
from unittest import mock
import pytest
from completion.waffle import ENABLE_COMPLETION_TRACKING_SWITCH
from django.core.exceptions import ObjectDoesNotExist
from edx_toggles.toggles.testutils import override_waffle_switch
from opaque_keys import InvalidKeyError
from common.djangoapps.student.models import CourseEnrollment
@@ -15,9 +13,8 @@ from common.djangoapps.student.tests.factories import UserFactory
from lms.djangoapps.courseware.models import StudentModule
from lms.djangoapps.instructor.access import allow_access
from lms.djangoapps.instructor.services import InstructorService
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase # lint-amnesty, pylint: disable=wrong-import-order
from xmodule.modulestore.tests.factories import CourseFactory, BlockFactory # lint-amnesty, pylint: disable=wrong-import-order
from xmodule.partitions.partitions import Group, UserPartition # lint-amnesty, pylint: disable=wrong-import-order
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
from xmodule.modulestore.tests.factories import BlockFactory, CourseFactory
class InstructorServiceTests(SharedModuleStoreTestCase):
@@ -54,8 +51,8 @@ class InstructorServiceTests(SharedModuleStoreTestCase):
)
@mock.patch('lms.djangoapps.grades.signals.handlers.PROBLEM_WEIGHTED_SCORE_CHANGED.send')
@mock.patch('completion.handlers.BlockCompletion.objects.submit_completion')
def test_reset_student_attempts_delete(self, mock_submit, _mock_signal):
@mock.patch('lms.djangoapps.instructor.tasks.update_exam_completion_task.apply_async', autospec=True)
def test_reset_student_attempts_delete(self, mock_completion_task, _mock_signal):
"""
Test delete student state.
"""
@@ -64,22 +61,19 @@ class InstructorServiceTests(SharedModuleStoreTestCase):
assert StudentModule.objects.filter(student=self.module_to_reset.student, course_id=self.course.id,
module_state_key=self.module_to_reset.module_state_key).count() == 1
with override_waffle_switch(ENABLE_COMPLETION_TRACKING_SWITCH, True):
self.service.delete_student_attempt(
self.student.username,
str(self.course.id),
str(self.subsection.location),
requesting_user=self.student,
)
self.service.delete_student_attempt(
self.student.username,
str(self.course.id),
str(self.subsection.location),
requesting_user=self.student,
)
# make sure the module has been deleted
assert StudentModule.objects.filter(student=self.module_to_reset.student, course_id=self.course.id,
module_state_key=self.module_to_reset.module_state_key).count() == 0
# Assert we send completion == 0.0 for both problems even though the second problem was never viewed
assert mock_submit.call_count == 2
mock_submit.assert_any_call(user=self.student, block_key=self.problem.location, completion=0.0)
mock_submit.assert_any_call(user=self.student, block_key=self.problem_2.location, completion=0.0)
# Assert we send update completion with 0.0
mock_completion_task.assert_called_once_with((self.student.username, str(self.subsection.location), 0.0))
def test_reset_bad_content_id(self):
"""
@@ -120,128 +114,13 @@ class InstructorServiceTests(SharedModuleStoreTestCase):
)
assert result is None
@mock.patch('completion.handlers.BlockCompletion.objects.submit_completion')
def test_complete_student_attempt_success(self, mock_submit):
@mock.patch('lms.djangoapps.instructor.tasks.update_exam_completion_task.apply_async', autospec=True)
def test_complete_student_attempt_success(self, mock_completion_task):
"""
Assert complete_student_attempt correctly publishes completion for all
completable children of the given content_id
Assert update_exam_completion task is triggered
"""
# Section, subsection, and unit are all aggregators and not completable so should
# not be submitted.
section = BlockFactory.create(parent=self.course, category='chapter')
subsection = BlockFactory.create(parent=section, category='sequential')
unit = BlockFactory.create(parent=subsection, category='vertical')
# should both be submitted
video = BlockFactory.create(parent=unit, category='video')
problem = BlockFactory.create(parent=unit, category='problem')
# Not a completable block
BlockFactory.create(parent=unit, category='discussion')
with override_waffle_switch(ENABLE_COMPLETION_TRACKING_SWITCH, True):
self.service.complete_student_attempt(self.student.username, str(subsection.location))
# Only Completable leaf blocks should have completion published
assert mock_submit.call_count == 2
mock_submit.assert_any_call(user=self.student, block_key=video.location, completion=1.0)
mock_submit.assert_any_call(user=self.student, block_key=problem.location, completion=1.0)
@mock.patch('completion.handlers.BlockCompletion.objects.submit_completion')
def test_complete_student_attempt_split_test(self, mock_submit):
"""
Asserts complete_student_attempt correctly publishes completion when a split test is involved
This test case exists because we ran into a bug about the user_service not existing
when a split_test existed inside of a subsection. Associated with this change was adding
in the user state into the module before attempting completion and this ensures that is
working properly.
"""
partition = UserPartition(
0,
'first_partition',
'First Partition',
[
Group(0, 'alpha'),
Group(1, 'beta')
]
)
course = CourseFactory.create(user_partitions=[partition])
section = BlockFactory.create(parent=course, category='chapter')
subsection = BlockFactory.create(parent=section, category='sequential')
c0_url = course.id.make_usage_key('vertical', 'split_test_cond0')
c1_url = course.id.make_usage_key('vertical', 'split_test_cond1')
split_test = BlockFactory.create(
parent=subsection,
category='split_test',
user_partition_id=0,
group_id_to_child={'0': c0_url, '1': c1_url},
)
cond0vert = BlockFactory.create(parent=split_test, category='vertical', location=c0_url)
BlockFactory.create(parent=cond0vert, category='video')
BlockFactory.create(parent=cond0vert, category='problem')
cond1vert = BlockFactory.create(parent=split_test, category='vertical', location=c1_url)
BlockFactory.create(parent=cond1vert, category='video')
BlockFactory.create(parent=cond1vert, category='html')
with override_waffle_switch(ENABLE_COMPLETION_TRACKING_SWITCH, True):
self.service.complete_student_attempt(self.student.username, str(subsection.location))
# Only the group the user was assigned to should have completion published.
# Either cond0vert's children or cond1vert's children
assert mock_submit.call_count == 2
@mock.patch('lms.djangoapps.instructor.tasks.log.error')
def test_complete_student_attempt_bad_user(self, mock_logger):
"""
Assert complete_student_attempt with a bad user raises error and returns None
"""
username = 'bad_user'
block_id = str(self.problem.location)
self.service.complete_student_attempt(username, block_id)
mock_logger.assert_called_once_with(
self.complete_error_prefix.format(user=username, content_id=block_id) + 'User does not exist!'
)
@mock.patch('lms.djangoapps.instructor.tasks.log.error')
def test_complete_student_attempt_bad_content_id(self, mock_logger):
"""
Assert complete_student_attempt with a bad content_id raises error and returns None
"""
username = self.student.username
self.service.complete_student_attempt(username, 'foo/bar/baz')
mock_logger.assert_called_once_with(
self.complete_error_prefix.format(user=username, content_id='foo/bar/baz') + 'Invalid content_id!'
)
@mock.patch('lms.djangoapps.instructor.tasks.log.error')
def test_complete_student_attempt_nonexisting_item(self, mock_logger):
"""
Assert complete_student_attempt with nonexisting item in the modulestore
raises error and returns None
"""
username = self.student.username
block = 'i4x://org.0/course_0/problem/fake_problem'
self.service.complete_student_attempt(username, block)
mock_logger.assert_called_once_with(
self.complete_error_prefix.format(user=username, content_id=block) + 'Block not found in the modulestore!'
)
@mock.patch('lms.djangoapps.instructor.tasks.log.error')
def test_complete_student_attempt_failed_module(self, mock_logger):
"""
Assert complete_student_attempt with failed get_block raises error and returns None
"""
username = self.student.username
with mock.patch('lms.djangoapps.instructor.tasks.get_block_for_descriptor', return_value=None):
self.service.complete_student_attempt(username, str(self.course.location))
mock_logger.assert_called_once_with(
self.complete_error_prefix.format(user=username, content_id=self.course.location) +
'Block unable to be created from descriptor!'
)
self.service.complete_student_attempt(self.student.username, str(self.subsection.location))
mock_completion_task.assert_called_once_with((self.student.username, str(self.subsection.location), 1.0))
def test_is_user_staff(self):
"""