本文整理汇总了Python中asynctest.mock.patch方法的典型用法代码示例。如果您正苦于以下问题:Python mock.patch方法的具体用法?Python mock.patch怎么用?Python mock.patch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类asynctest.mock
的用法示例。
在下文中一共展示了mock.patch方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_receive_invitation_bad_service_length
# 需要导入模块: from asynctest import mock [as 别名]
# 或者: from asynctest.mock import patch [as 别名]
def test_receive_invitation_bad_service_length(self):
self.manager.context.update_settings({"public_invites": True})
with async_mock.patch.object(
BaseWallet, "get_public_did", autospec=True
) as mock_wallet_get_public_did, async_mock.patch.object(
ConnectionManager, "create_invitation", autospec=True
) as conn_mgr_create_inv, async_mock.patch(
"aries_cloudagent.protocols.out_of_band.v1_0.manager.InvitationMessage",
autospec=True,
) as inv_message_cls:
mock_wallet_get_public_did.return_value = DIDInfo(
self.test_did, self.test_verkey, None
)
conn_inv_mock = async_mock.MagicMock()
inv_message_cls.deserialize.return_value = conn_inv_mock
conn_inv_mock.service_blocks = []
with self.assertRaises(OutOfBandManagerError):
inv_model = await self.manager.receive_invitation(conn_inv_mock)
示例2: test_post_save_exist
# 需要导入模块: from asynctest import mock [as 别名]
# 或者: from asynctest.mock import patch [as 别名]
def test_post_save_exist(self):
context = InjectionContext(enforce_typing=False)
mock_storage = async_mock.MagicMock()
mock_storage.update_record_value = async_mock.CoroutineMock()
mock_storage.update_record_tags = async_mock.CoroutineMock()
context.injector.bind_instance(BaseStorage, mock_storage)
record = BaseRecordImpl()
last_state = "last_state"
record._last_state = last_state
record._id = "id"
with async_mock.patch.object(
record, "post_save", async_mock.CoroutineMock()
) as post_save:
await record.save(context, reason="reason", webhook=False)
post_save.assert_called_once_with(context, False, last_state, False)
mock_storage.update_record_value.assert_called_once()
mock_storage.update_record_tags.assert_called_once()
示例3: test_retrieve_cached_id
# 需要导入模块: from asynctest import mock [as 别名]
# 或者: from asynctest.mock import patch [as 别名]
def test_retrieve_cached_id(self):
context = InjectionContext(enforce_typing=False)
mock_storage = async_mock.MagicMock(BaseStorage, autospec=True)
context.injector.bind_instance(BaseStorage, mock_storage)
record_id = "record_id"
stored = {"created_at": time_now(), "updated_at": time_now()}
with async_mock.patch.object(
BaseRecordImpl, "get_cached_key"
) as get_cached_key, async_mock.patch.object(
BaseRecordImpl, "cache_key"
) as cache_key:
get_cached_key.return_value = stored
result = await BaseRecordImpl.retrieve_by_id(context, record_id, True)
cache_key.assert_called_once_with(record_id)
get_cached_key.assert_awaited_once_with(context, cache_key.return_value)
mock_storage.get_record.assert_not_called()
assert isinstance(result, BaseRecordImpl)
assert result._id == record_id
assert result.value == stored
示例4: test_retrieve_uncached_id
# 需要导入模块: from asynctest import mock [as 别名]
# 或者: from asynctest.mock import patch [as 别名]
def test_retrieve_uncached_id(self):
context = InjectionContext(enforce_typing=False)
mock_storage = async_mock.MagicMock(BaseStorage, autospec=True)
context.injector.bind_instance(BaseStorage, mock_storage)
record_id = "record_id"
record_value = {"created_at": time_now(), "updated_at": time_now()}
stored = StorageRecord(
BaseRecordImpl.RECORD_TYPE, json.dumps(record_value), {}, record_id
)
with async_mock.patch.object(
BaseRecordImpl, "set_cached_key"
) as set_cached_key, async_mock.patch.object(
BaseRecordImpl, "cache_key"
) as cache_key:
mock_storage.get_record.return_value = stored
result = await BaseRecordImpl.retrieve_by_id(context, record_id, False)
mock_storage.get_record.assert_awaited_once_with(
BaseRecordImpl.RECORD_TYPE, record_id, {"retrieveTags": False}
)
set_cached_key.assert_awaited_once_with(
context, cache_key.return_value, record_value
)
assert isinstance(result, BaseRecordImpl)
assert result._id == record_id
assert result.value == record_value
示例5: test_setup_x
# 需要导入模块: from asynctest import mock [as 别名]
# 或者: from asynctest.mock import patch [as 别名]
def test_setup_x(self):
builder: ContextBuilder = StubContextBuilder(self.test_settings)
builder.update_settings(
{"admin.enabled": "1", "admin.webhook_urls": ["http://sample.webhook.ca"]}
)
conductor = test_module.Conductor(builder)
mock_om = async_mock.MagicMock(
setup=async_mock.CoroutineMock(),
register=async_mock.MagicMock(side_effect=KeyError("sample error")),
registered_schemes={},
)
with async_mock.patch.object(
test_module, "InboundTransportManager", autospec=True
) as mock_inbound_mgr, async_mock.patch.object(
test_module, "OutboundTransportManager", autospec=True
) as mock_outbound_mgr, async_mock.patch.object(
test_module, "LoggingConfigurator", async_mock.MagicMock()
) as mock_logger:
mock_outbound_mgr.return_value = mock_om
with self.assertRaises(KeyError):
await conductor.setup()
示例6: test_inbound_message_handler
# 需要导入模块: from asynctest import mock [as 别名]
# 或者: from asynctest.mock import patch [as 别名]
def test_inbound_message_handler(self):
builder: ContextBuilder = StubContextBuilder(self.test_settings)
conductor = test_module.Conductor(builder)
await conductor.setup()
with async_mock.patch.object(
conductor.dispatcher, "queue_message", autospec=True
) as mock_dispatch:
message_body = "{}"
receipt = MessageReceipt(direct_response_mode="snail mail")
message = InboundMessage(message_body, receipt)
conductor.inbound_message_router(message, can_respond=False)
mock_dispatch.assert_called_once()
assert mock_dispatch.call_args[0][0] is message
assert mock_dispatch.call_args[0][1] == conductor.outbound_message_router
assert mock_dispatch.call_args[0][2] is None # admin webhook router
assert callable(mock_dispatch.call_args[0][3])
示例7: test_outbound_message_handler_return_route
# 需要导入模块: from asynctest import mock [as 别名]
# 或者: from asynctest.mock import patch [as 别名]
def test_outbound_message_handler_return_route(self):
builder: ContextBuilder = StubContextBuilder(self.test_settings)
conductor = test_module.Conductor(builder)
test_to_verkey = "test-to-verkey"
test_from_verkey = "test-from-verkey"
await conductor.setup()
payload = "{}"
message = OutboundMessage(payload=payload)
message.reply_to_verkey = test_to_verkey
receipt = MessageReceipt()
receipt.recipient_verkey = test_from_verkey
inbound = InboundMessage("[]", receipt)
with async_mock.patch.object(
conductor.inbound_transport_manager, "return_to_session"
) as mock_return, async_mock.patch.object(
conductor, "queue_outbound", async_mock.CoroutineMock()
) as mock_queue:
mock_return.return_value = True
await conductor.outbound_message_router(conductor.context, message)
mock_return.assert_called_once_with(message)
mock_queue.assert_not_awaited()
示例8: test_admin
# 需要导入模块: from asynctest import mock [as 别名]
# 或者: from asynctest.mock import patch [as 别名]
def test_admin(self):
builder: ContextBuilder = StubContextBuilder(self.test_settings)
builder.update_settings({"admin.enabled": "1"})
conductor = test_module.Conductor(builder)
await conductor.setup()
admin = await conductor.context.inject(BaseAdminServer)
assert admin is conductor.admin_server
with async_mock.patch.object(
admin, "start", autospec=True
) as admin_start, async_mock.patch.object(
admin, "stop", autospec=True
) as admin_stop:
await conductor.start()
admin_start.assert_awaited_once_with()
await conductor.stop()
admin_stop.assert_awaited_once_with()
示例9: test_admin_startx
# 需要导入模块: from asynctest import mock [as 别名]
# 或者: from asynctest.mock import patch [as 别名]
def test_admin_startx(self):
builder: ContextBuilder = StubContextBuilder(self.test_settings)
builder.update_settings({"admin.enabled": "1", "debug.print_invitation": "1"})
conductor = test_module.Conductor(builder)
await conductor.setup()
admin = await conductor.context.inject(BaseAdminServer)
assert admin is conductor.admin_server
with async_mock.patch.object(
admin, "start", async_mock.CoroutineMock()
) as admin_start, async_mock.patch.object(
admin, "stop", autospec=True
) as admin_stop, async_mock.patch.object(
test_module, "ConnectionManager"
) as conn_mgr:
admin_start.side_effect = KeyError("trouble")
conn_mgr.return_value.create_invitation(
side_effect=KeyError("more trouble")
)
await conductor.start()
admin_start.assert_awaited_once_with()
await conductor.stop()
admin_stop.assert_awaited_once_with()
示例10: test_start_x_in
# 需要导入模块: from asynctest import mock [as 别名]
# 或者: from asynctest.mock import patch [as 别名]
def test_start_x_in(self):
builder: ContextBuilder = StubContextBuilder(self.test_settings)
builder.update_settings({"debug.test_suite_endpoint": True})
conductor = test_module.Conductor(builder)
with async_mock.patch.object(
test_module, "ConnectionManager"
) as mock_mgr, async_mock.patch.object(
test_module, "InboundTransportManager"
) as mock_intx_mgr:
mock_intx_mgr.return_value = async_mock.MagicMock(
setup=async_mock.CoroutineMock(),
start=async_mock.CoroutineMock(side_effect=KeyError("trouble")),
)
await conductor.setup()
mock_mgr.return_value.create_static_connection = async_mock.CoroutineMock()
with self.assertRaises(KeyError):
await conductor.start()
示例11: test_start_x_out
# 需要导入模块: from asynctest import mock [as 别名]
# 或者: from asynctest.mock import patch [as 别名]
def test_start_x_out(self):
builder: ContextBuilder = StubContextBuilder(self.test_settings)
builder.update_settings({"debug.test_suite_endpoint": True})
conductor = test_module.Conductor(builder)
with async_mock.patch.object(
test_module, "ConnectionManager"
) as mock_mgr, async_mock.patch.object(
test_module, "OutboundTransportManager"
) as mock_outx_mgr:
mock_outx_mgr.return_value = async_mock.MagicMock(
setup=async_mock.CoroutineMock(),
start=async_mock.CoroutineMock(side_effect=KeyError("trouble")),
)
await conductor.setup()
mock_mgr.return_value.create_static_connection = async_mock.CoroutineMock()
with self.assertRaises(KeyError):
await conductor.start()
示例12: test_simple_handler_200_response
# 需要导入模块: from asynctest import mock [as 别名]
# 或者: from asynctest.mock import patch [as 别名]
def test_simple_handler_200_response(self):
"""
Tests if a response is correctly handled, Starts a real aiohttp server
"""
@self.app.route(["/"], type=RouteTypes.HTTP, methods=["GET"])
async def index():
return web.json_response({"OK": True})
async with HttpClientContext(self.app) as client:
settings_mock = Settings()
with mock.patch(
"asyncworker.signals.handlers.http.settings", settings_mock
):
resp = await client.get("/")
self.assertEqual(resp.status, 200)
data = await resp.json()
self.assertDictEqual({"OK": True}, data)
示例13: test_add_registry_to_all_requests
# 需要导入模块: from asynctest import mock [as 别名]
# 或者: from asynctest.mock import patch [as 别名]
def test_add_registry_to_all_requests(self):
@self.app.route(["/"], type=RouteTypes.HTTP, methods=["GET"])
async def handler(wrapper: RequestWrapper):
request = wrapper.http_request
registry: TypesRegistry = request["types_registry"]
assert registry is not None
assert isinstance(registry, TypesRegistry)
return web.json_response({})
async with HttpClientContext(self.app) as client:
settings_mock = Settings()
with mock.patch(
"asyncworker.signals.handlers.http.settings", settings_mock
):
resp = await client.get("/")
self.assertEqual(200, resp.status)
示例14: test_resolves_handler_parameters_when_receiving_request_wrapper
# 需要导入模块: from asynctest import mock [as 别名]
# 或者: from asynctest.mock import patch [as 别名]
def test_resolves_handler_parameters_when_receiving_request_wrapper(
self
):
def my_decorator(handler):
async def _wrapper(wrapper: RequestWrapper):
return await call_http_handler(wrapper.http_request, handler)
return _wrapper
@self.app.route(["/"], type=RouteTypes.HTTP, methods=["GET"])
@my_decorator
async def handler(wrapper: RequestWrapper):
return web.json_response({"num": wrapper.http_request.query["num"]})
async with HttpClientContext(self.app) as client:
settings_mock = Settings()
with mock.patch(
"asyncworker.signals.handlers.http.settings", settings_mock
):
resp = await client.get("/", params={"num": 42})
self.assertEqual(200, resp.status)
resp_data = await resp.json()
self.assertEqual({"num": "42"}, resp_data)
示例15: test_handler_decorator_can_receive_aiohttp_request
# 需要导入模块: from asynctest import mock [as 别名]
# 或者: from asynctest.mock import patch [as 别名]
def test_handler_decorator_can_receive_aiohttp_request(self):
def my_decorator(handler):
async def _wrapper(request: web.Request):
return await call_http_handler(request, handler)
return _wrapper
@self.app.route(["/"], type=RouteTypes.HTTP, methods=["GET"])
@my_decorator
async def handler(wrapper: RequestWrapper):
return web.json_response({"num": wrapper.http_request.query["num"]})
async with HttpClientContext(self.app) as client:
settings_mock = Settings()
with mock.patch(
"asyncworker.signals.handlers.http.settings", settings_mock
):
resp = await client.get("/", params={"num": 42})
self.assertEqual(200, resp.status)
resp_data = await resp.json()
self.assertEqual({"num": "42"}, resp_data)