Exemplo n.º 1
0
def test_parse_cookies_called_when_cookies_is_a_target(parse_cookies, request):
    arg = Arg()
    p = Parser()
    p.parse_arg('foo', arg, request)
    assert parse_cookies.call_count == 0
    p.parse_arg('foo', arg, request, targets=('cookies',))
    assert parse_cookies.called
Exemplo n.º 2
0
def test_parse_required_arg_raises_validation_error(parse_json, request):
    arg = Arg(required=True)
    p = Parser()
    parse_json.return_value = Missing
    with pytest.raises(ValidationError) as excinfo:
        p.parse_arg('foo', arg, request)
    assert 'Required parameter ' + repr('foo') + ' not found.' in str(excinfo)
Exemplo n.º 3
0
def test_handle_error_called_when_parsing_raises_error(parse_json, handle_error, request):
    val_err = ValidationError('error occurred')
    parse_json.side_effect = val_err
    p = Parser()
    p.parse({'foo': Arg()}, request, targets=('json',))
    assert handle_error.called
    parse_json.side_effect = Exception('generic exception')
    p.parse({'foo': Arg()}, request, targets=('json',))
    assert handle_error.call_count == 2
Exemplo n.º 4
0
def test_parse(parse_json, request):
    parse_json.return_value = 42
    argmap = {
        'username': Arg(),
        'password': Arg()
    }
    p = Parser()
    ret = p.parse(argmap, request)
    assert {'username': 42, 'password': 42} == ret
Exemplo n.º 5
0
def test_parse_required_multiple_arg(parse_form, request):
    parse_form.return_value = []
    arg = Arg(multiple=True, required=True)
    p = Parser()
    with pytest.raises(ValidationError):
        p.parse_arg('foo', arg, request)

    parse_form.return_value = None
    with pytest.raises(ValidationError):
        p.parse_arg('foo', arg, request)
Exemplo n.º 6
0
def test_custom_error_handler(parse_json, request):
    class CustomError(Exception):
        pass

    def error_handler(error):
        raise CustomError(error)
    parse_json.side_effect = AttributeError('parse_json failed')
    p = Parser(error_handler=error_handler)
    with pytest.raises(CustomError):
        p.parse({'foo': Arg()}, request)
Exemplo n.º 7
0
def test_custom_target_handler(request):
    request.data = {'foo': 42}

    parser = Parser()

    @parser.target_handler('data')
    def parse_data(req, name, arg):
        return req.data.get(name)

    result = parser.parse({'foo': Arg(int)}, request, targets=('data', ))
    assert result['foo'] == 42
Exemplo n.º 8
0
def test_load_nondefault_called_by_parse_with_location(location, web_request):
    with mock.patch(
        f"webargs.core.Parser.load_{location}"
    ) as mock_loadfunc, mock.patch("webargs.core.Parser.load_json") as load_json:
        mock_loadfunc.return_value = {}
        load_json.return_value = {}
        p = Parser()

        # ensure that without location=..., the loader is not called (json is
        # called)
        p.parse({"foo": fields.Field()}, web_request)
        assert mock_loadfunc.call_count == 0
        assert load_json.call_count == 1

        # but when location=... is given, the loader *is* called and json is
        # not called
        p.parse({"foo": fields.Field()}, web_request, location=location)
        assert mock_loadfunc.call_count == 1
        # it was already 1, should not go up
        assert load_json.call_count == 1
Exemplo n.º 9
0
def test_parse_json_called_by_parse_arg(parse_json, web_request):
    arg = Arg()
    p = Parser()
    p.parse_arg('foo', arg, web_request)
    parse_json.assert_called_with(web_request, 'foo', arg)
Exemplo n.º 10
0
def test_parse_form_called_by_parse_arg(parse_form, request):
    arg = Arg()
    p = Parser()
    p.parse_arg('foo', arg, request)
    assert parse_form.called
Exemplo n.º 11
0
def test_location_as_init_argument(load_headers, web_request):
    p = Parser(location="headers")
    load_headers.return_value = {}
    p.parse({"foo": fields.Field()}, web_request)
    assert load_headers.called
Exemplo n.º 12
0
def test_parse_json_not_called_when_json_not_a_location(parse_json, web_request):
    field = fields.Field()
    p = Parser()
    p.parse_arg('foo', field, web_request, locations=('form', 'querystring'))
    assert parse_json.call_count == 0
Exemplo n.º 13
0
def test_parse_querystring_called_by_parse_arg(parse_querystring, web_request):
    field = fields.Field()
    p = Parser()
    p.parse_arg('foo', field, web_request)
    assert parse_querystring.called_once()
Exemplo n.º 14
0
def test_parse_files(parse_files, web_request):
    p = Parser()
    p.parse({'foo': fields.Field()}, web_request, locations=('files',))
    assert parse_files.called
