def test_init(): with pytest.raises(TypeError): OrderedDict([('a', 1), ('b', 2)], None) # too many args pairs = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)] assert sorted(OrderedDict(dict(pairs)).items()) == pairs # dict input assert sorted(OrderedDict(**dict(pairs)).items()) == pairs # kwds input assert list(OrderedDict(pairs).items()) == pairs # pairs input assert list(OrderedDict( [('a', 1), ('b', 2), ('c', 9), ('d', 4)], c=3, e=5).items()) == pairs # mixed input # make sure no positional args conflict with possible kwdargs if PY2 and sys.version_info >= (2, 7, 1) or PY3 and sys.version_info >= (3, 2): assert list(OrderedDict(self=42).items()) == [('self', 42)] assert list(OrderedDict(other=42).items()) == [('other', 42)] with pytest.raises(TypeError): OrderedDict(42) with pytest.raises(TypeError): OrderedDict((), ()) with pytest.raises(TypeError): OrderedDict.__init__() # 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) assert (list(d.items()) == [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)])
def test_init(): with pytest.raises(TypeError): OrderedDict([('a', 1), ('b', 2)], None) # too many args pairs = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)] assert sorted(OrderedDict(dict(pairs)).items()) == pairs # dict input assert sorted(OrderedDict(**dict(pairs)).items()) == pairs # kwds input assert list(OrderedDict(pairs).items()) == pairs # pairs input assert list( OrderedDict([('a', 1), ('b', 2), ('c', 9), ('d', 4)], c=3, e=5).items()) == pairs # mixed input # make sure no positional args conflict with possible kwdargs if PY2 and sys.version_info >= (2, 7, 1) or PY3 and sys.version_info >= (3, 2): assert list(OrderedDict(self=42).items()) == [('self', 42)] assert list(OrderedDict(other=42).items()) == [('other', 42)] with pytest.raises(TypeError): OrderedDict(42) with pytest.raises(TypeError): OrderedDict((), ()) with pytest.raises(TypeError): OrderedDict.__init__() # 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) assert (list(d.items()) == [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)])
def test_iterators(f): pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] shuffle(pairs) od = OrderedDict(pairs) assert list(f(od)) == [t[0] for t in f(pairs)] assert list(f(od.keys())) == [t[0] for t in f(pairs)] assert list(f(od.values())) == [t[1] for t in f(pairs)] assert list(f(od.items())) == list(f(pairs))
def test_delitem(): pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] od = OrderedDict(pairs) del od['a'] assert 'a' not in od with pytest.raises(KeyError): del od['a'] assert list(od.items()) == pairs[:2] + pairs[3:]
def test_reinsert(): # 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 assert list(od.items()) == [('b', 2), ('a', 1)]
def test_setdefault(): pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] shuffle(pairs) od = OrderedDict(pairs) pair_order = list(od.items()) assert od.setdefault('a', 10) == 3 # make sure order didn't change assert list(od.items()) == pair_order assert od.setdefault('x', 10) == 10 # make sure 'x' is added to the end assert list(od.items())[-1] == ('x', 10) # make sure setdefault still works when __missing__ is defined class Missing(OrderedDict): def __missing__(self, key): return 0 assert Missing().setdefault(5, 9) == 9
def test_sort(): pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] shuffled = pairs[:] shuffle(shuffled) od = OrderedDict(shuffled) od.sort(key=itemgetter(1)) assert list(od.items()) == pairs shuffle(shuffled) od = OrderedDict(shuffled) od.sort() assert list(od.items()) == list(sorted(pairs)) shuffle(shuffled) od = OrderedDict(shuffled) od.sort(reverse=True) assert list(od.items()) == list(reversed(sorted(pairs)))
def test_update(): with pytest.raises(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)) assert sorted(od.items()) == pairs # dict input od = OrderedDict() od.update(**dict(pairs)) assert sorted(od.items()) == pairs # kwds input od = OrderedDict() od.update(pairs) assert list(od.items()) == pairs # pairs input od = OrderedDict() od.update([('a', 1), ('b', 2), ('c', 9), ('d', 4)], c=3, e=5) assert list(od.items()) == pairs # mixed input # Issue 9137: Named argument called 'other' or 'self' # shouldn't be treated specially. if PY2 and sys.version_info >= (2, 7, 1) or PY3 and sys.version_info >= (3, 2): od = OrderedDict() od.update(self=23) assert list(od.items()) == [('self', 23)] od = OrderedDict() od.update(other={}) assert list(od.items()) == [('other', {})] od = OrderedDict() od.update(red=5, blue=6, other=7, self=8) assert 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) assert (list(d.items()) == [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)]) with pytest.raises(TypeError): OrderedDict().update(42) with pytest.raises(TypeError): OrderedDict().update((), ()) with pytest.raises(TypeError): OrderedDict.update()
def test_setitem(): od = OrderedDict([('d', 1), ('b', 2), ('c', 3), ('a', 4), ('e', 5)]) od['c'] = 10 # existing element od['f'] = 20 # new element assert (list(od.items()) == [('d', 1), ('b', 2), ('c', 10), ('a', 4), ('e', 5), ('f', 20)])