Debian Codegraph @

tests/collections_test.py

raw
1 # This document is free and open-source software, subject to the OSI-approved
2 # BSD license below.
3 #
4 # Copyright (c) 2014 Alexis Petrounias <www.petrounias.org>,
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
9 #
10 # * Redistributions of source code must retain the above copyright notice, this
11 # list of conditions and the following disclaimer.
12 #
13 # * Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
16 #
17 # * Neither the name of the author nor the names of its contributors may be used
18 # to endorse or promote products derived from this software without specific
19 # prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 """
32 Unit tests for collections
33 """
34 from collections import OrderedDict, defaultdict
35
36 import jsonpickle
37
38 __status__ = "Stable"
39 __version__ = "1.0.0"
40 __maintainer__ = "Alexis Petrounias <www.petrounias.org>"
41 __author__ = "Alexis Petrounias <www.petrounias.org>"
42
43
44 """
45 Classes featuring various collections used by
46 :mod:`restorable_collections_tests` unit tests; these classes must be
47 module-level (including the default dictionary factory method) so that they
48 can be pickled.
49
50 Tests restorable collections by creating pickled structures featuring
51 no cycles, self cycles, and mutual cycles, for all supported dictionary and
52 set wrappers. Python built-in dictionaries and sets are also tested with
53 expectation of failure via raising exceptions.
54
55 """
56
57
58 class Group:
59 def __init__(self, name):
60 super().__init__()
61 self.name = name
62 self.elements = []
63
64 def __repr__(self):
65 return f'Group({self.name})'
66
67
68 class C:
69 def __init__(self, v):
70 super().__init__()
71 self.v = v
72 self.plain = dict()
73 self.plain_ordered = OrderedDict()
74 self.plain_default = defaultdict(c_factory)
75
76 def add(self, key, value):
77 self.plain[key] = (key, value)
78 self.plain_ordered[key] = (key, value)
79 self.plain_default[key] = (key, value)
80
81 def __hash__(self):
82 return hash(self.v) if hasattr(self, 'v') else id(self)
83
84 def __repr__(self):
85 return f'C({self.v})'
86
87
88 def c_factory():
89 return (C(0), '_')
90
91
92 class D:
93 def __init__(self, v):
94 super().__init__()
95 self.v = v
96 self.plain = set()
97
98 def add(self, item):
99 self.plain.add(item)
100
101 def __hash__(self):
102 return hash(self.v) if hasattr(self, 'v') else id(self)
103
104 def __repr__(self):
105 return f'D({self.v})'
106
107
108 def pickle_and_unpickle(obj):
109 encoded = jsonpickle.encode(obj, keys=True)
110 return jsonpickle.decode(encoded, keys=True)
111
112
113 def test_dict_no_cycle():
114 g = Group('group')
115 c1 = C(42)
116 g.elements.append(c1)
117 c2 = C(67)
118 g.elements.append(c2)
119 c1.add(c2, 'a') # points to c2, which does not point to anything
120
121 assert c2 in c1.plain
122 assert c2 in c1.plain_ordered
123 assert c2 in c1.plain_default
124
125 gu = pickle_and_unpickle(g)
126 c1u = gu.elements[0]
127 c2u = gu.elements[1]
128
129 # check existence of keys directly
130 assert c2u in c1u.plain.keys()
131 assert c2u in c1u.plain_ordered.keys()
132 assert c2u in c1u.plain_default.keys()
133
134 # check direct key-based lookup
135 assert c2u == c1u.plain[c2u][0]
136 assert c2u == c1u.plain_ordered[c2u][0]
137 assert c2u == c1u.plain_default[c2u][0]
138
139 # check key lookup with key directly from keys()
140 plain_keys = list(c1u.plain.keys())
141 ordered_keys = list(c1u.plain_ordered.keys())
142 default_keys = list(c1u.plain_default.keys())
143 assert c2u == c1u.plain[plain_keys[0]][0]
144 assert c2u == c1u.plain_ordered[ordered_keys[0]][0]
145 assert c2u == c1u.plain_default[default_keys[0]][0]
146 assert c2u == c1u.plain[plain_keys[0]][0]
147 assert c2u == c1u.plain_ordered[ordered_keys[0]][0]
148 assert c2u == c1u.plain_default[default_keys[0]][0]
149
150
151 def test_dict_self_cycle():
152 g = Group('group')
153 c1 = C(42)
154 g.elements.append(c1)
155 c2 = C(67)
156 g.elements.append(c2)
157 c1.add(c1, 'a') # cycle to itself
158 c1.add(c2, 'b') # c2 does not point to itself nor c1
159
160 assert c1 in c1.plain
161 assert c1 in c1.plain_ordered
162 assert c1 in c1.plain_default
163
164 gu = pickle_and_unpickle(g)
165 c1u = gu.elements[0]
166 c2u = gu.elements[1]
167
168 # check existence of keys directly
169 # key c1u
170 assert c1u in list(c1u.plain.keys())
171 assert c1u in list(c1u.plain_ordered.keys())
172 assert c1u in list(c1u.plain_default.keys())
173
174 # key c2u
175 assert c2u in list(c1u.plain.keys())
176 assert c2u in list(c1u.plain_ordered.keys())
177 assert c2u in list(c1u.plain_default.keys())
178
179 # check direct key-based lookup
180
181 # key c1u
182 assert 42 == c1u.plain[c1u][0].v
183 assert 42 == c1u.plain_ordered[c1u][0].v
184
185 assert len(c1u.plain_default) == 2
186 assert 42 == c1u.plain_default[c1u][0].v
187 # No new entries were created.
188 assert len(c1u.plain_default) == 2
189
190 # key c2u
191 # succeeds because c2u does not have a cycle to itself
192 assert c2u == c1u.plain[c2u][0]
193 # succeeds because c2u does not have a cycle to itself
194 assert c2u == c1u.plain_ordered[c2u][0]
195 # succeeds because c2u does not have a cycle to itself
196 assert c2u == c1u.plain_default[c2u][0]
197 assert c2u == c1u.plain[c2u][0]
198 assert c2u == c1u.plain_ordered[c2u][0]
199 assert c2u == c1u.plain_default[c2u][0]
200
201 # check key lookup with key directly from keys()
202 # key c1u
203 plain_keys = list(c1u.plain.keys())
204 ordered_keys = list(c1u.plain_ordered.keys())
205 default_keys = list(c1u.plain_default.keys())
206 value = 42
207 assert value == c1u.plain[plain_keys[0]][0].v
208 42 == c1u.plain_ordered[ordered_keys[0]][0].v
209 42 == c1u.plain_default[default_keys[0]][0].v
210
211 # key c2u
212 # succeeds because c2u does not have a cycle to itself
213 assert c2u == c1u.plain[plain_keys[1]][0]
214 # succeeds because c2u does not have a cycle to itself
215 assert c2u == c1u.plain_ordered[ordered_keys[1]][0]
216 assert 67 == c1u.plain_default[default_keys[1]][0].v
217
218
219 def test_dict_mutual_cycle():
220 g = Group('group')
221 c1 = C(42)
222 g.elements.append(c1)
223 c2 = C(67)
224 g.elements.append(c2)
225
226 c1.add(c2, 'a') # points to c2, which points to c1, forming cycle
227 c2.add(c1, 'a') # points to c1 in order to form cycle
228
229 assert c2 in c1.plain
230 assert c2 in c1.plain_ordered
231 assert c2 in c1.plain_default
232
233 assert c1 in c2.plain
234 assert c1 in c2.plain_ordered
235 assert c1 in c2.plain_default
236
237 gu = pickle_and_unpickle(g)
238 c1u = gu.elements[0]
239 c2u = gu.elements[1]
240
241 # check existence of keys directly
242 # key c2u
243 assert c2u in c1u.plain.keys()
244 assert c2u in c1u.plain_ordered.keys()
245 assert c2u in c1u.plain_default.keys()
246
247 # key c1u
248 assert c1u in list(c2u.plain.keys())
249 assert c1u in list(c2u.plain_ordered.keys())
250 assert c1u in list(c2u.plain_default.keys())
251
252 # check direct key-based lookup
253 # key c2u, succeed because c2u added to c1u after __setstate__
254 assert c2u == c1u.plain[c2u][0]
255 assert c2u == c1u.plain_ordered[c2u][0]
256 assert c2u == c1u.plain_default[c2u][0]
257
258 # key c1u
259 assert isinstance(c2u.plain[c1u][0], C)
260 assert 42 == c2u.plain[c1u][0].v
261 assert isinstance(c2u.plain_ordered[c1u][0], C)
262
263 # succeeds
264 assert len(c2u.plain_default) == 1
265 assert 42 == c2u.plain_default[c1u][0].v
266 # Ensure that no new entry is created by verifying that the length
267 # has not changed.
268 assert len(c2u.plain_default) == 1
269
270 # check key lookup with key directly from keys()
271
272 # key c2u, succeed because c2u added to c1u after __setstate__
273 plain_keys = list(c1u.plain.keys())
274 ordered_keys = list(c1u.plain_ordered.keys())
275 default_keys = list(c1u.plain_default.keys())
276 assert c2u == c1u.plain[plain_keys[0]][0]
277 assert c2u == c1u.plain_ordered[ordered_keys[0]][0]
278 assert c2u == c1u.plain_default[default_keys[0]][0]
279
280 # key c1u
281 plain_keys = list(c2u.plain.keys())
282 ordered_keys = list(c2u.plain_ordered.keys())
283 default_keys = list(c2u.plain_default.keys())
284 assert 42 == c2u.plain[plain_keys[0]][0].v
285 assert 42 == c2u.plain_ordered[ordered_keys[0]][0].v
286 assert 42 == c2u.plain_default[default_keys[0]][0].v
287 assert isinstance(c2u.plain[plain_keys[0]], tuple)
288 assert isinstance(c2u.plain_ordered[ordered_keys[0]], tuple)
289
290
291 def test_set_no_cycle():
292 g = Group('group')
293 d1 = D(42)
294 g.elements.append(d1)
295 d2 = D(67)
296 g.elements.append(d2)
297 d1.add(d2) # d1 points to d1, d2 does not point to anything, no cycles
298
299 assert d2 in d1.plain
300
301 gu = pickle_and_unpickle(g)
302 d1u = gu.elements[0]
303 d2u = gu.elements[1]
304
305 # check element directly
306 assert d2u in d1u.plain
307 # check element taken from elements
308 assert list(d1u.plain)[0] in d1u.plain
309
310
311 def test_set_self_cycle():
312 g = Group('group')
313 d1 = D(42)
314 g.elements.append(d1)
315 d2 = D(67)
316 g.elements.append(d2)
317 d1.add(d1) # cycle to itself
318 d1.add(d2) # d1 also points to d2, but d2 does not point to d1
319
320 assert d1 in d1.plain
321
322 gu = pickle_and_unpickle(g)
323 d1u = gu.elements[0]
324 d2u = gu.elements[1]
325 assert d2u is not None
326
327 # check element directly
328 assert d1u in d1u.plain
329 # check element taken from elements
330 assert list(d1u.plain)[0] in d1u.plain
331 # succeeds because d2u added to d1u after __setstate__
332 assert list(d1u.plain)[1] in d1u.plain
333
334
335 def test_set_mutual_cycle():
336 g = Group('group')
337 d1 = D(42)
338 g.elements.append(d1)
339 d2 = D(67)
340 g.elements.append(d2)
341 d1.add(d2) # points to d2, which points to d1, forming cycle
342 d2.add(d1) # points to d1 in order to form cycle
343
344 assert d2 in d1.plain
345 assert d1 in d2.plain
346
347 gu = pickle_and_unpickle(g)
348 d1u = gu.elements[0]
349 d2u = gu.elements[1]
350
351 # check element directly
352 # succeeds because d2u added to d1u after __setstate__
353 assert d2u in d1u.plain
354 assert d1u in d2u.plain
355 # check element taken from elements
356 # succeeds because d2u added to d1u after __setstate__
357 assert list(d1u.plain)[0] in d1u.plain
358 assert list(d2u.plain)[0] in d2u.plain