ยปCore Development>Code coverage>Lib/test/test_dictviews.py

Python code coverage for Lib/test/test_dictviews.py

#countcontent
1n/aimport collections
2n/aimport copy
3n/aimport pickle
4n/aimport unittest
5n/a
6n/aclass DictSetTest(unittest.TestCase):
7n/a
8n/a def test_constructors_not_callable(self):
9n/a kt = type({}.keys())
10n/a self.assertRaises(TypeError, kt, {})
11n/a self.assertRaises(TypeError, kt)
12n/a it = type({}.items())
13n/a self.assertRaises(TypeError, it, {})
14n/a self.assertRaises(TypeError, it)
15n/a vt = type({}.values())
16n/a self.assertRaises(TypeError, vt, {})
17n/a self.assertRaises(TypeError, vt)
18n/a
19n/a def test_dict_keys(self):
20n/a d = {1: 10, "a": "ABC"}
21n/a keys = d.keys()
22n/a self.assertEqual(len(keys), 2)
23n/a self.assertEqual(set(keys), {1, "a"})
24n/a self.assertEqual(keys, {1, "a"})
25n/a self.assertNotEqual(keys, {1, "a", "b"})
26n/a self.assertNotEqual(keys, {1, "b"})
27n/a self.assertNotEqual(keys, {1})
28n/a self.assertNotEqual(keys, 42)
29n/a self.assertIn(1, keys)
30n/a self.assertIn("a", keys)
31n/a self.assertNotIn(10, keys)
32n/a self.assertNotIn("Z", keys)
33n/a self.assertEqual(d.keys(), d.keys())
34n/a e = {1: 11, "a": "def"}
35n/a self.assertEqual(d.keys(), e.keys())
36n/a del e["a"]
37n/a self.assertNotEqual(d.keys(), e.keys())
38n/a
39n/a def test_dict_items(self):
40n/a d = {1: 10, "a": "ABC"}
41n/a items = d.items()
42n/a self.assertEqual(len(items), 2)
43n/a self.assertEqual(set(items), {(1, 10), ("a", "ABC")})
44n/a self.assertEqual(items, {(1, 10), ("a", "ABC")})
45n/a self.assertNotEqual(items, {(1, 10), ("a", "ABC"), "junk"})
46n/a self.assertNotEqual(items, {(1, 10), ("a", "def")})
47n/a self.assertNotEqual(items, {(1, 10)})
48n/a self.assertNotEqual(items, 42)
49n/a self.assertIn((1, 10), items)
50n/a self.assertIn(("a", "ABC"), items)
51n/a self.assertNotIn((1, 11), items)
52n/a self.assertNotIn(1, items)
53n/a self.assertNotIn((), items)
54n/a self.assertNotIn((1,), items)
55n/a self.assertNotIn((1, 2, 3), items)
56n/a self.assertEqual(d.items(), d.items())
57n/a e = d.copy()
58n/a self.assertEqual(d.items(), e.items())
59n/a e["a"] = "def"
60n/a self.assertNotEqual(d.items(), e.items())
61n/a
62n/a def test_dict_mixed_keys_items(self):
63n/a d = {(1, 1): 11, (2, 2): 22}
64n/a e = {1: 1, 2: 2}
65n/a self.assertEqual(d.keys(), e.items())
66n/a self.assertNotEqual(d.items(), e.keys())
67n/a
68n/a def test_dict_values(self):
69n/a d = {1: 10, "a": "ABC"}
70n/a values = d.values()
71n/a self.assertEqual(set(values), {10, "ABC"})
72n/a self.assertEqual(len(values), 2)
73n/a
74n/a def test_dict_repr(self):
75n/a d = {1: 10, "a": "ABC"}
76n/a self.assertIsInstance(repr(d), str)
77n/a r = repr(d.items())
78n/a self.assertIsInstance(r, str)
79n/a self.assertTrue(r == "dict_items([('a', 'ABC'), (1, 10)])" or
80n/a r == "dict_items([(1, 10), ('a', 'ABC')])")
81n/a r = repr(d.keys())
82n/a self.assertIsInstance(r, str)
83n/a self.assertTrue(r == "dict_keys(['a', 1])" or
84n/a r == "dict_keys([1, 'a'])")
85n/a r = repr(d.values())
86n/a self.assertIsInstance(r, str)
87n/a self.assertTrue(r == "dict_values(['ABC', 10])" or
88n/a r == "dict_values([10, 'ABC'])")
89n/a
90n/a def test_keys_set_operations(self):
91n/a d1 = {'a': 1, 'b': 2}
92n/a d2 = {'b': 3, 'c': 2}
93n/a d3 = {'d': 4, 'e': 5}
94n/a self.assertEqual(d1.keys() & d1.keys(), {'a', 'b'})
95n/a self.assertEqual(d1.keys() & d2.keys(), {'b'})
96n/a self.assertEqual(d1.keys() & d3.keys(), set())
97n/a self.assertEqual(d1.keys() & set(d1.keys()), {'a', 'b'})
98n/a self.assertEqual(d1.keys() & set(d2.keys()), {'b'})
99n/a self.assertEqual(d1.keys() & set(d3.keys()), set())
100n/a self.assertEqual(d1.keys() & tuple(d1.keys()), {'a', 'b'})
101n/a
102n/a self.assertEqual(d1.keys() | d1.keys(), {'a', 'b'})
103n/a self.assertEqual(d1.keys() | d2.keys(), {'a', 'b', 'c'})
104n/a self.assertEqual(d1.keys() | d3.keys(), {'a', 'b', 'd', 'e'})
105n/a self.assertEqual(d1.keys() | set(d1.keys()), {'a', 'b'})
106n/a self.assertEqual(d1.keys() | set(d2.keys()), {'a', 'b', 'c'})
107n/a self.assertEqual(d1.keys() | set(d3.keys()),
108n/a {'a', 'b', 'd', 'e'})
109n/a self.assertEqual(d1.keys() | (1, 2), {'a', 'b', 1, 2})
110n/a
111n/a self.assertEqual(d1.keys() ^ d1.keys(), set())
112n/a self.assertEqual(d1.keys() ^ d2.keys(), {'a', 'c'})
113n/a self.assertEqual(d1.keys() ^ d3.keys(), {'a', 'b', 'd', 'e'})
114n/a self.assertEqual(d1.keys() ^ set(d1.keys()), set())
115n/a self.assertEqual(d1.keys() ^ set(d2.keys()), {'a', 'c'})
116n/a self.assertEqual(d1.keys() ^ set(d3.keys()),
117n/a {'a', 'b', 'd', 'e'})
118n/a self.assertEqual(d1.keys() ^ tuple(d2.keys()), {'a', 'c'})
119n/a
120n/a self.assertEqual(d1.keys() - d1.keys(), set())
121n/a self.assertEqual(d1.keys() - d2.keys(), {'a'})
122n/a self.assertEqual(d1.keys() - d3.keys(), {'a', 'b'})
123n/a self.assertEqual(d1.keys() - set(d1.keys()), set())
124n/a self.assertEqual(d1.keys() - set(d2.keys()), {'a'})
125n/a self.assertEqual(d1.keys() - set(d3.keys()), {'a', 'b'})
126n/a self.assertEqual(d1.keys() - (0, 1), {'a', 'b'})
127n/a
128n/a self.assertFalse(d1.keys().isdisjoint(d1.keys()))
129n/a self.assertFalse(d1.keys().isdisjoint(d2.keys()))
130n/a self.assertFalse(d1.keys().isdisjoint(list(d2.keys())))
131n/a self.assertFalse(d1.keys().isdisjoint(set(d2.keys())))
132n/a self.assertTrue(d1.keys().isdisjoint({'x', 'y', 'z'}))
133n/a self.assertTrue(d1.keys().isdisjoint(['x', 'y', 'z']))
134n/a self.assertTrue(d1.keys().isdisjoint(set(['x', 'y', 'z'])))
135n/a self.assertTrue(d1.keys().isdisjoint(set(['x', 'y'])))
136n/a self.assertTrue(d1.keys().isdisjoint(['x', 'y']))
137n/a self.assertTrue(d1.keys().isdisjoint({}))
138n/a self.assertTrue(d1.keys().isdisjoint(d3.keys()))
139n/a
140n/a de = {}
141n/a self.assertTrue(de.keys().isdisjoint(set()))
142n/a self.assertTrue(de.keys().isdisjoint([]))
143n/a self.assertTrue(de.keys().isdisjoint(de.keys()))
144n/a self.assertTrue(de.keys().isdisjoint([1]))
145n/a
146n/a def test_items_set_operations(self):
147n/a d1 = {'a': 1, 'b': 2}
148n/a d2 = {'a': 2, 'b': 2}
149n/a d3 = {'d': 4, 'e': 5}
150n/a self.assertEqual(
151n/a d1.items() & d1.items(), {('a', 1), ('b', 2)})
152n/a self.assertEqual(d1.items() & d2.items(), {('b', 2)})
153n/a self.assertEqual(d1.items() & d3.items(), set())
154n/a self.assertEqual(d1.items() & set(d1.items()),
155n/a {('a', 1), ('b', 2)})
156n/a self.assertEqual(d1.items() & set(d2.items()), {('b', 2)})
157n/a self.assertEqual(d1.items() & set(d3.items()), set())
158n/a
159n/a self.assertEqual(d1.items() | d1.items(),
160n/a {('a', 1), ('b', 2)})
161n/a self.assertEqual(d1.items() | d2.items(),
162n/a {('a', 1), ('a', 2), ('b', 2)})
163n/a self.assertEqual(d1.items() | d3.items(),
164n/a {('a', 1), ('b', 2), ('d', 4), ('e', 5)})
165n/a self.assertEqual(d1.items() | set(d1.items()),
166n/a {('a', 1), ('b', 2)})
167n/a self.assertEqual(d1.items() | set(d2.items()),
168n/a {('a', 1), ('a', 2), ('b', 2)})
169n/a self.assertEqual(d1.items() | set(d3.items()),
170n/a {('a', 1), ('b', 2), ('d', 4), ('e', 5)})
171n/a
172n/a self.assertEqual(d1.items() ^ d1.items(), set())
173n/a self.assertEqual(d1.items() ^ d2.items(),
174n/a {('a', 1), ('a', 2)})
175n/a self.assertEqual(d1.items() ^ d3.items(),
176n/a {('a', 1), ('b', 2), ('d', 4), ('e', 5)})
177n/a
178n/a self.assertEqual(d1.items() - d1.items(), set())
179n/a self.assertEqual(d1.items() - d2.items(), {('a', 1)})
180n/a self.assertEqual(d1.items() - d3.items(), {('a', 1), ('b', 2)})
181n/a self.assertEqual(d1.items() - set(d1.items()), set())
182n/a self.assertEqual(d1.items() - set(d2.items()), {('a', 1)})
183n/a self.assertEqual(d1.items() - set(d3.items()), {('a', 1), ('b', 2)})
184n/a
185n/a self.assertFalse(d1.items().isdisjoint(d1.items()))
186n/a self.assertFalse(d1.items().isdisjoint(d2.items()))
187n/a self.assertFalse(d1.items().isdisjoint(list(d2.items())))
188n/a self.assertFalse(d1.items().isdisjoint(set(d2.items())))
189n/a self.assertTrue(d1.items().isdisjoint({'x', 'y', 'z'}))
190n/a self.assertTrue(d1.items().isdisjoint(['x', 'y', 'z']))
191n/a self.assertTrue(d1.items().isdisjoint(set(['x', 'y', 'z'])))
192n/a self.assertTrue(d1.items().isdisjoint(set(['x', 'y'])))
193n/a self.assertTrue(d1.items().isdisjoint({}))
194n/a self.assertTrue(d1.items().isdisjoint(d3.items()))
195n/a
196n/a de = {}
197n/a self.assertTrue(de.items().isdisjoint(set()))
198n/a self.assertTrue(de.items().isdisjoint([]))
199n/a self.assertTrue(de.items().isdisjoint(de.items()))
200n/a self.assertTrue(de.items().isdisjoint([1]))
201n/a
202n/a def test_recursive_repr(self):
203n/a d = {}
204n/a d[42] = d.values()
205n/a self.assertRaises(RecursionError, repr, d)
206n/a
207n/a def test_copy(self):
208n/a d = {1: 10, "a": "ABC"}
209n/a self.assertRaises(TypeError, copy.copy, d.keys())
210n/a self.assertRaises(TypeError, copy.copy, d.values())
211n/a self.assertRaises(TypeError, copy.copy, d.items())
212n/a
213n/a def test_compare_error(self):
214n/a class Exc(Exception):
215n/a pass
216n/a
217n/a class BadEq:
218n/a def __hash__(self):
219n/a return 7
220n/a def __eq__(self, other):
221n/a raise Exc
222n/a
223n/a k1, k2 = BadEq(), BadEq()
224n/a v1, v2 = BadEq(), BadEq()
225n/a d = {k1: v1}
226n/a
227n/a self.assertIn(k1, d)
228n/a self.assertIn(k1, d.keys())
229n/a self.assertIn(v1, d.values())
230n/a self.assertIn((k1, v1), d.items())
231n/a
232n/a self.assertRaises(Exc, d.__contains__, k2)
233n/a self.assertRaises(Exc, d.keys().__contains__, k2)
234n/a self.assertRaises(Exc, d.items().__contains__, (k2, v1))
235n/a self.assertRaises(Exc, d.items().__contains__, (k1, v2))
236n/a with self.assertRaises(Exc):
237n/a v2 in d.values()
238n/a
239n/a def test_pickle(self):
240n/a d = {1: 10, "a": "ABC"}
241n/a for proto in range(pickle.HIGHEST_PROTOCOL + 1):
242n/a self.assertRaises((TypeError, pickle.PicklingError),
243n/a pickle.dumps, d.keys(), proto)
244n/a self.assertRaises((TypeError, pickle.PicklingError),
245n/a pickle.dumps, d.values(), proto)
246n/a self.assertRaises((TypeError, pickle.PicklingError),
247n/a pickle.dumps, d.items(), proto)
248n/a
249n/a def test_abc_registry(self):
250n/a d = dict(a=1)
251n/a
252n/a self.assertIsInstance(d.keys(), collections.KeysView)
253n/a self.assertIsInstance(d.keys(), collections.MappingView)
254n/a self.assertIsInstance(d.keys(), collections.Set)
255n/a self.assertIsInstance(d.keys(), collections.Sized)
256n/a self.assertIsInstance(d.keys(), collections.Iterable)
257n/a self.assertIsInstance(d.keys(), collections.Container)
258n/a
259n/a self.assertIsInstance(d.values(), collections.ValuesView)
260n/a self.assertIsInstance(d.values(), collections.MappingView)
261n/a self.assertIsInstance(d.values(), collections.Sized)
262n/a
263n/a self.assertIsInstance(d.items(), collections.ItemsView)
264n/a self.assertIsInstance(d.items(), collections.MappingView)
265n/a self.assertIsInstance(d.items(), collections.Set)
266n/a self.assertIsInstance(d.items(), collections.Sized)
267n/a self.assertIsInstance(d.items(), collections.Iterable)
268n/a self.assertIsInstance(d.items(), collections.Container)
269n/a
270n/a
271n/aif __name__ == "__main__":
272n/a unittest.main()