mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
bpo-38108: Makes mock objects inherit from Base (GH-16060)
This commit is contained in:
parent
f185a73249
commit
9a7d951950
4 changed files with 58 additions and 57 deletions
|
@ -379,6 +379,43 @@ class AsyncArguments(unittest.TestCase):
|
|||
RuntimeError('coroutine raised StopIteration')
|
||||
)
|
||||
|
||||
class AsyncMagicMethods(unittest.TestCase):
|
||||
def test_async_magic_methods_return_async_mocks(self):
|
||||
m_mock = MagicMock()
|
||||
self.assertIsInstance(m_mock.__aenter__, AsyncMock)
|
||||
self.assertIsInstance(m_mock.__aexit__, AsyncMock)
|
||||
self.assertIsInstance(m_mock.__anext__, AsyncMock)
|
||||
# __aiter__ is actually a synchronous object
|
||||
# so should return a MagicMock
|
||||
self.assertIsInstance(m_mock.__aiter__, MagicMock)
|
||||
|
||||
def test_sync_magic_methods_return_magic_mocks(self):
|
||||
a_mock = AsyncMock()
|
||||
self.assertIsInstance(a_mock.__enter__, MagicMock)
|
||||
self.assertIsInstance(a_mock.__exit__, MagicMock)
|
||||
self.assertIsInstance(a_mock.__next__, MagicMock)
|
||||
self.assertIsInstance(a_mock.__len__, MagicMock)
|
||||
|
||||
def test_magicmock_has_async_magic_methods(self):
|
||||
m_mock = MagicMock()
|
||||
self.assertTrue(hasattr(m_mock, "__aenter__"))
|
||||
self.assertTrue(hasattr(m_mock, "__aexit__"))
|
||||
self.assertTrue(hasattr(m_mock, "__anext__"))
|
||||
|
||||
def test_asyncmock_has_sync_magic_methods(self):
|
||||
a_mock = AsyncMock()
|
||||
self.assertTrue(hasattr(a_mock, "__enter__"))
|
||||
self.assertTrue(hasattr(a_mock, "__exit__"))
|
||||
self.assertTrue(hasattr(a_mock, "__next__"))
|
||||
self.assertTrue(hasattr(a_mock, "__len__"))
|
||||
|
||||
def test_magic_methods_are_async_functions(self):
|
||||
m_mock = MagicMock()
|
||||
self.assertIsInstance(m_mock.__aenter__, AsyncMock)
|
||||
self.assertIsInstance(m_mock.__aexit__, AsyncMock)
|
||||
# AsyncMocks are also coroutine functions
|
||||
self.assertTrue(asyncio.iscoroutinefunction(m_mock.__aenter__))
|
||||
self.assertTrue(asyncio.iscoroutinefunction(m_mock.__aexit__))
|
||||
|
||||
class AsyncContextManagerTest(unittest.TestCase):
|
||||
|
||||
|
@ -406,24 +443,6 @@ class AsyncContextManagerTest(unittest.TestCase):
|
|||
val = await response.json()
|
||||
return val
|
||||
|
||||
def test_async_magic_methods_are_async_mocks_with_magicmock(self):
|
||||
cm_mock = MagicMock(self.WithAsyncContextManager())
|
||||
self.assertIsInstance(cm_mock.__aenter__, AsyncMock)
|
||||
self.assertIsInstance(cm_mock.__aexit__, AsyncMock)
|
||||
|
||||
def test_magicmock_has_async_magic_methods(self):
|
||||
cm = MagicMock(name='magic_cm')
|
||||
self.assertTrue(hasattr(cm, "__aenter__"))
|
||||
self.assertTrue(hasattr(cm, "__aexit__"))
|
||||
|
||||
def test_magic_methods_are_async_functions(self):
|
||||
cm = MagicMock(name='magic_cm')
|
||||
self.assertIsInstance(cm.__aenter__, AsyncMock)
|
||||
self.assertIsInstance(cm.__aexit__, AsyncMock)
|
||||
# AsyncMocks are also coroutine functions
|
||||
self.assertTrue(asyncio.iscoroutinefunction(cm.__aenter__))
|
||||
self.assertTrue(asyncio.iscoroutinefunction(cm.__aexit__))
|
||||
|
||||
def test_set_return_value_of_aenter(self):
|
||||
def inner_test(mock_type):
|
||||
pc = self.ProductionCode()
|
||||
|
|
|
@ -271,9 +271,6 @@ class TestMockingMagicMethods(unittest.TestCase):
|
|||
self.assertEqual(mock == mock, True)
|
||||
self.assertEqual(mock != mock, False)
|
||||
|
||||
|
||||
# This should be fixed with issue38163
|
||||
@unittest.expectedFailure
|
||||
def test_asyncmock_defaults(self):
|
||||
mock = AsyncMock()
|
||||
self.assertEqual(int(mock), 1)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue