File: test_normdict.py

package info (click to toggle)
python-headerparser 0.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 404 kB
  • sloc: python: 3,133; makefile: 6; sh: 4
file content (237 lines) | stat: -rw-r--r-- 6,074 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
from __future__ import annotations
from collections.abc import Callable
import re
from typing import Optional
import pytest
from headerparser import NormalizedDict, lower


def test_empty() -> None:
    nd = NormalizedDict()
    assert dict(nd) == {}
    assert nd.body is None
    assert len(nd) == 0
    assert not bool(nd)
    assert nd.normalizer is lower


def test_one() -> None:
    nd = NormalizedDict({"Foo": "bar"})
    assert dict(nd) == {"Foo": "bar"}
    assert nd.body is None
    assert len(nd) == 1
    assert bool(nd)
    assert nd.normalizer is lower


def test_get_cases() -> None:
    nd = NormalizedDict({"Foo": "bar"})
    assert nd["Foo"] == "bar"
    assert nd["Foo"] == nd["foo"] == nd["FOO"] == nd["fOO"]


def test_set() -> None:
    nd = NormalizedDict()
    assert dict(nd) == {}
    nd["Foo"] = "bar"
    assert dict(nd) == {"Foo": "bar"}
    assert nd["Foo"] == "bar"
    assert nd["Foo"] == nd["foo"] == nd["FOO"] == nd["fOO"]
    nd["fOO"] = "quux"
    assert dict(nd) == {"fOO": "quux"}
    assert nd["Foo"] == "quux"
    assert nd["Foo"] == nd["foo"] == nd["FOO"] == nd["fOO"]


def test_del() -> None:
    nd = NormalizedDict({"Foo": "bar", "Bar": "FOO"})
    del nd["Foo"]
    assert dict(nd) == {"Bar": "FOO"}
    del nd["BAR"]
    assert dict(nd) == {}


def test_del_nexists() -> None:
    nd = NormalizedDict({"Foo": "bar", "Bar": "FOO"})
    with pytest.raises(KeyError):
        del nd["Baz"]


def test_eq_empty() -> None:
    nd = NormalizedDict()
    nd2 = NormalizedDict()
    assert nd == nd2


def test_eq_nonempty() -> None:
    nd = NormalizedDict({"Foo": "bar"})
    nd2 = NormalizedDict({"Foo": "bar"})
    assert nd == nd2


def test_eq_cases() -> None:
    nd = NormalizedDict({"Foo": "bar"})
    nd2 = NormalizedDict({"fOO": "bar"})
    assert nd == nd2


def test_neq() -> None:
    assert NormalizedDict({"Foo": "bar"}) != NormalizedDict({"Foo": "BAR"})


def test_normalized() -> None:
    nd = NormalizedDict({"Foo": "BAR"})
    nd2 = nd.normalized()
    assert isinstance(nd2, NormalizedDict)
    assert dict(nd2) == {"foo": "BAR"}
    assert nd2.body is None
    assert nd == nd2


def test_normalized_with_body() -> None:
    nd = NormalizedDict({"Foo": "BAR"}, body="Glarch.")
    nd2 = nd.normalized()
    assert isinstance(nd2, NormalizedDict)
    assert dict(nd2) == {"foo": "BAR"}
    assert nd2.body == "Glarch."
    assert nd == nd2


def test_normalized_dict() -> None:
    nd = NormalizedDict({"Foo": "BAR"})
    nd2 = nd.normalized_dict()
    assert isinstance(nd2, dict)
    assert nd2 == {"foo": "BAR"}


def test_eq_dict() -> None:
    nd = NormalizedDict({"Foo": "BAR"})
    assert nd == {"Foo": "BAR"}
    assert {"Foo": "BAR"} == nd
    assert nd == {"FOO": "BAR"}
    assert {"FOO": "BAR"} == nd
    assert nd == {"foo": "BAR"}
    assert {"foo": "BAR"} == nd
    assert nd != {"Foo": "bar"}
    assert {"Foo": "bar"} != nd


