Beispiel #1
0
 def test_is_child_AsyncMock(self):
     mock = MagicMock(spec_set=AsyncClass)
     self.assertTrue(iscoroutinefunction(mock.async_method))
     self.assertFalse(iscoroutinefunction(mock.normal_method))
     self.assertIsInstance(mock.async_method, AsyncMock)
     self.assertIsInstance(mock.normal_method, MagicMock)
     self.assertIsInstance(mock, MagicMock)
Beispiel #2
0
 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(iscoroutinefunction(m_mock.__aenter__))
     self.assertTrue(iscoroutinefunction(m_mock.__aexit__))
Beispiel #3
0
 def inner_test(mock_type):
     instance = self.WithAsyncIterator()
     mock_instance = mock_type(instance)
     # Check that the mock and the real thing bahave the same
     # __aiter__ is not actually async, so not a coroutinefunction
     self.assertFalse(iscoroutinefunction(instance.__aiter__))
     self.assertFalse(iscoroutinefunction(mock_instance.__aiter__))
     # __anext__ is async
     self.assertTrue(iscoroutinefunction(instance.__anext__))
     self.assertTrue(iscoroutinefunction(mock_instance.__anext__))
Beispiel #4
0
        async def test_async():
            with patch(f"{__name__}.async_func_args",
                       autospec=True) as mock_method:
                awaitable = mock_method(1, 2, c=3)
                self.assertIsInstance(mock_method.mock, AsyncMock)

                self.assertTrue(iscoroutinefunction(mock_method))
                self.assertTrue(asyncio.iscoroutine(awaitable))
                self.assertTrue(inspect.isawaitable(awaitable))

                # Verify the default values during mock setup
                self.assertEqual(mock_method.await_count, 0)
                self.assertEqual(mock_method.await_args_list, [])
                self.assertIsNone(mock_method.await_args)
                mock_method.assert_not_awaited()

                await awaitable

            self.assertEqual(mock_method.await_count, 1)
            self.assertEqual(mock_method.await_args, call(1, 2, c=3))
            self.assertEqual(mock_method.await_args_list, [call(1, 2, c=3)])
            mock_method.assert_awaited_once()
            mock_method.assert_awaited_once_with(1, 2, c=3)
            mock_method.assert_awaited_with(1, 2, c=3)
            mock_method.assert_awaited()

            mock_method.reset_mock()
            self.assertEqual(mock_method.await_count, 0)
            self.assertIsNone(mock_method.await_args)
            self.assertEqual(mock_method.await_args_list, [])
Beispiel #5
0
    def test_create_autospec(self):
        spec = create_autospec(async_func_args)
        awaitable = spec(1, 2, c=3)

        async def main():
            await awaitable

        self.assertEqual(spec.await_count, 0)
        self.assertIsNone(spec.await_args)
        self.assertEqual(spec.await_args_list, [])
        spec.assert_not_awaited()

        run(main())

        self.assertTrue(iscoroutinefunction(spec))
        self.assertTrue(asyncio.iscoroutine(awaitable))
        self.assertEqual(spec.await_count, 1)
        self.assertEqual(spec.await_args, call(1, 2, c=3))
        self.assertEqual(spec.await_args_list, [call(1, 2, c=3)])
        spec.assert_awaited_once()
        spec.assert_awaited_once_with(1, 2, c=3)
        spec.assert_awaited_with(1, 2, c=3)
        spec.assert_awaited()

        with self.assertRaises(AssertionError):
            spec.assert_any_await(e=1)
Beispiel #6
0
    def test_iscoroutinefunction_normal_function(self):
        def foo():
            pass

        mock = AsyncMock(foo)
        self.assertTrue(iscoroutinefunction(mock))
        self.assertTrue(inspect.iscoroutinefunction(mock))
Beispiel #7
0
 def test_assert_called_but_not_awaited(self):
     mock = AsyncMock(AsyncClass)
     with assertNeverAwaited(self):
         mock.async_method()
     self.assertTrue(iscoroutinefunction(mock.async_method))
     mock.async_method.assert_called()
     mock.async_method.assert_called_once()
     mock.async_method.assert_called_once_with()
     with self.assertRaises(AssertionError):
         mock.assert_awaited()
     with self.assertRaises(AssertionError):
         mock.async_method.assert_awaited()
Beispiel #8
0
    def test_magicmock_defaults(self):
        mock = MagicMock()
        self.assertEqual(int(mock), 1)
        self.assertEqual(complex(mock), 1j)
        self.assertEqual(float(mock), 1.0)
        self.assertNotIn(object(), mock)
        self.assertEqual(len(mock), 0)
        self.assertEqual(list(mock), [])
        self.assertEqual(hash(mock), object.__hash__(mock))
        self.assertEqual(str(mock), object.__str__(mock))
        self.assertTrue(bool(mock))
        self.assertEqual(round(mock), mock.__round__())
        self.assertEqual(math.trunc(mock), mock.__trunc__())
        self.assertEqual(math.floor(mock), mock.__floor__())
        self.assertEqual(math.ceil(mock), mock.__ceil__())
        self.assertTrue(iscoroutinefunction(mock.__aexit__))
        self.assertTrue(iscoroutinefunction(mock.__aenter__))
        self.assertIsInstance(mock.__aenter__, AsyncMock)
        self.assertIsInstance(mock.__aexit__, AsyncMock)

        # in Python 3 oct and hex use __index__
        # so these tests are for __index__ in py3k
        self.assertEqual(oct(mock), '0o1')
        self.assertEqual(hex(mock), '0x1')
Beispiel #9
0
 def test_async(mock_method):
     self.assertTrue(iscoroutinefunction(mock_method))
Beispiel #10
0
 def test_is_async_AsyncMock(self):
     mock = AsyncMock(spec_set=AsyncClass.async_method)
     self.assertTrue(iscoroutinefunction(mock))
     self.assertIsInstance(mock, AsyncMock)
Beispiel #11
0
 def test_iscoroutinefunction_default(self):
     mock = AsyncMock()
     self.assertTrue(iscoroutinefunction(mock))
Beispiel #12
0
 def test_async():
     with patch.object(AsyncClass, 'async_method') as mock_method:
         self.assertTrue(iscoroutinefunction(mock_method))