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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user