Exemplo n.º 1
0
    def test_init(self):
        with self.assertRaises(TypeError):
            OrderedDict([('a', 1), ('b', 2)], None)  # too many args
        pairs = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]
        self.assertEqual(sorted(OrderedDict(dict(pairs)).items()),
                         pairs)  # dict input
        self.assertEqual(sorted(OrderedDict(**dict(pairs)).items()),
                         pairs)  # kwds input
        self.assertEqual(list(OrderedDict(pairs).items()),
                         pairs)  # pairs input
        self.assertEqual(
            list(
                OrderedDict([('a', 1), ('b', 2), ('c', 9), ('d', 4)], c=3,
                            e=5).items()), pairs)  # mixed input

        # cyordereddict: remove this test because slot wrappers (on extension
        # types) cannot be inspected
        # make sure no positional args conflict with possible kwdargs
        # self.assertEqual(inspect.getargspec(OrderedDict.__dict__['__init__']).args,
        #                  ['self'])

        # Make sure that direct calls to __init__ do not clear previous contents
        d = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 44), ('e', 55)])
        d.__init__([('e', 5), ('f', 6)], g=7, d=4)
        self.assertEqual(list(d.items()), [('a', 1), ('b', 2), ('c', 3),
                                           ('d', 4), ('e', 5), ('f', 6),
                                           ('g', 7)])
Exemplo n.º 2
0
    def test_copying(self):
        # Check that ordered dicts are copyable, deepcopyable, picklable,
        # and have a repr/eval round-trip
        pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
        od = OrderedDict(pairs)
        update_test = OrderedDict()
        update_test.update(od)
        for label, dup in [
                    ('od.copy()', od.copy()),
                    ('copy.copy(od)', copy.copy(od)),
                    ('copy.deepcopy(od)', copy.deepcopy(od)),
                    ('pickle.loads(pickle.dumps(od, 0))',
                        pickle.loads(pickle.dumps(od, 0))),
                    ('pickle.loads(pickle.dumps(od, 1))',
                        pickle.loads(pickle.dumps(od, 1))),
                    ('pickle.loads(pickle.dumps(od, 2))',
                        pickle.loads(pickle.dumps(od, 2))),
#                     ('pickle.loads(pickle.dumps(od, 3))',
#                         pickle.loads(pickle.dumps(od, 3))),
                    ('pickle.loads(pickle.dumps(od, -1))',
                        pickle.loads(pickle.dumps(od, -1))),
                    ('eval(repr(od))', eval(repr(od))),
                    ('update_test', update_test),
                    ('OrderedDict(od)', OrderedDict(od)),
                    ]:
            with self.subTest(label=label):
                msg = "\ncopy: %s\nod: %s" % (dup, od)
                self.assertIsNot(dup, od, msg)
                self.assertEqual(dup, od)
Exemplo n.º 3
0
 def test_clear(self):
     pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
     shuffle(pairs)
     od = OrderedDict(pairs)
     self.assertEqual(len(od), len(pairs))
     od.clear()
     self.assertEqual(len(od), 0)
Exemplo n.º 4
0
 def test_clear(self):
     pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
     shuffle(pairs)
     od = OrderedDict(pairs)
     self.assertEqual(len(od), len(pairs))
     od.clear()
     self.assertEqual(len(od), 0)
Exemplo n.º 5
0
 def test_yaml_linkage(self):
     # Verify that __reduce__ is setup in a way that supports PyYAML's dump() feature.
     # In yaml, lists are native but tuples are not.
     pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
     od = OrderedDict(pairs)
     # yaml.dump(od) -->
     # '!!python/object/apply:__main__.OrderedDict\n- - [a, 1]\n  - [b, 2]\n'
     self.assertTrue(all(type(pair) == list for pair in od.__reduce__()[1]))
Exemplo n.º 6
0
 def test_delitem(self):
     pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
     od = OrderedDict(pairs)
     del od['a']
     self.assertNotIn('a', od)
     with self.assertRaises(KeyError):
         del od['a']
     self.assertEqual(list(od.items()), pairs[:2] + pairs[3:])
Exemplo n.º 7
0
 def test_delitem(self):
     pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
     od = OrderedDict(pairs)
     del od['a']
     self.assertNotIn('a', od)
     with self.assertRaises(KeyError):
         del od['a']
     self.assertEqual(list(od.items()), pairs[:2] + pairs[3:])
