Debian Codegraph @

tests/test_structures.py

raw
1 import pytest
2
3 from requests.structures import CaseInsensitiveDict, LookupDict
4
5
6 class TestCaseInsensitiveDict:
7 @pytest.fixture(autouse=True)
8 def setup(self):
9 """CaseInsensitiveDict instance with "Accept" header."""
10 self.case_insensitive_dict = CaseInsensitiveDict()
11 self.case_insensitive_dict["Accept"] = "application/json"
12
13 def test_list(self):
14 assert list(self.case_insensitive_dict) == ["Accept"]
15
16 possible_keys = pytest.mark.parametrize(
17 "key", ("accept", "ACCEPT", "aCcEpT", "Accept")
18 )
19
20 @possible_keys
21 def test_getitem(self, key):
22 assert self.case_insensitive_dict[key] == "application/json"
23
24 @possible_keys
25 def test_delitem(self, key):
26 del self.case_insensitive_dict[key]
27 assert key not in self.case_insensitive_dict
28
29 def test_lower_items(self):
30 assert list(self.case_insensitive_dict.lower_items()) == [
31 ("accept", "application/json")
32 ]
33
34 def test_repr(self):
35 assert repr(self.case_insensitive_dict) == "{'Accept': 'application/json'}"
36
37 def test_copy(self):
38 copy = self.case_insensitive_dict.copy()
39 assert copy is not self.case_insensitive_dict
40 assert copy == self.case_insensitive_dict
41
42 @pytest.mark.parametrize(
43 "other, result",
44 (
45 ({"AccePT": "application/json"}, True),
46 ({}, False),
47 (None, False),
48 ),
49 )
50 def test_instance_equality(self, other, result):
51 assert (self.case_insensitive_dict == other) is result
52
53
54 class TestLookupDict:
55 @pytest.fixture(autouse=True)
56 def setup(self):
57 """LookupDict instance with "bad_gateway" attribute."""
58 self.lookup_dict = LookupDict("test")
59 self.lookup_dict.bad_gateway = 502
60
61 def test_repr(self):
62 assert repr(self.lookup_dict) == "<lookup 'test'>"
63
64 get_item_parameters = pytest.mark.parametrize(
65 "key, value",
66 (
67 ("bad_gateway", 502),
68 ("not_a_key", None),
69 ),
70 )
71
72 @get_item_parameters
73 def test_getitem(self, key, value):
74 assert self.lookup_dict[key] == value
75
76 @get_item_parameters
77 def test_get(self, key, value):
78 assert self.lookup_dict.get(key) == value
79
80 def test_hasattr(self):
81 assert hasattr(self.lookup_dict, "bad_gateway") is True
82 assert hasattr(self.lookup_dict, "not_a_key") is False
83
84 def test_getattr(self):
85 assert getattr(self.lookup_dict, "bad_gateway") == 502
86 with pytest.raises(AttributeError):
87 getattr(self.lookup_dict, "not_a_key")
88
89 @get_item_parameters
90 def test_getattr_default(self, key, value):
91 assert getattr(self.lookup_dict, key, None) == value