diff options
author | Senthil Kumaran <senthil@uthcode.com> | 2016-01-09 07:43:29 (GMT) |
---|---|---|
committer | Senthil Kumaran <senthil@uthcode.com> | 2016-01-09 07:43:29 (GMT) |
commit | 81bc927da7f1b2cc0d2be642810b084666f63264 (patch) | |
tree | 526e6abdaa667eedc6653fddf8b1ae5a20c10146 /Lib/unittest/test/testmock/testpatch.py | |
parent | cbe6356c4260c3f040124fe213d1fd85280bedfe (diff) | |
download | cpython-81bc927da7f1b2cc0d2be642810b084666f63264.zip cpython-81bc927da7f1b2cc0d2be642810b084666f63264.tar.gz cpython-81bc927da7f1b2cc0d2be642810b084666f63264.tar.bz2 |
Issue #22138: Fix mock.patch behavior when patching descriptors. Restore
original values after patching.
Patch contributed by Sean McCully.
Diffstat (limited to 'Lib/unittest/test/testmock/testpatch.py')
-rw-r--r-- | Lib/unittest/test/testmock/testpatch.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/Lib/unittest/test/testmock/testpatch.py b/Lib/unittest/test/testmock/testpatch.py index 28fe86b..dfce369 100644 --- a/Lib/unittest/test/testmock/testpatch.py +++ b/Lib/unittest/test/testmock/testpatch.py @@ -1817,5 +1817,31 @@ class PatchTest(unittest.TestCase): self.assertEqual(stopped, ["three", "two", "one"]) + def test_special_attrs(self): + def foo(x=0): + """TEST""" + return x + with patch.object(foo, '__defaults__', (1, )): + self.assertEqual(foo(), 1) + self.assertEqual(foo(), 0) + + with patch.object(foo, '__doc__', "FUN"): + self.assertEqual(foo.__doc__, "FUN") + self.assertEqual(foo.__doc__, "TEST") + + with patch.object(foo, '__module__', "testpatch2"): + self.assertEqual(foo.__module__, "testpatch2") + self.assertEqual(foo.__module__, 'unittest.test.testmock.testpatch') + + with patch.object(foo, '__annotations__', dict([('s', 1, )])): + self.assertEqual(foo.__annotations__, dict([('s', 1, )])) + self.assertEqual(foo.__annotations__, dict()) + + def foo(*a, x=0): + return x + with patch.object(foo, '__kwdefaults__', dict([('x', 1, )])): + self.assertEqual(foo(), 1) + self.assertEqual(foo(), 0) + if __name__ == '__main__': unittest.main() |