本文整理汇总了Python中mock.MagicMock.method方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.method方法的具体用法?Python MagicMock.method怎么用?Python MagicMock.method使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mock.MagicMock
的用法示例。
在下文中一共展示了MagicMock.method方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_process
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import method [as 别名]
def test_process(self):
request = MagicMock()
request.method = 'GET'
step = SimpleStep(request)
assert isinstance(step.process(), TemplateResponse)
request.method = 'POST'
assert step.process() is None
示例2: test_process
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import method [as 别名]
def test_process(self):
request = MagicMock()
request.method = 'GET'
step = SimpleStep(request)
self.assertEqual(type(step.process()), TemplateResponse)
request.method = 'POST'
self.assertEqual(step.process(), None)
示例3: test_render_proc_ts
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import method [as 别名]
def test_render_proc_ts(self):
"""
**test_render_proc_ts** * Test that items are grabbed correctly
"""
with patch('cr_hunt.views.add_hunt_its') as addf:
with patch('cr_hunt.views.redirect') as red:
request = MagicMock()
request.method = "POST"
def side_effect(*args):
def second_call(*args):
return 'test321'
request.POST.get.side_effect = second_call
return 'test123'
request.POST.get = MagicMock(side_effect=side_effect)
#request.POST.get('title', '').return_value = 'test123'
#request.POST.get('start', '').return_value = 'test321'
request.user = MagicMock()
attrs = {'is_authenticated.return_value':True}
request.user.configure_mock(**attrs)
request.user.username = 'user1'
views.render_proc_ts(request)
assert addf.called
addf.assert_called_with('111222', 'test123', 'test321', 'user1')
assert red.called #test that redirect is called
red.assert_called_with('cr_aitem')
red.reset_mock()
attrs = {'is_authenticated.return_value':False}
request.user.configure_mock(**attrs)
views.render_proc_ts(request)
assert red.called
red.assert_called_with('cr_error')
示例4: test_render_fail_disconnected
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import method [as 别名]
def test_render_fail_disconnected(self):
json = JSON()
request = MagicMock()
request.method = 'POST'
request._disconnected = True
# When disconnected, returns empty string
self.assertEqual(json.render(request), '')
示例5: _make_request
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import method [as 别名]
def _make_request(
self,
method, postpath, reqbody,
resreadsreq, rescode, resbody):
m_request = MagicMock(name='Request')
m_request.method = method
m_request.postpath = postpath
if reqbody is None:
readrv = ''
elif reqbody == 'mangled JSON':
readrv = reqbody
else:
readrv = json.dumps(reqbody, indent=2)
m_request.content.read.return_value = readrv
r = self.tar.render(m_request)
self.assertEqual(r, server.NOT_DONE_YET)
expected = [
call.setResponseCode(rescode),
call.setHeader('Content-Type', 'application/json'),
call.write(json.dumps(resbody, indent=2)),
call.finish(),
]
if resreadsreq:
expected.insert(0, call.content.read())
check_mock(self, m_request, expected)
示例6: test_resolve_non_existing_endpoint
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import method [as 别名]
def test_resolve_non_existing_endpoint(self):
mock_http_request = MagicMock()
mock_http_request.method = "DELETE"
mock_api = MagicMock(spec_set=[])
action = action_resolver.resolve_action(mock_http_request, api=mock_api, url="foo")
assert_that(action, equal_to(None))
示例7: test_unit_register_with_POST
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import method [as 别名]
def test_unit_register_with_POST(self):
"""
We need to mock several things to make this unittest.
1. request
2. register_form() : For this, make seperate test.
- request has **method** attribute that specifies the method of the form.
3. Need to mock RegisterForm because we are not testing the register function.
- RegisterForm instance should have is_valid() method that returns boolean.
4. We need to mock HttpResponseRedirect.
"""
with patch('user_auth.views.RegisterForm') as form:
with patch('user_auth.views.HttpResponseRedirect') as redir:
request = MagicMock()
request.method = "POST" ## this will pass if request.POST == "POST" part.
request.POST = "some_input"
form.return_value = Mock()
form.return_value.is_valid = Mock(return_value=True)
# mock register_form
form.return_value.register_form = Mock(return_value=None)
register(request)
# make function call
form.assert_called_with("some_input")
# redir.assert_called_with('/')
self.assertTrue(form.return_value.register_form.called)
示例8: test_unit_register_with_invalidPOST
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import method [as 别名]
def test_unit_register_with_invalidPOST(self):
"""
This function tests the **register()** function with invalid post data.
we should mock
1. request
2. RegisterForm
- is_valid()
3. args
"""
with patch('user_auth.views.RegisterForm') as reg:
request = MagicMock()
request.method = "POST"
request.POST = "some input" # make input to RegisterForm
reg.return_value = Mock()
reg.return_value.is_valid = Mock(return_value=False) # make it pass the if part.
reg.return_value.register_form = Mock(return_value=None)
with patch('user_auth.views.render_to_response') as render:
render.return_value = "some value"
register(request)
# asserts
reg.assert_called_with("some input")
self.assertTrue(not reg.return_value.register_form.called)
self.assertTrue(render.called)
示例9: test_unit_login_user_with_post
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import method [as 别名]
def test_unit_login_user_with_post(self):
"""
This function tests **login_user()** with post data.
We need to mock
1. request
2. LogInForm
- is_valid
- cleaned_data
3. authenticate
- is_active
4. render_to_response
"""
with patch('user_auth.views.LogInForm') as form:
request = MagicMock()
request.method = "POST"
request.POST = "some data"
form.return_value = Mock()
form.return_value.is_valid = Mock(return_value=True)
form.return_value.login_process = Mock()
with patch('user_auth.views.render_to_response') as render:
login_user(request)
form.return_value.login_process.assert_called_with(request)
form.assert_called_with("some data")
self.assertTrue(render.called)
示例10: TestCommandParsing
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import method [as 别名]
def TestCommandParsing(self):
request = MagicMock()
request.path = "/test"
request.query = "item1=value1"
request.remote_ip = "1.2.3.4"
request.method = "PUT"
request.headers = {
'User': 'test_user',
'Creation-Time': 'test_time',
'User-Agent': 'test_user_agent'
}
request.body="item2=value2&item2=value3&item2=value4"
self.command = Command(request)
self.assertEqual(self.command.command_name, 'test')
self.assertEqual(self.command.command_type, 'PUT')
self.assertEqual(self.command.has_param('item1'), True)
self.assertEqual(self.command.has_param('item2'), True)
self.assertEqual(self.command.has_param('item3'), False)
self.assertEqual(self.command.get_param('item1'), 'value1')
self.assertEqual('value2' in self.command.get_param('item2'), True)
self.assertEqual('value3' in self.command.get_param('item2'), True)
self.assertEqual('value4' in self.command.get_param('item2'), True)
data = self.command.format_trace
self.assertEqual(data['Username'], 'test_user')
self.assertEqual(data['Created'], 'test_time')
self.assertEqual(data['Source_Address'], '1.2.3.4')
self.assertEqual(data['Source_ID'], 'test_user_agent')
示例11: assert_expire_in_extended
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import method [as 别名]
def assert_expire_in_extended(self, req_path, should_expire_in, should_extend, headers=None, query_string=None):
request = MagicMock(path=req_path)
request.method = "GET"
request.user = MagicMock()
request.user.is_authenticated.return_value = True
thirty_mins_ago = (datetime.datetime.now() - datetime.timedelta(minutes=30)).strftime("%Y-%m-%dT%H:%M:%S.%f")
request.session = {"_session_security": thirty_mins_ago}
if headers:
request.META = headers
if query_string:
request.REQUEST = query_string
before_response = {}
response = self.middleware.process_response(request, before_response)
self.assertIsNotNone(response.get("Session-Expires-In"))
self.assertIsAlmostEqualMinutes(response.get("Session-Expires-In"), should_expire_in - 30, delta=1.0)
self.middleware.process_request(request)
response2 = self.middleware.process_response(request, before_response)
self.assertIsNotNone(response2.get("Session-Expires-In"))
if should_extend:
self.assertIsAlmostEqualMinutes(response.get("Session-Expires-In"), should_expire_in, delta=1.0)
else:
self.assertIsAlmostEqualMinutes(response.get("Session-Expires-In"), should_expire_in - 30, delta=1.0)
示例12: test_process_response_works_not_logged_in
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import method [as 别名]
def test_process_response_works_not_logged_in(self):
# this should not add header
request = MagicMock(path="/")
request.method = "GET"
before_response = {}
response = self.middleware.process_response(request, before_response)
self.assertIsNone(response.get("Session-Expires-In"))
示例13: test_resolve_non_existing_action_options
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import method [as 别名]
def test_resolve_non_existing_action_options(self):
mock_http_request = MagicMock()
mock_http_request.method = "OPTIONS"
mock_api = MagicMock()
mock_resource = object()
mock_api.resolve_resource.return_value = mock_resource
action = action_resolver.resolve_action(mock_http_request, api=mock_api, url="foo")
assert_that(action, equal_to(None))
示例14: test_upstream_xauth
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import method [as 别名]
def test_upstream_xauth(self):
req = MagicMock()
req.headers = {}
req.method = 'GET'
req.path = '/v2/servers'
req.env = {'REMOTE_USER': 'jsmith'}
resp = MagicMock()
validate_token(req, resp, {})
self.assertIsNone(req.env.get('auth'))
示例15: test_upstream_admin
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import method [as 别名]
def test_upstream_admin(self):
req = MagicMock()
req.headers = {}
req.method = 'GET'
req.path = '/v2/servers'
req.env = {'is_admin': True}
resp = MagicMock()
validate_token(req, resp, {})
self.assertIsNone(req.env.get('auth'))