Best Python code snippet using autotest_python
test_dictviews.py
Source:test_dictviews.py
...9 def test_constructors_not_callable(self):10 kt = type({}.viewkeys())11 self.assertRaises(TypeError, kt, {})12 self.assertRaises(TypeError, kt)13 it = type({}.viewitems())14 self.assertRaises(TypeError, it, {})15 self.assertRaises(TypeError, it)16 vt = type({}.viewvalues())17 self.assertRaises(TypeError, vt, {})18 self.assertRaises(TypeError, vt)1920 def test_dict_keys(self):21 d = {1: 10, "a": "ABC"}22 keys = d.viewkeys()23 self.assertEqual(len(keys), 2)24 self.assertEqual(set(keys), set([1, "a"]))25 self.assertEqual(keys, set([1, "a"]))26 self.assertNotEqual(keys, set([1, "a", "b"]))27 self.assertNotEqual(keys, set([1, "b"]))28 self.assertNotEqual(keys, set([1]))29 self.assertNotEqual(keys, 42)30 self.assertIn(1, keys)31 self.assertIn("a", keys)32 self.assertNotIn(10, keys)33 self.assertNotIn("Z", keys)34 self.assertEqual(d.viewkeys(), d.viewkeys())35 e = {1: 11, "a": "def"}36 self.assertEqual(d.viewkeys(), e.viewkeys())37 del e["a"]38 self.assertNotEqual(d.viewkeys(), e.viewkeys())3940 def test_dict_items(self):41 d = {1: 10, "a": "ABC"}42 items = d.viewitems()43 self.assertEqual(len(items), 2)44 self.assertEqual(set(items), set([(1, 10), ("a", "ABC")]))45 self.assertEqual(items, set([(1, 10), ("a", "ABC")]))46 self.assertNotEqual(items, set([(1, 10), ("a", "ABC"), "junk"]))47 self.assertNotEqual(items, set([(1, 10), ("a", "def")]))48 self.assertNotEqual(items, set([(1, 10)]))49 self.assertNotEqual(items, 42)50 self.assertIn((1, 10), items)51 self.assertIn(("a", "ABC"), items)52 self.assertNotIn((1, 11), items)53 self.assertNotIn(1, items)54 self.assertNotIn((), items)55 self.assertNotIn((1,), items)56 self.assertNotIn((1, 2, 3), items)57 self.assertEqual(d.viewitems(), d.viewitems())58 e = d.copy()59 self.assertEqual(d.viewitems(), e.viewitems())60 e["a"] = "def"61 self.assertNotEqual(d.viewitems(), e.viewitems())6263 def test_dict_mixed_keys_items(self):64 d = {(1, 1): 11, (2, 2): 22}65 e = {1: 1, 2: 2}66 self.assertEqual(d.viewkeys(), e.viewitems())67 self.assertNotEqual(d.viewitems(), e.viewkeys())6869 def test_dict_values(self):70 d = {1: 10, "a": "ABC"}71 values = d.viewvalues()72 self.assertEqual(set(values), set([10, "ABC"]))73 self.assertEqual(len(values), 2)7475 def test_dict_repr(self):76 d = {1: 10, "a": "ABC"}77 self.assertIsInstance(repr(d), str)78 r = repr(d.viewitems())79 self.assertIsInstance(r, str)80 self.assertTrue(r == "dict_items([('a', 'ABC'), (1, 10)])" or81 r == "dict_items([(1, 10), ('a', 'ABC')])")82 r = repr(d.viewkeys())83 self.assertIsInstance(r, str)84 self.assertTrue(r == "dict_keys(['a', 1])" or85 r == "dict_keys([1, 'a'])")86 r = repr(d.viewvalues())87 self.assertIsInstance(r, str)88 self.assertTrue(r == "dict_values(['ABC', 10])" or89 r == "dict_values([10, 'ABC'])")9091 def test_keys_set_operations(self):92 d1 = {'a': 1, 'b': 2}93 d2 = {'b': 3, 'c': 2}94 d3 = {'d': 4, 'e': 5}95 self.assertEqual(d1.viewkeys() & d1.viewkeys(), {'a', 'b'})96 self.assertEqual(d1.viewkeys() & d2.viewkeys(), {'b'})97 self.assertEqual(d1.viewkeys() & d3.viewkeys(), set())98 self.assertEqual(d1.viewkeys() & set(d1.viewkeys()), {'a', 'b'})99 self.assertEqual(d1.viewkeys() & set(d2.viewkeys()), {'b'})100 self.assertEqual(d1.viewkeys() & set(d3.viewkeys()), set())101102 self.assertEqual(d1.viewkeys() | d1.viewkeys(), {'a', 'b'})103 self.assertEqual(d1.viewkeys() | d2.viewkeys(), {'a', 'b', 'c'})104 self.assertEqual(d1.viewkeys() | d3.viewkeys(), {'a', 'b', 'd', 'e'})105 self.assertEqual(d1.viewkeys() | set(d1.viewkeys()), {'a', 'b'})106 self.assertEqual(d1.viewkeys() | set(d2.viewkeys()), {'a', 'b', 'c'})107 self.assertEqual(d1.viewkeys() | set(d3.viewkeys()),108 {'a', 'b', 'd', 'e'})109110 self.assertEqual(d1.viewkeys() ^ d1.viewkeys(), set())111 self.assertEqual(d1.viewkeys() ^ d2.viewkeys(), {'a', 'c'})112 self.assertEqual(d1.viewkeys() ^ d3.viewkeys(), {'a', 'b', 'd', 'e'})113 self.assertEqual(d1.viewkeys() ^ set(d1.viewkeys()), set())114 self.assertEqual(d1.viewkeys() ^ set(d2.viewkeys()), {'a', 'c'})115 self.assertEqual(d1.viewkeys() ^ set(d3.viewkeys()),116 {'a', 'b', 'd', 'e'})117118 self.assertEqual(d1.viewkeys() - d1.viewkeys(), set())119 self.assertEqual(d1.viewkeys() - d2.viewkeys(), {'a'})120 self.assertEqual(d1.viewkeys() - d3.viewkeys(), {'a', 'b'})121 self.assertEqual(d1.viewkeys() - set(d1.viewkeys()), set())122 self.assertEqual(d1.viewkeys() - set(d2.viewkeys()), {'a'})123 self.assertEqual(d1.viewkeys() - set(d3.viewkeys()), {'a', 'b'})124125 def test_items_set_operations(self):126 d1 = {'a': 1, 'b': 2}127 d2 = {'a': 2, 'b': 2}128 d3 = {'d': 4, 'e': 5}129 self.assertEqual(130 d1.viewitems() & d1.viewitems(), {('a', 1), ('b', 2)})131 self.assertEqual(d1.viewitems() & d2.viewitems(), {('b', 2)})132 self.assertEqual(d1.viewitems() & d3.viewitems(), set())133 self.assertEqual(d1.viewitems() & set(d1.viewitems()),134 {('a', 1), ('b', 2)})135 self.assertEqual(d1.viewitems() & set(d2.viewitems()), {('b', 2)})136 self.assertEqual(d1.viewitems() & set(d3.viewitems()), set())137138 self.assertEqual(d1.viewitems() | d1.viewitems(),139 {('a', 1), ('b', 2)})140 self.assertEqual(d1.viewitems() | d2.viewitems(),141 {('a', 1), ('a', 2), ('b', 2)})142 self.assertEqual(d1.viewitems() | d3.viewitems(),143 {('a', 1), ('b', 2), ('d', 4), ('e', 5)})144 self.assertEqual(d1.viewitems() | set(d1.viewitems()),145 {('a', 1), ('b', 2)})146 self.assertEqual(d1.viewitems() | set(d2.viewitems()),147 {('a', 1), ('a', 2), ('b', 2)})148 self.assertEqual(d1.viewitems() | set(d3.viewitems()),149 {('a', 1), ('b', 2), ('d', 4), ('e', 5)})150151 self.assertEqual(d1.viewitems() ^ d1.viewitems(), set())152 self.assertEqual(d1.viewitems() ^ d2.viewitems(),153 {('a', 1), ('a', 2)})154 self.assertEqual(d1.viewitems() ^ d3.viewitems(),155 {('a', 1), ('b', 2), ('d', 4), ('e', 5)})156157 self.assertEqual(d1.viewitems() - d1.viewitems(), set())158 self.assertEqual(d1.viewitems() - d2.viewitems(), {('a', 1)})159 self.assertEqual(d1.viewitems() - d3.viewitems(), {('a', 1), ('b', 2)})160 self.assertEqual(d1.viewitems() - set(d1.viewitems()), set())161 self.assertEqual(d1.viewitems() - set(d2.viewitems()), {('a', 1)})162 self.assertEqual(d1.viewitems() - set(d3.viewitems()),163 {('a', 1), ('b', 2)})164165 def test_recursive_repr(self):166 d = {}167 d[42] = d.viewvalues()168 self.assertRaises(RuntimeError, repr, d)169170 def test_abc_registry(self):171 d = dict(a=1)172173 self.assertIsInstance(d.viewkeys(), collections.KeysView)174 self.assertIsInstance(d.viewkeys(), collections.MappingView)175 self.assertIsInstance(d.viewkeys(), collections.Set)176 self.assertIsInstance(d.viewkeys(), collections.Sized)177 self.assertIsInstance(d.viewkeys(), collections.Iterable)178 self.assertIsInstance(d.viewkeys(), collections.Container)179180 self.assertIsInstance(d.viewvalues(), collections.ValuesView)181 self.assertIsInstance(d.viewvalues(), collections.MappingView)182 self.assertIsInstance(d.viewvalues(), collections.Sized)183184 self.assertIsInstance(d.viewitems(), collections.ItemsView)185 self.assertIsInstance(d.viewitems(), collections.MappingView)186 self.assertIsInstance(d.viewitems(), collections.Set)187 self.assertIsInstance(d.viewitems(), collections.Sized)188 self.assertIsInstance(d.viewitems(), collections.Iterable)189 self.assertIsInstance(d.viewitems(), collections.Container)190191 def test_copy(self):192 d = {1: 10, "a": "ABC"}193 self.assertRaises(TypeError, copy.copy, d.viewkeys())194 self.assertRaises(TypeError, copy.copy, d.viewvalues())195 self.assertRaises(TypeError, copy.copy, d.viewitems())196197 def test_pickle(self):198 d = {1: 10, "a": "ABC"}199 for proto in range(pickle.HIGHEST_PROTOCOL + 1):200 self.assertRaises((TypeError, pickle.PicklingError),201 pickle.dumps, d.viewkeys(), proto)202 self.assertRaises((TypeError, pickle.PicklingError),203 pickle.dumps, d.viewvalues(), proto)204 self.assertRaises((TypeError, pickle.PicklingError),205 pickle.dumps, d.viewitems(), proto)206207208def test_main():209 test_support.run_unittest(DictSetTest)210211if __name__ == "__main__":
...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!