本文整理汇总了Python中unittest.mock.Mock.post方法的典型用法代码示例。如果您正苦于以下问题:Python Mock.post方法的具体用法?Python Mock.post怎么用?Python Mock.post使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unittest.mock.Mock
的用法示例。
在下文中一共展示了Mock.post方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_route_request_with_missing_method
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import post [as 别名]
def test_route_request_with_missing_method(self, mock_import_module):
# Setup
mock_module = Mock()
mock_post = Mock()
mock_post.return_value = "raw data response"
mock_module.post = mock_post
mock_module.get = None
mock_import_module.return_value = mock_module
server.CONFIG.get.return_value = '{"test_api": ["GET"]}'
self.mock_request.method = 'GET'
self.mock_request.params.decode.return_value = {
'request_data': 'here'
}
# Preconditions
# Run the test
response = server.route_request("test_api.json")
# Postconditions
self.assertEqual(self.mock_abort.call_count, 1)
self.assertEqual(self.mock_abort.call_args[0][0], 503)
self.assertEqual(response, None)
self.assertEqual(self.mock_database.call_count, 0)
示例2: test_route_request_with_bad_output_format
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import post [as 别名]
def test_route_request_with_bad_output_format(self, mock_import_module):
# Setup
mock_module = Mock()
mock_post = Mock()
mock_post.return_value = "raw data response"
mock_module.post = mock_post
mock_import_module.return_value = mock_module
server.CONFIG.get.return_value = '{"test_api": ["POST"]}'
self.mock_request.method = 'POST'
self.mock_request.params.decode.return_value = {
'request_data': 'here'
}
# Preconditions
# Run the test
response = server.route_request("test_api.xml")
# Postconditions
self.assertEqual(self.mock_abort.call_count, 1)
self.assertEqual(self.mock_abort.call_args[0][0], 405)
self.assertEqual(response, None)
self.assertEqual(self.mock_database.call_count, 0)
示例3: test_route_request_with_request_data_and_id_and_path
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import post [as 别名]
def test_route_request_with_request_data_and_id_and_path(self, mock_import_module):
# Setup
mock_module = Mock()
mock_post = Mock()
mock_post.return_value = "raw data response"
mock_module.post = mock_post
mock_import_module.return_value = mock_module
server.CONFIG.get.return_value = '{"test_api": ["POST"]}'
self.mock_request.method = 'POST'
self.mock_request.params.decode.return_value = {
'request_data': 'here'
}
# Preconditions
# Run the test
response = server.route_request("some_id/123/test_api/999.json")
# Postconditions
self.assertEqual(self.mock_abort.call_count, 0)
self.assertEqual(response, "raw data response")
self.assertEqual(self.mock_connection.close.call_count, 1)
get_call_args = mock_post.call_args[0][1]
self.assertDictEqual(get_call_args, {
'some_id': '123',
'id': '999',
'request_data': 'here'
})
示例4: should_create_revision_from_file_obj
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import post [as 别名]
def should_create_revision_from_file_obj(boto_session, getsize):
file = {'type': 'File',
'links': {'self': sentinel.self}}
open_file_obj = sentinel.file_obj
rev = {'_id': 1,
'type': 'Revision',
'attributes': {'url': sentinel.url},
'links': {'upload-complete': sentinel.upload_complete}}
getsize.return_value = 100
s = Mock(spec=ovation.session.Session)
def entity_path(type='', id=''):
return "/api/v1/{}/{}".format(type, id)
s.entity_path.side_effect = entity_path
aws_session = Mock()
s3 = Mock()
boto_session.return_value = aws_session
aws_session.resource = Mock(return_value=s3)
file_obj = Mock()
s3.Object = Mock(return_value=file_obj)
file_obj.upload_file = Mock()
file_obj.version_id = sentinel.version
s.post = Mock(return_value={'entities': [rev],
'aws': [{'aws': dict(access_key_id=sentinel.access_key,
secret_access_key=sentinel.secret_key,
session_token=sentinel.session_token,
bucket=sentinel.bucket,
key=sentinel.key)}]})
s.put = Mock(return_value=sentinel.result)
# Act
result = upload.upload_revision(s,
file,
open_file_obj,
content_type='text/plain',
file_name='my-file',
progress=None)
# Assert
boto_session.assert_called_with(aws_access_key_id=sentinel.access_key,
aws_secret_access_key=sentinel.secret_key,
aws_session_token=sentinel.session_token)
s3.Object.assert_called_with(sentinel.bucket, sentinel.key)
file_obj.upload_fileobj.assert_called_with(open_file_obj,
ExtraArgs={'ContentType': 'text/plain',
'ServerSideEncryption': 'AES256'})
s.put.assert_called_with(sentinel.upload_complete, entity=None)
assert_equal(result, sentinel.result)
示例5: should_create_file
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import post [as 别名]
def should_create_file():
s = Mock(spec=session.Session)
s.get = Mock()
s.get.side_effect = [session.DataDict({'type': 'Project',
'_id': make_uuid(),
'links': session.DataDict({'self': sentinel.parent_self})})]
s.post = Mock()
file = {'entities': [{'type': 'File',
'_id': make_uuid()}]}
s.post.return_value = file
expected_name = 'file name'
core.create_file(s, make_uuid(), expected_name)
s.post.assert_called_once_with(sentinel.parent_self, data={'entities': [{'type': 'File',
'attributes': {'name': expected_name}}]})
示例6: should_set_multipart_chunk_size
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import post [as 别名]
def should_set_multipart_chunk_size(boto_session, getsize):
file = {'type': 'File',
'links': {'self': sentinel.self}}
path = '/local/path/file.txt'
rev = {'_id': 1,
'type': 'Revision',
'attributes': {'url': sentinel.url},
'links': {'upload-complete': sentinel.upload_complete}}
getsize.return_value = sentinel.file_size
s = Mock(spec=ovation.session.Session)
def entity_path(type='', id=''):
return "/api/v1/{}/{}".format(type, id)
s.entity_path.side_effect = entity_path
aws_session = Mock()
s3 = Mock()
boto_session.return_value = aws_session
aws_session.resource = Mock(return_value=s3)
file_obj = Mock()
s3.Object = Mock(return_value=file_obj)
file_obj.upload_file = Mock()
file_obj.version_id = sentinel.version
_chunk_size = Mock(return_value=sentinel.chunk_size)
s.post = Mock(return_value={'entities': [rev],
'aws': [{'aws': dict(access_key_id=sentinel.access_key,
secret_access_key=sentinel.secret_key,
session_token=sentinel.session_token,
bucket=sentinel.bucket,
key=sentinel.key)}]})
s.put = Mock(return_value=sentinel.result)
# Act
upload.upload_revision(s, file, path, chunk_size=_chunk_size, progress=None)
# Assert
call = file_obj.upload_file.call_args_list[0]
assert_equal(call[1]['Config'].multipart_chunksize, sentinel.chunk_size)
示例7: test_comment_errors
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import post [as 别名]
def test_comment_errors(self, mock_session_factoy):
"""
Test if error responses are handled correctly when adding comments.
"""
mock_response = Mock()
mock_response.raise_for_status.side_effect = requests.RequestException()
mock_response.status_code = 400
mock_response.json.return_value = {
"errorMessages": [],
"errors": {
"comment": "Comment body can not be empty!"
}
}
mock_session = Mock()
mock_session.post = Mock(return_value=mock_response)
mock_session_factoy.return_value = mock_session
ticket = jira.JiraTicket(URL, PROJECT, ticket_id=TICKET_ID)
# This should not raise `IndexError: list index out of range` when
# handling error
ticket.add_comment('foobar')