def test_body_neq_dict() -> None:
    nd = NormalizedDict({"Foo": "BAR"}, body="")
    assert nd != {"Foo": "BAR"}
    assert {"Foo": "BAR"} != nd


def test_eq_body() -> None:
    nd = NormalizedDict({"Foo": "bar"}, body="")
    nd2 = NormalizedDict({"fOO": "bar"}, body="")
    assert nd == nd2


def test_neq_body() -> None:
    nd = NormalizedDict({"Foo": "bar"}, body="yes")
    nd2 = NormalizedDict({"fOO": "bar"}, body="no")
    assert nd != nd2


def test_neq_none() -> None:
    assert NormalizedDict() != None  # noqa: E711
    assert None != NormalizedDict()  # noqa: E711


def test_neq_bool() -> None:
    assert NormalizedDict() != False  # noqa: E712
    assert False != NormalizedDict()  # noqa: E712


def test_neq_int() -> None:
    assert NormalizedDict() != 42
    assert 42 != NormalizedDict()


def test_init_list() -> None:
    nd = NormalizedDict([("Foo", "bar"), ("Bar", "baz"), ("FOO", "quux")])
    assert dict(nd) == {"FOO": "quux", "Bar": "baz"}


def test_copy() -> None:
    nd = NormalizedDict({"Foo": "bar"})
    nd2 = nd.copy()
    assert nd is not nd2
    assert isinstance(nd2, NormalizedDict)
    assert dict(nd2) == {"Foo": "bar"}
    assert nd2.body is None
    assert nd == nd2
    nd2["Foo"] = "gnusto"
    assert dict(nd) == {"Foo": "bar"}
    assert dict(nd2) == {"Foo": "gnusto"}
    assert nd != nd2
    nd2["fOO"] = "quux"
    assert dict(nd) == {"Foo": "bar"}
    assert dict(nd2) == {"fOO": "quux"}
    assert nd != nd2
    nd2["Glarch"] = "baz"
    assert dict(nd) == {"Foo": "bar"}
    assert dict(nd2) == {"fOO": "quux", "Glarch": "baz"}
    assert nd != nd2


def test_copy_with_body() -> None:
    nd = NormalizedDict({"Foo": "bar"}, body="Glarch.")
    nd2 = nd.copy()
    assert nd is not nd2
    assert isinstance(nd2, NormalizedDict)
    assert dict(nd2) == {"Foo": "bar"}
    assert nd2.body == "Glarch."
    assert nd == nd2
    nd2.body = "quux"
    assert nd.body == "Glarch."
    assert nd2.body == "quux"
    assert nd != nd2


def test_neq_normalizers_empty() -> None:
    nd = NormalizedDict()
    nd2 = NormalizedDict(normalizer=lambda x: x)
    assert dict(nd) == dict(nd2) == {}
    assert nd != nd2


def test_neq_normalizers_nonempty() -> None:
    nd = NormalizedDict({"Foo": "bar"})
    nd2 = NormalizedDict({"Foo": "bar"}, normalizer=lambda x: x)
    assert dict(nd) == dict(nd2) == {"Foo": "bar"}
    assert nd != nd2


def normdash(s: str) -> str:
    return re.sub(r"[-_\s]+", "-", s.lower())


def identity(s: str) -> str:
    return s


@pytest.mark.parametrize(
    "data",
    [
        {},
        {"Foo": "Bar"},
        {"foo": "Bar"},
        {"FOO_BAR": "BAZ"},
    ],
)
@pytest.mark.parametrize("normalizer", [None, lower, normdash, identity])
@pytest.mark.parametrize("body", [None, "Glarch."])
def test_repr(
    data: dict[str, str],
    normalizer: Optional[Callable[[str], str]],
    body: Optional[str],
) -> None:
    nd = NormalizedDict(data, body=body, normalizer=normalizer)
    if normalizer is None:
        normalizer = lower
    assert repr(nd) == (
        f"headerparser.normdict.NormalizedDict({data!r},"
        f" normalizer={normalizer!r}, body={body!r})"
    )