Exemplo n.º 8
0
 def test_yaml_linkage(self):
     # Verify that __reduce__ is setup in a way that supports PyYAML's dump() feature.
     # In yaml, lists are native but tuples are not.
     pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
     od = OrderedDict(pairs)
     # yaml.dump(od) -->
     # '!!python/object/apply:__main__.OrderedDict\n- - [a, 1]\n  - [b, 2]\n'
     self.assertTrue(all(type(pair)==list for pair in od.__reduce__()[1]))
Exemplo n.º 9
0
 def test_reinsert(self):
     # Given insert a, insert b, delete a, re-insert a,
     # verify that a is now later than b.
     od = OrderedDict()
     od['a'] = 1
     od['b'] = 2
     del od['a']
     od['a'] = 1
     self.assertEqual(list(od.items()), [('b', 2), ('a', 1)])
Exemplo n.º 10
0
 def test_popitem(self):
     pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
     shuffle(pairs)
     od = OrderedDict(pairs)
     while pairs:
         self.assertEqual(od.popitem(), pairs.pop())
     with self.assertRaises(KeyError):
         od.popitem()
     self.assertEqual(len(od), 0)
Exemplo n.º 11
0
 def test_repr(self):
     od = OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5),
                       ('f', 6)])
     self.assertEqual(
         repr(od),
         "OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)])"
     )
     self.assertEqual(eval(repr(od)), od)
     self.assertEqual(repr(OrderedDict()), "OrderedDict()")
Exemplo n.º 12
0
 def test_reinsert(self):
     # Given insert a, insert b, delete a, re-insert a,
     # verify that a is now later than b.
     od = OrderedDict()
     od['a'] = 1
     od['b'] = 2
     del od['a']
     od['a'] = 1
     self.assertEqual(list(od.items()), [('b', 2), ('a', 1)])
Exemplo n.º 13
0
 def test_popitem(self):
     pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
     shuffle(pairs)
     od = OrderedDict(pairs)
     while pairs:
         self.assertEqual(od.popitem(), pairs.pop())
     with self.assertRaises(KeyError):
         od.popitem()
     self.assertEqual(len(od), 0)
Exemplo n.º 14
0
 def test_equality(self):
     pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
     shuffle(pairs)
     od1 = OrderedDict(pairs)
     od2 = OrderedDict(pairs)
     self.assertEqual(od1, od2)  # same order implies equality
     pairs = pairs[2:] + pairs[:2]
     od2 = OrderedDict(pairs)
     self.assertNotEqual(od1, od2)  # different order implies inequality
     # comparison to regular dict is not order sensitive
     self.assertEqual(od1, dict(od2))
     self.assertEqual(dict(od2), od1)
     # different length implied inequality
     self.assertNotEqual(od1, OrderedDict(pairs[:-1]))
Exemplo n.º 15
0
 def test_copying(self):
     # Check that ordered dicts are copyable, deepcopyable, picklable,
     # and have a repr/eval round-trip
     pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
     od = OrderedDict(pairs)
     update_test = OrderedDict()
     update_test.update(od)
     for label, dup in [
         ('od.copy()', od.copy()),
         ('copy.copy(od)', copy.copy(od)),
         ('copy.deepcopy(od)', copy.deepcopy(od)),
         ('pickle.loads(pickle.dumps(od, 0))',
          pickle.loads(pickle.dumps(od, 0))),
         ('pickle.loads(pickle.dumps(od, 1))',
          pickle.loads(pickle.dumps(od, 1))),
         ('pickle.loads(pickle.dumps(od, 2))',
          pickle.loads(pickle.dumps(od, 2))),
             #                     ('pickle.loads(pickle.dumps(od, 3))',
             #                         pickle.loads(pickle.dumps(od, 3))),
         ('pickle.loads(pickle.dumps(od, -1))',
          pickle.loads(pickle.dumps(od, -1))),
         ('eval(repr(od))', eval(repr(od))),
         ('update_test', update_test),
         ('OrderedDict(od)', OrderedDict(od)),
     ]:
         with self.subTest(label=label):
             msg = "\ncopy: %s\nod: %s" % (dup, od)
             self.assertIsNot(dup, od, msg)
             self.assertEqual(dup, od)
