Back to index

python3.2  3.2.2
Public Member Functions
test.test_collections.TestOrderedDict Class Reference

List of all members.

Public Member Functions

def test_init
def test_update
def test_abc
def test_clear
def test_delitem
def test_setitem
def test_iterators
def test_popitem
def test_pop
def test_equality
def test_copying
def test_yaml_linkage
def test_reduce_not_too_fat
def test_repr
def test_repr_recursive
def test_setdefault
def test_reinsert
def test_move_to_end
def test_sizeof
def test_override_update

Detailed Description

Definition at line 914 of file test_collections.py.


Member Function Documentation

Definition at line 973 of file test_collections.py.

00973 
00974     def test_abc(self):
00975         self.assertIsInstance(OrderedDict(), MutableMapping)
00976         self.assertTrue(issubclass(OrderedDict, MutableMapping))

Here is the call graph for this function:

Definition at line 977 of file test_collections.py.

00977 
00978     def test_clear(self):
00979         pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
00980         shuffle(pairs)
00981         od = OrderedDict(pairs)
00982         self.assertEqual(len(od), len(pairs))
00983         od.clear()
00984         self.assertEqual(len(od), 0)

Here is the call graph for this function:

Definition at line 1061 of file test_collections.py.

01061 
01062     def test_copying(self):
01063         # Check that ordered dicts are copyable, deepcopyable, picklable,
01064         # and have a repr/eval round-trip
01065         pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
01066         od = OrderedDict(pairs)
01067         update_test = OrderedDict()
01068         update_test.update(od)
01069         for i, dup in enumerate([
01070                     od.copy(),
01071                     copy.copy(od),
01072                     copy.deepcopy(od),
01073                     pickle.loads(pickle.dumps(od, 0)),
01074                     pickle.loads(pickle.dumps(od, 1)),
01075                     pickle.loads(pickle.dumps(od, 2)),
01076                     pickle.loads(pickle.dumps(od, 3)),
01077                     pickle.loads(pickle.dumps(od, -1)),
01078                     eval(repr(od)),
01079                     update_test,
01080                     OrderedDict(od),
01081                     ]):
01082             self.assertTrue(dup is not od)
01083             self.assertEqual(dup, od)
01084             self.assertEqual(list(dup.items()), list(od.items()))
01085             self.assertEqual(len(dup), len(od))
01086             self.assertEqual(type(dup), type(od))

Here is the call graph for this function:

Definition at line 985 of file test_collections.py.

00985 
00986     def test_delitem(self):
00987         pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
00988         od = OrderedDict(pairs)
00989         del od['a']
00990         self.assertNotIn('a', od)
00991         with self.assertRaises(KeyError):
00992             del od['a']
00993         self.assertEqual(list(od.items()), pairs[:2] + pairs[3:])

Here is the call graph for this function:

Definition at line 1046 of file test_collections.py.

01046 
01047     def test_equality(self):
01048         pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
01049         shuffle(pairs)
01050         od1 = OrderedDict(pairs)
01051         od2 = OrderedDict(pairs)
01052         self.assertEqual(od1, od2)          # same order implies equality
01053         pairs = pairs[2:] + pairs[:2]
01054         od2 = OrderedDict(pairs)
01055         self.assertNotEqual(od1, od2)       # different order implies inequality
01056         # comparison to regular dict is not order sensitive
01057         self.assertEqual(od1, dict(od2))
01058         self.assertEqual(dict(od2), od1)
01059         # different length implied inequality
01060         self.assertNotEqual(od1, OrderedDict(pairs[:-1]))

Here is the call graph for this function:

Definition at line 916 of file test_collections.py.

00916 
00917     def test_init(self):
00918         with self.assertRaises(TypeError):
00919             OrderedDict([('a', 1), ('b', 2)], None)                                 # too many args
00920         pairs = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]
00921         self.assertEqual(sorted(OrderedDict(dict(pairs)).items()), pairs)           # dict input
00922         self.assertEqual(sorted(OrderedDict(**dict(pairs)).items()), pairs)         # kwds input
00923         self.assertEqual(list(OrderedDict(pairs).items()), pairs)                   # pairs input
00924         self.assertEqual(list(OrderedDict([('a', 1), ('b', 2), ('c', 9), ('d', 4)],
00925                                           c=3, e=5).items()), pairs)                # mixed input
00926 
00927         # make sure no positional args conflict with possible kwdargs
00928         self.assertEqual(inspect.getargspec(OrderedDict.__dict__['__init__']).args,
00929                          ['self'])
00930 
00931         # Make sure that direct calls to __init__ do not clear previous contents
00932         d = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 44), ('e', 55)])
00933         d.__init__([('e', 5), ('f', 6)], g=7, d=4)
00934         self.assertEqual(list(d.items()),
00935             [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)])

