def test_dict_access_and_mod(self): # pylint: disable=too-many-locals, too-many-statements """ Test num dict access and modification """ # dicts for testing dict_0 = {} # Empty dictionary dict_1 = {1: 'Elephant'} # Single numeric key dict_2 = {1: 'Elephant', 2: 'Mouse'} # Multiple numeric keys # Construct NumDicts from dicts num_dict_0 = NumDict() num_dict_1 = NumDict(dict_1) num_dict_2 = NumDict(dict_2) # test __getitem__ self.assertEqual(num_dict_2[1], 'Elephant') with self.assertRaises(KeyError): _ = num_dict_1['Mouse'] # key is not numeric with self.assertRaises(KeyError): _ = num_dict_1.__getitem__('Mouse') # key is not numeric with self.assertRaises(KeyError): _ = num_dict_1[None] # key does not exist with self.assertRaises(KeyError): _ = num_dict_1.__getitem__(None) # key does not exist # Test __setitem__ num_dict_3 = NumDict(num_dict_2) self.assertEqual(num_dict_2, num_dict_3) num_dict_3[2] = 'Frog' self.assertNotEqual(num_dict_2, num_dict_3) # Check None keys and numeric key conversion num_dict_3['3'] = 'Armadillo' num_dict_3[None] = 'Cockroach' # Check long ints num_dict_3[12390809518259081208909880312] = 'Squid' num_dict_3['12390809518259081208909880312'] = 'Octopus' self.assertEqual(num_dict_3[12390809518259081208909880312], 'Octopus') with self.assertRaises(TypeError): num_dict_3.__setitem__('Gorilla', 1) # key is not numeric with self.assertRaises(TypeError): num_dict_3['Chimpanzee'] = 1 # key is not numeric with self.assertRaises(TypeError): num_dict_3[(4, 1)] = 1 # key is not numeric with self.assertRaises(TypeError): num_dict_3[[1, 3, 4]] = 1 # key is not numeric and is not hashable # Test __delitem__ del num_dict_3[3] del num_dict_3[None] with self.assertRaises(KeyError): del num_dict_3[3] # already deleted with self.assertRaises(KeyError): num_dict_3.__delitem__(3) # already deleted with self.assertRaises(KeyError): del num_dict_3['Mouse'] # key would not exist, since it is not numeric # Test clear num_dict_3.clear() self.assertEqual(num_dict_3, {}) # Test copy() num_dict_2a = dict_2.copy() self.assertEqual(num_dict_2, num_dict_2a) num_dict_2b = num_dict_2.copy() self.assertEqual(num_dict_2b, num_dict_2) num_dict_2c = UserDict({1: 'Elephant', 2: 'Mouse'}) num_dict_2d = num_dict_2c.copy() # making a copy of a UserDict is special cased self.assertEqual(num_dict_2c, num_dict_2d) class MyNumDict(NumDict): """ subclass Numdict for testing """ def display(self): """ add a method to subclass to differentiate from superclass """ print('MyNumDict:', self) my_num_dict = MyNumDict(num_dict_2) my_num_dict_a = my_num_dict.copy() self.assertEqual(my_num_dict_a, my_num_dict) my_num_dict[1] = 'Frog' self.assertNotEqual(my_num_dict_a, my_num_dict) # Test keys, items, values self.assertEqual(sorted(num_dict_2.keys()), sorted(dict_2.keys())) self.assertEqual(sorted(num_dict_2.items()), sorted(dict_2.items())) self.assertEqual(sorted(num_dict_2.values()), sorted(dict_2.values())) # Test "in". for i in num_dict_2: self.assertIn(i, num_dict_2) self.assertEqual(i in num_dict_1, i in dict_1) self.assertEqual(i in num_dict_0, i in dict_0) self.assertFalse(None in num_dict_2) self.assertEqual(None in num_dict_2, None in dict_2) dict_2[None] = 'Cow' num_dict_2[None] = dict_2[None] self.assertTrue(None in num_dict_2) self.assertEqual(None in num_dict_2, None in dict_2) self.assertEqual(num_dict_2.has_key(None), None in dict_2) if not PY3: self.assertEqual(num_dict_2.has_key(None), dict_2.has_key(None)) self.assertFalse('Penguin' in num_dict_2) # Test update test = NumDict() test.update(dict_2) self.assertEqual(test, num_dict_2) # Test get for i in num_dict_2: self.assertEqual(num_dict_2.get(i), num_dict_2[i]) self.assertEqual(num_dict_1.get(i), dict_1.get(i)) self.assertEqual(num_dict_0.get(i), dict_0.get(i)) for i in ['purple', None, 12312301924091284, 23]: self.assertEqual(num_dict_2.get(i), dict_2.get(i), i) with self.assertRaises(AssertionError): i = '1' self.assertEqual(num_dict_2.get(i), dict_2.get(i), i) # dict_2 expects string key which does not exist # Test "in" iteration. num_dict_2b = num_dict_2 for i in range(20): num_dict_2[i] = str(i) num_dict_2b[str(i)] = str(i) self.assertEqual(num_dict_2, num_dict_2b) ikeys = [] for k in num_dict_2: ikeys.append(k) self.assertEqual(set(ikeys), set(num_dict_2.keys())) # Test setdefault val = 1 test = NumDict() self.assertEqual(test.setdefault(val, 42), 42) self.assertEqual(test.setdefault(val, '42'), 42) self.assertNotEqual(test.setdefault(val, 42), '42') self.assertNotEqual(test.setdefault(val, '42'), '42') self.assertIn(val, test) self.assertEqual(test.setdefault(val, 23), 42) self.assertEqual(test.setdefault(val, '23'), 42) self.assertNotEqual(test.setdefault(val, 23), '42') self.assertNotEqual(test.setdefault(val, '23'), '42') self.assertIn(val, test) # Test pop val = 1 test = NumDict({val: 42}) self.assertEqual(test.pop(val), 42) self.assertRaises(KeyError, test.pop, val) self.assertEqual(test.pop(val, 1), 1) test[val] = 42 self.assertEqual(test.pop(val, 1), 42) # Test popitem val = 1 test = NumDict({val: 42}) self.assertEqual(test.popitem(), (val, 42)) self.assertRaises(KeyError, test.popitem)
def test_dict_access_and_modification(self): # dicts for testing d0 = {} # Empty dictionary d1 = {1: 'Elephant'} # Single numeric key d2 = {1: 'Elephant', 2: 'Mouse'} # Multiple numeric keys # Construct NumDicts from dicts n0 = NumDict() n1 = NumDict(d1) n2 = NumDict(d2) # test __getitem__ self.assertEqual(n2[1], 'Elephant') with self.assertRaises(KeyError): n1['Mouse'] # key is not numeric with self.assertRaises(KeyError): n1.__getitem__('Mouse') # key is not numeric with self.assertRaises(KeyError): n1[None] # key does not exist with self.assertRaises(KeyError): n1.__getitem__(None) # key does not exist # Test __setitem__ n3 = NumDict(n2) self.assertEqual(n2, n3) n3[2] = 'Frog' self.assertNotEqual(n2, n3) # Check None keys and numeric key conversion n3['3'] = 'Armadillo' n3[None] = 'Cockroach' # Check long ints n3[12390809518259081208909880312] = 'Squid' n3['12390809518259081208909880312'] = 'Octopus' self.assertEqual(n3[12390809518259081208909880312], 'Octopus') with self.assertRaises(TypeError): n3.__setitem__('Gorilla', 1) # key is not numeric with self.assertRaises(TypeError): n3['Chimpanzee'] = 1 # key is not numeric with self.assertRaises(TypeError): n3[(4, 1)] = 1 # key is not numeric with self.assertRaises(TypeError): n3[[1, 3, 4]] = 1 # key is not numeric and is not hashable # Test __delitem__ del n3[3] del n3[None] with self.assertRaises(KeyError): del n3[3] # already deleted with self.assertRaises(KeyError): n3.__delitem__(3) # already deleted with self.assertRaises(KeyError): del n3['Mouse'] # key would not exist, since it is not numeric # Test clear n3.clear() self.assertEqual(n3, {}) # Test copy() n2a = d2.copy() self.assertEqual(n2, n2a) n2b = n2.copy() self.assertEqual(n2b, n2) n2c = UserDict({1: 'Elephant', 2: 'Mouse'}) n2d = n2c.copy() # making a copy of a UserDict is special cased self.assertEqual(n2c, n2d) class MyNumDict(NumDict): """ subclass Numdict for testing """ def display(self): """ add a method to subclass to differentiate from superclass """ print('MyNumDict:', self) m2 = MyNumDict(n2) m2a = m2.copy() self.assertEqual(m2a, m2) m2[1] = 'Frog' self.assertNotEqual(m2a, m2) # Test keys, items, values self.assertEqual(sorted(n2.keys()), sorted(d2.keys())) self.assertEqual(sorted(n2.items()), sorted(d2.items())) self.assertEqual(sorted(n2.values()), sorted(d2.values())) # Test "in". for i in n2: self.assertIn(i, n2) self.assertEqual(i in n1, i in d1) self.assertEqual(i in n0, i in d0) self.assertFalse(None in n2) self.assertEqual(None in n2, None in d2) d2[None] = 'Cow' n2[None] = d2[None] self.assertTrue(None in n2) self.assertEqual(None in n2, None in d2) self.assertEqual(n2.has_key(None), None in d2) if not PY3: self.assertEqual(n2.has_key(None), d2.has_key(None)) self.assertFalse('Penguin' in n2) # Test update t = NumDict() t.update(d2) self.assertEqual(t, n2) # Test get for i in n2: self.assertEqual(n2.get(i), n2[i]) self.assertEqual(n1.get(i), d1.get(i)) self.assertEqual(n0.get(i), d0.get(i)) for i in ['purple', None, 12312301924091284, 23]: self.assertEqual(n2.get(i), d2.get(i), i) with self.assertRaises(AssertionError): i = '1' self.assertEqual(n2.get(i), d2.get(i), i) # d2 expects string key which does not exist # Test "in" iteration. n2b = n2 for i in range(20): n2[i] = str(i) n2b[str(i)] = str(i) self.assertEqual(n2, n2b) ikeys = [] for k in n2: ikeys.append(k) self.assertEqual(set(ikeys), set(n2.keys())) # Test setdefault x = 1 t = NumDict() self.assertEqual(t.setdefault(x, 42), 42) self.assertEqual(t.setdefault(x, '42'), 42) self.assertNotEqual(t.setdefault(x, 42), '42') self.assertNotEqual(t.setdefault(x, '42'), '42') self.assertIn(x, t) self.assertEqual(t.setdefault(x, 23), 42) self.assertEqual(t.setdefault(x, '23'), 42) self.assertNotEqual(t.setdefault(x, 23), '42') self.assertNotEqual(t.setdefault(x, '23'), '42') self.assertIn(x, t) # Test pop x = 1 t = NumDict({x: 42}) self.assertEqual(t.pop(x), 42) self.assertRaises(KeyError, t.pop, x) self.assertEqual(t.pop(x, 1), 1) t[x] = 42 self.assertEqual(t.pop(x, 1), 42) # Test popitem x = 1 t = NumDict({x: 42}) self.assertEqual(t.popitem(), (x, 42)) self.assertRaises(KeyError, t.popitem)