Beispiel #1
0
    def test_async_arg_lists(self):
        def assert_attrs(mock):
            names = ('call_args_list', 'method_calls', 'mock_calls')
            for name in names:
                attr = getattr(mock, name)
                self.assertIsInstance(attr, _CallList)
                self.assertIsInstance(attr, list)
                self.assertEqual(attr, [])

        assert_attrs(self.mock)
        with assertNeverAwaited(self):
            self.mock()
        with assertNeverAwaited(self):
            self.mock(1, 2)
        with assertNeverAwaited(self):
            self.mock(a=3)

        self.mock.reset_mock()
        assert_attrs(self.mock)

        a_mock = AsyncMock(AsyncClass)
        with assertNeverAwaited(self):
            a_mock.async_method()
        with assertNeverAwaited(self):
            a_mock.async_method(1, a=3)

        a_mock.reset_mock()
        assert_attrs(a_mock)
Beispiel #2
0
    def test_async_arg_lists(self):
        def assert_attrs(mock):
            names = ('call_args_list', 'method_calls', 'mock_calls')
            for name in names:
                attr = getattr(mock, name)
                self.assertIsInstance(attr, _CallList)
                self.assertIsInstance(attr, list)
                self.assertEqual(attr, [])

        assert_attrs(self.mock)
        with self.assertWarns(RuntimeWarning):
            # Will raise warnings because never awaited
            self.mock()
            self.mock(1, 2)
            self.mock(a=3)

        self.mock.reset_mock()
        assert_attrs(self.mock)

        a_mock = AsyncMock(AsyncClass)
        with self.assertWarns(RuntimeWarning):
            # Will raise warnings because never awaited
            a_mock.async_method()
            a_mock.async_method(1, a=3)

        a_mock.reset_mock()
        assert_attrs(a_mock)
Beispiel #3
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 #4
0
 def test_assert_called_twice_and_awaited_once(self):
     mock = AsyncMock(AsyncClass)
     coroutine = mock.async_method()
     with self.assertWarns(RuntimeWarning):
         # The first call will be awaited so no warning there
         # But this call will never get awaited, so it will warn here
         mock.async_method()
     with self.assertRaises(AssertionError):
         mock.async_method.assert_awaited()
     mock.async_method.assert_called()
     asyncio.run(self._await_coroutine(coroutine))
     mock.async_method.assert_awaited()
     mock.async_method.assert_awaited_once()
Beispiel #5
0
 def test_assert_called_but_not_awaited(self):
     mock = AsyncMock(AsyncClass)
     with self.assertWarns(RuntimeWarning):
         # Will raise a warning because never awaited
         mock.async_method()
     self.assertTrue(asyncio.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 #6
0
    def test_assert_has_mock_calls_on_async_mock_with_spec(self):
        a_class_mock = AsyncMock(AsyncClass)
        with assertNeverAwaited(self):
            a_class_mock.async_method()
        kalls_empty = [('', (), {})]
        self.assertEqual(a_class_mock.async_method.mock_calls, kalls_empty)
        self.assertEqual(a_class_mock.mock_calls, [call.async_method()])

        with assertNeverAwaited(self):
            a_class_mock.async_method(1, 2, 3, a=4, b=5)
        method_kalls = [call(), call(1, 2, 3, a=4, b=5)]
        mock_kalls = [call.async_method(), call.async_method(1, 2, 3, a=4, b=5)]
        self.assertEqual(a_class_mock.async_method.mock_calls, method_kalls)
        self.assertEqual(a_class_mock.mock_calls, mock_kalls)
Beispiel #7
0
    def test_assert_has_mock_calls_on_async_mock_with_spec(self):
        a_class_mock = AsyncMock(AsyncClass)
        with self.assertWarns(RuntimeWarning):
            # Will raise a warning because never awaited
            a_class_mock.async_method()
        kalls_empty = [('', (), {})]
        self.assertEqual(a_class_mock.async_method.mock_calls, kalls_empty)
        self.assertEqual(a_class_mock.mock_calls, [call.async_method()])

        with self.assertWarns(RuntimeWarning):
            # Will raise a warning because never awaited
            a_class_mock.async_method(1, 2, 3, a=4, b=5)
        method_kalls = [call(), call(1, 2, 3, a=4, b=5)]
        mock_kalls = [call.async_method(), call.async_method(1, 2, 3, a=4, b=5)]
        self.assertEqual(a_class_mock.async_method.mock_calls, method_kalls)
        self.assertEqual(a_class_mock.mock_calls, mock_kalls)
Beispiel #8
0
 def test_assert_called_once_and_awaited_twice(self):
     mock = AsyncMock(AsyncClass)
     coroutine = mock.async_method()
     mock.async_method.assert_called_once()
     asyncio.run(self._await_coroutine(coroutine))
     with self.assertRaises(RuntimeError):
         # Cannot reuse already awaited coroutine
         asyncio.run(self._await_coroutine(coroutine))
     mock.async_method.assert_awaited()
Beispiel #9
0
    def test_assert_called_then_awaited(self):
        mock = AsyncMock(AsyncClass)
        mock_coroutine = 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.async_method.assert_awaited()

        asyncio.run(self._await_coroutine(mock_coroutine))
        # Assert we haven't re-called the function
        mock.async_method.assert_called_once()
        mock.async_method.assert_awaited()
        mock.async_method.assert_awaited_once()
        mock.async_method.assert_awaited_once_with()