[4bae1ef] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | from random import shuffle |
---|
| 4 | import copy |
---|
| 5 | import inspect |
---|
| 6 | import pickle |
---|
| 7 | import unittest |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | from ordereddict import OrderedDict |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | class TestOrderedDict(unittest.TestCase): |
---|
| 14 | |
---|
| 15 | def test_init(self): |
---|
| 16 | self.assertRaises(TypeError, OrderedDict, ([('a', 1), ('b', 2)], None)) |
---|
| 17 | # too many args |
---|
| 18 | pairs = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)] |
---|
| 19 | self.assertEqual(sorted(OrderedDict(dict(pairs)).items()), pairs) # dict input |
---|
| 20 | self.assertEqual(sorted(OrderedDict(**dict(pairs)).items()), pairs) # kwds input |
---|
| 21 | self.assertEqual(list(OrderedDict(pairs).items()), pairs) # pairs input |
---|
| 22 | self.assertEqual(list(OrderedDict([('a', 1), ('b', 2), ('c', 9), ('d', 4)], |
---|
| 23 | c=3, e=5).items()), pairs) # mixed input |
---|
| 24 | |
---|
| 25 | # make sure no positional args conflict with possible kwdargs |
---|
| 26 | self.assertEqual(inspect.getargspec(OrderedDict.__dict__['__init__'])[0], |
---|
| 27 | ['self']) |
---|
| 28 | |
---|
| 29 | # Make sure that direct calls to __init__ do not clear previous contents |
---|
| 30 | d = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 44), ('e', 55)]) |
---|
| 31 | d.__init__([('e', 5), ('f', 6)], g=7, d=4) |
---|
| 32 | self.assertEqual(list(d.items()), |
---|
| 33 | [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)]) |
---|
| 34 | |
---|
| 35 | def test_update(self): |
---|
| 36 | self.assertRaises(TypeError, OrderedDict().update, [('a', 1), ('b', |
---|
| 37 | 2)], None) # too many args |
---|
| 38 | pairs = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)] |
---|
| 39 | od = OrderedDict() |
---|
| 40 | od.update(dict(pairs)) |
---|
| 41 | self.assertEqual(sorted(od.items()), pairs) # dict input |
---|
| 42 | od = OrderedDict() |
---|
| 43 | od.update(**dict(pairs)) |
---|
| 44 | self.assertEqual(sorted(od.items()), pairs) # kwds input |
---|
| 45 | od = OrderedDict() |
---|
| 46 | od.update(pairs) |
---|
| 47 | self.assertEqual(list(od.items()), pairs) # pairs input |
---|
| 48 | od = OrderedDict() |
---|
| 49 | od.update([('a', 1), ('b', 2), ('c', 9), ('d', 4)], c=3, e=5) |
---|
| 50 | self.assertEqual(list(od.items()), pairs) # mixed input |
---|
| 51 | |
---|
| 52 | # Make sure that direct calls to update do not clear previous contents |
---|
| 53 | # add that updates items are not moved to the end |
---|
| 54 | d = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 44), ('e', 55)]) |
---|
| 55 | d.update([('e', 5), ('f', 6)], g=7, d=4) |
---|
| 56 | self.assertEqual(list(d.items()), |
---|
| 57 | [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)]) |
---|
| 58 | |
---|
| 59 | def test_clear(self): |
---|
| 60 | pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
---|
| 61 | shuffle(pairs) |
---|
| 62 | od = OrderedDict(pairs) |
---|
| 63 | self.assertEqual(len(od), len(pairs)) |
---|
| 64 | od.clear() |
---|
| 65 | self.assertEqual(len(od), 0) |
---|
| 66 | |
---|
| 67 | def test_delitem(self): |
---|
| 68 | pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
---|
| 69 | od = OrderedDict(pairs) |
---|
| 70 | del od['a'] |
---|
| 71 | self.assert_('a' not in od) |
---|
| 72 | self.assertRaises(KeyError, od.__delitem__, 'a') |
---|
| 73 | self.assertEqual(list(od.items()), pairs[:2] + pairs[3:]) |
---|
| 74 | |
---|
| 75 | def test_setitem(self): |
---|
| 76 | od = OrderedDict([('d', 1), ('b', 2), ('c', 3), ('a', 4), ('e', 5)]) |
---|
| 77 | od['c'] = 10 # existing element |
---|
| 78 | od['f'] = 20 # new element |
---|
| 79 | self.assertEqual(list(od.items()), |
---|
| 80 | [('d', 1), ('b', 2), ('c', 10), ('a', 4), ('e', 5), ('f', 20)]) |
---|
| 81 | |
---|
| 82 | def test_iterators(self): |
---|
| 83 | pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
---|
| 84 | shuffle(pairs) |
---|
| 85 | od = OrderedDict(pairs) |
---|
| 86 | self.assertEqual(list(od), [t[0] for t in pairs]) |
---|
| 87 | self.assertEqual(list(od.keys()), [t[0] for t in pairs]) |
---|
| 88 | self.assertEqual(list(od.values()), [t[1] for t in pairs]) |
---|
| 89 | self.assertEqual(list(od.items()), pairs) |
---|
| 90 | self.assertEqual(list(reversed(od)), |
---|
| 91 | [t[0] for t in reversed(pairs)]) |
---|
| 92 | |
---|
| 93 | def test_popitem(self): |
---|
| 94 | pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
---|
| 95 | shuffle(pairs) |
---|
| 96 | od = OrderedDict(pairs) |
---|
| 97 | while pairs: |
---|
| 98 | self.assertEqual(od.popitem(), pairs.pop()) |
---|
| 99 | self.assertRaises(KeyError, od.popitem) |
---|
| 100 | self.assertEqual(len(od), 0) |
---|
| 101 | |
---|
| 102 | def test_pop(self): |
---|
| 103 | pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
---|
| 104 | shuffle(pairs) |
---|
| 105 | od = OrderedDict(pairs) |
---|
| 106 | shuffle(pairs) |
---|
| 107 | while pairs: |
---|
| 108 | k, v = pairs.pop() |
---|
| 109 | self.assertEqual(od.pop(k), v) |
---|
| 110 | self.assertRaises(KeyError, od.pop, 'xyz') |
---|
| 111 | self.assertEqual(len(od), 0) |
---|
| 112 | self.assertEqual(od.pop(k, 12345), 12345) |
---|
| 113 | |
---|
| 114 | def test_equality(self): |
---|
| 115 | pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
---|
| 116 | shuffle(pairs) |
---|
| 117 | od1 = OrderedDict(pairs) |
---|
| 118 | od2 = OrderedDict(pairs) |
---|
| 119 | self.assertEqual(od1, od2) # same order implies equality |
---|
| 120 | pairs = pairs[2:] + pairs[:2] |
---|
| 121 | od2 = OrderedDict(pairs) |
---|
| 122 | self.assertNotEqual(od1, od2) # different order implies inequality |
---|
| 123 | # comparison to regular dict is not order sensitive |
---|
| 124 | self.assertEqual(od1, dict(od2)) |
---|
| 125 | self.assertEqual(dict(od2), od1) |
---|
| 126 | # different length implied inequality |
---|
| 127 | self.assertNotEqual(od1, OrderedDict(pairs[:-1])) |
---|
| 128 | |
---|
| 129 | def test_copying(self): |
---|
| 130 | # Check that ordered dicts are copyable, deepcopyable, picklable, |
---|
| 131 | # and have a repr/eval round-trip |
---|
| 132 | pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
---|
| 133 | od = OrderedDict(pairs) |
---|
| 134 | update_test = OrderedDict() |
---|
| 135 | update_test.update(od) |
---|
| 136 | for i, dup in enumerate([ |
---|
| 137 | od.copy(), |
---|
| 138 | copy.copy(od), |
---|
| 139 | copy.deepcopy(od), |
---|
| 140 | pickle.loads(pickle.dumps(od, 0)), |
---|
| 141 | pickle.loads(pickle.dumps(od, 1)), |
---|
| 142 | pickle.loads(pickle.dumps(od, 2)), |
---|
| 143 | pickle.loads(pickle.dumps(od, -1)), |
---|
| 144 | eval(repr(od)), |
---|
| 145 | update_test, |
---|
| 146 | OrderedDict(od), |
---|
| 147 | ]): |
---|
| 148 | self.assert_(dup is not od) |
---|
| 149 | self.assertEquals(dup, od) |
---|
| 150 | self.assertEquals(list(dup.items()), list(od.items())) |
---|
| 151 | self.assertEquals(len(dup), len(od)) |
---|
| 152 | self.assertEquals(type(dup), type(od)) |
---|
| 153 | |
---|
| 154 | def test_repr(self): |
---|
| 155 | od = OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]) |
---|
| 156 | self.assertEqual(repr(od), |
---|
| 157 | "OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)])") |
---|
| 158 | self.assertEqual(eval(repr(od)), od) |
---|
| 159 | self.assertEqual(repr(OrderedDict()), "OrderedDict()") |
---|
| 160 | |
---|
| 161 | def test_setdefault(self): |
---|
| 162 | pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
---|
| 163 | shuffle(pairs) |
---|
| 164 | od = OrderedDict(pairs) |
---|
| 165 | pair_order = list(od.items()) |
---|
| 166 | self.assertEqual(od.setdefault('a', 10), 3) |
---|
| 167 | # make sure order didn't change |
---|
| 168 | self.assertEqual(list(od.items()), pair_order) |
---|
| 169 | self.assertEqual(od.setdefault('x', 10), 10) |
---|
| 170 | # make sure 'x' is added to the end |
---|
| 171 | self.assertEqual(list(od.items())[-1], ('x', 10)) |
---|
| 172 | |
---|
| 173 | def test_reinsert(self): |
---|
| 174 | # Given insert a, insert b, delete a, re-insert a, |
---|
| 175 | # verify that a is now later than b. |
---|
| 176 | od = OrderedDict() |
---|
| 177 | od['a'] = 1 |
---|
| 178 | od['b'] = 2 |
---|
| 179 | del od['a'] |
---|
| 180 | od['a'] = 1 |
---|
| 181 | self.assertEqual(list(od.items()), [('b', 2), ('a', 1)]) |
---|
| 182 | |
---|
| 183 | if __name__ == "__main__": |
---|
| 184 | |
---|
| 185 | unittest.main() |
---|
| 186 | |
---|