Exemplo n.º 15
0
def test_parse_json_called_by_parse_arg(parse_json, request):
    arg = Arg()
    p = Parser()
    p.parse_arg('foo', arg, request)
    assert parse_json.called
Exemplo n.º 16
0
def test_targets_as_init_arguments(parse_headers, request):
    p = Parser(targets=('headers', ))
    p.parse({'foo': Arg()}, request)
    assert parse_headers.called
Exemplo n.º 17
0
def test_conversion(parse_json, request):
    parse_json.return_value = 42
    arg = Arg(str)
    assert Parser().parse_arg('foo', arg, request, targets=('json', )) == '42'
Exemplo n.º 18
0
def test_parse_required_arg(parse_json, request):
    arg = Arg(required=True)
    parse_json.return_value = 42
    p = Parser()
    result = p.parse_arg('foo', arg, request, targets=('json', ))
    assert result == 42
Exemplo n.º 19
0
def test_parse_required_arg_raises_validation_error(request):
    arg = Arg(required=True)
    p = Parser()
    with pytest.raises(ValidationError) as excinfo:
        p.parse_arg('foo', arg, request)
    assert 'Required parameter ' + repr('foo') + ' not found.' in str(excinfo)
Exemplo n.º 20
0
def test_parse(parse_json, request):
    parse_json.return_value = 42
    argmap = {'username': Arg(), 'password': Arg()}
    p = Parser()
    ret = p.parse(argmap, request)
    assert {'username': 42, 'password': 42} == ret
Exemplo n.º 21
0
def test_fallback_used_if_all_other_functions_return_none(fallback, request):
    arg = Arg()
    p = Parser()
    p.parse_arg('foo', arg, request)
    assert fallback.called
Exemplo n.º 22
0
def test_parse_json_not_called_when_json_not_a_target(parse_json, request):
    arg = Arg()
    p = Parser()
    p.parse_arg('foo', arg, request, targets=('form', 'querystring'))
    assert parse_json.call_count == 0
Exemplo n.º 23
0
def test_parse_json_called_with_source(parse_json, web_request):
    arg = Arg(source='bar')
    p = Parser()
    p.parse_arg('foo', arg, web_request)
    parse_json.assert_called_with(web_request, 'bar', arg)
Exemplo n.º 24
0
def test_parse_files(parse_files, request):
    p = Parser()
    p.parse({'foo': Arg()}, request, targets=('files', ))
    assert parse_files.called
Exemplo n.º 25
0
def test_parse_json_called_by_parse_arg(parse_json, request):
    arg = Arg()
    p = Parser()
    p.parse_arg('foo', arg, request)
    parse_json.assert_called_with(request, 'foo', arg)
Exemplo n.º 26
0
def test_parse_json_called_with_source(parse_json, request):
    arg = Arg(source='bar')
    p = Parser()
    p.parse_arg('foo', arg, request)
    parse_json.assert_called_with(request, 'bar', arg)
Exemplo n.º 27
0
def test_parse_json_called_by_parse_arg(parse_json, web_request):
    field = fields.Field()
    p = Parser()
    p.parse_arg('foo', field, web_request)
    parse_json.assert_called_with(web_request, 'foo', field)
Exemplo n.º 28
0
def test_fallback_used_if_all_other_functions_return_none(fallback, request):
    arg = Arg()
    p = Parser()
    p.parse({'foo': arg}, request)
    fallback.assert_called
Exemplo n.º 29
0
def test_parse_form_called_by_parse_arg(parse_form, web_request):
    field = fields.Field()
    p = Parser()
    p.parse_arg('foo', field, web_request)
    assert parse_form.called_once()
Exemplo n.º 30
0
def test_parse_querystring_called_by_parse_arg(parse_querystring, request):
    arg = Arg()
    p = Parser()
    p.parse_arg('foo', arg, request)
    assert parse_querystring.called
Exemplo n.º 31
0
def test_handle_error_reraises_errors(web_request):
    p = Parser()
    with pytest.raises(ValidationError):
        p.handle_error(ValidationError("error raised"), web_request, Schema())
Exemplo n.º 32
0
def test_parse_json_called_by_parse_arg(parse_json, request):
    arg = Arg()
    p = Parser()
    p.parse_arg('foo', arg, request)
    assert parse_json.called
Exemplo n.º 33
0
def test_load_json_called_by_parse_default(load_json, web_request):
    schema = dict2schema({"foo": fields.Field()})()
    load_json.return_value = {"foo": 1}
    p = Parser()
    p.parse(schema, web_request)
    load_json.assert_called_with(web_request, schema)
Exemplo n.º 34
0
def test_fallback_used_if_all_other_functions_return_none(
        fallback, web_request):
    arg = Arg()
    p = Parser()
    p.parse({'foo': arg}, web_request)
    fallback.assert_called
