2012-06-18 21:42:32 +00:00
|
|
|
from netlib import odict
|
|
|
|
import tutils
|
|
|
|
|
|
|
|
|
|
|
|
class TestODict:
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2012-06-18 21:42:32 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.od = odict.ODict()
|
|
|
|
|
2015-04-14 22:28:17 +00:00
|
|
|
def test_repr(self):
|
|
|
|
h = odict.ODict()
|
|
|
|
h["one"] = ["two"]
|
|
|
|
assert repr(h)
|
|
|
|
|
2012-06-18 21:42:32 +00:00
|
|
|
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"
|
|
|
|
]
|
2015-04-14 22:28:17 +00:00
|
|
|
out = self.od.format()
|
2012-06-18 21:42:32 +00:00
|
|
|
for i in expected:
|
|
|
|
assert out.find(i) >= 0
|
|
|
|
|
2014-03-02 03:54:21 +00:00
|
|
|
def test_getset_state(self):
|
|
|
|
self.od.add("foo", 1)
|
|
|
|
self.od.add("foo", 2)
|
|
|
|
self.od.add("bar", 3)
|
2014-09-16 23:47:07 +00:00
|
|
|
state = self.od.get_state()
|
|
|
|
nd = odict.ODict.from_state(state)
|
2014-03-02 03:54:21 +00:00
|
|
|
assert nd == self.od
|
2015-02-27 19:40:17 +00:00
|
|
|
b = odict.ODict()
|
|
|
|
b.load_state(state)
|
|
|
|
assert b == self.od
|
2014-03-02 03:54:21 +00:00
|
|
|
|
2012-06-18 21:42:32 +00:00
|
|
|
def test_dictToHeader2(self):
|
|
|
|
self.od["one"] = ["uno"]
|
|
|
|
expected1 = "one: uno\r\n"
|
|
|
|
expected2 = "\r\n"
|
2015-04-14 22:28:17 +00:00
|
|
|
out = self.od.format()
|
2012-06-18 21:42:32 +00:00
|
|
|
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)
|
|
|
|
|
2013-01-05 07:08:48 +00:00
|
|
|
def test_iter(self):
|
|
|
|
assert not [i for i in self.od]
|
|
|
|
self.od.add("foo", 1)
|
|
|
|
assert [i for i in self.od]
|
|
|
|
|
2013-02-27 20:28:48 +00:00
|
|
|
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
|
|
|
|
|
2012-06-18 21:42:32 +00:00
|
|
|
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()
|
2015-02-27 19:40:17 +00:00
|
|
|
assert not self.od != self.od.copy()
|
2012-06-18 21:42:32 +00:00
|
|
|
|
|
|
|
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"]
|
2015-05-27 09:18:54 +00:00
|
|
|
assert self.od.get("two") is None
|
2012-06-18 21:42:32 +00:00
|
|
|
|
2012-08-18 06:14:13 +00:00
|
|
|
def test_get_first(self):
|
|
|
|
self.od.add("one", "two")
|
|
|
|
self.od.add("one", "three")
|
|
|
|
assert self.od.get_first("one") == "two"
|
2015-05-27 09:18:54 +00:00
|
|
|
assert self.od.get_first("two") is None
|
2012-08-18 06:14:13 +00:00
|
|
|
|
2015-04-14 01:50:57 +00:00
|
|
|
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"]
|
2012-06-18 21:42:32 +00:00
|
|
|
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2012-06-18 21:42:32 +00:00
|
|
|
class TestODictCaseless:
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2012-06-18 21:42:32 +00:00
|
|
|
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
|
2013-02-27 20:28:48 +00:00
|
|
|
|
|
|
|
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
|
2015-04-14 22:28:17 +00:00
|
|
|
|
|
|
|
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"],
|
|
|
|
]
|