本文整理汇总了Python中asynctest.MagicMock方法的典型用法代码示例。如果您正苦于以下问题:Python asynctest.MagicMock方法的具体用法?Python asynctest.MagicMock怎么用?Python asynctest.MagicMock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类asynctest
的用法示例。
在下文中一共展示了asynctest.MagicMock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: session
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import MagicMock [as 别名]
def session(event_loop):
fake_retry_policy = asynctest.MagicMock(wraps=aiozk.session.RetryPolicy.forever())
session = aiozk.session.Session(
'zookeeper.test',
timeout=10,
retry_policy=fake_retry_policy,
allow_read_only=True,
read_timeout=30,
loop=asynctest.MagicMock(wraps=event_loop),
)
session.state.transition_to(aiozk.session.States.CONNECTED)
session.conn = asynctest.MagicMock()
session.conn.send = asynctest.CoroutineMock()
session.conn.close = asynctest.CoroutineMock()
session.ensure_safe_state = asynctest.CoroutineMock()
session.set_heartbeat = mock.Mock()
return session
示例2: test_is_draining_yes
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import MagicMock [as 别名]
def test_is_draining_yes(self):
fake_response = mock.Mock(
status=503,
text=asynctest.CoroutineMock(
return_value="Service service in down state since 1435694078.778886 "
"until 1435694178.780000: Drained by Paasta"
),
)
fake_task = mock.Mock(host="fake_host", ports=[54321])
with mock_ClientSession(
get=mock.Mock(
return_value=asynctest.MagicMock(
__aenter__=asynctest.CoroutineMock(return_value=fake_response)
)
)
):
assert await self.drain_method.is_draining(fake_task) is True
示例3: test_mock_returns_coroutine_according_to_spec
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import MagicMock [as 别名]
def test_mock_returns_coroutine_according_to_spec(self, klass):
spec = Test()
for attr in ('spec', 'spec_set', ):
with self.subTest(spec_type=attr):
mock = klass(**{attr: spec})
self.assertIsInstance(mock.a_function, (asynctest.Mock, asynctest.MagicMock))
self.assertNotIsInstance(mock.a_function, asynctest.CoroutineMock)
self.assertIsInstance(mock.a_coroutine, asynctest.CoroutineMock)
self.assertIsInstance(mock.a_classmethod_coroutine, asynctest.CoroutineMock)
self.assertIsInstance(mock.a_staticmethod_coroutine, asynctest.CoroutineMock)
mock.a_coroutine.return_value = "PROBE"
self.assertEqual("PROBE", run_coroutine(mock.a_coroutine()))
self.assertIsInstance(mock.an_async_coroutine, asynctest.CoroutineMock)
self.assertIsInstance(mock.an_async_classmethod_coroutine, asynctest.CoroutineMock)
self.assertIsInstance(mock.an_async_staticmethod_coroutine, asynctest.CoroutineMock)
mock.an_async_coroutine.return_value = "PROBE"
self.assertEqual("PROBE", run_coroutine(mock.an_async_coroutine()))
# Ensure the name of the mock is correctly set, tests bug #49.
示例4: test_mock_supports_async_context_manager
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import MagicMock [as 别名]
def test_mock_supports_async_context_manager(self, klass):
called = False
instance = self.WithAsyncContextManager()
mock_instance = asynctest.mock.MagicMock(instance)
async def use_context_manager():
nonlocal called
async with mock_instance as result:
called = True
return result
result = run_coroutine(use_context_manager())
self.assertFalse(instance.entered)
self.assertFalse(instance.exited)
self.assertTrue(called)
self.assertTrue(mock_instance.__aenter__.called)
self.assertTrue(mock_instance.__aexit__.called)
self.assertIsNot(mock_instance, result)
self.assertIsInstance(result, asynctest.mock.MagicMock)
示例5: test_mock_customize_async_context_manager_with_coroutine
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import MagicMock [as 别名]
def test_mock_customize_async_context_manager_with_coroutine(self, klass):
enter_called = False
exit_called = False
async def enter_coroutine(*args):
nonlocal enter_called
enter_called = True
async def exit_coroutine(*args):
nonlocal exit_called
exit_called = True
instance = self.WithAsyncContextManager()
mock_instance = asynctest.mock.MagicMock(instance)
mock_instance.__aenter__ = enter_coroutine
mock_instance.__aexit__ = exit_coroutine
async def use_context_manager():
async with mock_instance:
pass
run_coroutine(use_context_manager())
self.assertTrue(enter_called)
self.assertTrue(exit_called)
示例6: test_mock_aiter_and_anext
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import MagicMock [as 别名]
def test_mock_aiter_and_anext(self, klass):
classes = self.get_async_iterator_classes()
for iterator_class in classes:
with self.subTest(iterator_class=iterator_class.__name__):
instance = iterator_class()
mock_instance = asynctest.MagicMock(instance)
self.assertEqual(asyncio.iscoroutine(instance.__aiter__),
asyncio.iscoroutine(mock_instance.__aiter__))
self.assertEqual(asyncio.iscoroutine(instance.__anext__),
asyncio.iscoroutine(mock_instance.__anext__))
iterator = instance.__aiter__()
if asyncio.iscoroutine(iterator):
iterator = run_coroutine(iterator)
mock_iterator = mock_instance.__aiter__()
if asyncio.iscoroutine(mock_iterator):
mock_iterator = run_coroutine(mock_iterator)
self.assertEqual(asyncio.iscoroutine(iterator.__aiter__),
asyncio.iscoroutine(mock_iterator.__aiter__))
self.assertEqual(asyncio.iscoroutine(iterator.__anext__),
asyncio.iscoroutine(mock_iterator.__anext__))
示例7: test_patch_as_decorator_uses_MagicMock
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import MagicMock [as 别名]
def test_patch_as_decorator_uses_MagicMock(self):
called = []
@asynctest.mock.patch('test.test_mock.Test')
def test_mock_class(mock):
self.assertIsInstance(mock, asynctest.mock.MagicMock)
called.append("test_mock_class")
@asynctest.mock.patch('test.test_mock.Test.a_function')
def test_mock_function(mock):
self.assertIsInstance(mock, asynctest.mock.MagicMock)
called.append("test_mock_function")
test_mock_class()
test_mock_function()
self.assertIn("test_mock_class", called)
self.assertIn("test_mock_function", called)
示例8: dummy_db_pool
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import MagicMock [as 别名]
def dummy_db_pool(monkeypatch):
"""
A fixture which provides a mock database connection.
Yields
------
MagicMock
The mock db connection that will be used
"""
dummy = MagicMock()
# A MagicMock can't be used in an 'await' expression,
# so we need to make connection.set_type_codec a CoroutineMock
# (awaited in stream_result_as_json())
dummy.acquire.return_value.__aenter__.return_value.set_type_codec = CoroutineMock()
async def f(*args, **kwargs):
return dummy
monkeypatch.setattr(asyncpg, "create_pool", f)
yield dummy
示例9: setUp
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import MagicMock [as 别名]
def setUp(self):
self.realm = asynctest.MagicMock(spec_set=KeycloakRealm)
self.realm.client = asynctest.MagicMock(spec_set=KeycloakClient)
self.realm.client.get = asynctest.CoroutineMock()
self.realm.client.post = asynctest.CoroutineMock()
self.realm.client.put = asynctest.CoroutineMock()
self.realm.client.delete = asynctest.CoroutineMock()
self.client_id = 'client-id'
self.client_secret = 'client-secret'
self.openid_client = await KeycloakOpenidConnect(
realm=self.realm,
client_id=self.client_id,
client_secret=self.client_secret
)
self.openid_client.well_known.contents = {
'end_session_endpoint': 'https://logout',
'jwks_uri': 'https://certs',
'userinfo_endpoint': 'https://userinfo',
'authorization_endpoint': 'https://authorization',
'token_endpoint': 'https://token'
}
示例10: test_put
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import MagicMock [as 别名]
def test_put(self):
"""
Case: A PUT request get executed
Expected: The correct parameters get given to the request library
"""
self.Session_mock.return_value.headers = asynctest.MagicMock()
self.client._handle_response = asynctest.CoroutineMock()
response = await self.client.put(url='https://example.com/test',
data={'some': 'data'},
headers={'some': 'header'},
extra='param')
self.Session_mock.return_value.put.assert_called_once_with(
'https://example.com/test',
data={'some': 'data'},
headers={'some': 'header'},
params={'extra': 'param'}
)
self.client._handle_response.assert_awaited_once_with(
self.Session_mock.return_value.put.return_value
)
self.assertEqual(response, self.client._handle_response.return_value)
示例11: test_delete
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import MagicMock [as 别名]
def test_delete(self):
"""
Case: A DELETE request get executed
Expected: The correct parameters get given to the request library
"""
self.Session_mock.return_value.headers = asynctest.MagicMock()
self.Session_mock.return_value.delete = asynctest.CoroutineMock()
response = await self.client.delete(url='https://example.com/test',
headers={'some': 'header'},
extra='param')
self.Session_mock.return_value.delete.assert_called_once_with(
'https://example.com/test',
headers={'some': 'header'},
extra='param'
)
self.assertEqual(
response,
self.Session_mock.return_value.delete.return_value
)
示例12: test_handle_response
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import MagicMock [as 别名]
def test_handle_response(self):
"""
Case: Response get processed
Expected: Decoded json get returned else raw_response
"""
req_ctx = asynctest.MagicMock()
response = req_ctx.__aenter__.return_value
response.json = asynctest.CoroutineMock()
response.read = asynctest.CoroutineMock()
processed_response = await self.client._handle_response(req_ctx)
response.raise_for_status.assert_called_once_with()
response.json.assert_awaited_once_with(content_type=None)
self.assertEqual(processed_response, await response.json())
response.json.side_effect = ValueError
processed_response = await self.client._handle_response(req_ctx)
self.assertEqual(processed_response, await response.read())
示例13: test_calls_locked_client
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import MagicMock [as 别名]
def test_calls_locked_client(self, decorator, decorator_call):
decorator.cache.get = CoroutineMock(side_effect=[None, None, None, "value"])
decorator.cache._add = CoroutineMock(side_effect=[True, ValueError])
lock1 = MagicMock(spec=RedLock)
lock2 = MagicMock(spec=RedLock)
with patch("aiocache.decorators.RedLock", side_effect=[lock1, lock2]):
await asyncio.gather(decorator_call(value="value"), decorator_call(value="value"))
assert decorator.cache.get.call_count == 4
assert lock1.__aenter__.call_count == 1
assert lock1.__aexit__.call_count == 1
assert lock2.__aenter__.call_count == 1
assert lock2.__aexit__.call_count == 1
decorator.cache.set.assert_called_with(
"stub()[('value', 'value')]", "value", ttl=SENTINEL
)
assert stub.call_count == 1
示例14: test_plugins
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import MagicMock [as 别名]
def test_plugins(self):
self = MagicMock()
plugin1 = MagicMock()
plugin1.pre_dummy = CoroutineMock()
plugin1.post_dummy = CoroutineMock()
plugin2 = MagicMock()
plugin2.pre_dummy = CoroutineMock()
plugin2.post_dummy = CoroutineMock()
self.plugins = [plugin1, plugin2]
@API.plugins
async def dummy(self, *args, **kwargs):
return True
assert await dummy(self) is True
plugin1.pre_dummy.assert_called_with(self)
plugin1.post_dummy.assert_called_with(self, took=ANY, ret=True)
plugin2.pre_dummy.assert_called_with(self)
plugin2.post_dummy.assert_called_with(self, took=ANY, ret=True)
示例15: redis_connection
# 需要导入模块: import asynctest [as 别名]
# 或者: from asynctest import MagicMock [as 别名]
def redis_connection():
conn = MagicMock()
conn.__enter__ = MagicMock(return_value=conn)
conn.__exit__ = MagicMock()
conn.get = CoroutineMock()
conn.mget = CoroutineMock()
conn.set = CoroutineMock()
conn.setex = CoroutineMock()
conn.mset = CoroutineMock()
conn.incrby = CoroutineMock()
conn.exists = CoroutineMock()
conn.persist = CoroutineMock()
conn.expire = CoroutineMock()
conn.delete = CoroutineMock()
conn.flushdb = CoroutineMock()
conn.eval = CoroutineMock()
conn.keys = CoroutineMock()
conn.multi_exec = MagicMock(return_value=conn)
conn.execute = CoroutineMock()
return conn