Skip to content
Myr

test_queryparams.py

This file

Path

tests/models/test_queryparams.py

143 lines · 3.7 KiB · Python

Last changed by

Raise `TypeError` on invalid query params. (#2523)Tom Christiebefore adoption4cbf13e

History of this file

Take it

The walk stopped after 200 commits: lines older than ecbece1 point at it.

:pencil: Docs patch following PR #1791 section compatibility.encoding (#1812)TAHRI Ahmed Rbefore adoptionecbece1

lines 1–78why, one step back
1import pytest
23import 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 [("a", "123"), ("a", "456"), ("b", "789")],13 (("a", "123"), ("a", "456"), ("b", "789")),14 ],15)16def test_queryparams(source):17 q = httpx.QueryParams(source)18 assert "a" in q19 assert "A" not in q20 assert "c" not in q21 assert q["a"] == "123"22 assert q.get("a") == "123"23 assert q.get("nope", default=None) is None24 assert q.get_list("a") == ["123", "456"]2526 assert list(q.keys()) == ["a", "b"]27 assert list(q.values()) == ["123", "789"]28 assert list(q.items()) == [("a", "123"), ("b", "789")]29 assert len(q) == 230 assert list(q) == ["a", "b"]31 assert dict(q) == {"a": "123", "b": "789"}32 assert str(q) == "a=123&a=456&b=789"33 assert repr(q) == "QueryParams('a=123&a=456&b=789')"34 assert httpx.QueryParams({"a": "123", "b": "456"}) == httpx.QueryParams(35 [("a", "123"), ("b", "456")]36 )37 assert httpx.QueryParams({"a": "123", "b": "456"}) == httpx.QueryParams(38 "a=123&b=456"39 )40 assert httpx.QueryParams({"a": "123", "b": "456"}) == httpx.QueryParams(41 {"b": "456", "a": "123"}42 )43 assert httpx.QueryParams() == httpx.QueryParams({})44 assert httpx.QueryParams([("a", "123"), ("a", "456")]) == httpx.QueryParams(45 "a=123&a=456"46 )47 assert httpx.QueryParams({"a": "123", "b": "456"}) != "invalid"4849 q = httpx.QueryParams([("a", "123"), ("a", "456")])50 assert httpx.QueryParams(q) == q515253def test_queryparam_types():54 q = httpx.QueryParams(None)55 assert str(q) == ""5657 q = httpx.QueryParams({"a": True})58 assert str(q) == "a=true"5960 q = httpx.QueryParams({"a": False})61 assert str(q) == "a=false"6263 q = httpx.QueryParams({"a": ""})64 assert str(q) == "a="6566 q = httpx.QueryParams({"a": None})67 assert str(q) == "a="6869 q = httpx.QueryParams({"a": 1.23})70 assert str(q) == "a=1.23"7172 q = httpx.QueryParams({"a": 123})73 assert str(q) == "a=123"7475 q = httpx.QueryParams({"a": [1, 2]})76 assert str(q) == "a=1&a=2"7778

Fix empty query params (#2354)Tom Christiebefore adoptiond5900cd

lines 79–89why, one step back
79def test_empty_query_params():
80 q = httpx.QueryParams({"a": ""})81 assert str(q) == "a="8283 q = httpx.QueryParams("a=")84 assert str(q) == "a="8586 q = httpx.QueryParams("a")87 assert str(q) == "a="8889

Raise `TypeError` on invalid query params. (#2523)Tom Christiebefore adoption4cbf13e

lines 90–96why, one step back
90def test_invalid_query_params():
91 with pytest.raises(92 TypeError, match=r"Expected str, int, float, bool, or None. Got 'bytes'."93 ):94 httpx.QueryParams({"a": b"bytes"})9596

:pencil: Docs patch following PR #1791 section compatibility.encoding (#1812)TAHRI Ahmed Rbefore adoptionecbece1

lines 97–143why, one step back
97def test_queryparam_update_is_hard_deprecated():
98 q = httpx.QueryParams("a=123")99 with pytest.raises(RuntimeError):100 q.update({"a": "456"})101102103def test_queryparam_setter_is_hard_deprecated():104 q = httpx.QueryParams("a=123")105 with pytest.raises(RuntimeError):106 q["a"] = "456"107108109def test_queryparam_set():110 q = httpx.QueryParams("a=123")111 q = q.set("a", "456")112 assert q == httpx.QueryParams("a=456")113114115def test_queryparam_add():116 q = httpx.QueryParams("a=123")117 q = q.add("a", "456")118 assert q == httpx.QueryParams("a=123&a=456")119120121def test_queryparam_remove():122 q = httpx.QueryParams("a=123")123 q = q.remove("a")124 assert q == httpx.QueryParams("")125126127def test_queryparam_merge():128 q = httpx.QueryParams("a=123")129 q = q.merge({"b": "456"})130 assert q == httpx.QueryParams("a=123&b=456")131 q = q.merge({"a": "000", "c": "789"})132 assert q == httpx.QueryParams("a=000&b=456&c=789")133134135def test_queryparams_are_hashable():136 params = (137 httpx.QueryParams("a=123"),138 httpx.QueryParams({"a": 123}),139 httpx.QueryParams("b=456"),140 httpx.QueryParams({"b": 456}),141 )142143 assert len(set(params)) == 2