mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Allow tuples as well as lists in MultiParams, refs #799
This commit is contained in:
parent
0da7f49b24
commit
d96ac1d52c
2 changed files with 19 additions and 9 deletions
|
|
@ -759,14 +759,14 @@ class MultiParams:
|
||||||
if isinstance(data, dict):
|
if isinstance(data, dict):
|
||||||
for key in data:
|
for key in data:
|
||||||
assert isinstance(
|
assert isinstance(
|
||||||
data[key], list
|
data[key], (list, tuple)
|
||||||
), "dictionary data should be a dictionary of key => [list]"
|
), "dictionary data should be a dictionary of key => [list]"
|
||||||
self._data = data
|
self._data = data
|
||||||
elif isinstance(data, list):
|
elif isinstance(data, list) or isinstance(data, tuple):
|
||||||
new_data = {}
|
new_data = {}
|
||||||
for item in data:
|
for item in data:
|
||||||
assert (
|
assert (
|
||||||
isinstance(item, list) and len(item) == 2
|
isinstance(item, (list, tuple)) and len(item) == 2
|
||||||
), "list data should be a list of [key, value] pairs"
|
), "list data should be a list of [key, value] pairs"
|
||||||
key, value = item
|
key, value = item
|
||||||
new_data.setdefault(key, []).append(value)
|
new_data.setdefault(key, []).append(value)
|
||||||
|
|
|
||||||
|
|
@ -439,10 +439,20 @@ def test_call_with_supported_arguments():
|
||||||
utils.call_with_supported_arguments(foo, a=1)
|
utils.call_with_supported_arguments(foo, a=1)
|
||||||
|
|
||||||
|
|
||||||
def test_multi_params_list():
|
@pytest.mark.parametrize("data,should_raise", [
|
||||||
p1 = utils.MultiParams([["foo", "bar"], ["foo", "baz"]])
|
([["foo", "bar"], ["foo", "baz"]], False),
|
||||||
|
([("foo", "bar"), ("foo", "baz")], False),
|
||||||
|
((["foo", "bar"], ["foo", "baz"]), False),
|
||||||
|
([["foo", "bar"], ["foo", "baz", "bax"]], True),
|
||||||
|
({"foo": ["bar", "baz"]}, False),
|
||||||
|
({"foo": ("bar", "baz")}, False),
|
||||||
|
({"foo": "bar"}, True),
|
||||||
|
])
|
||||||
|
def test_multi_params(data, should_raise):
|
||||||
|
if should_raise:
|
||||||
|
with pytest.raises(AssertionError):
|
||||||
|
utils.MultiParams(data)
|
||||||
|
return
|
||||||
|
p1 = utils.MultiParams(data)
|
||||||
assert "bar" == p1["foo"]
|
assert "bar" == p1["foo"]
|
||||||
assert ["bar", "baz"] == p1.getlist("foo")
|
assert ["bar", "baz"] == list(p1.getlist("foo"))
|
||||||
# Should raise an error if list isn't pairs
|
|
||||||
with pytest.raises(AssertionError):
|
|
||||||
utils.MultiParams([["foo", "bar"], ["foo", "baz", "bar"]])
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue