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