summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_unittest/testmock/testpatch.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_unittest/testmock/testpatch.py')
-rw-r--r--Lib/test/test_unittest/testmock/testpatch.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/Lib/test/test_unittest/testmock/testpatch.py b/Lib/test/test_unittest/testmock/testpatch.py
index d0046d7..be75fda 100644
--- a/Lib/test/test_unittest/testmock/testpatch.py
+++ b/Lib/test/test_unittest/testmock/testpatch.py
@@ -7,9 +7,11 @@ import sys
from collections import OrderedDict
import unittest
+import test
from test.test_unittest.testmock import support
from test.test_unittest.testmock.support import SomeClass, is_instance
+from test.support.import_helper import DirsOnSysPath
from test.test_importlib.util import uncache
from unittest.mock import (
NonCallableMock, CallableMixin, sentinel,
@@ -1728,6 +1730,71 @@ class PatchTest(unittest.TestCase):
'exception traceback not propagated')
+ def test_name_resolution_import_rebinding(self):
+ # Currently mock.patch uses pkgutil.resolve_name(), but repeat
+ # similar tests just for the case.
+ # The same data is also used for testing import in test_import and
+ # pkgutil.resolve_name() in test_pkgutil.
+ path = os.path.join(os.path.dirname(test.__file__), 'test_import', 'data')
+ def check(name):
+ p = patch(name)
+ p.start()
+ p.stop()
+ def check_error(name):
+ p = patch(name)
+ self.assertRaises(AttributeError, p.start)
+ with uncache('package3', 'package3.submodule'), DirsOnSysPath(path):
+ check('package3.submodule.A.attr')
+ check_error('package3.submodule.B.attr')
+ with uncache('package3', 'package3.submodule'), DirsOnSysPath(path):
+ check('package3.submodule:A.attr')
+ check_error('package3.submodule:B.attr')
+ with uncache('package3', 'package3.submodule'), DirsOnSysPath(path):
+ check('package3:submodule.B.attr')
+ check_error('package3:submodule.A.attr')
+ check('package3.submodule.A.attr')
+ check_error('package3.submodule.B.attr')
+ check('package3:submodule.B.attr')
+ check_error('package3:submodule.A.attr')
+ with uncache('package3', 'package3.submodule'), DirsOnSysPath(path):
+ check('package3:submodule.B.attr')
+ check_error('package3:submodule.A.attr')
+ check('package3.submodule:A.attr')
+ check_error('package3.submodule:B.attr')
+ check('package3:submodule.B.attr')
+ check_error('package3:submodule.A.attr')
+
+ def test_name_resolution_import_rebinding2(self):
+ path = os.path.join(os.path.dirname(test.__file__), 'test_import', 'data')
+ def check(name):
+ p = patch(name)
+ p.start()
+ p.stop()
+ def check_error(name):
+ p = patch(name)
+ self.assertRaises(AttributeError, p.start)
+ with uncache('package4', 'package4.submodule'), DirsOnSysPath(path):
+ check('package4.submodule.A.attr')
+ check_error('package4.submodule.B.attr')
+ with uncache('package4', 'package4.submodule'), DirsOnSysPath(path):
+ check('package4.submodule:A.attr')
+ check_error('package4.submodule:B.attr')
+ with uncache('package4', 'package4.submodule'), DirsOnSysPath(path):
+ check('package4:submodule.B.attr')
+ check_error('package4:submodule.A.attr')
+ check('package4.submodule.A.attr')
+ check_error('package4.submodule.B.attr')
+ check('package4:submodule.A.attr')
+ check_error('package4:submodule.B.attr')
+ with uncache('package4', 'package4.submodule'), DirsOnSysPath(path):
+ check('package4:submodule.B.attr')
+ check_error('package4:submodule.A.attr')
+ check('package4.submodule:A.attr')
+ check_error('package4.submodule:B.attr')
+ check('package4:submodule.A.attr')
+ check_error('package4:submodule.B.attr')
+
+
def test_create_and_specs(self):
for kwarg in ('spec', 'spec_set', 'autospec'):
p = patch('%s.doesnotexist' % __name__, create=True,