server/context: add get_param_as_list
This commit is contained in:
parent
3d4ceb13b8
commit
063b0c4799
2 changed files with 20 additions and 0 deletions
|
@ -15,6 +15,16 @@ class Context(object):
|
|||
def get_file(self, name):
|
||||
return self.files.get(name, None)
|
||||
|
||||
def get_param_as_list(self, name, required=False, default=None):
|
||||
if name in self.input:
|
||||
param = self.input[name]
|
||||
if not isinstance(param, list):
|
||||
return [param]
|
||||
return param
|
||||
if not required:
|
||||
return default
|
||||
raise errors.ValidationError('Required paramter %r is missing.' % name)
|
||||
|
||||
def get_param_as_string(self, name, required=False, default=None):
|
||||
if name in self.input:
|
||||
param = self.input[name]
|
||||
|
|
|
@ -13,6 +13,16 @@ def test_get_file():
|
|||
assert ctx.get_file('key') == b'content'
|
||||
assert ctx.get_file('key2') is None
|
||||
|
||||
def test_getting_list_parameter():
|
||||
ctx = api.Context()
|
||||
ctx.input = {'key': 'value', 'list': ['1', '2', '3']}
|
||||
assert ctx.get_param_as_list('key') == ['value']
|
||||
assert ctx.get_param_as_list('key2') is None
|
||||
assert ctx.get_param_as_list('key2', default=['def']) == ['def']
|
||||
assert ctx.get_param_as_list('list') == ['1', '2', '3']
|
||||
with pytest.raises(errors.ValidationError):
|
||||
ctx.get_param_as_list('key2', required=True)
|
||||
|
||||
def test_getting_string_parameter():
|
||||
ctx = api.Context()
|
||||
ctx.input = {'key': 'value', 'list': ['1', '2', '3']}
|
||||
|
|
Loading…
Reference in a new issue