本文整理汇总了Python中mock.MagicMock.params方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.params方法的具体用法?Python MagicMock.params怎么用?Python MagicMock.params使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mock.MagicMock
的用法示例。
在下文中一共展示了MagicMock.params方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: req
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import params [as 别名]
def req():
"""XXX: Very simple mock request, improve this"""
req = MagicMock()
req.header = ["0", "1", "2", "3", "4", "5"]
req.session = {}
req.params = {}
return req
示例2: test_collection_get2
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import params [as 别名]
def test_collection_get2(self):
from lembrar import service
db = MagicMock()
service.get_doc_db_from_request = lambda x: db
request = MagicMock()
request.params = {}
test_ob = service.Docs(request)
request.params['skip'] = 20
test_ob.collection_get()
assert call(skip=20, limit=20) == db.find.mock_calls[0]
示例3: test_date_converter2
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import params [as 别名]
def test_date_converter2(self):
from lembrar.service import date_converter
request = MagicMock()
request.validated = {}
request.params = {}
request.params['created'] = 'crap'
date_converter(request)
assert [call.add('parameters', 'created',
'Cannot parse date format')] \
== request.errors.mock_calls
示例4: test_date_converter1
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import params [as 别名]
def test_date_converter1(self):
from lembrar.service import date_converter
from datetime import datetime
before = datetime.utcnow()
request = MagicMock()
request.validated = {}
request.params = {}
date_converter(request)
after = datetime.utcnow()
assert before <= request.validated['created'] <= after
示例5: test_parameter_validator
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import params [as 别名]
def test_parameter_validator(monkeypatch):
request = MagicMock(name='request')
request.args = {}
request.headers = {}
request.params = {}
app = MagicMock(name='app')
app.response_class = flask.Response
monkeypatch.setattr('flask.request', request)
monkeypatch.setattr('flask.current_app', app)
def orig_handler(*args, **kwargs):
return 'OK'
params = [{'name': 'p1', 'in': 'path', 'type': 'integer', 'required': True},
{'name': 'h1', 'in': 'header', 'type': 'string', 'enum': ['a', 'b']},
{'name': 'q1', 'in': 'query', 'type': 'integer', 'maximum': 3},
{'name': 'a1', 'in': 'query', 'type': 'array', 'minItems': 2, 'maxItems': 3,
'items': {'type': 'integer', 'minimum': 0}}]
validator = ParameterValidator(params, FlaskApi)
handler = validator(orig_handler)
kwargs = {'query': {}, 'headers': {}}
request = MagicMock(path_params={}, **kwargs)
assert json.loads(handler(request).data.decode())['detail'] == "Missing path parameter 'p1'"
request = MagicMock(path_params={'p1': '123'}, **kwargs)
assert handler(request) == 'OK'
request = MagicMock(path_params={'p1': ''}, **kwargs)
assert json.loads(handler(request).data.decode())['detail'] == "Wrong type, expected 'integer' for path parameter 'p1'"
request = MagicMock(path_params={'p1': 'foo'}, **kwargs)
assert json.loads(handler(request).data.decode())['detail'] == "Wrong type, expected 'integer' for path parameter 'p1'"
request = MagicMock(path_params={'p1': '1.2'}, **kwargs)
assert json.loads(handler(request).data.decode())['detail'] == "Wrong type, expected 'integer' for path parameter 'p1'"
request = MagicMock(path_params={'p1': 1}, query={'q1': '4'}, headers={})
assert json.loads(handler(request).data.decode())['detail'].startswith('4 is greater than the maximum of 3')
request = MagicMock(path_params={'p1': 1}, query={'q1': '3'}, headers={})
assert handler(request) == 'OK'
request = MagicMock(path_params={'p1': 1}, query={'a1': ['1', '2']}, headers={})
assert handler(request) == "OK"
request = MagicMock(path_params={'p1': 1}, query={'a1': ['1', 'a']}, headers={})
assert json.loads(handler(request).data.decode())['detail'].startswith("'a' is not of type 'integer'")
request = MagicMock(path_params={'p1': 1}, query={'a1': ['1', '-1']}, headers={})
assert json.loads(handler(request).data.decode())['detail'].startswith("-1 is less than the minimum of 0")
request = MagicMock(path_params={'p1': 1}, query={'a1': ['1']}, headers={})
assert json.loads(handler(request).data.decode())['detail'].startswith("[1] is too short")
request = MagicMock(path_params={'p1': 1}, query={'a1': ['1', '2', '3', '4']}, headers={})
assert json.loads(handler(request).data.decode())['detail'].startswith("[1, 2, 3, 4] is too long")
request = MagicMock(path_params={'p1': '123'}, query={}, headers={'h1': 'a'})
assert handler(request) == 'OK'
request = MagicMock(path_params={'p1': '123'}, query={}, headers={'h1': 'x'})
assert json.loads(handler(request).data.decode())['detail'].startswith("'x' is not one of ['a', 'b']")
示例6: test_collection_put
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import params [as 别名]
def test_collection_put(self):
from lembrar import service
db = MagicMock()
service.get_doc_db_from_request = lambda x: db
request = MagicMock()
request.params = dict(test='test1', file=MagicMock())
test_ob = service.Docs(request)
test_ob.collection_put()
assert [call(request.params['file'].file,
request.validated['created'], test='test1')] \
== db.add_one.mock_calls
示例7: test_collection_get1
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import params [as 别名]
def test_collection_get1(self):
from lembrar import service
db = MagicMock()
service.get_doc_db_from_request = lambda x: db
request = MagicMock()
request.params = {}
test_ob = service.Docs(request)
fake_doc = MagicMock()
def find(limit, skip):
return [fake_doc]
db.find = find
assert [fake_doc.to_jsonable_dict()] == test_ob.collection_get()
示例8: make_request
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import params [as 别名]
def make_request(secure, host, path, params):
request = MagicMock()
# Generate the string of paramaters1
params_str = ''
for param in params:
params_str += '%s=%s&' % (param, params[param])
secure = 's' if secure else ''
request.url = 'http%s://%s/%s?%s' % (secure, host, path, params_str)
request.host = host
request.host_url = 'http%s://%s' % (secure, host)
request.params = params
return request
示例9: test_injection
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import params [as 别名]
def test_injection(monkeypatch):
request = MagicMock(name='request', path_params={'p1': '123'})
request.args = {}
request.headers = {}
request.params = {}
func = MagicMock()
def handler(**kwargs):
func(**kwargs)
parameter_to_arg({}, [], handler)(request)
func.assert_called_with(p1='123')
示例10: test_parameter_validator
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import params [as 别名]
def test_parameter_validator(monkeypatch):
request = MagicMock(name='request')
request.args = {}
request.headers = {}
request.params = {}
app = MagicMock(name='app')
app.response_class = lambda a, mimetype, status: json.loads(''.join(a))['detail']
monkeypatch.setattr('flask.request', request)
monkeypatch.setattr('flask.current_app', app)
def orig_handler(*args, **kwargs):
return 'OK'
params = [{'name': 'p1', 'in': 'path', 'type': 'integer', 'required': True},
{'name': 'h1', 'in': 'header', 'type': 'string', 'enum': ['a', 'b']},
{'name': 'q1', 'in': 'query', 'type': 'integer', 'maximum': 3},
{'name': 'a1', 'in': 'query', 'type': 'array', 'minItems': 2, 'maxItems': 3,
'items': {'type': 'integer', 'minimum': 0}}]
validator = ParameterValidator(params)
handler = validator(orig_handler)
assert handler() == "Missing path parameter 'p1'"
assert handler(p1='123') == 'OK'
assert handler(p1='') == "Wrong type, expected 'integer' for path parameter 'p1'"
assert handler(p1='foo') == "Wrong type, expected 'integer' for path parameter 'p1'"
assert handler(p1='1.2') == "Wrong type, expected 'integer' for path parameter 'p1'"
request.args = {'q1': '4'}
assert handler(p1=1).startswith('4 is greater than the maximum of 3')
request.args = {'q1': '3'}
assert handler(p1=1) == 'OK'
request.args = {'a1': "1,2"}
assert handler(p1=1) == "OK"
request.args = {'a1': "1,a"}
assert handler(p1=1).startswith("'a' is not of type 'integer'")
request.args = {'a1': "1,-1"}
assert handler(p1=1).startswith("-1 is less than the minimum of 0")
request.args = {'a1': "1"}
assert handler(p1=1).startswith("[1] is too short")
request.args = {'a1': "1,2,3,4"}
assert handler(p1=1).startswith("[1, 2, 3, 4] is too long")
del request.args['a1']
request.headers = {'h1': 'a'}
assert handler(p1='123') == 'OK'
request.headers = {'h1': 'x'}
assert handler(p1='123').startswith("'x' is not one of ['a', 'b']")
示例11: test_date_converter3
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import params [as 别名]
def test_date_converter3(self):
from lembrar.service import date_converter
from datetime import datetime
request = MagicMock()
request.validated = {}
request.params = {}
request.params['created'] = '2011-07-22 10:11:33'
date_converter(request)
assert datetime(
2011,
7,
22,
10,
11,
33,
) == request.validated['created']
示例12: test_list_members
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import params [as 别名]
def test_list_members(self):
collection_handler = webservice.CollectionHandler(r'/', '')
collection_handler.model = webservice.SimpleDataModel("test")
request = MagicMock()
request.body = '{"key": "value"}'
request.params = MagicMock()
request.path = "/"
response = collection_handler.list_members(request)
items = collection_handler.model.get_items(
request.params,
context=collection_handler._get_context(request))
expected_body = "%s\n" % json.dumps(items, indent=2)
self.assertEqual('application/json', response.content_type)
self.assertEqual(expected_body, response.body)
self.assertEqual('application/json', response.content_type)
self.assertEqual(str(httplib.OK) + " OK", response.status)
示例13: test_parameter_validator
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import params [as 别名]
def test_parameter_validator(monkeypatch):
request = MagicMock(name='request')
request.args = {}
request.headers = {}
request.params = {}
app = MagicMock(name='app')
app.response_class = lambda a, mimetype, status: json.loads(a)['detail']
monkeypatch.setattr('flask.request', request)
monkeypatch.setattr('flask.current_app', app)
def orig_handler(*args, **kwargs):
return 'OK'
params = [{'name': 'p1', 'in': 'path', 'type': 'integer', 'required': True},
{'name': 'h1', 'in': 'header', 'type': 'string', 'enum': ['a', 'b']},
{'name': 'q1', 'in': 'query', 'type': 'integer', 'maximum': 3},
{'name': 'a1', 'in': 'query', 'type': 'array', 'items': {'type': 'integer', 'minimum': 0}}]
validator = ParameterValidator(params)
handler = validator(orig_handler)
assert handler() == "Missing path parameter 'p1'"
assert handler(p1='123') == 'OK'
assert handler(p1='') == "Wrong type, expected 'integer' for path parameter 'p1'"
assert handler(p1='foo') == "Wrong type, expected 'integer' for path parameter 'p1'"
assert handler(p1='1.2') == "Wrong type, expected 'integer' for path parameter 'p1'"
request.args = {'q1': '4'}
assert handler(p1=1) == 'Invalid value, must be at most 3'
request.args = {'q1': '3'}
assert handler(p1=1) == 'OK'
request.args = {'a1': '1,2'}
assert handler(p1=1) == "OK"
request.args = {'a1': '1,a'}
assert handler(p1=1) == "Wrong type, expected 'integer' for query parameter 'a1'"
request.args = {'a1': '1,-1'}
assert handler(p1=1) == "Invalid value, must be at least 0"
del request.args['a1']
request.headers = {'h1': 'a'}
assert handler(p1='123') == 'OK'
request.headers = {'h1': 'x'}
assert handler(p1='123') == "Enum value must be one of ['a', 'b']"
示例14: test_action
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import params [as 别名]
def test_action(self):
element_handler = webservice.ElementHandler(r'/', '')
element_handler.model = webservice.SimpleDataModel("test")
request = MagicMock()
request.path = "/"
response = element_handler.action(request)
self.assertEqual(400, response.status_code)
self.assertEqual('application/json', response.content_type)
self.assertEqual(json.loads(response.body)['description'],
"Missing required action parameter.")
request.params = MagicMock()
request.params.getall.return_value = ['do_test']
request.params["action"] = "do_test"
request.path = "/"
response = element_handler.action(request)
self.assertEqual(501, response.status_code)
self.assertEqual('application/json', response.content_type)
self.assertEqual(json.loads(response.body)['description'],
"Method not supported")
# test action impl returning python primitives
simple_data = [1, 2]
element_handler.model.do_test_action = lambda *a, **kwa: simple_data
response = element_handler.action(request)
self.assertEqual(200, response.status_code)
self.assertEqual('application/json', response.content_type)
self.assertEqual(json.loads(response.body), simple_data)
# test action impl returning custom webob response
custom_data = webob.Response(body="test", status=599,
content_type="custom/test")
element_handler.model.do_test_action = lambda *a, **kwa: custom_data
response = element_handler.action(request)
self.assertEqual(599, response.status_code)
self.assertEqual('custom/test', response.content_type)
self.assertEqual(response.body, "test")