Exemplo n.º 16
0
 def test_repr_recursive(self):
     # See issue #9826
     od = OrderedDict.fromkeys('abc')
     od['x'] = od
     self.assertEqual(
         repr(od),
         "OrderedDict([('a', None), ('b', None), ('c', None), ('x', ...)])")
Exemplo n.º 17
0
 def test_pickle_recursive(self):
     od = OrderedDict()
     od[1] = od
     #for proto in range(-1, pickle.HIGHEST_PROTOCOL + 1):
     dup = pickle.loads(pickle.dumps(od))
     self.assertIsNot(dup, od)
     self.assertEqual(list(dup.keys()), [1])
     self.assertIs(dup[1], dup)
Exemplo n.º 18
0
    def test_init(self):
        with self.assertRaises(TypeError):
            OrderedDict([('a', 1), ('b', 2)], None)                                 # too many args
        pairs = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]
        self.assertEqual(sorted(OrderedDict(dict(pairs)).items()), pairs)           # dict input
        self.assertEqual(sorted(OrderedDict(**dict(pairs)).items()), pairs)         # kwds input
        self.assertEqual(list(OrderedDict(pairs).items()), pairs)                   # pairs input
        self.assertEqual(list(OrderedDict([('a', 1), ('b', 2), ('c', 9), ('d', 4)],
                                          c=3, e=5).items()), pairs)                # mixed input

        # cyordereddict: remove this test because slot wrappers (on extension
        # types) cannot be inspected
        # make sure no positional args conflict with possible kwdargs
        # self.assertEqual(inspect.getargspec(OrderedDict.__dict__['__init__']).args,
        #                  ['self'])

        # Make sure that direct calls to __init__ do not clear previous contents
        d = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 44), ('e', 55)])
        d.__init__([('e', 5), ('f', 6)], g=7, d=4)
        self.assertEqual(list(d.items()),
            [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)])
Exemplo n.º 19
0
 def test_iterators(self):
     pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
     shuffle(pairs)
     od = OrderedDict(pairs)
     self.assertEqual(list(od), [t[0] for t in pairs])
     self.assertEqual(list(od.keys()), [t[0] for t in pairs])
     self.assertEqual(list(od.values()), [t[1] for t in pairs])
     self.assertEqual(list(od.items()), pairs)
     self.assertEqual(list(reversed(od)), [t[0] for t in reversed(pairs)])
     self.assertEqual(list(reversed(od.keys())),
                      [t[0] for t in reversed(pairs)])
     self.assertEqual(list(reversed(od.values())),
                      [t[1] for t in reversed(pairs)])
     self.assertEqual(list(reversed(od.items())), list(reversed(pairs)))
Exemplo n.º 20
0
 def test_move_to_end(self):
     od = OrderedDict.fromkeys('abcde')
     self.assertEqual(list(od), list('abcde'))
     od.move_to_end('c')
     self.assertEqual(list(od), list('abdec'))
     od.move_to_end('c', 0)
     self.assertEqual(list(od), list('cabde'))
     od.move_to_end('c', 0)
     self.assertEqual(list(od), list('cabde'))
     od.move_to_end('e')
     self.assertEqual(list(od), list('cabde'))
     with self.assertRaises(KeyError):
         od.move_to_end('x')
Exemplo n.º 21
0
 def test_move_to_end(self):
     od = OrderedDict.fromkeys('abcde')
     self.assertEqual(list(od), list('abcde'))
     od.move_to_end('c')
     self.assertEqual(list(od), list('abdec'))
     od.move_to_end('c', 0)
     self.assertEqual(list(od), list('cabde'))
     od.move_to_end('c', 0)
     self.assertEqual(list(od), list('cabde'))
     od.move_to_end('e')
     self.assertEqual(list(od), list('cabde'))
     with self.assertRaises(KeyError):
         od.move_to_end('x')
Exemplo n.º 22
0
    def test_pop(self):
        pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
        shuffle(pairs)
        od = OrderedDict(pairs)
        shuffle(pairs)
        while pairs:
            k, v = pairs.pop()
            self.assertEqual(od.pop(k), v)
        with self.assertRaises(KeyError):
            od.pop('xyz')
        self.assertEqual(len(od), 0)
        self.assertEqual(od.pop(k, 12345), 12345)

        # make sure pop still works when __missing__ is defined
        class Missing(OrderedDict):
            def __missing__(self, key):
                return 0
        m = Missing(a=1)
        self.assertEqual(m.pop('b', 5), 5)
        self.assertEqual(m.pop('a', 6), 1)
        self.assertEqual(m.pop('a', 6), 6)
        with self.assertRaises(KeyError):
            m.pop('a')
