async def test_parse_none(self, app): async with app.test_request_context('/bubble'): parser = RequestParser() parser.add_argument('foo') args = await parser.parse_args() assert args['foo'] is None
async def test_parse_lte(self, app): async with app.test_request_context('/bubble?foo<=bar'): parser = RequestParser() parser.add_argument('foo', operators=['<=']) args = await parser.parse_args() assert args['foo'] == 'bar'
async def test_parse_foo_operators_ignore(self, app): async with app.test_request_context('/bubble'): parser = RequestParser() parser.add_argument('foo', ignore=True, store_missing=True) args = await parser.parse_args() assert args['foo'] is None
async def test_parse_unicode_app(self, app): parser = RequestParser() parser.add_argument('foo') async with app.test_request_context('/bubble?foo=barß'): args = await parser.parse_args() assert args['foo'] == 'barß'
async def test_split_multiple_cast(self, app): async with app.test_request_context('/bubble?foo=1,2,3'): parser = RequestParser() parser.add_argument('foo', type=int, action='split') args = await parser.parse_args() assert args['foo'] == [1, 2, 3]
async def test_parse_dest(self, app): async with app.test_request_context('/bubble?foo=bar'): parser = RequestParser() parser.add_argument('foo', dest='bat') args = await parser.parse_args() assert args['bat'] == 'bar'
async def test_parse_append_single(self, app): async with app.test_request_context('/bubble?foo=bar'): parser = RequestParser() parser.add_argument('foo', action='append'), args = await parser.parse_args() assert args['foo'] == ['bar']
async def test_split_multiple(self, app): async with app.test_request_context('/bubble?foo=bar,bat'): parser = RequestParser() parser.add_argument('foo', action='split'), args = await parser.parse_args() assert args['foo'] == ['bar', 'bat']
async def test_parse_append_default(self, app): async with app.test_request_context('/bubble?'): parser = RequestParser() parser.add_argument('foo', action='append', store_missing=True), args = await parser.parse_args() assert args['foo'] is None
async def test_json_location(self, app): async with app.test_request_context('/bubble', method='POST'): parser = RequestParser() parser.add_argument('foo', location='json', store_missing=True) args = await parser.parse_args() assert args['foo'] is None
async def test_parse_default(self, app): async with app.test_request_context('/bubble'): parser = RequestParser() parser.add_argument('foo', default='bar', store_missing=True) args = await parser.parse_args() assert args['foo'] == 'bar'
async def test_parse_choices_correct(self, app): async with app.test_request_context('/bubble?foo=bat') as ctx: req = ctx.request parser = RequestParser() parser.add_argument('foo', choices=['bat']), args = await parser.parse_args(req) assert args['foo'] == 'bat'
def test_unknown_type(self): parser = RequestParser() parser.add_argument('unknown', type=lambda v: v) assert parser.__schema__ == [{ 'name': 'unknown', 'type': 'string', 'in': 'query', }]
def test_location_files(self): parser = RequestParser() parser.add_argument('in_files', type=FileStorage, location='files') assert parser.__schema__ == [{ 'name': 'in_files', 'type': 'file', 'in': 'formData', }]
def test_location_form(self): parser = RequestParser() parser.add_argument('in_form', type=int, location='form') assert parser.__schema__ == [{ 'name': 'in_form', 'type': 'integer', 'in': 'formData', }]
async def test_strict_parsing_on_partial_hit(self, app): async with app.test_request_context( '/bubble?foo=1&bar=bees&n=22') as ctx: req = ctx.request parser = RequestParser() parser.add_argument('foo', type=int) with pytest.raises(BadRequest): await parser.parse_args(req, strict=True)
def test_location_json(self): parser = RequestParser() parser.add_argument('in_json', type=str, location='json') assert parser.__schema__ == [{ 'name': 'in_json', 'type': 'string', 'in': 'body', }]
async def test_not_json_location_and_content_type_json(self, app): parser = RequestParser() parser.add_argument('foo', location='args') async with app.test_request_context('/bubble', method='get', headers=JSON_HEADERS): await parser.parse_args() # Should not raise a 400: BadRequest
async def test_strict_parsing_off_partial_hit(self, app): async with app.test_request_context( '/bubble?foo=1&bar=bees&n=22') as ctx: req = ctx.request parser = RequestParser() parser.add_argument('foo', type=int) args = await parser.parse_args(req) assert args['foo'] == 1
async def test_passing_arguments_object(self, app): async with app.test_request_context('/bubble?foo=bar') as ctx: req = ctx.request parser = RequestParser() parser.add_argument(Argument('foo')) args = await parser.parse_args(req) assert args['foo'] == 'bar'
async def test_parse_lte_gte_mock(self, app, mocker): async with app.test_request_context('/bubble?foo<=bar'): mock_type = mocker.Mock() parser = RequestParser() parser.add_argument('foo', type=mock_type, operators=['<=']) await parser.parse_args() mock_type.assert_called_with('bar', 'foo', '<=')
async def test_request_parse_copy_including_settings(self): parser = RequestParser(trim=True, store_missing=False, bundle_errors=True) parser_copy = parser.copy() assert parser.trim == parser_copy.trim assert parser.store_missing == parser_copy.store_missing assert parser.bundle_errors == parser_copy.bundle_errors
async def test_type_callable(self, app): async with app.test_request_context('/bubble?foo=1') as ctx: req = ctx.request parser = RequestParser() parser.add_argument('foo', type=lambda x: x, required=False), args = await parser.parse_args(req) assert args['foo'] == '1'
async def test_none_argument(self, app): parser = RequestParser() parser.add_argument('foo', location='json') async with app.test_request_context('/bubble', method='post', data={'foo': None}, headers=JSON_HEADERS): args = await parser.parse_args() assert args['foo'] is None
async def test_parse_choices_sensitive(self, app): async with app.test_request_context('/bubble?foo=BAT') as ctx: req = ctx.request parser = RequestParser() parser.add_argument('foo', choices=['bat'], case_sensitive=True), with pytest.raises(BadRequest): await parser.parse_args(req)
def test_default_as_false(self): parser = RequestParser() parser.add_argument('bool', type=inputs.boolean, default=False) assert parser.__schema__ == [{ 'name': 'bool', 'type': 'boolean', 'in': 'query', 'default': False, }]
def test_required(self): parser = RequestParser() parser.add_argument('int', type=int, required=True) assert parser.__schema__ == [{ 'name': 'int', 'type': 'integer', 'in': 'query', 'required': True, }]
def test_default(self): parser = RequestParser() parser.add_argument('int', type=int, default=5) assert parser.__schema__ == [{ 'name': 'int', 'type': 'integer', 'in': 'query', 'default': 5, }]
def test_choices(self): parser = RequestParser() parser.add_argument('string', type=str, choices=['a', 'b']) assert parser.__schema__ == [{ 'name': 'string', 'type': 'string', 'in': 'query', 'enum': ['a', 'b'], 'collectionFormat': 'multi', }]
async def test_request_parser_remove_argument(self, app): async with app.test_request_context('/bubble?foo=baz') as ctx: req = ctx.request parser = RequestParser() parser.add_argument('foo', type=int) parser_copy = parser.copy() parser_copy.remove_argument('foo') args = await parser_copy.parse_args(req) assert args == {}