diff options
author | Karthikeyan Singaravelan <tir.karthi@gmail.com> | 2020-03-11 20:36:12 +0530 |
---|---|---|
committer | Chris Withers <chris@withers.org> | 2020-03-11 17:25:49 +0000 |
commit | 742b7f025cfe641fcbb8ef6b1043d00ccfdcf840 (patch) | |
tree | 150d359f12ef36b3dfbd7e00c6732be6e85c2fc0 | |
parent | bab8ceab6219866b1589bec37e74c7a6dd85bc80 (diff) | |
download | mock-742b7f025cfe641fcbb8ef6b1043d00ccfdcf840.tar.gz |
bpo-39915: Ensure await_args_list is updated according to the order in which coroutines were awaited (GH-18924)
Create call objects with awaited arguments instead of using call_args which has only last call value.
Backports: e553f204bf0e39b1d701a364bc71b286acb9433f
Signed-off-by: Chris Withers <chris@simplistix.co.uk>
-rw-r--r-- | NEWS.d/2020-03-10-19-38-47.bpo-39915.CjPeiY.rst | 4 | ||||
-rw-r--r-- | mock/mock.py | 2 | ||||
-rw-r--r-- | mock/tests/testasync.py | 11 |
3 files changed, 16 insertions, 1 deletions
diff --git a/NEWS.d/2020-03-10-19-38-47.bpo-39915.CjPeiY.rst b/NEWS.d/2020-03-10-19-38-47.bpo-39915.CjPeiY.rst new file mode 100644 index 0000000..2c36947 --- /dev/null +++ b/NEWS.d/2020-03-10-19-38-47.bpo-39915.CjPeiY.rst @@ -0,0 +1,4 @@ +Ensure :attr:`unittest.mock.AsyncMock.await_args_list` has call objects in +the order of awaited arguments instead of using +:attr:`unittest.mock.Mock.call_args` which has the last value of the call. +Patch by Karthikeyan Singaravelan. diff --git a/mock/mock.py b/mock/mock.py index e91920d..4766672 100644 --- a/mock/mock.py +++ b/mock/mock.py @@ -2186,7 +2186,7 @@ class AsyncMockMixin(Base): # This is nearly just like super(), except for special handling # of coroutines - _call = self.call_args + _call = _Call((args, kwargs), two=True) self.await_count += 1 self.await_args = _call self.await_args_list.append(_call) diff --git a/mock/tests/testasync.py b/mock/tests/testasync.py index 8afa49c..9fd2b65 100644 --- a/mock/tests/testasync.py +++ b/mock/tests/testasync.py @@ -513,6 +513,17 @@ class AsyncArguments(IsolatedAsyncioTestCase): mock.assert_awaited() self.assertTrue(ran) + async def test_await_args_list_order(self): + async_mock = AsyncMock() + mock2 = async_mock(2) + mock1 = async_mock(1) + await mock1 + await mock2 + async_mock.assert_has_awaits([call(1), call(2)]) + self.assertEqual(async_mock.await_args_list, [call(1), call(2)]) + self.assertEqual(async_mock.call_args_list, [call(2), call(1)]) + + class AsyncMagicMethods(unittest.TestCase): def test_async_magic_methods_return_async_mocks(self): m_mock = MagicMock() |