refactor: switch from mock to unittest.mock (#34844)
As of Python 3.3, the 3rd-party `mock` package has been subsumed into the standard `unittest.mock` package. Refactoring tests to use the latter will allow us to drop `mock` as a dependency, which is currently coming in transitively through requirements/edx/paver.in. We don't actually drop the `mock` dependency in this PR. That will happen naturally in: * https://github.com/openedx/edx-platform/pull/34830
This commit is contained in:
@@ -5,12 +5,11 @@ Tests for the xblock view of the CMS API. This tests only the view itself,
|
||||
not the underlying Xblock service.
|
||||
It checks that the assets_handler method of the Xblock service is called with the expected parameters.
|
||||
"""
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
from django.core.files import File
|
||||
from django.http import JsonResponse
|
||||
|
||||
from django.urls import reverse
|
||||
from mock import MagicMock
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase
|
||||
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
"""
|
||||
Unit tests for Contentstore Proctored Exam Settings.
|
||||
"""
|
||||
from unittest.mock import patch
|
||||
|
||||
import ddt
|
||||
from mock import patch
|
||||
from django.conf import settings
|
||||
from django.test.utils import override_settings
|
||||
from django.urls import reverse
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
"""
|
||||
Unit tests for course settings views.
|
||||
"""
|
||||
from unittest.mock import patch
|
||||
|
||||
import ddt
|
||||
from django.conf import settings
|
||||
from django.urls import reverse
|
||||
from mock import patch
|
||||
from rest_framework import status
|
||||
|
||||
from cms.djangoapps.contentstore.tests.utils import CourseTestCase
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
"""
|
||||
Unit tests for course settings views.
|
||||
"""
|
||||
from unittest.mock import patch
|
||||
|
||||
import ddt
|
||||
from django.conf import settings
|
||||
from django.contrib.staticfiles.storage import staticfiles_storage
|
||||
@@ -12,7 +14,6 @@ from edxval.api import (
|
||||
get_transcript_credentials_state_for_org,
|
||||
get_transcript_preferences,
|
||||
)
|
||||
from mock import patch
|
||||
from rest_framework import status
|
||||
|
||||
from cms.djangoapps.contentstore.video_storage_handlers import get_all_transcript_languages
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"""
|
||||
General testing utilities.
|
||||
"""
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
|
||||
import functools
|
||||
@@ -9,7 +10,6 @@ from contextlib import contextmanager
|
||||
|
||||
from django.dispatch import Signal
|
||||
from markupsafe import escape
|
||||
from mock import Mock, patch
|
||||
|
||||
|
||||
@contextmanager
|
||||
|
||||
@@ -3,7 +3,7 @@ Unit tests for user activity methods.
|
||||
"""
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
from unittest.mock import Mock
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import ddt
|
||||
from django.contrib.auth import get_user_model
|
||||
@@ -12,7 +12,6 @@ from django.urls import reverse
|
||||
from edx_django_utils.cache import TieredCache
|
||||
from edx_toggles.toggles.testutils import override_waffle_flag
|
||||
from freezegun import freeze_time
|
||||
from mock import patch
|
||||
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
|
||||
from xmodule.modulestore.tests.factories import CourseFactory, BlockFactory
|
||||
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
"""
|
||||
Tests for the Course Home Course Metadata API in the Course Home API
|
||||
"""
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
|
||||
import ddt
|
||||
import json
|
||||
import mock
|
||||
from django.db import transaction
|
||||
from django.urls import reverse
|
||||
from edx_toggles.toggles.testutils import override_waffle_flag
|
||||
from unittest.mock import patch
|
||||
|
||||
from common.djangoapps.course_modes.models import CourseMode
|
||||
from common.djangoapps.student.models import CourseEnrollment
|
||||
@@ -108,7 +107,7 @@ class CourseHomeMetadataTests(BaseCourseHomeTests):
|
||||
CourseEnrollment.enroll(self.user, self.course.id, 'audit')
|
||||
with override_waffle_flag(COURSEWARE_MFE_MILESTONES_STREAK_DISCOUNT, active=True):
|
||||
UPDATES_METHOD_NAME = 'common.djangoapps.student.models.user.UserCelebration.perform_streak_updates'
|
||||
with mock.patch(UPDATES_METHOD_NAME, return_value=3):
|
||||
with patch(UPDATES_METHOD_NAME, return_value=3):
|
||||
response = self.client.get(self.url, content_type='application/json')
|
||||
celebrations = response.json()['celebrations']
|
||||
assert celebrations['streak_length_to_celebrate'] == 3
|
||||
@@ -187,7 +186,7 @@ class CourseHomeMetadataTests(BaseCourseHomeTests):
|
||||
self.update_masquerade(role=masquerade_role)
|
||||
|
||||
consent_url = 'dump/consent/url' if dsc_required else None
|
||||
with mock.patch('openedx.features.enterprise_support.api.get_enterprise_consent_url', return_value=consent_url):
|
||||
with patch('openedx.features.enterprise_support.api.get_enterprise_consent_url', return_value=consent_url):
|
||||
response = self.client.get(self.url)
|
||||
|
||||
self._assert_course_access_response(response, expect_course_access, error_code)
|
||||
|
||||
@@ -6,15 +6,15 @@ import pytest
|
||||
import json
|
||||
import logging
|
||||
from contextlib import contextmanager
|
||||
from unittest import mock
|
||||
from unittest.mock import ANY, Mock, patch
|
||||
|
||||
import ddt
|
||||
import mock
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.management import call_command
|
||||
from django.test.client import RequestFactory
|
||||
from django.urls import reverse
|
||||
from eventtracking.processors.exceptions import EventEmissionExit
|
||||
from mock import ANY, Mock, patch
|
||||
from opaque_keys.edx.keys import CourseKey
|
||||
from openedx_events.learning.signals import FORUM_THREAD_CREATED, FORUM_THREAD_RESPONSE_CREATED, FORUM_RESPONSE_COMMENT_CREATED
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import datetime
|
||||
import json
|
||||
import unittest
|
||||
from unittest import mock
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
|
||||
@@ -14,7 +15,6 @@ from django.core.exceptions import MultipleObjectsReturned
|
||||
from django.test import TestCase
|
||||
from edx_when.api import get_dates_for_course, set_dates_for_course
|
||||
from edx_when.field_data import DateLookupFieldData
|
||||
from mock.mock import MagicMock, patch
|
||||
from opaque_keys.edx.keys import CourseKey
|
||||
from pytz import UTC
|
||||
from xmodule.fields import Date
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
"""
|
||||
Tests for serializers for the Mobile Course Info
|
||||
"""
|
||||
from unittest.mock import MagicMock, Mock, patch
|
||||
from typing import Dict, List, Tuple, Union
|
||||
|
||||
import ddt
|
||||
from django.test import TestCase
|
||||
from mock import MagicMock, Mock, patch
|
||||
from typing import Dict, List, Tuple, Union
|
||||
|
||||
from common.djangoapps.student.tests.factories import UserFactory
|
||||
from lms.djangoapps.mobile_api.course_info.serializers import (
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"""
|
||||
Tests for course_info
|
||||
"""
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
import ddt
|
||||
@@ -11,7 +12,6 @@ from django.test import RequestFactory
|
||||
from django.urls import reverse
|
||||
from edx_toggles.toggles.testutils import override_waffle_flag
|
||||
from milestones.tests.utils import MilestonesTestCaseMixin
|
||||
from mock import patch
|
||||
from rest_framework import status
|
||||
|
||||
from common.djangoapps.student.tests.factories import UserFactory # pylint: disable=unused-import
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""
|
||||
Tests for the Catalog apps `api.py` functions.
|
||||
"""
|
||||
from mock import patch
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""
|
||||
course_overview api tests
|
||||
"""
|
||||
from mock import patch
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.http.response import Http404
|
||||
from opaque_keys.edx.keys import CourseKey
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
"""
|
||||
Tests for discussions tasks.
|
||||
"""
|
||||
from unittest import mock
|
||||
|
||||
import ddt
|
||||
import mock
|
||||
from edx_toggles.toggles.testutils import override_waffle_flag
|
||||
from openedx_events.learning.data import DiscussionTopicContext
|
||||
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
"""
|
||||
Tests for Management commands of comprehensive theming.
|
||||
"""
|
||||
|
||||
from django.core.management import call_command
|
||||
from django.test import TestCase, override_settings
|
||||
from unittest.mock import patch
|
||||
|
||||
import pavelib.assets
|
||||
|
||||
|
||||
class TestUpdateAssets(TestCase):
|
||||
"""
|
||||
Test comprehensive theming helper functions.
|
||||
"""
|
||||
|
||||
@patch.object(pavelib.assets, 'sh')
|
||||
@override_settings(COMPREHENSIVE_THEME_DIRS='common/test')
|
||||
def test_deprecated_wrapper(self, mock_sh):
|
||||
call_command('compile_sass', '--themes', 'fake-theme1', 'fake-theme2')
|
||||
assert mock_sh.called_once_with(
|
||||
"npm run compile-sass -- " +
|
||||
"--theme-dir common/test --theme fake-theme-1 --theme fake-theme-2"
|
||||
)
|
||||
@@ -1,11 +1,11 @@
|
||||
"""
|
||||
Test the get_learners_to_retire.py script
|
||||
"""
|
||||
from unittest.mock import DEFAULT, patch
|
||||
|
||||
import os
|
||||
|
||||
from click.testing import CliRunner
|
||||
from mock import DEFAULT, patch
|
||||
from requests.exceptions import HTTPError
|
||||
|
||||
from scripts.user_retirement.get_learners_to_retire import get_learners_to_retire
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
"""
|
||||
Test the retire_one_learner.py script
|
||||
"""
|
||||
from unittest.mock import DEFAULT, patch
|
||||
|
||||
from click.testing import CliRunner
|
||||
from mock import DEFAULT, patch
|
||||
|
||||
from scripts.user_retirement.retire_one_learner import (
|
||||
END_STATES,
|
||||
|
||||
@@ -4,12 +4,12 @@ Test the retirement_archive_and_cleanup.py script
|
||||
|
||||
import datetime
|
||||
import os
|
||||
from unittest.mock import DEFAULT, call, patch
|
||||
|
||||
import boto3
|
||||
import pytest
|
||||
from botocore.exceptions import ClientError
|
||||
from click.testing import CliRunner
|
||||
from mock import DEFAULT, call, patch
|
||||
from moto import mock_ec2, mock_s3
|
||||
|
||||
from scripts.user_retirement.retirement_archive_and_cleanup import (
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
"""
|
||||
Test the retirement_bulk_status_update.py script
|
||||
"""
|
||||
from unittest.mock import DEFAULT, patch
|
||||
|
||||
from click.testing import CliRunner
|
||||
from mock import DEFAULT, patch
|
||||
|
||||
from scripts.user_retirement.retirement_bulk_status_update import (
|
||||
ERR_BAD_CONFIG,
|
||||
|
||||
@@ -8,9 +8,9 @@ import os
|
||||
import time
|
||||
import unicodedata
|
||||
from datetime import date
|
||||
from unittest.mock import DEFAULT, patch
|
||||
|
||||
from click.testing import CliRunner
|
||||
from mock import DEFAULT, patch
|
||||
from six import PY2, itervalues
|
||||
|
||||
from scripts.user_retirement.retirement_partner_report import \
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
Tests for edX API calls.
|
||||
"""
|
||||
import unittest
|
||||
from unittest.mock import DEFAULT, patch
|
||||
from urllib.parse import urljoin
|
||||
|
||||
import requests
|
||||
import responses
|
||||
from ddt import data, ddt, unpack
|
||||
from mock import DEFAULT, patch
|
||||
from requests.exceptions import ConnectionError, HTTPError
|
||||
from responses import GET, PATCH, POST, matchers
|
||||
from responses.registries import OrderedRegistry
|
||||
|
||||
@@ -6,11 +6,11 @@ import json
|
||||
import re
|
||||
import unittest
|
||||
from itertools import islice
|
||||
from unittest.mock import Mock, call, mock_open, patch
|
||||
|
||||
import backoff
|
||||
import ddt
|
||||
import requests_mock
|
||||
from mock import Mock, call, mock_open, patch
|
||||
|
||||
import scripts.user_retirement.utils.jenkins as jenkins
|
||||
from scripts.user_retirement.utils.exception import BackendError
|
||||
|
||||
@@ -3,8 +3,8 @@ Tests for the Salesforce API functionality
|
||||
"""
|
||||
import logging
|
||||
from contextlib import contextmanager
|
||||
from unittest import mock
|
||||
|
||||
import mock
|
||||
import pytest
|
||||
from simple_salesforce import SalesforceError
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
Tests for the Segment API functionality
|
||||
"""
|
||||
import json
|
||||
from unittest import mock
|
||||
|
||||
import mock
|
||||
import pytest
|
||||
import requests
|
||||
from six import text_type
|
||||
|
||||
@@ -6,10 +6,10 @@ import io
|
||||
import os
|
||||
import os.path
|
||||
import xml.sax.saxutils as saxutils
|
||||
from unittest.mock import MagicMock, Mock
|
||||
|
||||
import fs.osfs
|
||||
from mako.lookup import TemplateLookup
|
||||
from mock import MagicMock, Mock
|
||||
from path import Path
|
||||
|
||||
from xmodule.capa.capa_problem import LoncapaProblem, LoncapaSystem
|
||||
|
||||
@@ -3,6 +3,7 @@ Test capa problem.
|
||||
"""
|
||||
import textwrap
|
||||
import unittest
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
from django.conf import settings
|
||||
from django.test import override_settings
|
||||
@@ -10,7 +11,6 @@ import pytest
|
||||
import ddt
|
||||
from lxml import etree
|
||||
from markupsafe import Markup
|
||||
from mock import patch, MagicMock
|
||||
|
||||
from xmodule.capa.correctmap import CorrectMap
|
||||
from xmodule.capa.responsetypes import LoncapaProblemError
|
||||
|
||||
@@ -6,9 +6,9 @@ CAPA HTML rendering tests.
|
||||
import os
|
||||
import textwrap
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
import ddt
|
||||
import mock
|
||||
from lxml import etree
|
||||
from xmodule.capa.tests.helpers import new_loncapa_problem, test_capa_system
|
||||
from openedx.core.djangolib.markup import HTML
|
||||
|
||||
@@ -23,12 +23,12 @@ import textwrap
|
||||
import unittest
|
||||
import xml.sax.saxutils as saxutils
|
||||
from collections import OrderedDict
|
||||
from unittest.mock import ANY, patch
|
||||
|
||||
import pytest
|
||||
import six
|
||||
from lxml import etree
|
||||
from lxml.html import fromstring
|
||||
from mock import ANY, patch
|
||||
from pyparsing import ParseException
|
||||
from six.moves import zip
|
||||
|
||||
|
||||
@@ -9,10 +9,10 @@ import textwrap
|
||||
import unittest
|
||||
import zipfile
|
||||
from datetime import datetime
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
import calc
|
||||
import mock
|
||||
import pyparsing
|
||||
import random2 as random
|
||||
import requests
|
||||
|
||||
@@ -6,12 +6,11 @@ Tests of the Capa XModule
|
||||
|
||||
import datetime
|
||||
import json
|
||||
import mock
|
||||
import os
|
||||
import random
|
||||
import textwrap
|
||||
import unittest
|
||||
from unittest.mock import DEFAULT, Mock, patch
|
||||
from unittest.mock import DEFAULT, Mock, PropertyMock, patch
|
||||
|
||||
import pytest
|
||||
import ddt
|
||||
@@ -820,7 +819,7 @@ class ProblemBlockTest(unittest.TestCase): # lint-amnesty, pylint: disable=miss
|
||||
# Disabled grading method
|
||||
with patch(
|
||||
'xmodule.capa_block.ProblemBlock.is_grading_method_enabled',
|
||||
new_callable=mock.PropertyMock,
|
||||
new_callable=PropertyMock,
|
||||
return_value=False
|
||||
):
|
||||
# First Attempt
|
||||
@@ -846,7 +845,7 @@ class ProblemBlockTest(unittest.TestCase): # lint-amnesty, pylint: disable=miss
|
||||
# Enabled grading method
|
||||
with patch(
|
||||
'xmodule.capa_block.ProblemBlock.is_grading_method_enabled',
|
||||
new_callable=mock.PropertyMock,
|
||||
new_callable=PropertyMock,
|
||||
return_value=True
|
||||
):
|
||||
# Third Attempt
|
||||
@@ -887,7 +886,7 @@ class ProblemBlockTest(unittest.TestCase): # lint-amnesty, pylint: disable=miss
|
||||
# Enabled grading method
|
||||
with patch(
|
||||
'xmodule.capa_block.ProblemBlock.is_grading_method_enabled',
|
||||
new_callable=mock.PropertyMock,
|
||||
new_callable=PropertyMock,
|
||||
return_value=True
|
||||
):
|
||||
# First Attempt
|
||||
@@ -913,7 +912,7 @@ class ProblemBlockTest(unittest.TestCase): # lint-amnesty, pylint: disable=miss
|
||||
# Disabled grading method
|
||||
with patch(
|
||||
'xmodule.capa_block.ProblemBlock.is_grading_method_enabled',
|
||||
new_callable=mock.PropertyMock,
|
||||
new_callable=PropertyMock,
|
||||
return_value=False
|
||||
):
|
||||
# Third Attempt
|
||||
@@ -1636,7 +1635,7 @@ class ProblemBlockTest(unittest.TestCase): # lint-amnesty, pylint: disable=miss
|
||||
# Disabled grading method
|
||||
with patch(
|
||||
'xmodule.capa_block.ProblemBlock.is_grading_method_enabled',
|
||||
new_callable=mock.PropertyMock,
|
||||
new_callable=PropertyMock,
|
||||
return_value=False
|
||||
):
|
||||
# Score is the last score
|
||||
@@ -1652,12 +1651,12 @@ class ProblemBlockTest(unittest.TestCase): # lint-amnesty, pylint: disable=miss
|
||||
# Enabled grading method
|
||||
with patch(
|
||||
'xmodule.capa_block.ProblemBlock.is_grading_method_enabled',
|
||||
new_callable=mock.PropertyMock,
|
||||
new_callable=PropertyMock,
|
||||
return_value=True
|
||||
):
|
||||
with patch(
|
||||
'xmodule.capa.capa_problem.LoncapaProblem.is_grading_method_enabled',
|
||||
new_callable=mock.PropertyMock,
|
||||
new_callable=PropertyMock,
|
||||
return_value=True
|
||||
):
|
||||
# Change grading method to 'first_score'
|
||||
@@ -1706,12 +1705,12 @@ class ProblemBlockTest(unittest.TestCase): # lint-amnesty, pylint: disable=miss
|
||||
# Enabled grading method
|
||||
with patch(
|
||||
'xmodule.capa_block.ProblemBlock.is_grading_method_enabled',
|
||||
new_callable=mock.PropertyMock,
|
||||
new_callable=PropertyMock,
|
||||
return_value=True
|
||||
):
|
||||
with patch(
|
||||
'xmodule.capa.capa_problem.LoncapaProblem.is_grading_method_enabled',
|
||||
new_callable=mock.PropertyMock,
|
||||
new_callable=PropertyMock,
|
||||
return_value=True
|
||||
):
|
||||
# Grading method is 'last_score'
|
||||
@@ -1745,7 +1744,7 @@ class ProblemBlockTest(unittest.TestCase): # lint-amnesty, pylint: disable=miss
|
||||
# Disabled grading method
|
||||
with patch(
|
||||
'xmodule.capa_block.ProblemBlock.is_grading_method_enabled',
|
||||
new_callable=mock.PropertyMock,
|
||||
new_callable=PropertyMock,
|
||||
return_value=False
|
||||
):
|
||||
block.rescore(only_if_higher=False)
|
||||
|
||||
Reference in New Issue
Block a user