]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39915: Ensure await_args_list is updated according to the order in which coroutin...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 14 Mar 2020 07:12:57 +0000 (00:12 -0700)
committerGitHub <noreply@github.com>
Sat, 14 Mar 2020 07:12:57 +0000 (07:12 +0000)
Create call objects with awaited arguments instead of using call_args which has only last call value.
(cherry picked from commit e553f204bf0e39b1d701a364bc71b286acb9433f)

Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
Lib/unittest/mock.py
Lib/unittest/test/testmock/testasync.py
Misc/NEWS.d/next/Library/2020-03-10-19-38-47.bpo-39915.CjPeiY.rst [new file with mode: 0644]

index 204b3e7789b333d211e25d9a0b653a00023a8a10..a8f74a95791c1f881c98d026406887231f443b14 100644 (file)
@@ -2136,7 +2136,7 @@ class AsyncMockMixin(Base):
         # This is nearly just like super(), except for sepcial 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)
index e68022afdcceaac4f7ed47763f5ca6be1c1051b3..e84c66c0bdd05e5a00e446840fc4e50e173527c6 100644 (file)
@@ -496,6 +496,17 @@ class AsyncArguments(unittest.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()
diff --git a/Misc/NEWS.d/next/Library/2020-03-10-19-38-47.bpo-39915.CjPeiY.rst b/Misc/NEWS.d/next/Library/2020-03-10-19-38-47.bpo-39915.CjPeiY.rst
new file mode 100644 (file)
index 0000000..2c36947
--- /dev/null
@@ -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.