Exemplo n.º 23
0
 def test_iterators(self):
     pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
     shuffle(pairs)
     od = OrderedDict(pairs)
     self.assertEqual(list(od), [t[0] for t in pairs])
     self.assertEqual(list(od.keys()), [t[0] for t in pairs])
     self.assertEqual(list(od.values()), [t[1] for t in pairs])
     self.assertEqual(list(od.items()), pairs)
     self.assertEqual(list(reversed(od)),
                      [t[0] for t in reversed(pairs)])
     self.assertEqual(list(reversed(od.keys())),
                      [t[0] for t in reversed(pairs)])
     self.assertEqual(list(reversed(od.values())),
                      [t[1] for t in reversed(pairs)])
     self.assertEqual(list(reversed(od.items())), list(reversed(pairs)))
Exemplo n.º 24
0
    def test_setdefault(self):
        pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
        shuffle(pairs)
        od = OrderedDict(pairs)
        pair_order = list(od.items())
        self.assertEqual(od.setdefault('a', 10), 3)
        # make sure order didn't change
        self.assertEqual(list(od.items()), pair_order)
        self.assertEqual(od.setdefault('x', 10), 10)
        # make sure 'x' is added to the end
        self.assertEqual(list(od.items())[-1], ('x', 10))

        # make sure setdefault still works when __missing__ is defined
        class Missing(OrderedDict):
            def __missing__(self, key):
                return 0

        self.assertEqual(Missing().setdefault(5, 9), 9)
Exemplo n.º 25
0
    def test_setdefault(self):
        pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
        shuffle(pairs)
        od = OrderedDict(pairs)
        pair_order = list(od.items())
        self.assertEqual(od.setdefault('a', 10), 3)
        # make sure order didn't change
        self.assertEqual(list(od.items()), pair_order)
        self.assertEqual(od.setdefault('x', 10), 10)
        # make sure 'x' is added to the end
        self.assertEqual(list(od.items())[-1], ('x', 10))

        # make sure setdefault still works when __missing__ is defined
        class Missing(OrderedDict):
            def __missing__(self, key):
                return 0
        self.assertEqual(Missing().setdefault(5, 9), 9)
Exemplo n.º 26
0
    def test_pop(self):
        pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
        shuffle(pairs)
        od = OrderedDict(pairs)
        shuffle(pairs)
        while pairs:
            k, v = pairs.pop()
            self.assertEqual(od.pop(k), v)
        with self.assertRaises(KeyError):
            od.pop('xyz')
        self.assertEqual(len(od), 0)
        self.assertEqual(od.pop(k, 12345), 12345)

        # make sure pop still works when __missing__ is defined
        class Missing(OrderedDict):
            def __missing__(self, key):
                return 0

        m = Missing(a=1)
        self.assertEqual(m.pop('b', 5), 5)
        self.assertEqual(m.pop('a', 6), 1)
        self.assertEqual(m.pop('a', 6), 6)
        with self.assertRaises(KeyError):
            m.pop('a')
Exemplo n.º 27
0
 def test_setitem(self):
     od = OrderedDict([('d', 1), ('b', 2), ('c', 3), ('a', 4), ('e', 5)])
     od['c'] = 10           # existing element
     od['f'] = 20           # new element
     self.assertEqual(list(od.items()),
                      [('d', 1), ('b', 2), ('c', 10), ('a', 4), ('e', 5), ('f', 20)])
Exemplo n.º 28
0
 def test_abc(self):
     self.assertIsInstance(OrderedDict(), MutableMapping)
     self.assertTrue(issubclass(OrderedDict, MutableMapping))
