本文整理汇总了Python中unittest.mock.Mock方法的典型用法代码示例。如果您正苦于以下问题:Python mock.Mock方法的具体用法?Python mock.Mock怎么用?Python mock.Mock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unittest.mock
的用法示例。
在下文中一共展示了mock.Mock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mock_get_build_data
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import Mock [as 别名]
def mock_get_build_data(drydock_state):
def side_effect(**kwargs):
build_data = objects.BuildData(
node_name="test",
task_id="tid",
generator="lshw",
data_format="text/plain",
data_element="<mocktest></mocktest>")
return [build_data]
drydock_state.real_get_build_data = drydock_state.get_build_data
drydock_state.get_build_data = Mock(side_effect=side_effect)
yield
drydock_state.get_build_data = Mock(wraps=None, side_effect=None)
drydock_state.get_build_data = drydock_state.real_get_build_data
示例2: test_apply_logicalnames_else
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import Mock [as 别名]
def test_apply_logicalnames_else(self, input_files, deckhand_orchestrator,
drydock_state, mock_get_build_data):
"""Test node apply_logicalnames hits the else block"""
input_file = input_files.join("deckhand_fullsite.yaml")
design_ref = "file://%s" % str(input_file)
design_status, design_data = deckhand_orchestrator.get_effective_site(
design_ref)
def side_effect(**kwargs):
return []
drydock_state.get_build_data = Mock(side_effect=side_effect)
nodes = design_data.baremetal_nodes
for n in nodes or []:
n.apply_logicalnames(design_data, state_manager=drydock_state)
assert n.logicalnames == {}
示例3: test_contextual
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import Mock [as 别名]
def test_contextual(self, translation):
lang = mock.Mock()
translation.return_value = lang
trans = mock.Mock()
trans.return_value = 'translated'
lang.gettext = trans
lang.ugettext = trans
result = _message.Message._translate_msgid(
('context', 'message'),
domain='domain',
has_contextual_form=True,
has_plural_form=False,
)
self.assertEqual('translated', result)
trans.assert_called_with(
'context' + _message.CONTEXT_SEPARATOR + 'message'
)
示例4: test_contextual_untranslatable
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import Mock [as 别名]
def test_contextual_untranslatable(self, translation):
msg_with_context = 'context' + _message.CONTEXT_SEPARATOR + 'message'
lang = mock.Mock()
translation.return_value = lang
trans = mock.Mock()
trans.return_value = msg_with_context
lang.gettext = trans
lang.ugettext = trans
result = _message.Message._translate_msgid(
('context', 'message'),
domain='domain',
has_contextual_form=True,
has_plural_form=False,
)
self.assertEqual('message', result)
trans.assert_called_with(msg_with_context)
示例5: test_voice_recording_view
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import Mock [as 别名]
def test_voice_recording_view(self):
http_client = Mock()
http_client.request.return_value = '{"data":[{"id":"12345678-9012-3456-7890-123456789012","format":"wav","legId":"87654321-0987-6543-2109-876543210987","status":"done","duration":32,"type":"transfer","createdAt":"2018-01-01T00:00:01Z","updatedAt":"2018-01-01T00:00:05Z","deletedAt":null}],"_links":{"file":"/calls/12348765-4321-0987-6543-210987654321/legs/87654321-0987-6543-2109-876543210987/recordings/12345678-9012-3456-7890-123456789012.wav","self":"/calls/12345678-9012-3456-7890-123456789012/legs/12348765-4321-0987-6543-210987654321/recordings/12345678-9012-3456-7890-123456789012"},"pagination":{"totalCount":0,"pageCount":0,"currentPage":0,"perPage":0}}'
voice_recording = Client('', http_client).voice_recording_view(
'12348765-4321-0987-6543-210987654321',
'87654321-0987-6543-2109-876543210987',
'12345678-9012-3456-7890-123456789012'
)
http_client.request.assert_called_once_with(
'https://voice.messagebird.com/calls/12348765-4321-0987-6543-210987654321/legs/87654321-0987-6543-2109-876543210987/recordings/12345678-9012-3456-7890-123456789012',
'GET', None)
self.assertEqual('12345678-9012-3456-7890-123456789012', voice_recording.id)
self.assertEqual('done', voice_recording.status)
self.assertEqual('wav', voice_recording.format)
self.assertEqual(datetime(2018, 1, 1, 0, 0, 1, tzinfo=tzutc()), voice_recording.createdAt)
self.assertEqual(datetime(2018, 1, 1, 0, 0, 5, tzinfo=tzutc()), voice_recording.updatedAt)
self.assertEqual(2, len(voice_recording._links))
self.assertIsInstance(str(voice_recording), str)
示例6: test_create_message
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import Mock [as 别名]
def test_create_message(self):
http_client = Mock()
http_client.request.return_value = '{"id":"id","conversationId":"conversation-id","channelId":"channel-id","type":"text","content":{"text":"Example Text Message"},"direction":"sent","status":"pending","createdDatetime":"2019-04-02T11:57:52.142641447Z","updatedDatetime":"2019-04-02T11:57:53.142641447Z"}'
data = {
'channelId': 1234,
'type': 'text',
'content': {
'text': 'this is a message'
},
}
msg = Client('', http_client).conversation_create_message('conversation-id', data)
self.assertEqual(datetime(2019, 4, 2, 11, 57, 53, tzinfo=tzutc()), msg.updatedDatetime)
self.assertEqual(datetime(2019, 4, 2, 11, 57, 52, tzinfo=tzutc()), msg.createdDatetime)
http_client.request.assert_called_once_with('conversations/conversation-id/messages', 'POST', data)
示例7: test_voice_create_webhook
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import Mock [as 别名]
def test_voice_create_webhook(self):
http_client = Mock()
http_client.request.return_value = '''{
"data": [
{
"id": "534e1848-235f-482d-983d-e3e11a04f58a",
"url": "https://example.com/",
"token": "foobar",
"createdAt": "2017-03-15T14:10:07Z",
"updatedAt": "2017-03-15T14:10:07Z"
}
],
"_links": {
"self": "/webhooks/534e1848-235f-482d-983d-e3e11a04f58a"
}
}'''
create_webhook_request = VoiceCreateWebhookRequest(url="https://example.com/", title="FooBar", token="foobar")
created_webhook = Client('', http_client).voice_create_webhook(create_webhook_request)
http_client.request.assert_called_once_with(VOICE_API_ROOT + '/' + VOICE_WEB_HOOKS_PATH, 'POST',
create_webhook_request.__dict__())
self.assertEqual(create_webhook_request.url, created_webhook.url)
self.assertEqual(create_webhook_request.token, created_webhook.token)
示例8: test_voice_update_webhook
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import Mock [as 别名]
def test_voice_update_webhook(self):
http_client = Mock()
http_client.request.return_value = '''{
"data": [
{
"id": "534e1848-235f-482d-983d-e3e11a04f58a",
"url": "https://example.com/baz",
"token": "foobar",
"createdAt": "2017-03-15T13:27:02Z",
"updatedAt": "2017-03-15T13:28:01Z"
}
],
"_links": {
"self": "/webhooks/534e1848-235f-482d-983d-e3e11a04f58a"
}
}'''
webhook_id = '534e1848-235f-482d-983d-e3e11a04f58a'
update_webhook_request = VoiceUpdateWebhookRequest(title="FooBar", token="foobar")
updated_webhook = Client('', http_client).voice_update_webhook(webhook_id, update_webhook_request)
http_client.request.assert_called_once_with(
VOICE_API_ROOT + '/' + VOICE_WEB_HOOKS_PATH + '/' + webhook_id, 'PUT', update_webhook_request.__dict__())
self.assertEqual(update_webhook_request.token, updated_webhook.token)
示例9: test_voicemessages_list
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import Mock [as 别名]
def test_voicemessages_list(self):
http_client = Mock()
http_client.request.return_value = '{ "offset": 0, "limit": 10, "count": 2, "totalCount": 2, "links": { "first": "https://rest.messagebird.com/voicemessages/?offset=0&limit=30", "previous": null, "next": null, "last": "https://rest.messagebird.com/voicemessages/?offset=0&limit=30" }, "items": [ { "id": "12345678-9012-3456-7890-123456789012", "href": "https://rest.messagebird.com/voicemessages/12345678-9012-3456-7890-123456789012", "originator": null, "body": "This is a test message.", "reference": null, "language": "en-gb", "voice": "male", "repeat": 1, "ifMachine": "continue", "machineTimeout": 7000, "scheduledDatetime": null, "createdDatetime": "2020-02-04T15:15:30+00:00", "recipients": { "totalCount": 1, "totalSentCount": 1, "totalDeliveredCount": 1, "totalDeliveryFailedCount": 0, "items": [ { "recipient": 31612345678, "originator": null, "status": "answered", "statusDatetime": "2020-02-04T15:15:57+00:00" } ] } }, { "id": "12345678-9012-3456-7890-123456789013", "href": "https://rest.messagebird.com/voicemessages/12345678-9012-3456-7890-123456789013", "originator": null, "body": "The voice message to be sent", "reference": null, "language": "en-gb", "voice": "female", "repeat": 1, "ifMachine": "delay", "machineTimeout": 7000, "scheduledDatetime": null, "createdDatetime": "2020-02-04T12:26:44+00:00", "recipients": { "totalCount": 1, "totalSentCount": 1, "totalDeliveredCount": 1, "totalDeliveryFailedCount": 0, "items": [ { "recipient": 31612345678, "originator": null, "status": "answered", "statusDatetime": "2020-02-04T12:27:32+00:00" } ] } } ] }'
voice_messages = Client('', http_client).voice_message_list()
http_client.request.assert_called_once_with(
'voicemessages?limit=10&offset=0', 'GET', None)
voice_messages_check = {
'12345678-9012-3456-7890-123456789012': {
"id": '12345678-9012-3456-7890-123456789012',
"href": "https://rest.messagebird.com/voicemessages/12345678-9012-3456-7890-123456789012"
},
'12345678-9012-3456-7890-123456789013': {
"id": '12345678-9012-3456-7890-123456789013',
"href": "https://rest.messagebird.com/voicemessages/12345678-9012-3456-7890-123456789013"
}
}
for item in voice_messages.items:
message_specific = voice_messages_check.get(item.id)
self.assertEqual(message_specific['id'], item.id)
self.assertEqual(message_specific['href'], item.href)
self.assertIsInstance(str(voice_messages), str)
示例10: test_download_voice_transcription
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import Mock [as 别名]
def test_download_voice_transcription(self):
http_client = Mock()
http_client.request.return_value = ''
url = 'https://voice.messagebird.com/calls/'
call_id = '74bd9fac-742e-4f7c-aef2-bf068c80fd00'
leg_id = 'd931d0e8-3385-43b3-964b-c9dda2581213'
recording_id = '4c444e1e-3cea-4b52-90f7-55dec7b2e05e'
transcription_id = 'fb455b79-c4de-419f-8f72-8c199975c12a'
Client('', http_client).voice_transcription_download(
call_id,
leg_id,
recording_id,
transcription_id
)
http_client.request.assert_called_once_with(
url + call_id + '/legs/' + leg_id + '/recordings/' + recording_id + '/transcriptions/' + transcription_id,
'GET',
None
)
示例11: test_conversation_start
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import Mock [as 别名]
def test_conversation_start(self):
http_client = Mock()
http_client.request.return_value = '{"id":"1234","contactId":"1234","contact":{"id":"1234","href":"https://contacts.messagebird.com/v2/contacts/1234","msisdn":99999999999,"displayName":"99999999999","firstName":"","lastName":"","customDetails":{},"attributes":{},"createdDatetime":"2019-04-02T08:19:37Z","updatedDatetime":"2019-04-02T08:19:38Z"},"channels":[{"id":"1234","name":"channel-name","platformId":"sms","status":"active","createdDatetime":"2019-04-01T15:25:12Z","updatedDatetime":"0001-01-01T00:00:00Z"}],"status":"active","createdDatetime":"2019-04-02T08:19:37Z","updatedDatetime":"2019-04-02T08:54:42.497114599Z","lastReceivedDatetime":"2019-04-02T08:54:42.464955904Z","lastUsedChannelId":"1234","messages":{"totalCount":1,"href":"https://conversations.messagebird.com/v1/conversations/1234/messages"}}'
data = {
'channelId': '1234',
'to': '+99999999999',
'type': "text",
'content': {
'text': 'Message Example'
},
}
msg = Client('', http_client).conversation_start(data)
http_client.request.assert_called_once_with('conversations/start', 'POST', data)
self.assertEqual('1234', msg.id)
self.assertEqual(99999999999, msg.contact.msisdn)
self.assertEqual(datetime(2019, 4, 2, 8, 19, 37, tzinfo=tzutc()), msg.contact.createdDatetime)
self.assertEqual(datetime(2019, 4, 2, 8, 19, 38, tzinfo=tzutc()), msg.contact.updatedDatetime)
self.assertEqual('channel-name', msg.channels[0].name)
示例12: test_conversation_webhook_update
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import Mock [as 别名]
def test_conversation_webhook_update(self):
http_client = Mock()
http_client.request.return_value = json.dumps({"id": "985ae50937a94c64b392531ea87a0263",
"url": "https://example.com/webhook",
"channelId": "853eeb5348e541a595da93b48c61a1ae",
"events": [
"message.created",
"message.updated",
],
"status": "enabled",
"createdDatetime": "2018-08-29T10:04:23Z",
"updatedDatetime": "2018-08-29T10:10:23Z"
})
webhookRequestData = {
'events': [CONVERSATION_WEBHOOK_EVENT_CONVERSATION_CREATED,
CONVERSATION_WEBHOOK_EVENT_CONVERSATION_UPDATED],
'url': 'https://example.com/webhook',
'status': 'enabled'
}
web_hook = Client('', http_client).conversation_update_webhook('webhook-id', webhookRequestData)
http_client.request.assert_called_once_with('webhooks/webhook-id', 'PATCH', webhookRequestData)
示例13: test_message_create
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import Mock [as 别名]
def test_message_create(self):
http_client = Mock()
http_client.request.return_value = '{}'
Client('', http_client).message_create(
'MessageBird', ['31612345678', '31687654321'], 'Hello World', {'datacoding': 'unicode'})
http_client.request.assert_called_once_with(
'messages', 'POST',
{
'datacoding': 'unicode',
'originator': 'MessageBird',
'body': 'Hello World',
'recipients': '31612345678,31687654321'
}
)
示例14: test_execute_push_conflict_s3
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import Mock [as 别名]
def test_execute_push_conflict_s3(self, mock_boto3_session):
mock_client = mock.Mock()
mock_session = mock.Mock()
mock_client.list_objects_v2.return_value = {"Contents": [{"Key": "test_v1.0.0.pkl"}]}
mock_session.client.return_value = mock_client
mock_boto3_session.return_value = mock_session
config = sb.objects.config.Config(version="1.0.0")
args = argparse.Namespace(job="push", force=False, artifact='test', user='sean', token='abc123')
expectedException = "This artifact version already exists. Please bump the version or use the force parameter (-f) to overwrite the artifact."
try:
self.s3.execute(config, args)
self.fail("Exception Not Thrown")
except Exception as exc:
self.assertEqual(str(exc), expectedException)
mock_client.list_objects_v2.assert_called_with(Bucket="my-bucket", Prefix="test_v1.0.0.pkl")
示例15: test_terminal_register_raise_exception
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import Mock [as 别名]
def test_terminal_register_raise_exception(self):
mock = Mock()
mock.post.side_effect = RegisterError("Error")
self.service.http = mock
with self.assertRaises(RegisterError):
self.service.terminal_register("hello")