本文整理汇总了Python中unittest.mock.Mock.get方法的典型用法代码示例。如果您正苦于以下问题:Python Mock.get方法的具体用法?Python Mock.get怎么用?Python Mock.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unittest.mock.Mock
的用法示例。
在下文中一共展示了Mock.get方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_route_request_with_id
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import get [as 别名]
def test_route_request_with_id(self, mock_import_module):
# Setup
mock_module = Mock()
mock_get = Mock()
mock_get.return_value = "raw data response"
mock_module.get = mock_get
mock_import_module.return_value = mock_module
server.CONFIG.get.return_value = '{"test_api": ["GET"]}'
self.mock_request.method = 'GET'
# Preconditions
# Run the test
response = server.route_request("test_api/123.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_get.call_args[0][1]
self.assertDictEqual(get_call_args, {
'id': '123'
})
示例2: test_route_request_with_missing_method
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import get [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)
示例3: testBuild_namespace
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import get [as 别名]
def testBuild_namespace(self):
file_path = ['folder1','folder2','src','folder3','folder4','test.php']
settings = Mock()
settings.get = Mock(return_value=['src','tests'])
view = Mock()
view.settings = Mock(return_value=settings)
view.file_name = Mock(return_value=os.sep.join(file_path))
assert build_namespace(view) == 'folder3\\folder4'
示例4: tests_load_session_fail
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import get [as 别名]
def tests_load_session_fail(self):
async def sess_get(key):
return aioredis.ProtocolError
redis_mock = Mock()
redis_mock.get = sess_get
res = None
with self.assertLogs():
self.loop.run_until_complete(self.handler.analyze(None, redis_mock))
示例5: test_update_false
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import get [as 别名]
def test_update_false(self, mock_httplib2):
"""Test that the update returns False upon Error."""
mock_service = Mock()
mock_service.get = Mock(
side_effect=mock_httplib2.ServerNotFoundError("unit test"))
cal = calendar.GoogleCalendarEventDevice(self.hass, mock_service, None,
{'name': "test"})
result = cal.data.update()
assert not result
示例6: test_on_post_private_token_fails
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import get [as 别名]
def test_on_post_private_token_fails(self):
#setup data
self.set_data({'private_token': '180e0f54-076e-4a3f-a1bb-a557b0868f5d'})
#setup mocks
User = Mock()
self.view._model = Mock(return_value=User)
User.get = Mock(side_effect=Exception)
with assert_raises(falcon.HTTPForbidden):
self.view.on_post(self.req, self.res)
示例7: should_create_file
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import get [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}}]})
示例8: requests_factory
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import get [as 别名]
def requests_factory(text='some text',
location='some location',
content=b'some content'):
requests = Mock()
get_response = Mock()
get_response.text = text
get_response.content = content
head_response = Mock()
head_response.headers = {'Location': location}
requests.get = Mock(return_value=get_response)
requests.head = Mock(return_value=head_response)
return requests
示例9: test_on_post_name_fails_beacause_no_password
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import get [as 别名]
def test_on_post_name_fails_beacause_no_password(self):
#setup data
self.set_data({'name': 'user'})
#setup mocks
User = Mock()
user = Mock()
self.view._model = Mock(return_value=User)
User.get = Mock(return_value=user)
#action
with assert_raises(falcon.HTTPBadRequest):
self.view.on_post(self.req, self.res)
#assertions
User.get.assert_called_once_with(name='user')
示例10: test_create_stats
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import get [as 别名]
def test_create_stats(self):
async def sess_get():
return session
async def set_of_members(key):
return set()
async def set_add():
return ''
redis_mock = Mock()
redis_mock.get = sess_get
redis_mock.smembers = set_of_members
redis_mock.zadd = set_add
with patch('builtins.open', new_callable=mock_open) as m:
stats = self.loop.run_until_complete(self.handler.create_stats(self.session, redis_mock))
self.assertEqual(stats['possible_owners'], {'attacker': 1.0})
示例11: test_on_post_private_token
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import get [as 别名]
def test_on_post_private_token(self, uuid_mock):
#setup data
self.set_data({'private_token': '180e0f54-076e-4a3f-a1bb-a557b0868f5d'})
#setup mocks
User = Mock()
user = Mock()
self.view._model = Mock(return_value=User)
User.get = Mock(return_value=user)
user.ttl = Mock(return_value=user)
user.update = Mock(return_value=user)
uuid_mock.return_value = '<TOKEN-UUID>'
#action
self.view.on_post(self.req, self.res)
#assertions
User.get.assert_called_once_with(private_token='180e0f54-076e-4a3f-a1bb-a557b0868f5d')
user.ttl.assert_called_once_with(3600)
user.update.assert_called_once_with(auth_token='<TOKEN-UUID>')
示例12: test_on_post_name_password_fails
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import get [as 别名]
def test_on_post_name_password_fails(self):
#setup data
self.set_data({'name': 'user', 'password': 'secret'})
#setup mocks
User = Mock()
user = Mock()
self.view._model = Mock(return_value=User)
User.get = Mock(return_value=user)
user.ttl = Mock(return_value=user)
user.password = 'hashed-secret'
user.make_password = Mock(return_value='different-hashed-secret')
user.update = Mock(return_value=user)
#action
with assert_raises(falcon.HTTPForbidden):
self.view.on_post(self.req, self.res)
#assertions
User.get.assert_called_once_with(name='user')
user.make_password.assert_called_once_with('secret')
示例13: test_on_post_name_password
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import get [as 别名]
def test_on_post_name_password(self, uuid_mock):
#setup data
self.set_data({'name': 'user', 'password': 'secret'})
#setup mocks
User = Mock()
user = Mock()
self.view._model = Mock(return_value=User)
User.get = Mock(return_value=user)
user.ttl = Mock(return_value=user)
user.password = 'hashed-secret'
user.make_password = Mock(return_value='hashed-secret')
user.update = Mock(return_value=user)
uuid_mock.return_value = '<TOKEN-UUID>'
#action
self.view.on_post(self.req, self.res)
#assertions
User.get.assert_called_once_with(name='user')
user.make_password.assert_called_once_with('secret')
user.ttl.assert_called_once_with(3600)
user.update.assert_called_once_with(auth_token='<TOKEN-UUID>')