Exemplo n.º 29
0
    def test_update(self):
        with self.assertRaises(TypeError):
            OrderedDict().update([('a', 1), ('b', 2)], None)  # too many args
        pairs = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]
        od = OrderedDict()
        od.update(dict(pairs))
        self.assertEqual(sorted(od.items()), pairs)  # dict input
        od = OrderedDict()
        od.update(**dict(pairs))
        self.assertEqual(sorted(od.items()), pairs)  # kwds input
        od = OrderedDict()
        od.update(pairs)
        self.assertEqual(list(od.items()), pairs)  # pairs input
        od = OrderedDict()
        od.update([('a', 1), ('b', 2), ('c', 9), ('d', 4)], c=3, e=5)
        self.assertEqual(list(od.items()), pairs)  # mixed input

        # Issue 9137: Named argument called 'other' or 'self'
        # shouldn't be treated specially.
        od = OrderedDict()
        od.update(self=23)
        self.assertEqual(list(od.items()), [('self', 23)])
        od = OrderedDict()
        od.update(other={})
        self.assertEqual(list(od.items()), [('other', {})])
        od = OrderedDict()
        od.update(red=5, blue=6, other=7, self=8)
        self.assertEqual(sorted(list(od.items())), [('blue', 6), ('other', 7),
                                                    ('red', 5), ('self', 8)])

        # Make sure that direct calls to update do not clear previous contents
        # add that updates items are not moved to the end
        d = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 44), ('e', 55)])
        d.update([('e', 5), ('f', 6)], g=7, d=4)
        self.assertEqual(list(d.items()), [('a', 1), ('b', 2), ('c', 3),
                                           ('d', 4), ('e', 5), ('f', 6),
                                           ('g', 7)])
Exemplo n.º 30
0
 def test_sizeof(self):
     # Wimpy test: Just verify the reported size is larger than a regular dict
     d = dict(a=1)
     od = OrderedDict(**d)
     self.assertGreater(sys.getsizeof(od), sys.getsizeof(d))
Exemplo n.º 31
0
 def test_reduce_not_too_fat(self):
     # do not save instance dictionary if not needed
     pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
     od = OrderedDict(pairs)
     _, _, _, _, args = od.__reduce__()
     self.assertEqual(list(args), pairs)
Exemplo n.º 32
0
 def test_repr_recursive(self):
     # See issue #9826
     od = OrderedDict.fromkeys('abc')
     od['x'] = od
     self.assertEqual(repr(od),
         "OrderedDict([('a', None), ('b', None), ('c', None), ('x', ...)])")
Exemplo n.º 33
0
 def test_reduce_not_too_fat(self):
     # do not save instance dictionary if not needed
     pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
     od = OrderedDict(pairs)
     _, _, _, _, args = od.__reduce__()
     self.assertEqual(list(args), pairs)
Exemplo n.º 34
0
    def test_update(self):
        with self.assertRaises(TypeError):
            OrderedDict().update([('a', 1), ('b', 2)], None)                        # too many args
        pairs = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]
        od = OrderedDict()
        od.update(dict(pairs))
        self.assertEqual(sorted(od.items()), pairs)                                 # dict input
        od = OrderedDict()
        od.update(**dict(pairs))
        self.assertEqual(sorted(od.items()), pairs)                                 # kwds input
        od = OrderedDict()
        od.update(pairs)
        self.assertEqual(list(od.items()), pairs)                                   # pairs input
        od = OrderedDict()
        od.update([('a', 1), ('b', 2), ('c', 9), ('d', 4)], c=3, e=5)
        self.assertEqual(list(od.items()), pairs)                                   # mixed input

        # Issue 9137: Named argument called 'other' or 'self'
        # shouldn't be treated specially.
        od = OrderedDict()
        od.update(self=23)
        self.assertEqual(list(od.items()), [('self', 23)])
        od = OrderedDict()
        od.update(other={})
        self.assertEqual(list(od.items()), [('other', {})])
        od = OrderedDict()
        od.update(red=5, blue=6, other=7, self=8)
        self.assertEqual(sorted(list(od.items())),
                         [('blue', 6), ('other', 7), ('red', 5), ('self', 8)])

        # Make sure that direct calls to update do not clear previous contents
        # add that updates items are not moved to the end
        d = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 44), ('e', 55)])
        d.update([('e', 5), ('f', 6)], g=7, d=4)
        self.assertEqual(list(d.items()),
            [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)])
Exemplo n.º 35
0
 def test_setitem(self):
     od = OrderedDict([('d', 1), ('b', 2), ('c', 3), ('a', 4), ('e', 5)])
     od['c'] = 10  # existing element
     od['f'] = 20  # new element
     self.assertEqual(list(od.items()), [('d', 1), ('b', 2), ('c', 10),
                                         ('a', 4), ('e', 5), ('f', 20)])