Skip to content

Commit

Permalink
Raise ValueError if _get_value called with invalid location
Browse files Browse the repository at this point in the history
  • Loading branch information
sloria committed Sep 27, 2015
1 parent 9f885ec commit 4779c46
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
4 changes: 3 additions & 1 deletion tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
build_dir = os.path.join(docs_dir, '_build')

@task
def test(coverage=False):
def test(coverage=False, browse=False):
import pytest
args = []
if coverage:
args.extend(['--cov=webargs', '--cov-report=term', '--cov-report=html'])
retcode = pytest.main(args)
if coverage and browse:
webbrowser.open_new_tab(os.path.join('htmlcov', 'index.html'))
sys.exit(retcode)

@task
Expand Down
7 changes: 6 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def test_default_can_be_none(parser, web_request):
assert result['val'] is None


def test_value_error_raised_if_invalid_location(web_request):
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:
Expand Down Expand Up @@ -304,6 +304,11 @@ def validate(val):
parser.parse(args, web_request, locations=('json', ), validate=validate)
assert excinfo.value.messages == ['Invalid value.']

def test_invalid_argument_for_validate(web_request, parser):
with pytest.raises(ValueError) as excinfo:
parser.parse({}, web_request, validate='notcallable')
assert 'not a callable or list of callables.' in excinfo.value.args[0]

def test_get_value_basic():
assert get_value({'foo': 42}, 'foo', False) == 42
assert get_value({'foo': 42}, 'bar', False) is missing
Expand Down
2 changes: 1 addition & 1 deletion tests/testapp/echo/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'name': fields.Str(missing='World')
}
hello_multi = {
'name': fields.List(fields.Str(), multiple=True)
'name': fields.List(fields.Str())
}

def render_json_response(data):
Expand Down
3 changes: 2 additions & 1 deletion webargs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Meta(object):
return cls if not instance else cls(**kwargs)

def is_multiple(field):
"""Return whether or not `field` handles repeated/multi-value arguments."""
return isinstance(field, ma.fields.List)

def get_value(d, name, multiple):
Expand Down Expand Up @@ -157,7 +158,7 @@ def _get_value(self, name, argobj, req, location):
function = getattr(self, func)
value = function(req, name, argobj)
else:
value = None
raise ValueError('Invalid location: "{0}"'.format(location))
return value

def parse_arg(self, name, field, req, locations=None):
Expand Down

0 comments on commit 4779c46

Please sign in to comment.