Skip to content

Commit

Permalink
test: update unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanttV committed Mar 8, 2024
1 parent 83da6c5 commit c8c2dd9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions openassessment/runtime_imports/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def import_waffle_flag():
def import_student_module():
"""
Helper method that imports StudentModule from edx-platform at runtime.
https://github.com/openedx/edx-platform/blob/master/lms/djangoapps/courseware/models.py#L79
"""
from lms.djangoapps.courseware.models import StudentModule
return StudentModule
7 changes: 5 additions & 2 deletions openassessment/xblock/test/test_allow_resubmission.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ class ConfigDataMock:

def __init__(self):
self.allow_learner_resubmissions = True
self.submission_due = "2029-01-01T00:00:00+00:00"
self.resubmissions_grace_period_hours = 0
self.resubmissions_grace_period_minutes = 0

def is_closed(self, step: str): # pylint: disable=unused-argument
return (False, None, None, None)


class WorkflowDataMock:
"""Mock class for the WorkflowAPI object."""
Expand Down Expand Up @@ -104,6 +106,7 @@ def test_allow_resubmission_resubmissions_not_allowed(self):
Test case for the allow_resubmission function when learner resubmissions are not allowed.
"""
self.config_data.allow_learner_resubmissions = False

result = allow_resubmission(
self.config_data, self.workflow_data, self.submission_data
)
Expand All @@ -114,7 +117,7 @@ def test_allow_resubmission_submission_date_exceeded(self):
"""
Test case for the allow_resubmission function when the submission date has been exceeded.
"""
self.config_data.submission_due = "2020-01-01T00:00:00+00:00"
self.config_data.is_closed = lambda step: (True, "due", None, None)

result = allow_resubmission(
self.config_data, self.workflow_data, self.submission_data
Expand Down
37 changes: 34 additions & 3 deletions openassessment/xblock/test/test_submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,54 @@ def test_cannot_submit_in_preview_mode(self, xblock):
self.assertEqual(resp[1], "ENOPREVIEW")
self.assertIsNot(resp[2], None)

@patch("openassessment.xblock.openassessmentblock.import_student_module")
@patch("openassessment.xblock.openassessmentblock.reset_student_attempts")
@patch("openassessment.xblock.openassessmentblock.get_user_by_username_or_email")
@scenario("data/basic_scenario.xml", user_id="Bob")
def test_reset_submission(self, xblock, mock_user: Mock, mock_reset: Mock):
def test_reset_submission(
self, xblock, mock_user: Mock, mock_reset: Mock, mock_student_module: Mock
):
xblock.xmodule_runtime = Mock(course_id=COURSE_ID)
mock_user.return_value = "test-user"
mock_reset.return_value = True
mock_student_module.return_value = "test-student-module"

resp = self.request(xblock, "reset_submission", json.dumps({}), response_format="json")

self.assertTrue(resp["success"])
self.assertEqual(resp["msg"], "Submission reset successfully.")

@patch("openassessment.xblock.openassessmentblock.import_student_module")
@patch("openassessment.xblock.openassessmentblock.get_user_by_username_or_email")
@scenario("data/basic_scenario.xml", user_id="Bob")
def test_reset_submission_error(self, xblock):
def test_reset_submission_user_not_found_error(
self, xblock, mock_user: Mock, mock_student_module: Mock
):
mock_student_module.return_value = "test-student-module"
mock_user.side_effect = get_user_model().DoesNotExist

resp = self.request(xblock, "reset_submission", json.dumps({}), response_format="json")
self.assertFalse(resp["success"])
self.assertEqual(resp["msg"], "Error resetting submission.")
self.assertEqual(resp["msg"], "The user does not exist.")

@patch("openassessment.xblock.openassessmentblock.import_student_module")
@patch("openassessment.xblock.openassessmentblock.reset_student_attempts")
@patch("openassessment.xblock.openassessmentblock.get_user_by_username_or_email")
@scenario("data/basic_scenario.xml", user_id="Bob")
def test_reset_submission_submission_not_found_error(
self, xblock, mock_user: Mock, mock_reset: Mock, mock_student_module: Mock
):
xblock.xmodule_runtime = Mock(course_id=COURSE_ID)
mock_user.side_effect = "test-user"
error_mock = Mock()
error_mock.DoesNotExist = ObjectDoesNotExist
mock_student_module.return_value = error_mock
mock_reset.side_effect = ObjectDoesNotExist

resp = self.request(xblock, "reset_submission", json.dumps({}), response_format="json")

self.assertFalse(resp["success"])
self.assertEqual(resp["msg"], "There is no submission to reset.")

@scenario('data/over_grade_scenario.xml', user_id='Alice')
def test_closed_submissions(self, xblock):
Expand Down

0 comments on commit c8c2dd9

Please sign in to comment.