Skip to content

Commit

Permalink
Allow tuples as well as lists in MultiParams, refs #799
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jun 5, 2020
1 parent 2db60f8 commit 24c9377
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
6 changes: 3 additions & 3 deletions datasette/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,14 +759,14 @@ def __init__(self, data):
if isinstance(data, dict):
for key in data:
assert isinstance(
data[key], list
data[key], (list, tuple)
), "dictionary data should be a dictionary of key => [list]"
self._data = data
elif isinstance(data, list):
elif isinstance(data, list) or isinstance(data, tuple):
new_data = {}
for item in data:
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"
key, value = item
new_data.setdefault(key, []).append(value)
Expand Down
22 changes: 16 additions & 6 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,20 @@ def foo(a, b):
utils.call_with_supported_arguments(foo, a=1)


def test_multi_params_list():
p1 = utils.MultiParams([["foo", "bar"], ["foo", "baz"]])
@pytest.mark.parametrize("data,should_raise", [
([["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", "baz"] == p1.getlist("foo")
# Should raise an error if list isn't pairs
with pytest.raises(AssertionError):
utils.MultiParams([["foo", "bar"], ["foo", "baz", "bar"]])
assert ["bar", "baz"] == list(p1.getlist("foo"))

0 comments on commit 24c9377

Please sign in to comment.