Here is the call graph for this function:

Definition at line 1001 of file test_collections.py.

01001 
01002     def test_iterators(self):
01003         pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
01004         shuffle(pairs)
01005         od = OrderedDict(pairs)
01006         self.assertEqual(list(od), [t[0] for t in pairs])
01007         self.assertEqual(list(od.keys()), [t[0] for t in pairs])
01008         self.assertEqual(list(od.values()), [t[1] for t in pairs])
01009         self.assertEqual(list(od.items()), pairs)
01010         self.assertEqual(list(reversed(od)),
01011                          [t[0] for t in reversed(pairs)])

Here is the call graph for this function:

Definition at line 1146 of file test_collections.py.

01146 
01147     def test_move_to_end(self):
01148         od = OrderedDict.fromkeys('abcde')
01149         self.assertEqual(list(od), list('abcde'))
01150         od.move_to_end('c')
01151         self.assertEqual(list(od), list('abdec'))
01152         od.move_to_end('c', 0)
01153         self.assertEqual(list(od), list('cabde'))
01154         od.move_to_end('c', 0)
01155         self.assertEqual(list(od), list('cabde'))
01156         od.move_to_end('e')
01157         self.assertEqual(list(od), list('cabde'))
01158         with self.assertRaises(KeyError):
01159             od.move_to_end('x')

Here is the call graph for this function:

Definition at line 1166 of file test_collections.py.

01166 
01167     def test_override_update(self):
01168         # Verify that subclasses can override update() without breaking __init__()
01169         class MyOD(OrderedDict):
01170             def update(self, *args, **kwds):
01171                 raise Exception()
01172         items = [('a', 1), ('c', 3), ('b', 2)]
01173         self.assertEqual(list(MyOD(items).items()), items)

Here is the call graph for this function:

Definition at line 1022 of file test_collections.py.

01022 
01023     def test_pop(self):
01024         pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
01025         shuffle(pairs)
01026         od = OrderedDict(pairs)
01027         shuffle(pairs)
01028         while pairs:
01029             k, v = pairs.pop()
01030             self.assertEqual(od.pop(k), v)
01031         with self.assertRaises(KeyError):
01032             od.pop('xyz')
01033         self.assertEqual(len(od), 0)
01034         self.assertEqual(od.pop(k, 12345), 12345)
01035 
01036         # make sure pop still works when __missing__ is defined
01037         class Missing(OrderedDict):
01038             def __missing__(self, key):
01039                 return 0
01040         m = Missing(a=1)
01041         self.assertEqual(m.pop('b', 5), 5)
01042         self.assertEqual(m.pop('a', 6), 1)
01043         self.assertEqual(m.pop('a', 6), 6)
01044         with self.assertRaises(KeyError):
01045             m.pop('a')

Here is the call graph for this function:

Definition at line 1012 of file test_collections.py.

01012 
01013     def test_popitem(self):
01014         pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
01015         shuffle(pairs)
01016         od = OrderedDict(pairs)
01017         while pairs:
01018             self.assertEqual(od.popitem(), pairs.pop())
01019         with self.assertRaises(KeyError):
01020             od.popitem()
01021         self.assertEqual(len(od), 0)

Here is the call graph for this function:

Definition at line 1096 of file test_collections.py.

01096 
01097     def test_reduce_not_too_fat(self):
01098         # do not save instance dictionary if not needed
01099         pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
01100         od = OrderedDict(pairs)
01101         self.assertEqual(len(od.__reduce__()), 2)
01102         od.x = 10
01103         self.assertEqual(len(od.__reduce__()), 3)

Here is the call graph for this function:

Definition at line 1136 of file test_collections.py.

01136 
01137     def test_reinsert(self):
01138         # Given insert a, insert b, delete a, re-insert a,
01139         # verify that a is now later than b.
01140         od = OrderedDict()
01141         od['a'] = 1
01142         od['b'] = 2
01143         del od['a']
01144         od['a'] = 1
01145         self.assertEqual(list(od.items()), [('b', 2), ('a', 1)])

Here is the call graph for this function:

Definition at line 1104 of file test_collections.py.

01104 
01105     def test_repr(self):
01106         od = OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)])
01107         self.assertEqual(repr(od),
01108             "OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)])")
01109         self.assertEqual(eval(repr(od)), od)
01110         self.assertEqual(repr(OrderedDict()), "OrderedDict()")

