Skip to content

Commit

Permalink
test: update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanttV committed May 9, 2024
1 parent 5c8b0c8 commit 8eac737
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 50 deletions.
35 changes: 10 additions & 25 deletions platform_plugin_turnitin/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from unittest.mock import Mock, patch

from django.test.utils import override_settings
from opaque_keys.edx.keys import UsageKey

from platform_plugin_turnitin.extensions.filters import ORASubmissionViewTurnitinWarning

Expand All @@ -18,30 +17,23 @@ def setUp(self) -> None:
self.template_name = "template_name"
self.new_template_name = "turnitin/oa_response.html"

@patch("platform_plugin_turnitin.extensions.filters.modulestore")
@patch.object(UsageKey, "from_string")
def test_run_filter_turnitin_submission_disabled(self, mock_from_string: Mock, mock_modulestore: Mock):
@patch("platform_plugin_turnitin.extensions.filters.enabled_in_course")
def test_run_filter_turnitin_submission_disabled(self, mock_enabled_in_course: Mock):
"""
Test `run_filter` method when Turnitin submission is disabled.
Expected result: The dictionary contains the same context and template name.
"""
mock_from_string.return_value.course_key = "test_course_key"
mock_modulestore.return_value.get_course.return_value = Mock(
other_course_settings={"ENABLE_TURNITIN_SUBMISSION": False},
)
mock_enabled_in_course.return_value = False

result = self.pipeline_step.run_filter(self.context, self.template_name)

self.assertEqual(result["context"], self.context)
self.assertEqual(result["template_name"], self.template_name)

@patch("platform_plugin_turnitin.extensions.filters.modulestore")
@patch.object(UsageKey, "from_string")
@patch("platform_plugin_turnitin.extensions.filters.enabled_in_course")
@override_settings(ENABLE_TURNITIN_SUBMISSION=True)
def test_run_filter_turnitin_submission_enabled_by_global_setting(
self, mock_from_string: Mock, mock_modulestore: Mock
):
def test_run_filter_turnitin_submission_enabled_by_global_setting(self, mock_enabled_in_course: Mock):
"""
Test `run_filter` method when Turnitin submission is enabled by the global setting.
Expand All @@ -51,23 +43,16 @@ def test_run_filter_turnitin_submission_enabled_by_global_setting(

self.assertEqual(result["context"], self.context)
self.assertEqual(result["template_name"], self.new_template_name)
mock_from_string.assert_not_called()
mock_modulestore.assert_not_called()

@patch("platform_plugin_turnitin.extensions.filters.modulestore")
@patch.object(UsageKey, "from_string")
def test_run_filter_turnitin_submission_enabled_by_course_setting(
self, mock_from_string: Mock, mock_modulestore: Mock
):
mock_enabled_in_course.assert_not_called()

@patch("platform_plugin_turnitin.extensions.filters.enabled_in_course")
def test_run_filter_turnitin_submission_enabled_by_course_setting(self, mock_enabled_in_course: Mock):
"""
Test `run_filter` method when Turnitin submission is enabled by the course setting.
Expected result: The dictionary contains the context and the new template name.
"""
mock_from_string.return_value.course_key = "test_course_key"
mock_modulestore.return_value.get_course.return_value = Mock(
other_course_settings={"ENABLE_TURNITIN_SUBMISSION": True},
)
mock_enabled_in_course.return_value = True

result = self.pipeline_step.run_filter(self.context, self.template_name)

Expand Down
55 changes: 30 additions & 25 deletions platform_plugin_turnitin/tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,49 @@ class TestHandlers(TestCase):
"""Tests for the handlers module."""

def setUp(self) -> None:
self.submission = Mock(location="test_block_id")

@patch("platform_plugin_turnitin.handlers.call_ora_submission_created_task")
@patch("platform_plugin_turnitin.handlers.modulestore")
@patch("platform_plugin_turnitin.handlers.UsageKey")
def test_ora_submission_created_all_disabled(
self, mock_usage_key: Mock, mock_modulestore: Mock, mock_call_task: Mock
):
"""Test `ora_submission_created` when Turnitin submission is disabled globally and for the course."""
mock_usage_key.from_string.return_value.course_key = "course_key"
mock_modulestore.return_value.get_course.return_value = Mock(
other_course_settings={"ENABLE_TURNITIN_SUBMISSION": False},
self.submission = Mock(
uuid="submission_uuid",
location="block_id",
anonymous_user_id="user_id",
answer=Mock(parts=[], file_names=[], file_urls=[]),
)

@patch("platform_plugin_turnitin.handlers.ora_submission_created_task.delay")
@patch("platform_plugin_turnitin.handlers.enabled_in_course")
def test_ora_submission_created_all_disabled(self, mock_enabled_in_course: Mock, mock_call_task: Mock):
"""Test `ora_submission_created` when Turnitin submission is disabled globally and for the course."""
mock_enabled_in_course.return_value = False

ora_submission_created(self.submission)

mock_call_task.assert_not_called()

@override_settings(ENABLE_TURNITIN_SUBMISSION=True)
@patch("platform_plugin_turnitin.handlers.call_ora_submission_created_task")
@patch("platform_plugin_turnitin.handlers.ora_submission_created_task.delay")
def test_ora_submission_created_global_enabled(self, mock_call_task: Mock):
"""Test `ora_submission_created` when Turnitin submission is enabled globally."""
ora_submission_created(self.submission)

mock_call_task.assert_called_once_with(self.submission)
mock_call_task.assert_called_once_with(
self.submission.uuid,
self.submission.anonymous_user_id,
self.submission.answer.parts,
self.submission.answer.file_names,
self.submission.answer.file_urls,
)

@patch("platform_plugin_turnitin.handlers.call_ora_submission_created_task")
@patch("platform_plugin_turnitin.handlers.modulestore")
@patch("platform_plugin_turnitin.handlers.UsageKey")
def test_ora_submission_created_course_enabled(
self, mock_usage_key: Mock, mock_modulestore: Mock, mock_call_task: Mock
):
@patch("platform_plugin_turnitin.handlers.ora_submission_created_task.delay")
@patch("platform_plugin_turnitin.handlers.enabled_in_course")
def test_ora_submission_created_course_enabled(self, mock_enabled_in_course: Mock, mock_call_task: Mock):
"""Test `ora_submission_created` when Turnitin submission is enabled for the course."""
mock_usage_key.from_string.return_value.course_key = "course_key"
mock_modulestore.return_value.get_course.return_value = Mock(
other_course_settings={"ENABLE_TURNITIN_SUBMISSION": True},
)
mock_enabled_in_course.return_value = True

ora_submission_created(self.submission)

mock_call_task.assert_called_once_with(self.submission)
mock_call_task.assert_called_once_with(
self.submission.uuid,
self.submission.anonymous_user_id,
self.submission.answer.parts,
self.submission.answer.file_names,
self.submission.answer.file_urls,
)

0 comments on commit 8eac737

Please sign in to comment.