aboutsummaryrefslogtreecommitdiffstats
path: root/test/netlib/test_odict.py
blob: f0985ef64462d02027f273bc24448000546e1019 (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
50pre { line-height: 125%; margin: 0; }
td.linenos pre { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
span.linenos { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
td.linenos pre.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight { background: #ffffff; }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Lite
from netlib import odict, tutils


class TestODict(object):

    def test_repr(self):
        h = odict.ODict()
        h["one"] = ["two"]
        assert repr(h)

    def test_str_err(self):
        h = odict.ODict()
        with tutils.raises(ValueError):
            h["key"] = u"foo"
        with tutils.raises(ValueError):
            h["key"] = b"foo"

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

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

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

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

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

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

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

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

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

    def test_extend(self):
        a = odict.ODict([["a", "b"], ["c", "d"]])
        b = odict.ODict([["a", "b"], ["e", "f"]])
        a.extend(b)
        assert len(a) == 4
        assert a["a"] == ["b", "b"]


class TestODictCaseless(object):

    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):
        od = odict.ODictCaseless()
        od["Foo"] = ["1"]
        assert "foo" in od
        assert od.items()[0][0] == "Foo"
        assert od.get("foo") == ["1"]
        assert od.get("foo", [""]) == ["1"]
        assert od.get("Foo", [""]) == ["1"]
        assert od.get("xx", "yy") == "yy"

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

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

    def test_add_order(self):
        od = odict.ODict(
            [
                ["one", "uno"],
                ["two", "due"],
                ["three", "tre"],
            ]
        )
        od["two"] = ["foo", "bar"]
        assert od.lst == [
            ["one", "uno"],
            ["two", "foo"],
            ["three", "tre"],
            ["two", "bar"],
        ]