aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_odict.py
blob: a682d7ebe016bc6f03cc1008bfe5c4aa95e57e2a (plain)
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
from netlib import odict
import tutils


class TestODict:
    def setUp(self):
        self.od = odict.ODict()

    def test_str_err(self):
        h = odict.ODict()
        tutils.raises(ValueError, h.__setitem__, "key", "foo")

    def test_dictToHeader1(self):
        self.od.add("one", "uno")
        self.od.add("two", "due")
        self.od.add("two", "tre")
        expected = [
            "one: uno\r\n",
            "two: due\r\n",
            "two: tre\r\n",
            "\r\n"
        ]
        out = repr(self.od)
        for i in expected:
            assert out.find(i) >= 0

    def test_getset_state(self):
        self.od.add("foo", 1)
        self.od.add("foo", 2)
        self.od.add("bar", 3)
        state = self.od.get_state()
        nd = odict.ODict.from_state(state)
        assert nd == self.od
        nd.load_state(state)

    def test_dictToHeader2(self):
        self.od["one"] = ["uno"]
        expected1 = "one: uno\r\n"
        expected2 = "\r\n"
        out = repr(self.od)
        assert out.find(expected1) >= 0
        assert out.find(expected2) >= 0

    def test_match_re(self):
        h = odict.ODict()
        h.add("one", "uno")
        h.add("two", "due")
        h.add("two", "tre")
        assert h.match_re("uno")
        assert h.match_re("two: due")
        assert not h.match_re("nonono")

    def test_in_any(self):
        self.od["one"] = ["atwoa", "athreea"]
        assert self.od.in_any("one", "two")
        assert self.od.in_any("one", "three")
        assert not self.od.in_any("one", "four")
        assert not self.od.in_any("nonexistent", "foo")
        assert not self.od.in_any("one", "TWO")
        assert self.od.in_any("one", "TWO", True)

    def test_iter(self):
        assert not [i for i in self.od]
        self.od.add("foo", 1)
        assert [i for i in self.od]

    def test_keys(self):
        assert not self.od.keys()
        self.od.add("foo", 1)
        assert self.od.keys() == ["foo"]
        self.od.add("foo", 2)
        assert self.od.keys() == ["foo"]
        self.od.add("bar", 2)
        assert len(self.od.keys()) == 2

    def test_copy(self):
        self.od.add("foo", 1)
        self.od.add("foo", 2)
        self.od.add("bar", 3)
        assert self.od == self.od.copy()

    def test_del(self):
        self.od.add("foo", 1)
        self.od.add("Foo", 2)
        self.od.add("bar", 3)
        del self.od["foo"]
        assert len(self.od.lst) == 2

    def test_replace(self):
        self.od.add("one", "two")
        self.od.add("two", "one")
        assert self.od.replace("one", "vun") == 2
        assert self.od.lst == [
            ["vun", "two"],
            ["two", "vun"],
        ]

    def test_get(self):
        self.od.add("one", "two")
        assert self.od.get("one") == ["two"]
        assert self.od.get("two") == None

    def test_get_first(self):
        self.od.add("one", "two")
        self.od.add("one", "three")
        assert self.od.get_first("one") == "two"
        assert self.od.get_first("two") == None


class TestODictCaseless:
    def setUp(self):
        self.od = odict.ODictCaseless()

    def test_override(self):
        o = odict.ODictCaseless()
        o.add('T', 'application/x-www-form-urlencoded; charset=UTF-8')
        o["T"] = ["foo"]
        assert o["T"] == ["foo"]

    def test_case_preservation(self):
        self.od["Foo"] = ["1"]
        assert "foo" in self.od
        assert self.od.items()[0][0] == "Foo"
        assert self.od.get("foo") == ["1"]
        assert self.od.get("foo", [""]) == ["1"]
        assert self.od.get("Foo", [""]) == ["1"]
        assert self.od.get("xx", "yy") == "yy"

    def test_del(self):
        self.od.add("foo", 1)
        self.od.add("Foo", 2)
        self.od.add("bar", 3)
        del self.od["foo"]
        assert len(self.od) == 1

    def test_keys(self):
        assert not self.od.keys()
        self.od.add("foo", 1)
        assert self.od.keys() == ["foo"]
        self.od.add("Foo", 2)
        assert self.od.keys() == ["foo"]
        self.od.add("bar", 2)
        assert len(self.od.keys()) == 2