def test_basics(self): c = ChainMap() c['a'] = 1 c['b'] = 2 d = c.new_child() d['b'] = 20 d['c'] = 30 self.assertEqual(d.maps, [{'b':20, 'c':30}, {'a':1, 'b':2}]) # check internal state self.assertEqual(sorted(d.items()), sorted(dict(a=1, b=20, c=30).items())) # check items/iter/getitem self.assertEqual(len(d), 3) # check len for key in 'abc': # check contains self.assertIn(key, d) for k, v in dict(a=1, b=20, c=30, z=100).items(): # check get self.assertEqual(d.get(k, 100), v) del d['b'] # unmask a value self.assertEqual(d.maps, [{'c':30}, {'a':1, 'b':2}]) # check internal state self.assertEqual(sorted(d.items()), sorted(dict(a=1, b=2, c=30).items())) # check items/iter/getitem self.assertEqual(len(d), 3) # check len for key in 'abc': # check contains self.assertIn(key, d) for k, v in dict(a=1, b=2, c=30, z=100).items(): # check get self.assertEqual(d.get(k, 100), v) self.assertIn(repr(d), [ # check repr type(d).__name__ + "({'c': 30}, {'a': 1, 'b': 2})", type(d).__name__ + "({'c': 30}, {'b': 2, 'a': 1})" ]) for e in d.copy(), copy.copy(d): # check shallow copies self.assertEqual(d, e) self.assertEqual(d.maps, e.maps) self.assertIsNot(d, e) self.assertIsNot(d.maps[0], e.maps[0]) for m1, m2 in zip(d.maps[1:], e.maps[1:]): self.assertIs(m1, m2) _ChainMap = ChainMap for e in [pickle.loads(pickle.dumps(d)), copy.deepcopy(d), eval(repr(d)) ]: # check deep copies self.assertEqual(d, e) self.assertEqual(d.maps, e.maps) self.assertIsNot(d, e) for m1, m2 in zip(d.maps, e.maps): self.assertIsNot(m1, m2, e) f = d.new_child() f['b'] = 5 self.assertEqual(f.maps, [{'b': 5}, {'c':30}, {'a':1, 'b':2}]) self.assertEqual(f.parents.maps, [{'c':30}, {'a':1, 'b':2}]) # check parents self.assertEqual(f['b'], 5) # find first in chain self.assertEqual(f.parents['b'], 2) # look beyond maps[0]
def test_basics(self): c = ChainMap() c['a'] = 1 c['b'] = 2 d = c.new_child() d['b'] = 20 d['c'] = 30 self.assertEqual(d.maps, [{'b':20, 'c':30}, {'a':1, 'b':2}]) # check internal state self.assertEqual(d.items(), dict(a=1, b=20, c=30).items()) # check items/iter/getitem self.assertEqual(len(d), 3) # check len for key in 'abc': # check contains self.assertIn(key, d) for k, v in dict(a=1, b=20, c=30, z=100).items(): # check get self.assertEqual(d.get(k, 100), v) del d['b'] # unmask a value self.assertEqual(d.maps, [{'c':30}, {'a':1, 'b':2}]) # check internal state self.assertEqual(d.items(), dict(a=1, b=2, c=30).items()) # check items/iter/getitem self.assertEqual(len(d), 3) # check len for key in 'abc': # check contains self.assertIn(key, d) for k, v in dict(a=1, b=2, c=30, z=100).items(): # check get self.assertEqual(d.get(k, 100), v) self.assertIn(repr(d), [ # check repr type(d).__name__ + "({'c': 30}, {'a': 1, 'b': 2})", type(d).__name__ + "({'c': 30}, {'b': 2, 'a': 1})" ]) for e in d.copy(), copy.copy(d): # check shallow copies self.assertEqual(d, e) self.assertEqual(d.maps, e.maps) self.assertIsNot(d, e) self.assertIsNot(d.maps[0], e.maps[0]) for m1, m2 in zip(d.maps[1:], e.maps[1:]): self.assertIs(m1, m2) _ChainMap = ChainMap for e in [pickle.loads(pickle.dumps(d)), copy.deepcopy(d), eval(repr(d)) ]: # check deep copies self.assertEqual(d, e) self.assertEqual(d.maps, e.maps) self.assertIsNot(d, e) for m1, m2 in zip(d.maps, e.maps): self.assertIsNot(m1, m2, e) f = d.new_child() f['b'] = 5 self.assertEqual(f.maps, [{'b': 5}, {'c':30}, {'a':1, 'b':2}]) self.assertEqual(f.parents.maps, [{'c':30}, {'a':1, 'b':2}]) # check parents self.assertEqual(f['b'], 5) # find first in chain self.assertEqual(f.parents['b'], 2) # look beyond maps[0]
def test_dict_coercion(self): d = ChainMap(dict(a=1, b=2), dict(b=20, c=30)) self.assertEqual(dict(d), dict(a=1, b=2, c=30)) self.assertEqual(dict(d.items()), dict(a=1, b=2, c=30))
def test_bool(self): self.assertFalse(ChainMap()) self.assertFalse(ChainMap({}, {})) self.assertTrue(ChainMap({1: 2}, {})) self.assertTrue(ChainMap({}, {1: 2}))
def test_contructor(self): self.assertEqual(ChainMap().maps, [{}]) # no-args --> one new dict self.assertEqual(ChainMap({1: 2}).maps, [{1: 2}]) # 1 arg --> list