summaryrefslogtreecommitdiff
path: root/tests/utils_tests/test_lazyobject.py
diff options
context:
space:
mode:
authordjango-bot <ops@djangoproject.com>2022-02-03 20:24:19 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-02-07 20:37:05 +0100
commit9c19aff7c7561e3a82978a272ecdaad40dda5c00 (patch)
treef0506b668a013d0063e5fba3dbf4863b466713ba /tests/utils_tests/test_lazyobject.py
parentf68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff)
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'tests/utils_tests/test_lazyobject.py')
-rw-r--r--tests/utils_tests/test_lazyobject.py140
1 files changed, 74 insertions, 66 deletions
diff --git a/tests/utils_tests/test_lazyobject.py b/tests/utils_tests/test_lazyobject.py
index e5bccc6362..131d893a44 100644
--- a/tests/utils_tests/test_lazyobject.py
+++ b/tests/utils_tests/test_lazyobject.py
@@ -13,7 +13,8 @@ class Foo:
"""
A simple class with just one attribute.
"""
- foo = 'bar'
+
+ foo = "bar"
def __eq__(self, other):
return self.foo == other.foo
@@ -24,6 +25,7 @@ class LazyObjectTestCase(TestCase):
"""
Wrap the given object into a LazyObject
"""
+
class AdHocLazyObject(LazyObject):
def _setup(self):
self._wrapped = wrapped_object
@@ -32,39 +34,39 @@ class LazyObjectTestCase(TestCase):
def test_getattr(self):
obj = self.lazy_wrap(Foo())
- self.assertEqual(obj.foo, 'bar')
+ self.assertEqual(obj.foo, "bar")
def test_setattr(self):
obj = self.lazy_wrap(Foo())
- obj.foo = 'BAR'
- obj.bar = 'baz'
- self.assertEqual(obj.foo, 'BAR')
- self.assertEqual(obj.bar, 'baz')
+ obj.foo = "BAR"
+ obj.bar = "baz"
+ self.assertEqual(obj.foo, "BAR")
+ self.assertEqual(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'
- self.assertEqual(obj.foo, 'BAR')
- self.assertEqual(obj.bar, 'baz')
+ obj.bar = "baz"
+ obj.foo = "BAR"
+ self.assertEqual(obj.foo, "BAR")
+ self.assertEqual(obj.bar, "baz")
def test_delattr(self):
obj = self.lazy_wrap(Foo())
- obj.bar = 'baz'
- self.assertEqual(obj.bar, 'baz')
+ obj.bar = "baz"
+ self.assertEqual(obj.bar, "baz")
del obj.bar
with self.assertRaises(AttributeError):
obj.bar
def test_cmp(self):
- obj1 = self.lazy_wrap('foo')
- obj2 = self.lazy_wrap('bar')
- obj3 = self.lazy_wrap('foo')
- self.assertEqual(obj1, 'foo')
+ obj1 = self.lazy_wrap("foo")
+ obj2 = self.lazy_wrap("bar")
+ obj3 = self.lazy_wrap("foo")
+ self.assertEqual(obj1, "foo")
self.assertEqual(obj1, obj3)
self.assertNotEqual(obj1, obj2)
- self.assertNotEqual(obj1, 'bar')
+ self.assertNotEqual(obj1, "bar")
def test_lt(self):
obj1 = self.lazy_wrap(1)
@@ -77,12 +79,12 @@ class LazyObjectTestCase(TestCase):
self.assertGreater(obj2, obj1)
def test_bytes(self):
- obj = self.lazy_wrap(b'foo')
- self.assertEqual(bytes(obj), b'foo')
+ obj = self.lazy_wrap(b"foo")
+ self.assertEqual(bytes(obj), b"foo")
def test_text(self):
- obj = self.lazy_wrap('foo')
- self.assertEqual(str(obj), 'foo')
+ obj = self.lazy_wrap("foo")
+ self.assertEqual(str(obj), "foo")
def test_bool(self):
# Refs #21840
@@ -92,11 +94,11 @@ class LazyObjectTestCase(TestCase):
self.assertTrue(t)
def test_dir(self):
- obj = self.lazy_wrap('foo')
- self.assertEqual(dir(obj), dir('foo'))
+ obj = self.lazy_wrap("foo")
+ self.assertEqual(dir(obj), dir("foo"))
def test_len(self):
- for seq in ['asd', [1, 2, 3], {'a': 1, 'b': 2, 'c': 3}]:
+ for seq in ["asd", [1, 2, 3], {"a": 1, "b": 2, "c": 3}]:
obj = self.lazy_wrap(seq)
self.assertEqual(len(obj), 3)
@@ -109,16 +111,16 @@ class LazyObjectTestCase(TestCase):
self.assertIsInstance(self.lazy_wrap(Bar()), Foo)
def test_hash(self):
- obj = self.lazy_wrap('foo')
- d = {obj: 'bar'}
- self.assertIn('foo', d)
- self.assertEqual(d['foo'], 'bar')
+ obj = self.lazy_wrap("foo")
+ d = {obj: "bar"}
+ self.assertIn("foo", d)
+ self.assertEqual(d["foo"], "bar")
def test_contains(self):
test_data = [
- ('c', 'abcde'),
+ ("c", "abcde"),
(2, [1, 2, 3]),
- ('a', {'a': 1, 'b': 2, 'c': 3}),
+ ("a", {"a": 1, "b": 2, "c": 3}),
(2, {1, 2, 3}),
]
for needle, haystack in test_data:
@@ -131,70 +133,66 @@ class LazyObjectTestCase(TestCase):
def test_getitem(self):
obj_list = self.lazy_wrap([1, 2, 3])
- obj_dict = self.lazy_wrap({'a': 1, 'b': 2, 'c': 3})
+ obj_dict = self.lazy_wrap({"a": 1, "b": 2, "c": 3})
self.assertEqual(obj_list[0], 1)
self.assertEqual(obj_list[-1], 3)
self.assertEqual(obj_list[1:2], [2])
- self.assertEqual(obj_dict['b'], 2)
+ self.assertEqual(obj_dict["b"], 2)
with self.assertRaises(IndexError):
obj_list[3]
with self.assertRaises(KeyError):
- obj_dict['f']
+ 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_dict = self.lazy_wrap({"a": 1, "b": 2, "c": 3})
obj_list[0] = 100
self.assertEqual(obj_list, [100, 2, 3])
obj_list[1:2] = [200, 300, 400]
self.assertEqual(obj_list, [100, 200, 300, 400, 3])
- obj_dict['a'] = 100
- obj_dict['d'] = 400
- self.assertEqual(obj_dict, {'a': 100, 'b': 2, 'c': 3, 'd': 400})
+ obj_dict["a"] = 100
+ obj_dict["d"] = 400
+ self.assertEqual(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})
+ obj_dict = self.lazy_wrap({"a": 1, "b": 2, "c": 3})
del obj_list[-1]
- del obj_dict['c']
+ del obj_dict["c"]
self.assertEqual(obj_list, [1, 2])
- self.assertEqual(obj_dict, {'a': 1, 'b': 2})
+ self.assertEqual(obj_dict, {"a": 1, "b": 2})
with self.assertRaises(IndexError):
del obj_list[3]
with self.assertRaises(KeyError):
- del obj_dict['f']
+ 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']
- self.assertEqual(
- list(self.lazy_wrap(IterObject(original_list))),
- original_list
- )
+ original_list = ["test", "123"]
+ self.assertEqual(list(self.lazy_wrap(IterObject(original_list))), original_list)
def test_pickle(self):
# See ticket #16563
obj = self.lazy_wrap(Foo())
- obj.bar = 'baz'
+ obj.bar = "baz"
pickled = pickle.dumps(obj)
unpickled = pickle.loads(pickled)
self.assertIsInstance(unpickled, Foo)
@@ -311,21 +309,23 @@ class SimpleLazyObjectTestCase(LazyObjectTestCase):
obj = self.lazy_wrap(42)
# __repr__ contains __repr__ of setup function and does not evaluate
# the SimpleLazyObject
- self.assertRegex(repr(obj), '^<SimpleLazyObject:')
+ self.assertRegex(repr(obj), "^<SimpleLazyObject:")
self.assertIs(obj._wrapped, empty) # make sure evaluation hasn't been triggered
self.assertEqual(obj, 42) # evaluate the lazy object
self.assertIsInstance(obj._wrapped, int)
- self.assertEqual(repr(obj), '<SimpleLazyObject: 42>')
+ self.assertEqual(repr(obj), "<SimpleLazyObject: 42>")
def test_trace(self):
# See ticket #19456
old_trace_func = sys.gettrace()
try:
+
def trace_func(frame, event, arg):
- frame.f_locals['self'].__class__
+ 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:
@@ -346,16 +346,16 @@ class SimpleLazyObjectTestCase(LazyObjectTestCase):
def test_dict(self):
# See ticket #18447
- lazydict = SimpleLazyObject(lambda: {'one': 1})
- self.assertEqual(lazydict['one'], 1)
- lazydict['one'] = -1
- self.assertEqual(lazydict['one'], -1)
- self.assertIn('one', lazydict)
- self.assertNotIn('two', lazydict)
+ lazydict = SimpleLazyObject(lambda: {"one": 1})
+ self.assertEqual(lazydict["one"], 1)
+ lazydict["one"] = -1
+ self.assertEqual(lazydict["one"], -1)
+ self.assertIn("one", lazydict)
+ self.assertNotIn("two", lazydict)
self.assertEqual(len(lazydict), 1)
- del lazydict['one']
+ del lazydict["one"]
with self.assertRaises(KeyError):
- lazydict['one']
+ lazydict["one"]
def test_list_set(self):
lazy_list = SimpleLazyObject(lambda: [1, 2, 3, 4, 5])
@@ -373,17 +373,18 @@ 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'
+ self.baz = "wrong"
def __reduce__(self):
- self.baz = 'right'
+ self.baz = "right"
return super().__reduce__()
def __eq__(self, other):
if self.__class__ != other.__class__:
return False
- for attr in ['bar', 'baz', 'quux']:
+ for attr in ["bar", "baz", "quux"]:
if hasattr(self, attr) != hasattr(other, attr):
return False
elif getattr(self, attr, None) != getattr(other, attr, None):
@@ -395,12 +396,13 @@ 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'
+ self.quux = "quux"
return super().__reduce_ex__(proto)
@@ -410,6 +412,7 @@ class BazProxy(Baz):
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
@@ -422,6 +425,7 @@ class SimpleLazyObjectPickleTestCase(TestCase):
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.
@@ -437,7 +441,7 @@ class SimpleLazyObjectPickleTestCase(TestCase):
pickled = pickle.dumps(obj, protocol)
unpickled = pickle.loads(pickled)
self.assertEqual(unpickled, obj)
- self.assertEqual(unpickled.baz, 'right')
+ self.assertEqual(unpickled.baz, "right")
def test_pickle_model(self):
"""
@@ -452,7 +456,11 @@ class SimpleLazyObjectPickleTestCase(TestCase):
lazy_category.categoryinfo
lazy_category_2 = SimpleLazyObject(lambda: category)
with warnings.catch_warnings(record=True) as recorded:
- self.assertEqual(pickle.loads(pickle.dumps(lazy_category, protocol)), category)
- self.assertEqual(pickle.loads(pickle.dumps(lazy_category_2, protocol)), category)
+ self.assertEqual(
+ pickle.loads(pickle.dumps(lazy_category, protocol)), category
+ )
+ self.assertEqual(
+ pickle.loads(pickle.dumps(lazy_category_2, protocol)), category
+ )
# Assert that there were no warnings.
self.assertEqual(len(recorded), 0)