-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_mock.py
34 lines (26 loc) · 930 Bytes
/
async_mock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import asyncio
import collections
def async_mock_method(func):
return _Method(func)
class _Method:
def __init__(self, func):
self._func = func
def __get__(self, instance, owner=None):
method = _BoundMethod(self._func, instance)
setattr(instance, self._func.__name__, method)
return method
class _BoundMethod:
def __init__(self, func, instance):
self._func = func
self._instance = instance
self.side_effect = None
async def __call__(self, *args, **kwargs):
try:
return await self._func(self._instance, *args, **kwargs)
finally:
if self.side_effect:
side_effect, *self.side_effect = self.side_effect
if asyncio.iscoroutine(side_effect):
return await side_effect
else:
return side_effect(self._instance, *args, **kwargs)