Here is the call graph for this function:

Definition at line 1111 of file test_collections.py.

01111 
01112     def test_repr_recursive(self):
01113         # See issue #9826
01114         od = OrderedDict.fromkeys('abc')
01115         od['x'] = od
01116         self.assertEqual(repr(od),
01117             "OrderedDict([('a', None), ('b', None), ('c', None), ('x', ...)])")

Here is the call graph for this function:

Definition at line 1118 of file test_collections.py.

01118 
01119     def test_setdefault(self):
01120         pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
01121         shuffle(pairs)
01122         od = OrderedDict(pairs)
01123         pair_order = list(od.items())
01124         self.assertEqual(od.setdefault('a', 10), 3)
01125         # make sure order didn't change
01126         self.assertEqual(list(od.items()), pair_order)
01127         self.assertEqual(od.setdefault('x', 10), 10)
01128         # make sure 'x' is added to the end
01129         self.assertEqual(list(od.items())[-1], ('x', 10))
01130 
01131         # make sure setdefault still works when __missing__ is defined
01132         class Missing(OrderedDict):
01133             def __missing__(self, key):
01134                 return 0
01135         self.assertEqual(Missing().setdefault(5, 9), 9)

Here is the call graph for this function:

Definition at line 994 of file test_collections.py.

00994 
00995     def test_setitem(self):
00996         od = OrderedDict([('d', 1), ('b', 2), ('c', 3), ('a', 4), ('e', 5)])
00997         od['c'] = 10           # existing element
00998         od['f'] = 20           # new element
00999         self.assertEqual(list(od.items()),
01000                          [('d', 1), ('b', 2), ('c', 10), ('a', 4), ('e', 5), ('f', 20)])

Here is the call graph for this function:

Definition at line 1160 of file test_collections.py.

01160 
01161     def test_sizeof(self):
01162         # Wimpy test: Just verify the reported size is larger than a regular dict
01163         d = dict(a=1)
01164         od = OrderedDict(**d)
01165         self.assertGreater(sys.getsizeof(od), sys.getsizeof(d))

Here is the call graph for this function:

Definition at line 936 of file test_collections.py.

00936 
00937     def test_update(self):
00938         with self.assertRaises(TypeError):
00939             OrderedDict().update([('a', 1), ('b', 2)], None)                        # too many args
00940         pairs = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]
00941         od = OrderedDict()
00942         od.update(dict(pairs))
00943         self.assertEqual(sorted(od.items()), pairs)                                 # dict input
00944         od = OrderedDict()
00945         od.update(**dict(pairs))
00946         self.assertEqual(sorted(od.items()), pairs)                                 # kwds input
00947         od = OrderedDict()
00948         od.update(pairs)
00949         self.assertEqual(list(od.items()), pairs)                                   # pairs input
00950         od = OrderedDict()
00951         od.update([('a', 1), ('b', 2), ('c', 9), ('d', 4)], c=3, e=5)
00952         self.assertEqual(list(od.items()), pairs)                                   # mixed input
00953 
00954         # Issue 9137: Named argument called 'other' or 'self'
00955         # shouldn't be treated specially.
00956         od = OrderedDict()
00957         od.update(self=23)
00958         self.assertEqual(list(od.items()), [('self', 23)])
00959         od = OrderedDict()
00960         od.update(other={})
00961         self.assertEqual(list(od.items()), [('other', {})])
00962         od = OrderedDict()
00963         od.update(red=5, blue=6, other=7, self=8)
00964         self.assertEqual(sorted(list(od.items())),
00965                          [('blue', 6), ('other', 7), ('red', 5), ('self', 8)])
00966 
00967         # Make sure that direct calls to update do not clear previous contents
00968         # add that updates items are not moved to the end
00969         d = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 44), ('e', 55)])
00970         d.update([('e', 5), ('f', 6)], g=7, d=4)
00971         self.assertEqual(list(d.items()),
00972             [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)])

Here is the call graph for this function:

Definition at line 1087 of file test_collections.py.

01087 
01088     def test_yaml_linkage(self):
01089         # Verify that __reduce__ is setup in a way that supports PyYAML's dump() feature.
01090         # In yaml, lists are native but tuples are not.
01091         pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
01092         od = OrderedDict(pairs)
01093         # yaml.dump(od) -->
01094         # '!!python/object/apply:__main__.OrderedDict\n- - [a, 1]\n  - [b, 2]\n'
01095         self.assertTrue(all(type(pair)==list for pair in od.__reduce__()[1]))

Here is the call graph for this function:


The documentation for this class was generated from the following file: