Py3: use unittest.mock instead of mock (#3481)

* Py3: use unittest.mock instead of mock

* noqa

* oops

* just pull in patch()

* ignore RuntimeError when stopping patch

* ignore RuntimeError from default_session_mock.stop()
This commit is contained in:
David Baumgold 2021-04-06 11:22:42 +02:00 committed by GitHub
parent 9fa7613c4d
commit 3af87963d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 36 additions and 15 deletions

View File

@ -8,3 +8,9 @@ try:
import collections.abc as collections_abc # noqa
except ImportError:
import collections as collections_abc # noqa
try:
from unittest.mock import patch # noqa
except ImportError:
# for python 2.7
from mock import patch # noqa

View File

@ -19,10 +19,10 @@ from distutils.version import LooseVersion
from six.moves.urllib.parse import urlparse
from werkzeug.wrappers import Request
import mock
from moto import settings
import responses
from moto.packages.httpretty import HTTPretty
from moto.compat import patch
from .utils import (
convert_httpretty_response,
convert_regex_to_flask_path,
@ -56,7 +56,7 @@ class BaseMockAWS(object):
"AWS_SECRET_ACCESS_KEY": "foobar_secret",
}
self.ORIG_KEYS = {}
self.default_session_mock = mock.patch("boto3.DEFAULT_SESSION", None)
self.default_session_mock = patch("boto3.DEFAULT_SESSION", None)
if self.__class__.nested_count == 0:
self.reset()
@ -94,7 +94,12 @@ class BaseMockAWS(object):
if self.__class__.nested_count == 0:
if self.__class__.mocks_active:
self.default_session_mock.stop()
try:
self.default_session_mock.stop()
except RuntimeError:
# We only need to check for this exception in Python 3.6 and 3.7
# https://bugs.python.org/issue36366
pass
self.unmock_env_variables()
self.__class__.mocks_active = False
self.disable_patching()
@ -456,7 +461,6 @@ class ServerModeMockAWS(BaseMockAWS):
self.reset()
from boto3 import client as real_boto3_client, resource as real_boto3_resource
import mock
def fake_boto3_client(*args, **kwargs):
region = self._get_region(*args, **kwargs)
@ -501,10 +505,10 @@ class ServerModeMockAWS(BaseMockAWS):
if message_body is not None:
self.send(message_body)
self._client_patcher = mock.patch("boto3.client", fake_boto3_client)
self._resource_patcher = mock.patch("boto3.resource", fake_boto3_resource)
self._client_patcher = patch("boto3.client", fake_boto3_client)
self._resource_patcher = patch("boto3.resource", fake_boto3_resource)
if six.PY2:
self._httplib_patcher = mock.patch(
self._httplib_patcher = patch(
"httplib.HTTPConnection._send_output", fake_httplib_send_output
)

View File

@ -60,7 +60,6 @@ install_requires += [
"configparser<5.0; python_version < '3'",
"Jinja2>=2.10.1",
"Jinja2<3.0.0; python_version < '3'",
"mock",
"mock<=3.0.5; python_version < '3'",
"more-itertools",
"more-itertools==5.0.0; python_version < '3'",

View File

@ -2,8 +2,8 @@ from __future__ import unicode_literals
import json
import yaml
from mock import patch
import sure # noqa
from tests.compat import patch
from moto.cloudformation.exceptions import ValidationError
from moto.cloudformation.models import FakeStack

View File

@ -1,7 +1,8 @@
from __future__ import unicode_literals
from mock import patch
import sure # noqa
from tests.compat import patch
from moto.server import main, create_backend_app, DomainDispatcherApplication

View File

@ -36,7 +36,10 @@ class TestCore:
self.stream_arn = None
for m in self.mocks:
m.stop()
try:
m.stop()
except RuntimeError:
pass
def test_verify_stream(self):
conn = boto3.client("dynamodb", region_name="us-east-1")
@ -200,7 +203,10 @@ class TestEdges:
def teardown(self):
for m in self.mocks:
m.stop()
try:
m.stop()
except RuntimeError:
pass
def test_enable_stream_on_table(self):
conn = boto3.client("dynamodb", region_name="us-east-1")

View File

@ -1,7 +1,6 @@
# #!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import mock
from moto.packages.httpretty.core import (
HTTPrettyRequest,

View File

@ -28,7 +28,10 @@ class TestDBInstanceFilters(object):
@classmethod
def teardown_class(cls):
cls.mock_rds.stop()
try:
cls.mock_rds.stop()
except RuntimeError:
pass
def test_invalid_filter_name_raises_error(self):
with pytest.raises(ClientError) as ex:
@ -203,7 +206,10 @@ class TestDBSnapshotFilters(object):
@classmethod
def teardown_class(cls):
cls.mock_rds.stop()
try:
cls.mock_rds.stop()
except RuntimeError:
pass
def test_invalid_filter_name_raises_error(self):
with pytest.raises(ClientError) as ex: