Skip to content

Commit

Permalink
refactor: switch from mock to unittest.mock
Browse files Browse the repository at this point in the history
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 paver.in.
  • Loading branch information
kdmccormick committed May 22, 2024
1 parent 69e5fa4 commit 5393b00
Show file tree
Hide file tree
Showing 30 changed files with 49 additions and 73 deletions.
5 changes: 2 additions & 3 deletions cms/djangoapps/contentstore/rest_api/v0/tests/test_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,7 +14,6 @@
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
Expand Down
2 changes: 1 addition & 1 deletion common/test/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
General testing utilities.
"""
from unittest.mock import Mock, patch


import functools
Expand All @@ -9,7 +10,6 @@

from django.dispatch import Signal
from markupsafe import escape
from mock import Mock, patch


@contextmanager
Expand Down
3 changes: 1 addition & 2 deletions lms/djangoapps/course_goals/tests/test_user_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

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
Expand All @@ -12,7 +12,6 @@
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

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -108,7 +107,7 @@ def test_streak_data_in_response(self):
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
Expand Down Expand Up @@ -187,7 +186,7 @@ def test_course_access(
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)
Expand Down
4 changes: 2 additions & 2 deletions lms/djangoapps/discussion/django_comment_client/base/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
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

Expand Down
2 changes: 1 addition & 1 deletion lms/djangoapps/instructor/tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
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
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion lms/djangoapps/mobile_api/tests/test_course_info_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Tests for course_info
"""
from unittest.mock import patch


import ddt
Expand All @@ -11,7 +12,6 @@
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
Expand Down
2 changes: 1 addition & 1 deletion openedx/core/djangoapps/catalog/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion openedx/core/djangoapps/discussions/tests/test_tasks.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
24 changes: 0 additions & 24 deletions openedx/core/djangoapps/theming/tests/test_commands.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion scripts/user_retirement/tests/test_retire_one_learner.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

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 (
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
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 \
Expand Down
2 changes: 1 addition & 1 deletion scripts/user_retirement/tests/utils/test_edx_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion scripts/user_retirement/tests/utils/test_jenkins.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"""
import logging
from contextlib import contextmanager
from unittest import mock

import mock
import pytest
from simple_salesforce import SalesforceError

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion xmodule/capa/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
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
Expand Down
2 changes: 1 addition & 1 deletion xmodule/capa/tests/test_capa_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
"""
import textwrap
import unittest
from unittest.mock import patch, MagicMock

from django.conf import settings
from django.test import override_settings
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
Expand Down
2 changes: 1 addition & 1 deletion xmodule/capa/tests/test_html_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
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
Expand Down
Loading

0 comments on commit 5393b00

Please sign in to comment.