test_queryparams.py
This file
Path
tests/models/test_queryparams.py
95 lines · 2.5 KiB · Python
Last changed by
Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
History of this fileTake it
1import pytest23import httpx456@pytest.mark.parametrize(7 "source",8 [9 "a=123&a=456&b=789",10 {"a": ["123", "456"], "b": 789},11 {"a": ("123", "456"), "b": 789},12 ],13)14def test_queryparams(source):15 q = httpx.QueryParams(source)16 assert "a" in q17 assert "A" not in q18 assert "c" not in q19 assert q["a"] == "456"20 assert q.get("a") == "456"21 assert q.get("nope", default=None) is None22 assert q.get_list("a") == ["123", "456"]2324 with pytest.warns(DeprecationWarning):25 assert q.getlist("a") == ["123", "456"]2627 assert list(q.keys()) == ["a", "b"]28 assert list(q.values()) == ["456", "789"]29 assert list(q.items()) == [("a", "456"), ("b", "789")]30 assert len(q) == 231 assert list(q) == ["a", "b"]32 assert dict(q) == {"a": "456", "b": "789"}33 assert str(q) == "a=123&a=456&b=789"34 assert repr(q) == "QueryParams('a=123&a=456&b=789')"35 assert httpx.QueryParams({"a": "123", "b": "456"}) == httpx.QueryParams(36 [("a", "123"), ("b", "456")]37 )38 assert httpx.QueryParams({"a": "123", "b": "456"}) == httpx.QueryParams(39 "a=123&b=456"40 )41 assert httpx.QueryParams({"a": "123", "b": "456"}) == httpx.QueryParams(42 {"b": "456", "a": "123"}43 )44 assert httpx.QueryParams() == httpx.QueryParams({})45 assert httpx.QueryParams([("a", "123"), ("a", "456")]) == httpx.QueryParams(46 "a=123&a=456"47 )48 assert httpx.QueryParams({"a": "123", "b": "456"}) != "invalid"4950 q = httpx.QueryParams([("a", "123"), ("a", "456")])51 assert httpx.QueryParams(q) == q525354def test_queryparam_types():55 q = httpx.QueryParams(None)56 assert str(q) == ""5758 q = httpx.QueryParams({"a": True})59 assert str(q) == "a=true"6061 q = httpx.QueryParams({"a": False})62 assert str(q) == "a=false"6364 q = httpx.QueryParams({"a": ""})65 assert str(q) == "a="6667 q = httpx.QueryParams({"a": None})68 assert str(q) == "a="6970 q = httpx.QueryParams({"a": 1.23})71 assert str(q) == "a=1.23"7273 q = httpx.QueryParams({"a": 123})74 assert str(q) == "a=123"7576 q = httpx.QueryParams({"a": [1, 2]})77 assert str(q) == "a=1&a=2"787980def test_queryparam_setters():81 q = httpx.QueryParams({"a": 1})82 q.update([])8384 assert str(q) == "a=1"8586 q = httpx.QueryParams([("a", 1), ("a", 2)])87 q["a"] = "3"88 assert str(q) == "a=3"8990 q = httpx.QueryParams([("a", 1), ("b", 1)])91 u = httpx.QueryParams([("b", 2), ("b", 3)])92 q.update(u)9394 assert str(q) == "a=1&b=2&b=3"95 assert q["b"] == u["b"]