mirror of
https://github.com/python/cpython.git
synced 2025-12-23 09:19:18 +00:00
bpo-37555: Add tests checking every function using _call_matcher both with and without spec
This commit is contained in:
parent
d3522b1e17
commit
f47699de12
2 changed files with 43 additions and 10 deletions
|
|
@ -2,8 +2,8 @@ import asyncio
|
||||||
import inspect
|
import inspect
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from unittest.mock import (call, AsyncMock, patch, MagicMock, create_autospec,
|
from unittest.mock import (ANY, call, AsyncMock, patch, MagicMock,
|
||||||
_AwaitEvent)
|
create_autospec, _AwaitEvent)
|
||||||
|
|
||||||
|
|
||||||
def tearDownModule():
|
def tearDownModule():
|
||||||
|
|
@ -599,6 +599,30 @@ class AsyncMockAssert(unittest.TestCase):
|
||||||
asyncio.run(self._runnable_test('SomethingElse'))
|
asyncio.run(self._runnable_test('SomethingElse'))
|
||||||
self.mock.assert_has_awaits(calls)
|
self.mock.assert_has_awaits(calls)
|
||||||
|
|
||||||
|
def test_awaits_asserts_with_any(self):
|
||||||
|
class Foo:
|
||||||
|
def __eq__(self, other): pass
|
||||||
|
|
||||||
|
asyncio.run(self._runnable_test(Foo(), 1))
|
||||||
|
|
||||||
|
self.mock.assert_has_awaits([call(ANY, 1)])
|
||||||
|
self.mock.assert_awaited_with(ANY, 1)
|
||||||
|
self.mock.assert_any_await(ANY, 1)
|
||||||
|
|
||||||
|
def test_awaits_asserts_with_spec_and_any(self):
|
||||||
|
class Foo:
|
||||||
|
def __eq__(self, other): pass
|
||||||
|
|
||||||
|
mock_with_spec = AsyncMock(spec=Foo)
|
||||||
|
|
||||||
|
async def _custom_mock_runnable_test(*args):
|
||||||
|
await mock_with_spec(*args)
|
||||||
|
|
||||||
|
asyncio.run(_custom_mock_runnable_test(Foo(), 1))
|
||||||
|
mock_with_spec.assert_has_awaits([call(ANY, 1)])
|
||||||
|
mock_with_spec.assert_awaited_with(ANY, 1)
|
||||||
|
mock_with_spec.assert_any_await(ANY, 1)
|
||||||
|
|
||||||
def test_assert_has_awaits_ordered(self):
|
def test_assert_has_awaits_ordered(self):
|
||||||
calls = [call('NormalFoo'), call('baz')]
|
calls = [call('NormalFoo'), call('baz')]
|
||||||
with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError):
|
||||||
|
|
|
||||||
|
|
@ -63,20 +63,29 @@ class AnyTest(unittest.TestCase):
|
||||||
]
|
]
|
||||||
self.assertEqual(expected, mock.mock_calls)
|
self.assertEqual(expected, mock.mock_calls)
|
||||||
self.assertEqual(mock.mock_calls, expected)
|
self.assertEqual(mock.mock_calls, expected)
|
||||||
mock.assert_has_calls(expected)
|
|
||||||
|
|
||||||
def test_assert_has_calls_with_any_and_spec_set(self):
|
def test_any_no_spec(self):
|
||||||
# This is a regression test for bpo-37555
|
# This is a regression test for bpo-37555
|
||||||
class Foo(object):
|
class Foo:
|
||||||
def __eq__(self, other): pass
|
def __eq__(self, other): pass
|
||||||
def __ne__(self, other): pass
|
|
||||||
|
|
||||||
mock = Mock(spec_set=Foo)
|
mock = Mock()
|
||||||
expected = [call(ANY)]
|
mock(Foo(), 1)
|
||||||
mock(Foo())
|
mock.assert_has_calls([call(ANY, 1)])
|
||||||
|
mock.assert_called_with(ANY, 1)
|
||||||
|
mock.assert_any_call(ANY, 1)
|
||||||
|
|
||||||
mock.assert_has_calls(expected)
|
def test_any_and_spec_set(self):
|
||||||
|
# This is a regression test for bpo-37555
|
||||||
|
class Foo:
|
||||||
|
def __eq__(self, other): pass
|
||||||
|
|
||||||
|
mock = Mock(spec=Foo)
|
||||||
|
|
||||||
|
mock(Foo(), 1)
|
||||||
|
mock.assert_has_calls([call(ANY, 1)])
|
||||||
|
mock.assert_called_with(ANY, 1)
|
||||||
|
mock.assert_any_call(ANY, 1)
|
||||||
|
|
||||||
class CallTest(unittest.TestCase):
|
class CallTest(unittest.TestCase):
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue