Skip to content

Commit 94f0c8d

Browse files
committed
Create new unit test class.
1 parent bb90744 commit 94f0c8d

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

Lib/unittest/test/testmock/testasync.py

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,43 @@ def test_add_side_effect_iterable(self):
379379
RuntimeError('coroutine raised StopIteration')
380380
)
381381

382+
class AsyncMagicMethods(unittest.TestCase):
383+
def test_async_magic_methods_return_async_mocks(self):
384+
m_mock = MagicMock()
385+
self.assertIsInstance(m_mock.__aenter__, AsyncMock)
386+
self.assertIsInstance(m_mock.__aexit__, AsyncMock)
387+
self.assertIsInstance(m_mock.__anext__, AsyncMock)
388+
# __aiter__ is actually a synchronous object
389+
# so should return a MagicMock
390+
self.assertIsInstance(m_mock.__aiter__, MagicMock)
391+
392+
def test_sync_magic_methods_return_magic_mocks(self):
393+
a_mock = AsyncMock()
394+
self.assertIsInstance(a_mock.__enter__, MagicMock)
395+
self.assertIsInstance(a_mock.__exit__, MagicMock)
396+
self.assertIsInstance(a_mock.__next__, MagicMock)
397+
self.assertIsInstance(a_mock.__len__, MagicMock)
398+
399+
def test_magicmock_has_async_magic_methods(self):
400+
m_mock = MagicMock()
401+
self.assertTrue(hasattr(m_mock, "__aenter__"))
402+
self.assertTrue(hasattr(m_mock, "__aexit__"))
403+
self.assertTrue(hasattr(m_mock, "__anext__"))
404+
405+
def test_asyncmock_has_sync_magic_methods(self):
406+
a_mock = AsyncMock()
407+
self.assertTrue(hasattr(a_mock, "__enter__"))
408+
self.assertTrue(hasattr(a_mock, "__exit__"))
409+
self.assertTrue(hasattr(a_mock, "__next__"))
410+
self.assertTrue(hasattr(a_mock, "__len__"))
411+
412+
def test_magic_methods_are_async_functions(self):
413+
m_mock = MagicMock()
414+
self.assertIsInstance(m_mock.__aenter__, AsyncMock)
415+
self.assertIsInstance(m_mock.__aexit__, AsyncMock)
416+
# AsyncMocks are also coroutine functions
417+
self.assertTrue(asyncio.iscoroutinefunction(m_mock.__aenter__))
418+
self.assertTrue(asyncio.iscoroutinefunction(m_mock.__aexit__))
382419

383420
class AsyncContextManagerTest(unittest.TestCase):
384421

@@ -406,43 +443,6 @@ async def main(self):
406443
val = await response.json()
407444
return val
408445

409-
def test_async_magic_methods_return_async_mocks(self):
410-
cm_mock = MagicMock()
411-
self.assertIsInstance(cm_mock.__aenter__, AsyncMock)
412-
self.assertIsInstance(cm_mock.__aexit__, AsyncMock)
413-
self.assertIsInstance(cm_mock.__anext__, AsyncMock)
414-
# __aiter__ is actually a synchronous object
415-
# so should return a MagicMock
416-
self.assertIsInstance(cm_mock.__aiter__, MagicMock)
417-
418-
def test_sync_magic_methods_return_magic_mocks(self):
419-
am_mock = AsyncMock()
420-
self.assertIsInstance(am_mock.__enter__, MagicMock)
421-
self.assertIsInstance(am_mock.__exit__, MagicMock)
422-
self.assertIsInstance(am_mock.__next__, MagicMock)
423-
self.assertIsInstance(am_mock.__len__, MagicMock)
424-
425-
def test_magicmock_has_async_magic_methods(self):
426-
cm = MagicMock(name='magic_cm')
427-
self.assertTrue(hasattr(cm, "__aenter__"))
428-
self.assertTrue(hasattr(cm, "__aexit__"))
429-
self.assertTrue(hasattr(cm, "__anext__"))
430-
431-
def test_asyncmock_has_sync_magic_methods(self):
432-
am = AsyncMock(name='async_cm')
433-
self.assertTrue(hasattr(am, "__enter__"))
434-
self.assertTrue(hasattr(am, "__exit__"))
435-
self.assertTrue(hasattr(am, "__next__"))
436-
self.assertTrue(hasattr(am, "__len__"))
437-
438-
def test_magic_methods_are_async_functions(self):
439-
cm = MagicMock(name='magic_cm')
440-
self.assertIsInstance(cm.__aenter__, AsyncMock)
441-
self.assertIsInstance(cm.__aexit__, AsyncMock)
442-
# AsyncMocks are also coroutine functions
443-
self.assertTrue(asyncio.iscoroutinefunction(cm.__aenter__))
444-
self.assertTrue(asyncio.iscoroutinefunction(cm.__aexit__))
445-
446446
def test_set_return_value_of_aenter(self):
447447
def inner_test(mock_type):
448448
pc = self.ProductionCode()

0 commit comments

Comments
 (0)