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:
parent
9fa7613c4d
commit
3af87963d1
@ -8,3 +8,9 @@ try:
|
|||||||
import collections.abc as collections_abc # noqa
|
import collections.abc as collections_abc # noqa
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import collections as collections_abc # noqa
|
import collections as collections_abc # noqa
|
||||||
|
|
||||||
|
try:
|
||||||
|
from unittest.mock import patch # noqa
|
||||||
|
except ImportError:
|
||||||
|
# for python 2.7
|
||||||
|
from mock import patch # noqa
|
||||||
|
@ -19,10 +19,10 @@ from distutils.version import LooseVersion
|
|||||||
from six.moves.urllib.parse import urlparse
|
from six.moves.urllib.parse import urlparse
|
||||||
from werkzeug.wrappers import Request
|
from werkzeug.wrappers import Request
|
||||||
|
|
||||||
import mock
|
|
||||||
from moto import settings
|
from moto import settings
|
||||||
import responses
|
import responses
|
||||||
from moto.packages.httpretty import HTTPretty
|
from moto.packages.httpretty import HTTPretty
|
||||||
|
from moto.compat import patch
|
||||||
from .utils import (
|
from .utils import (
|
||||||
convert_httpretty_response,
|
convert_httpretty_response,
|
||||||
convert_regex_to_flask_path,
|
convert_regex_to_flask_path,
|
||||||
@ -56,7 +56,7 @@ class BaseMockAWS(object):
|
|||||||
"AWS_SECRET_ACCESS_KEY": "foobar_secret",
|
"AWS_SECRET_ACCESS_KEY": "foobar_secret",
|
||||||
}
|
}
|
||||||
self.ORIG_KEYS = {}
|
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:
|
if self.__class__.nested_count == 0:
|
||||||
self.reset()
|
self.reset()
|
||||||
@ -94,7 +94,12 @@ class BaseMockAWS(object):
|
|||||||
|
|
||||||
if self.__class__.nested_count == 0:
|
if self.__class__.nested_count == 0:
|
||||||
if self.__class__.mocks_active:
|
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.unmock_env_variables()
|
||||||
self.__class__.mocks_active = False
|
self.__class__.mocks_active = False
|
||||||
self.disable_patching()
|
self.disable_patching()
|
||||||
@ -456,7 +461,6 @@ class ServerModeMockAWS(BaseMockAWS):
|
|||||||
self.reset()
|
self.reset()
|
||||||
|
|
||||||
from boto3 import client as real_boto3_client, resource as real_boto3_resource
|
from boto3 import client as real_boto3_client, resource as real_boto3_resource
|
||||||
import mock
|
|
||||||
|
|
||||||
def fake_boto3_client(*args, **kwargs):
|
def fake_boto3_client(*args, **kwargs):
|
||||||
region = self._get_region(*args, **kwargs)
|
region = self._get_region(*args, **kwargs)
|
||||||
@ -501,10 +505,10 @@ class ServerModeMockAWS(BaseMockAWS):
|
|||||||
if message_body is not None:
|
if message_body is not None:
|
||||||
self.send(message_body)
|
self.send(message_body)
|
||||||
|
|
||||||
self._client_patcher = mock.patch("boto3.client", fake_boto3_client)
|
self._client_patcher = patch("boto3.client", fake_boto3_client)
|
||||||
self._resource_patcher = mock.patch("boto3.resource", fake_boto3_resource)
|
self._resource_patcher = patch("boto3.resource", fake_boto3_resource)
|
||||||
if six.PY2:
|
if six.PY2:
|
||||||
self._httplib_patcher = mock.patch(
|
self._httplib_patcher = patch(
|
||||||
"httplib.HTTPConnection._send_output", fake_httplib_send_output
|
"httplib.HTTPConnection._send_output", fake_httplib_send_output
|
||||||
)
|
)
|
||||||
|
|
||||||
|
1
setup.py
1
setup.py
@ -60,7 +60,6 @@ install_requires += [
|
|||||||
"configparser<5.0; python_version < '3'",
|
"configparser<5.0; python_version < '3'",
|
||||||
"Jinja2>=2.10.1",
|
"Jinja2>=2.10.1",
|
||||||
"Jinja2<3.0.0; python_version < '3'",
|
"Jinja2<3.0.0; python_version < '3'",
|
||||||
"mock",
|
|
||||||
"mock<=3.0.5; python_version < '3'",
|
"mock<=3.0.5; python_version < '3'",
|
||||||
"more-itertools",
|
"more-itertools",
|
||||||
"more-itertools==5.0.0; python_version < '3'",
|
"more-itertools==5.0.0; python_version < '3'",
|
||||||
|
@ -2,8 +2,8 @@ from __future__ import unicode_literals
|
|||||||
import json
|
import json
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from mock import patch
|
|
||||||
import sure # noqa
|
import sure # noqa
|
||||||
|
from tests.compat import patch
|
||||||
|
|
||||||
from moto.cloudformation.exceptions import ValidationError
|
from moto.cloudformation.exceptions import ValidationError
|
||||||
from moto.cloudformation.models import FakeStack
|
from moto.cloudformation.models import FakeStack
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
from mock import patch
|
|
||||||
import sure # noqa
|
import sure # noqa
|
||||||
|
|
||||||
|
from tests.compat import patch
|
||||||
from moto.server import main, create_backend_app, DomainDispatcherApplication
|
from moto.server import main, create_backend_app, DomainDispatcherApplication
|
||||||
|
|
||||||
|
|
||||||
|
@ -36,7 +36,10 @@ class TestCore:
|
|||||||
self.stream_arn = None
|
self.stream_arn = None
|
||||||
|
|
||||||
for m in self.mocks:
|
for m in self.mocks:
|
||||||
m.stop()
|
try:
|
||||||
|
m.stop()
|
||||||
|
except RuntimeError:
|
||||||
|
pass
|
||||||
|
|
||||||
def test_verify_stream(self):
|
def test_verify_stream(self):
|
||||||
conn = boto3.client("dynamodb", region_name="us-east-1")
|
conn = boto3.client("dynamodb", region_name="us-east-1")
|
||||||
@ -200,7 +203,10 @@ class TestEdges:
|
|||||||
|
|
||||||
def teardown(self):
|
def teardown(self):
|
||||||
for m in self.mocks:
|
for m in self.mocks:
|
||||||
m.stop()
|
try:
|
||||||
|
m.stop()
|
||||||
|
except RuntimeError:
|
||||||
|
pass
|
||||||
|
|
||||||
def test_enable_stream_on_table(self):
|
def test_enable_stream_on_table(self):
|
||||||
conn = boto3.client("dynamodb", region_name="us-east-1")
|
conn = boto3.client("dynamodb", region_name="us-east-1")
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
# #!/usr/bin/env python
|
# #!/usr/bin/env python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import mock
|
|
||||||
|
|
||||||
from moto.packages.httpretty.core import (
|
from moto.packages.httpretty.core import (
|
||||||
HTTPrettyRequest,
|
HTTPrettyRequest,
|
||||||
|
@ -28,7 +28,10 @@ class TestDBInstanceFilters(object):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def teardown_class(cls):
|
def teardown_class(cls):
|
||||||
cls.mock_rds.stop()
|
try:
|
||||||
|
cls.mock_rds.stop()
|
||||||
|
except RuntimeError:
|
||||||
|
pass
|
||||||
|
|
||||||
def test_invalid_filter_name_raises_error(self):
|
def test_invalid_filter_name_raises_error(self):
|
||||||
with pytest.raises(ClientError) as ex:
|
with pytest.raises(ClientError) as ex:
|
||||||
@ -203,7 +206,10 @@ class TestDBSnapshotFilters(object):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def teardown_class(cls):
|
def teardown_class(cls):
|
||||||
cls.mock_rds.stop()
|
try:
|
||||||
|
cls.mock_rds.stop()
|
||||||
|
except RuntimeError:
|
||||||
|
pass
|
||||||
|
|
||||||
def test_invalid_filter_name_raises_error(self):
|
def test_invalid_filter_name_raises_error(self):
|
||||||
with pytest.raises(ClientError) as ex:
|
with pytest.raises(ClientError) as ex:
|
||||||
|
Loading…
Reference in New Issue
Block a user