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