Exemplo n.º 35
0
def test_parse_json_not_called_when_json_not_a_target(parse_json, request):
    arg = Arg()
    p = Parser()
    p.parse_arg('foo', arg, request, targets=('form', 'querystring'))
    assert parse_json.call_count == 0
Exemplo n.º 36
0
def test_targets_as_init_arguments(parse_headers, request):
    p = Parser(targets=('headers',))
    p.parse({'foo': Arg()}, request)
    assert parse_headers.called
Exemplo n.º 37
0
def test_fallback_used_if_all_other_functions_return_none(fallback, request):
    arg = Arg()
    p = Parser()
    p.parse_arg('foo', arg, request)
    assert fallback.called
Exemplo n.º 38
0
def test_parse_form_called_by_parse_arg(parse_form, request):
    arg = Arg()
    p = Parser()
    p.parse_arg('foo', arg, request)
    assert parse_form.called
Exemplo n.º 39
0
def test_parse_required_arg(parse_json, request):
    arg = Arg(required=True)
    parse_json.return_value = 42
    p = Parser()
    result = p.parse_arg('foo', arg, request, targets=('json', ))
    assert result == 42
Exemplo n.º 40
0
def test_parse(parse_json, web_request):
    parse_json.return_value = 42
    argmap = {"username": fields.Field(), "password": fields.Field()}
    p = Parser()
    ret = p.parse(argmap, web_request)
    assert {"username": 42, "password": 42} == ret
Exemplo n.º 41
0
def test_value_error_raised_if_invalid_target(request):
    arg = Arg()
    p = Parser()
    with pytest.raises(ValueError) as excinfo:
        p.parse_arg('foo', arg, request, targets=('invalidtarget', 'headers'))
    assert 'Invalid targets arguments: {0}'.format(['invalidtarget']) in str(excinfo)
Exemplo n.º 42
0
def test_parse_files(parse_files, web_request):
    p = Parser()
    p.parse({"foo": fields.Field()}, web_request, locations=("files", ))
    assert parse_files.called
Exemplo n.º 43
0
def test_handle_error_reraises_errors():
    p = Parser()
    with pytest.raises(ValidationError):
        p.handle_error(ValidationError('error raised'))
Exemplo n.º 44
0
def test_parse_required_arg(parse_json, web_request):
    arg = fields.Field(required=True)
    parse_json.return_value = 42
    p = Parser()
    result = p.parse_arg('foo', arg, web_request, locations=('json', ))
    assert result == 42
Exemplo n.º 45
0
def test_parse_files(parse_files, request):
    p = Parser()
    p.parse({'foo': Arg()}, request, targets=('files',))
    assert parse_files.called
Exemplo n.º 46
0
def test_value_error_raised_if_parse_arg_called_with_invalid_location(web_request):
    field = fields.Field()
    p = Parser()
    with pytest.raises(ValueError) as excinfo:
        p.parse_arg('foo', field, web_request, locations=('invalidlocation', 'headers'))
    assert 'Invalid locations arguments: {0}'.format(['invalidlocation']) in str(excinfo)
Exemplo n.º 47
0
def test_handle_error_reraises_errors():
    p = Parser()
    with pytest.raises(ValidationError):
        p.handle_error(ValidationError('error raised'))
Exemplo n.º 48
0
def test_locations_as_init_arguments(parse_headers, web_request):
    p = Parser(locations=('headers',))
    p.parse({'foo': fields.Field()}, web_request)
    assert parse_headers.called
Exemplo n.º 49
0
def test_parse_querystring_called_by_parse_arg(parse_querystring, request):
    arg = Arg()
    p = Parser()
    p.parse_arg('foo', arg, request)
    assert parse_querystring.called
Exemplo n.º 50
0
from webargs import fields
import webargs
from webargs.core import Parser
from survey_stats import state as st
from werkzeug.routing import BaseConverter

p = Parser()

stats_args = {'q': fields.Str(required=True)}
"""
    qn = req.args.get('q')
    vars = [] if not 'v' in req.args else req.args.get('v').split(',')
    resp = True if not 'r' in req.args else not 0 ** int(req.args.get('r'),2)
    filt = {} if not 'f' in req.args else dict(
        map(lambda fv: (fv.split(':')[0],
                        fv.split(':')[1].split(',')),
            req.args.get('f').split(';')))
    logging.info(filt)

"""


class SurveyYearValidator(BaseConverter):
    """
    year(int) for which survey data is available.
    """
    def to_python(self, value):
        if not int(value) in yrbss.survey_years:
            raise ValueError('Selected year is not available!' + \
                             ' Choose from: %s' % str(st.survey['yrbss'].survey_years))
        return int(value)