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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import asyncio
import functools
import sys
from unittest import mock
from .helpers import validating_transport
def get_completed_future(result=None):
future = asyncio.Future()
future.set_result(result)
return future
def wrap_in_future(fn):
"""Return a completed Future whose result is the return of fn.
Added to simplify using unittest.Mock in async code. Python 3.8's AsyncMock would be preferable.
"""
@functools.wraps(fn)
def wrapper(*args, **kwargs):
result = fn(*args, **kwargs)
return get_completed_future(result)
return wrapper
class AsyncMockTransport(mock.MagicMock):
"""Mock with do-nothing aenter/exit for mocking async transport.
This is unnecessary on 3.8+, where MagicMocks implement aenter/exit.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if sys.version_info < (3, 8):
self.__aenter__ = mock.Mock(return_value=get_completed_future())
self.__aexit__ = mock.Mock(return_value=get_completed_future())
def async_validating_transport(requests, responses):
sync_transport = validating_transport(requests, responses)
return AsyncMockTransport(send=wrap_in_future(sync_transport.send))
|