Skip to content

Commit

Permalink
bpo-39915: Ensure await_args_list is updated according to the order i…
Browse files Browse the repository at this point in the history
…n 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>
  • Loading branch information
tirkarthi authored and cjw296 committed Mar 11, 2020
1 parent bab8cea commit 742b7f0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions NEWS.d/2020-03-10-19-38-47.bpo-39915.CjPeiY.rst
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion mock/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2186,7 +2186,7 @@ async def _execute_mock_call(_mock_self, *args, **kwargs):
# 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)
Expand Down
11 changes: 11 additions & 0 deletions mock/tests/testasync.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,17 @@ def inner():
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()
Expand Down

0 comments on commit 742b7f0

Please sign in to comment.