2022-01-12 14:28:43 +00:00
|
|
|
"""
|
|
|
|
For 3.7 compat
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
from unittest.mock import Mock
|
|
|
|
|
|
|
|
|
|
|
|
class AsyncMock(Mock):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.await_count = 0
|
|
|
|
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
self.call_count += 1
|
2023-10-25 02:28:52 +01:00
|
|
|
parent = super()
|
2022-01-12 14:28:43 +00:00
|
|
|
|
|
|
|
async def dummy():
|
|
|
|
self.await_count += 1
|
|
|
|
return parent.__call__(*args, **kwargs)
|
|
|
|
|
|
|
|
return dummy()
|
|
|
|
|
|
|
|
def __await__(self):
|
|
|
|
return self().__await__()
|
|
|
|
|
2022-06-27 09:19:26 +01:00
|
|
|
def reset_mock(self, *args, **kwargs):
|
|
|
|
super().reset_mock(*args, **kwargs)
|
|
|
|
self.await_count = 0
|
|
|
|
|
2022-01-12 14:28:43 +00:00
|
|
|
def assert_awaited_once(self):
|
|
|
|
if not self.await_count == 1:
|
|
|
|
msg = (
|
|
|
|
f"Expected to have been awaited once."
|
|
|
|
f" Awaited {self.await_count} times."
|
|
|
|
)
|
|
|
|
raise AssertionError(msg)
|
2022-06-27 09:19:26 +01:00
|
|
|
|
|
|
|
def assert_awaited_once_with(self, *args, **kwargs):
|
|
|
|
if not self.await_count == 1:
|
|
|
|
msg = (
|
|
|
|
f"Expected to have been awaited once."
|
|
|
|
f" Awaited {self.await_count} times."
|
|
|
|
)
|
|
|
|
raise AssertionError(msg)
|
|
|
|
self.assert_awaited_once()
|
|
|
|
return self.assert_called_with(*args, **kwargs)
|