Debian Codegraph @

tests/utils_tests/test_lazyobject.py

raw
1 import copy
2 import pickle
3 import sys
4 import unittest
5 import warnings
6
7 from django.test import TestCase
8 from django.utils.functional import LazyObject, SimpleLazyObject, empty
9
10 from .models import Category, CategoryInfo
11
12
13 class Foo:
14 """
15 A simple class with just one attribute.
16 """
17
18 foo = "bar"
19
20 def __eq__(self, other):
21 return self.foo == other.foo
22
23
24 class LazyObjectTestCase(unittest.TestCase):
25 def lazy_wrap(self, wrapped_object):
26 """
27 Wrap the given object into a LazyObject
28 """
29
30 class AdHocLazyObject(LazyObject):
31 def _setup(self):
32 self._wrapped = wrapped_object
33
34 return AdHocLazyObject()
35
36 def test_getattribute(self):
37 """
38 Proxy methods don't exist on wrapped objects unless they're set.
39 """
40 attrs = [
41 "__getitem__",
42 "__setitem__",
43 "__delitem__",
44 "__iter__",
45 "__len__",
46 "__contains__",
47 ]
48 foo = Foo()
49 obj = self.lazy_wrap(foo)
50 for attr in attrs:
51 with self.subTest(attr):
52 self.assertFalse(hasattr(obj, attr))
53 setattr(foo, attr, attr)
54 obj_with_attr = self.lazy_wrap(foo)
55 self.assertTrue(hasattr(obj_with_attr, attr))
56 self.assertEqual(getattr(obj_with_attr, attr), attr)
57
58 def test_getattr(self):
59 obj = self.lazy_wrap(Foo())
60 self.assertEqual(obj.foo, "bar")
61
62 def test_getattr_falsey(self):
63 class Thing:
64 def __getattr__(self, key):
65 return []
66
67 obj = self.lazy_wrap(Thing())
68 self.assertEqual(obj.main, [])
69
70 def test_setattr(self):
71 obj = self.lazy_wrap(Foo())
72 obj.foo = "BAR"
73 obj.bar = "baz"
74 self.assertEqual(obj.foo, "BAR")
75 self.assertEqual(obj.bar, "baz")
76
77 def test_setattr2(self):
78 # Same as test_setattr but in reversed order
79 obj = self.lazy_wrap(Foo())
80 obj.bar = "baz"
81 obj.foo = "BAR"
82 self.assertEqual(obj.foo, "BAR")
83 self.assertEqual(obj.bar, "baz")
84
85 def test_delattr(self):
86 obj = self.lazy_wrap(Foo())
87 obj.bar = "baz"
88 self.assertEqual(obj.bar, "baz")
89 del obj.bar
90 with self.assertRaises(AttributeError):
91 obj.bar
92
93 def test_cmp(self):
94 obj1 = self.lazy_wrap("foo")
95 obj2 = self.lazy_wrap("bar")
96 obj3 = self.lazy_wrap("foo")
97 self.assertEqual(obj1, "foo")
98 self.assertEqual(obj1, obj3)
99 self.assertNotEqual(obj1, obj2)
100 self.assertNotEqual(obj1, "bar")
101
102 def test_lt(self):
103 obj1 = self.lazy_wrap(1)
104 obj2 = self.lazy_wrap(2)
105 self.assertLess(obj1, obj2)
106
107 def test_gt(self):
108 obj1 = self.lazy_wrap(1)
109 obj2 = self.lazy_wrap(2)
110 self.assertGreater(obj2, obj1)
111
112 def test_bytes(self):
113 obj = self.lazy_wrap(b"foo")
114 self.assertEqual(bytes(obj), b"foo")
115
116 def test_text(self):
117 obj = self.lazy_wrap("foo")
118 self.assertEqual(str(obj), "foo")
119
120 def test_bool(self):
121 # Refs #21840
122 for f in [False, 0, (), {}, [], None, set()]:
123 self.assertFalse(self.lazy_wrap(f))
124 for t in [True, 1, (1,), {1: 2}, [1], object(), {1}]:
125 self.assertTrue(t)
126
127 def test_dir(self):
128 obj = self.lazy_wrap("foo")
129 self.assertEqual(dir(obj), dir("foo"))
130
131 def test_len(self):
132 for seq in ["asd", [1, 2, 3], {"a": 1, "b": 2, "c": 3}]:
133 obj = self.lazy_wrap(seq)
134 self.assertEqual(len(obj), 3)
135
136 def test_class(self):
137 self.assertIsInstance(self.lazy_wrap(42), int)
138
139 class Bar(Foo):
140 pass
141
142 self.assertIsInstance(self.lazy_wrap(Bar()), Foo)
143
144 def test_hash(self):
145 obj = self.lazy_wrap("foo")
146 d = {obj: "bar"}
147 self.assertIn("foo", d)
148 self.assertEqual(d["foo"], "bar")
149
150 def test_contains(self):
151 test_data = [
152 ("c", "abcde"),
153 (2, [1, 2, 3]),
154 ("a", {"a": 1, "b": 2, "c": 3}),
155 (2, {1, 2, 3}),
156 ]
157 for needle, haystack in test_data:
158 self.assertIn(needle, self.lazy_wrap(haystack))
159
160 # __contains__ doesn't work when the haystack is a string and the
161 # needle a LazyObject.
162 for needle_haystack in test_data[1:]:
163 self.assertIn(self.lazy_wrap(needle), haystack)
164 self.assertIn(self.lazy_wrap(needle), self.lazy_wrap(haystack))
165
166 def test_getitem(self):
167 obj_list = self.lazy_wrap([1, 2, 3])
168 obj_dict = self.lazy_wrap({"a": 1, "b": 2, "c": 3})
169
170 self.assertEqual(obj_list[0], 1)
171 self.assertEqual(obj_list[-1], 3)
172 self.assertEqual(obj_list[1:2], [2])
173
174 self.assertEqual(obj_dict["b"], 2)
175
176 with self.assertRaises(IndexError):
177 obj_list[3]
178
179 with self.assertRaises(KeyError):
180 obj_dict["f"]
181
182 def test_setitem(self):
183 obj_list = self.lazy_wrap([1, 2, 3])
184 obj_dict = self.lazy_wrap({"a": 1, "b": 2, "c": 3})
185
186 obj_list[0] = 100
187 self.assertEqual(obj_list, [100, 2, 3])
188 obj_list[1:2] = [200, 300, 400]
189 self.assertEqual(obj_list, [100, 200, 300, 400, 3])
190
191 obj_dict["a"] = 100
192 obj_dict["d"] = 400
193 self.assertEqual(obj_dict, {"a": 100, "b": 2, "c": 3, "d": 400})
194
195 def test_delitem(self):
196 obj_list = self.lazy_wrap([1, 2, 3])
197 obj_dict = self.lazy_wrap({"a": 1, "b": 2, "c": 3})
198
199 del obj_list[-1]
200 del obj_dict["c"]
201 self.assertEqual(obj_list, [1, 2])
202 self.assertEqual(obj_dict, {"a": 1, "b": 2})
203
204 with self.assertRaises(IndexError):
205 del obj_list[3]
206
207 with self.assertRaises(KeyError):
208 del obj_dict["f"]
209
210 def test_iter(self):
211 # Tests whether an object's custom `__iter__` method is being
212 # used when iterating over it.
213
214 class IterObject:
215 def __init__(self, values):
216 self.values = values
217
218 def __iter__(self):
219 return iter(self.values)
220
221 original_list = ["test", "123"]
222 self.assertEqual(list(self.lazy_wrap(IterObject(original_list))), original_list)
223
224 def test_pickle(self):
225 # See ticket #16563
226 obj = self.lazy_wrap(Foo())
227 obj.bar = "baz"
228 pickled = pickle.dumps(obj)
229 unpickled = pickle.loads(pickled)
230 self.assertIsInstance(unpickled, Foo)
231 self.assertEqual(unpickled, obj)
232 self.assertEqual(unpickled.foo, obj.foo)
233 self.assertEqual(unpickled.bar, obj.bar)
234
235 # Test copying lazy objects wrapping both builtin types and user-defined
236 # classes since a lot of the relevant code does __dict__ manipulation and
237 # builtin types don't have __dict__.
238
239 def test_copy_list(self):
240 # Copying a list works and returns the correct objects.
241 lst = [1, 2, 3]
242
243 obj = self.lazy_wrap(lst)
244 len(lst) # forces evaluation
245 obj2 = copy.copy(obj)
246
247 self.assertIsNot(obj, obj2)
248 self.assertIsInstance(obj2, list)
249 self.assertEqual(obj2, [1, 2, 3])
250
251 def test_copy_list_no_evaluation(self):
252 # Copying a list doesn't force evaluation.
253 lst = [1, 2, 3]
254
255 obj = self.lazy_wrap(lst)
256 obj2 = copy.copy(obj)
257
258 self.assertIsNot(obj, obj2)
259 self.assertIs(obj._wrapped, empty)
260 self.assertIs(obj2._wrapped, empty)
261
262 def test_copy_class(self):
263 # Copying a class works and returns the correct objects.
264 foo = Foo()
265
266 obj = self.lazy_wrap(foo)
267 str(foo) # forces evaluation
268 obj2 = copy.copy(obj)
269
270 self.assertIsNot(obj, obj2)
271 self.assertIsInstance(obj2, Foo)
272 self.assertEqual(obj2, Foo())
273
274 def test_copy_class_no_evaluation(self):
275 # Copying a class doesn't force evaluation.
276 foo = Foo()
277
278 obj = self.lazy_wrap(foo)
279 obj2 = copy.copy(obj)
280
281 self.assertIsNot(obj, obj2)
282 self.assertIs(obj._wrapped, empty)
283 self.assertIs(obj2._wrapped, empty)
284
285 def test_deepcopy_list(self):
286 # Deep copying a list works and returns the correct objects.
287 lst = [1, 2, 3]
288
289 obj = self.lazy_wrap(lst)
290 len(lst) # forces evaluation
291 obj2 = copy.deepcopy(obj)
292
293 self.assertIsNot(obj, obj2)
294 self.assertIsInstance(obj2, list)
295 self.assertEqual(obj2, [1, 2, 3])
296
297 def test_deepcopy_list_no_evaluation(self):
298 # Deep copying doesn't force evaluation.
299 lst = [1, 2, 3]
300
301 obj = self.lazy_wrap(lst)
302 obj2 = copy.deepcopy(obj)
303
304 self.assertIsNot(obj, obj2)
305 self.assertIs(obj._wrapped, empty)
306 self.assertIs(obj2._wrapped, empty)
307
308 def test_deepcopy_class(self):
309 # Deep copying a class works and returns the correct objects.
310 foo = Foo()
311
312 obj = self.lazy_wrap(foo)
313 str(foo) # forces evaluation
314 obj2 = copy.deepcopy(obj)
315
316 self.assertIsNot(obj, obj2)
317 self.assertIsInstance(obj2, Foo)
318 self.assertEqual(obj2, Foo())
319
320 def test_deepcopy_class_no_evaluation(self):
321 # Deep copying doesn't force evaluation.
322 foo = Foo()
323
324 obj = self.lazy_wrap(foo)
325 obj2 = copy.deepcopy(obj)
326
327 self.assertIsNot(obj, obj2)
328 self.assertIs(obj._wrapped, empty)
329 self.assertIs(obj2._wrapped, empty)
330
331
332 class SimpleLazyObjectTestCase(LazyObjectTestCase):
333 # By inheriting from LazyObjectTestCase and redefining the lazy_wrap()
334 # method which all testcases use, we get to make sure all behaviors
335 # tested in the parent testcase also apply to SimpleLazyObject.
336 def lazy_wrap(self, wrapped_object):
337 return SimpleLazyObject(lambda: wrapped_object)
338
339 def test_repr(self):
340 # First, for an unevaluated SimpleLazyObject
341 obj = self.lazy_wrap(42)
342 # __repr__ contains __repr__ of setup function and does not evaluate
343 # the SimpleLazyObject
344 self.assertRegex(repr(obj), "^<SimpleLazyObject:")
345 self.assertIs(obj._wrapped, empty) # make sure evaluation hasn't been triggered
346
347 self.assertEqual(obj, 42) # evaluate the lazy object
348 self.assertIsInstance(obj._wrapped, int)
349 self.assertEqual(repr(obj), "<SimpleLazyObject: 42>")
350
351 def test_add(self):
352 obj1 = self.lazy_wrap(1)
353 self.assertEqual(obj1 + 1, 2)
354 obj2 = self.lazy_wrap(2)
355 self.assertEqual(obj2 + obj1, 3)
356 self.assertEqual(obj1 + obj2, 3)
357
358 def test_radd(self):
359 obj1 = self.lazy_wrap(1)
360 self.assertEqual(1 + obj1, 2)
361
362 def test_trace(self):
363 # See ticket #19456
364 old_trace_func = sys.gettrace()
365 try:
366
367 def trace_func(frame, event, arg):
368 frame.f_locals["self"].__class__
369 if old_trace_func is not None:
370 old_trace_func(frame, event, arg)
371
372 sys.settrace(trace_func)
373 self.lazy_wrap(None)
374 finally:
375 sys.settrace(old_trace_func)
376
377 def test_none(self):
378 i = [0]
379
380 def f():
381 i[0] += 1
382 return None
383
384 x = SimpleLazyObject(f)
385 self.assertEqual(str(x), "None")
386 self.assertEqual(i, [1])
387 self.assertEqual(str(x), "None")
388 self.assertEqual(i, [1])
389
390 def test_dict(self):
391 # See ticket #18447
392 lazydict = SimpleLazyObject(lambda: {"one": 1})
393 self.assertEqual(lazydict["one"], 1)
394 lazydict["one"] = -1
395 self.assertEqual(lazydict["one"], -1)
396 self.assertIn("one", lazydict)
397 self.assertNotIn("two", lazydict)
398 self.assertEqual(len(lazydict), 1)
399 del lazydict["one"]
400 with self.assertRaises(KeyError):
401 lazydict["one"]
402
403 def test_list_set(self):
404 lazy_list = SimpleLazyObject(lambda: [1, 2, 3, 4, 5])
405 lazy_set = SimpleLazyObject(lambda: {1, 2, 3, 4})
406 self.assertIn(1, lazy_list)
407 self.assertIn(1, lazy_set)
408 self.assertNotIn(6, lazy_list)
409 self.assertNotIn(6, lazy_set)
410 self.assertEqual(len(lazy_list), 5)
411 self.assertEqual(len(lazy_set), 4)
412
413
414 class BaseBaz:
415 """
416 A base class with a funky __reduce__ method, meant to simulate the
417 __reduce__ method of Model, which sets self._django_version.
418 """
419
420 def __init__(self):
421 self.baz = "wrong"
422
423 def __reduce__(self):
424 self.baz = "right"
425 return super().__reduce__()
426
427 def __eq__(self, other):
428 if self.__class__ != other.__class__:
429 return False
430 for attr in ["bar", "baz", "quux"]:
431 if hasattr(self, attr) != hasattr(other, attr):
432 return False
433 elif getattr(self, attr, None) != getattr(other, attr, None):
434 return False
435 return True
436
437
438 class Baz(BaseBaz):
439 """
440 A class that inherits from BaseBaz and has its own __reduce_ex__ method.
441 """
442
443 def __init__(self, bar):
444 self.bar = bar
445 super().__init__()
446
447 def __reduce_ex__(self, proto):
448 self.quux = "quux"
449 return super().__reduce_ex__(proto)
450
451
452 class BazProxy(Baz):
453 """
454 A class that acts as a proxy for Baz. It does some scary mucking about with
455 dicts, which simulates some crazy things that people might do with
456 e.g. proxy models.
457 """
458
459 def __init__(self, baz):
460 self.__dict__ = baz.__dict__
461 self._baz = baz
462 # Grandparent super
463 super(BaseBaz, self).__init__()
464
465
466 class SimpleLazyObjectPickleTestCase(TestCase):
467 """
468 Regression test for pickling a SimpleLazyObject wrapping a model (#25389).
469 Also covers other classes with a custom __reduce__ method.
470 """
471
472 def test_pickle_with_reduce(self):
473 """
474 Test in a fairly synthetic setting.
475 """
476 # Test every pickle protocol available
477 for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
478 lazy_objs = [
479 SimpleLazyObject(lambda: BaseBaz()),
480 SimpleLazyObject(lambda: Baz(1)),
481 SimpleLazyObject(lambda: BazProxy(Baz(2))),
482 ]
483 for obj in lazy_objs:
484 pickled = pickle.dumps(obj, protocol)
485 unpickled = pickle.loads(pickled)
486 self.assertEqual(unpickled, obj)
487 self.assertEqual(unpickled.baz, "right")
488
489 def test_pickle_model(self):
490 """
491 Test on an actual model, based on the report in #25426.
492 """
493 category = Category.objects.create(name="thing1")
494 CategoryInfo.objects.create(category=category)
495 # Test every pickle protocol available
496 for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
497 lazy_category = SimpleLazyObject(lambda: category)
498 # Test both if we accessed a field on the model and if we didn't.
499 lazy_category.categoryinfo
500 lazy_category_2 = SimpleLazyObject(lambda: category)
501 with warnings.catch_warnings(record=True) as recorded:
502 self.assertEqual(
503 pickle.loads(pickle.dumps(lazy_category, protocol)), category
504 )
505 self.assertEqual(
506 pickle.loads(pickle.dumps(lazy_category_2, protocol)), category
507 )
508 # Assert that there were no warnings.
509 self.assertEqual(len(recorded), 0)