-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
2u/optimizer test happy path #36122
2u/optimizer test happy path #36122
Changes from all commits
542d352
f078b56
51d82e3
83da44e
e94635a
5fb3921
35b519d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,16 @@ | |
|
||
import copy | ||
import json | ||
import os | ||
from tempfile import NamedTemporaryFile | ||
from unittest import mock, TestCase | ||
from uuid import uuid4 | ||
|
||
import pytest as pytest | ||
from django.conf import settings | ||
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user | ||
from django.test.utils import override_settings | ||
from django.core.files import File | ||
from edx_toggles.toggles.testutils import override_waffle_flag | ||
from opaque_keys.edx.locator import CourseLocator | ||
from organizations.models import OrganizationCourse | ||
|
@@ -22,7 +25,8 @@ | |
export_olx, | ||
update_special_exams_and_publish, | ||
rerun_course, | ||
_convert_to_standard_url | ||
_convert_to_standard_url, | ||
_check_broken_links | ||
) | ||
from cms.djangoapps.contentstore.tests.test_libraries import LibraryTestCase | ||
from cms.djangoapps.contentstore.tests.utils import CourseTestCase | ||
|
@@ -31,7 +35,8 @@ | |
from openedx.core.djangoapps.course_apps.toggles import EXAMS_IDA | ||
from openedx.core.djangoapps.embargo.models import Country, CountryAccessRule, RestrictedCourse | ||
from xmodule.modulestore.django import modulestore # lint-amnesty, pylint: disable=wrong-import-order | ||
from xmodule.modulestore.tests.django_utils import TEST_DATA_SPLIT_MODULESTORE | ||
from xmodule.modulestore.tests.django_utils import TEST_DATA_SPLIT_MODULESTORE, ModuleStoreTestCase | ||
from celery import Task | ||
|
||
TEST_DATA_CONTENTSTORE = copy.deepcopy(settings.CONTENTSTORE) | ||
TEST_DATA_CONTENTSTORE['DOC_STORE_CONFIG']['db'] = 'test_xcontent_%s' % uuid4().hex | ||
|
@@ -207,7 +212,61 @@ def test_register_exams_failure(self, _mock_register_exams_proctoring, _mock_reg | |
course_publish.assert_called() | ||
|
||
|
||
class CourseOptimizerTestCase(TestCase): | ||
class MockCourseLinkCheckTask(Task): | ||
def __init__(self): | ||
self.status = mock.Mock() | ||
|
||
|
||
class CheckBrokenLinksTaskTest(ModuleStoreTestCase): | ||
def setUp(self): | ||
super().setUp() | ||
self.mock_urls = [ | ||
["block-v1:edX+DemoX+Demo_Course+type@vertical+block@1", "http://example.com/valid"], | ||
["block-v1:edX+DemoX+Demo_Course+type@vertical+block@2", "http://example.com/invalid"] | ||
] | ||
self.expected_file_contents = [ | ||
['block-v1:edX+DemoX+Demo_Course+type@vertical+block@1', 'http://example.com/valid', False], | ||
['block-v1:edX+DemoX+Demo_Course+type@vertical+block@2', 'http://example.com/invalid', False] | ||
] | ||
|
||
@mock.patch('cms.djangoapps.contentstore.tasks.UserTaskArtifact', autospec=True) | ||
@mock.patch('cms.djangoapps.contentstore.tasks.UserTaskStatus', autospec=True) | ||
@mock.patch('cms.djangoapps.contentstore.tasks._scan_course_for_links') | ||
@mock.patch('cms.djangoapps.contentstore.tasks._save_broken_links_file', autospec=True) | ||
@mock.patch('cms.djangoapps.contentstore.tasks._write_broken_links_to_file', autospec=True) | ||
def test_check_broken_links_stores_broken_and_locked_urls( | ||
self, | ||
mock_write_broken_links_to_file, | ||
mock_save_broken_links_file, | ||
mock_scan_course_for_links, | ||
_mock_user_task_status, | ||
mock_user_task_artifact | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't these inputs match the mock.patch function names as closely as possible? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right, that's probably cleaner. I think they all match except for |
||
): | ||
''' | ||
The test should verify that the check_broken_links task correctly | ||
identifies and stores broken or locked URLs in the course. | ||
The expected behavior is that the task scans the course, | ||
validates the URLs, filters the results, and stores them in a | ||
JSON file. | ||
''' | ||
# Arrange | ||
mock_user = UserFactory.create(username='student', password='password') | ||
mock_course_key_string = "course-v1:edX+DemoX+Demo_Course" | ||
mock_task = MockCourseLinkCheckTask() | ||
mock_scan_course_for_links.return_value = self.mock_urls | ||
|
||
# Act | ||
_check_broken_links(mock_task, mock_user.id, mock_course_key_string, 'en') # pylint: disable=no-value-for-parameter | ||
|
||
# Assert | ||
### Check that UserTaskArtifact was called with the correct arguments | ||
mock_user_task_artifact.assert_called_once_with(status=mock.ANY, name='BrokenLinks') | ||
|
||
### Check that the correct links are written to the file | ||
mock_write_broken_links_to_file.assert_called_once_with(self.expected_file_contents, mock.ANY) | ||
|
||
### Check that _save_broken_links_file was called with the correct arguments | ||
mock_save_broken_links_file.assert_called_once_with(mock_user_task_artifact.return_value, mock.ANY) | ||
|
||
|
||
def test_user_does_not_exist_raises_exception(self): | ||
|
@@ -265,4 +324,4 @@ def test_max_number_of_retries_is_respected(self): | |
raise NotImplementedError | ||
|
||
def test_scan_generates_file_named_by_course_key(self): | ||
raise NotImplementedErro | ||
raise NotImplementedError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the point of this function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of the decorator, I was not getting anywhere with mocking. It is difficult to test an actual celery task directly.
Then I realized that the decorated function is just there to kick off the task. Often it is cleaner anyway to separate the actual business logic that happens inside the task from it.
Introducing a function that handles the logic (
_check_broken_links
) allows me to call that underscored function instead, and makes it easy to test it. I don't need to test the actual task, just what it does.