Best Python code snippet using autotest_python
test_dictviews.py
Source:test_dictviews.py
...4import collections5from test import test_support6class DictSetTest(unittest.TestCase):7 def test_constructors_not_callable(self):8 kt = type({}.viewkeys())9 self.assertRaises(TypeError, kt, {})10 self.assertRaises(TypeError, kt)11 it = type({}.viewitems())12 self.assertRaises(TypeError, it, {})13 self.assertRaises(TypeError, it)14 vt = type({}.viewvalues())15 self.assertRaises(TypeError, vt, {})16 self.assertRaises(TypeError, vt)17 def test_dict_keys(self):18 d = {1: 10, "a": "ABC"}19 keys = d.viewkeys()20 self.assertEqual(len(keys), 2)21 self.assertEqual(set(keys), set([1, "a"]))22 self.assertEqual(keys, set([1, "a"]))23 self.assertNotEqual(keys, set([1, "a", "b"]))24 self.assertNotEqual(keys, set([1, "b"]))25 self.assertNotEqual(keys, set([1]))26 self.assertNotEqual(keys, 42)27 self.assertIn(1, keys)28 self.assertIn("a", keys)29 self.assertNotIn(10, keys)30 self.assertNotIn("Z", keys)31 self.assertEqual(d.viewkeys(), d.viewkeys())32 e = {1: 11, "a": "def"}33 self.assertEqual(d.viewkeys(), e.viewkeys())34 del e["a"]35 self.assertNotEqual(d.viewkeys(), e.viewkeys())36 def test_dict_items(self):37 d = {1: 10, "a": "ABC"}38 items = d.viewitems()39 self.assertEqual(len(items), 2)40 self.assertEqual(set(items), set([(1, 10), ("a", "ABC")]))41 self.assertEqual(items, set([(1, 10), ("a", "ABC")]))42 self.assertNotEqual(items, set([(1, 10), ("a", "ABC"), "junk"]))43 self.assertNotEqual(items, set([(1, 10), ("a", "def")]))44 self.assertNotEqual(items, set([(1, 10)]))45 self.assertNotEqual(items, 42)46 self.assertIn((1, 10), items)47 self.assertIn(("a", "ABC"), items)48 self.assertNotIn((1, 11), items)49 self.assertNotIn(1, items)50 self.assertNotIn((), items)51 self.assertNotIn((1,), items)52 self.assertNotIn((1, 2, 3), items)53 self.assertEqual(d.viewitems(), d.viewitems())54 e = d.copy()55 self.assertEqual(d.viewitems(), e.viewitems())56 e["a"] = "def"57 self.assertNotEqual(d.viewitems(), e.viewitems())58 def test_dict_mixed_keys_items(self):59 d = {(1, 1): 11, (2, 2): 22}60 e = {1: 1, 2: 2}61 self.assertEqual(d.viewkeys(), e.viewitems())62 self.assertNotEqual(d.viewitems(), e.viewkeys())63 def test_dict_values(self):64 d = {1: 10, "a": "ABC"}65 values = d.viewvalues()66 self.assertEqual(set(values), set([10, "ABC"]))67 self.assertEqual(len(values), 2)68 def test_dict_repr(self):69 d = {1: 10, "a": "ABC"}70 self.assertIsInstance(repr(d), str)71 r = repr(d.viewitems())72 self.assertIsInstance(r, str)73 self.assertTrue(r == "dict_items([('a', 'ABC'), (1, 10)])" or74 r == "dict_items([(1, 10), ('a', 'ABC')])")75 r = repr(d.viewkeys())76 self.assertIsInstance(r, str)77 self.assertTrue(r == "dict_keys(['a', 1])" or78 r == "dict_keys([1, 'a'])")79 r = repr(d.viewvalues())80 self.assertIsInstance(r, str)81 self.assertTrue(r == "dict_values(['ABC', 10])" or82 r == "dict_values([10, 'ABC'])")83 def test_keys_set_operations(self):84 d1 = {'a': 1, 'b': 2}85 d2 = {'b': 3, 'c': 2}86 d3 = {'d': 4, 'e': 5}87 self.assertEqual(d1.viewkeys() & d1.viewkeys(), {'a', 'b'})88 self.assertEqual(d1.viewkeys() & d2.viewkeys(), {'b'})89 self.assertEqual(d1.viewkeys() & d3.viewkeys(), set())90 self.assertEqual(d1.viewkeys() & set(d1.viewkeys()), {'a', 'b'})91 self.assertEqual(d1.viewkeys() & set(d2.viewkeys()), {'b'})92 self.assertEqual(d1.viewkeys() & set(d3.viewkeys()), set())93 self.assertEqual(d1.viewkeys() & tuple(d1.viewkeys()), {'a', 'b'})94 self.assertEqual(d1.viewkeys() | d1.viewkeys(), {'a', 'b'})95 self.assertEqual(d1.viewkeys() | d2.viewkeys(), {'a', 'b', 'c'})96 self.assertEqual(d1.viewkeys() | d3.viewkeys(), {'a', 'b', 'd', 'e'})97 self.assertEqual(d1.viewkeys() | set(d1.viewkeys()), {'a', 'b'})98 self.assertEqual(d1.viewkeys() | set(d2.viewkeys()), {'a', 'b', 'c'})99 self.assertEqual(d1.viewkeys() | set(d3.viewkeys()),100 {'a', 'b', 'd', 'e'})101 self.assertEqual(d1.viewkeys() | (1, 2), {'a', 'b', 1, 2})102 self.assertEqual(d1.viewkeys() ^ d1.viewkeys(), set())103 self.assertEqual(d1.viewkeys() ^ d2.viewkeys(), {'a', 'c'})104 self.assertEqual(d1.viewkeys() ^ d3.viewkeys(), {'a', 'b', 'd', 'e'})105 self.assertEqual(d1.viewkeys() ^ set(d1.viewkeys()), set())106 self.assertEqual(d1.viewkeys() ^ set(d2.viewkeys()), {'a', 'c'})107 self.assertEqual(d1.viewkeys() ^ set(d3.viewkeys()),108 {'a', 'b', 'd', 'e'})109 self.assertEqual(d1.viewkeys() ^ tuple(d2.keys()), {'a', 'c'})110 self.assertEqual(d1.viewkeys() - d1.viewkeys(), set())111 self.assertEqual(d1.viewkeys() - d2.viewkeys(), {'a'})112 self.assertEqual(d1.viewkeys() - d3.viewkeys(), {'a', 'b'})113 self.assertEqual(d1.viewkeys() - set(d1.viewkeys()), set())114 self.assertEqual(d1.viewkeys() - set(d2.viewkeys()), {'a'})115 self.assertEqual(d1.viewkeys() - set(d3.viewkeys()), {'a', 'b'})116 self.assertEqual(d1.viewkeys() - (0, 1), {'a', 'b'})117 def test_items_set_operations(self):118 d1 = {'a': 1, 'b': 2}119 d2 = {'a': 2, 'b': 2}120 d3 = {'d': 4, 'e': 5}121 self.assertEqual(122 d1.viewitems() & d1.viewitems(), {('a', 1), ('b', 2)})123 self.assertEqual(d1.viewitems() & d2.viewitems(), {('b', 2)})124 self.assertEqual(d1.viewitems() & d3.viewitems(), set())125 self.assertEqual(d1.viewitems() & set(d1.viewitems()),126 {('a', 1), ('b', 2)})127 self.assertEqual(d1.viewitems() & set(d2.viewitems()), {('b', 2)})128 self.assertEqual(d1.viewitems() & set(d3.viewitems()), set())129 self.assertEqual(d1.viewitems() | d1.viewitems(),130 {('a', 1), ('b', 2)})131 self.assertEqual(d1.viewitems() | d2.viewitems(),132 {('a', 1), ('a', 2), ('b', 2)})133 self.assertEqual(d1.viewitems() | d3.viewitems(),134 {('a', 1), ('b', 2), ('d', 4), ('e', 5)})135 self.assertEqual(d1.viewitems() | set(d1.viewitems()),136 {('a', 1), ('b', 2)})137 self.assertEqual(d1.viewitems() | set(d2.viewitems()),138 {('a', 1), ('a', 2), ('b', 2)})139 self.assertEqual(d1.viewitems() | set(d3.viewitems()),140 {('a', 1), ('b', 2), ('d', 4), ('e', 5)})141 self.assertEqual(d1.viewitems() ^ d1.viewitems(), set())142 self.assertEqual(d1.viewitems() ^ d2.viewitems(),143 {('a', 1), ('a', 2)})144 self.assertEqual(d1.viewitems() ^ d3.viewitems(),145 {('a', 1), ('b', 2), ('d', 4), ('e', 5)})146 self.assertEqual(d1.viewitems() - d1.viewitems(), set())147 self.assertEqual(d1.viewitems() - d2.viewitems(), {('a', 1)})148 self.assertEqual(d1.viewitems() - d3.viewitems(), {('a', 1), ('b', 2)})149 self.assertEqual(d1.viewitems() - set(d1.viewitems()), set())150 self.assertEqual(d1.viewitems() - set(d2.viewitems()), {('a', 1)})151 self.assertEqual(d1.viewitems() - set(d3.viewitems()),152 {('a', 1), ('b', 2)})153 def test_recursive_repr(self):154 d = {}155 d[42] = d.viewvalues()156 self.assertRaises(RuntimeError, repr, d)157 def test_abc_registry(self):158 d = dict(a=1)159 self.assertIsInstance(d.viewkeys(), collections.KeysView)160 self.assertIsInstance(d.viewkeys(), collections.MappingView)161 self.assertIsInstance(d.viewkeys(), collections.Set)162 self.assertIsInstance(d.viewkeys(), collections.Sized)163 self.assertIsInstance(d.viewkeys(), collections.Iterable)164 self.assertIsInstance(d.viewkeys(), collections.Container)165 self.assertIsInstance(d.viewvalues(), collections.ValuesView)166 self.assertIsInstance(d.viewvalues(), collections.MappingView)167 self.assertIsInstance(d.viewvalues(), collections.Sized)168 self.assertIsInstance(d.viewitems(), collections.ItemsView)169 self.assertIsInstance(d.viewitems(), collections.MappingView)170 self.assertIsInstance(d.viewitems(), collections.Set)171 self.assertIsInstance(d.viewitems(), collections.Sized)172 self.assertIsInstance(d.viewitems(), collections.Iterable)173 self.assertIsInstance(d.viewitems(), collections.Container)174 def test_copy(self):175 d = {1: 10, "a": "ABC"}176 self.assertRaises(TypeError, copy.copy, d.viewkeys())177 self.assertRaises(TypeError, copy.copy, d.viewvalues())178 self.assertRaises(TypeError, copy.copy, d.viewitems())179 def test_pickle(self):180 d = {1: 10, "a": "ABC"}181 for proto in range(pickle.HIGHEST_PROTOCOL + 1):182 self.assertRaises((TypeError, pickle.PicklingError),183 pickle.dumps, d.viewkeys(), proto)184 self.assertRaises((TypeError, pickle.PicklingError),185 pickle.dumps, d.viewvalues(), proto)186 self.assertRaises((TypeError, pickle.PicklingError),187 pickle.dumps, d.viewitems(), proto)188def test_main():189 test_support.run_unittest(DictSetTest)190if __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!!