aurweb.util: add extend_query and to_qs helpers

The first addition, extend_query, can be used to take an existing
query parameter dictionary and inject an *additions as replacement
key/value pairs.

The second, to_qs, converts a query parameter dictionary to
a query string in the form "a=b&c=d".

These two functions simplify and make dedupe_qs and quote_plus more
efficient in terms of constructing custom query string overrides.

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-08-28 16:33:33 -07:00
parent 49cc12f99d
commit a114bd3e16
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
2 changed files with 28 additions and 1 deletions

View file

@ -35,3 +35,18 @@ def test_dedupe_qs():
def test_number_format():
assert util.number_format(0.222, 2) == "0.22"
assert util.number_format(0.226, 2) == "0.23"
def test_extend_query():
""" Test extension of a query via extend_query. """
query = {"a": "b"}
extended = util.extend_query(query, ("a", "c"), ("b", "d"))
assert extended.get("a") == "c"
assert extended.get("b") == "d"
def test_to_qs():
""" Test conversion from a query dictionary to a query string. """
query = {"a": "b", "c": [1, 2, 3]}
qs = util.to_qs(query)
assert qs == "a=b&c=1&c=2&c=3"