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
|
import sys
import unittest
import wrapt
class TestLazyObjectProxy(unittest.TestCase):
def test_lazy_object_proxy(self):
status = {"created": 0}
def factory():
status["created"] += 1
return 42
proxy = wrapt.LazyObjectProxy(factory)
self.assertEqual(status["created"], 0)
self.assertEqual(proxy.__wrapped__, 42)
self.assertEqual(int(proxy), 42)
self.assertEqual(status["created"], 1)
self.assertEqual(int(proxy), 42)
self.assertEqual(status["created"], 1)
def test_lazy_import(self):
if "sched" in sys.modules:
del sys.modules["sched"]
sched_mod = wrapt.lazy_import("sched")
self.assertFalse("sched" in sys.modules)
self.assertIsNotNone(sched_mod.scheduler)
self.assertTrue("sched" in sys.modules)
def test_lazy_import_attribute(self):
if "sched" in sys.modules:
del sys.modules["sched"]
scheduler = wrapt.lazy_import("sched", "scheduler")
self.assertFalse("sched" in sys.modules)
import sched
self.assertIsNotNone(scheduler.__wrapped__)
self.assertIs(scheduler.__wrapped__, sched.scheduler)
self.assertTrue("sched" in sys.modules)
def test_lazy_import_dotted(self):
if "http.client" in sys.modules:
del sys.modules["http.client"]
client_mod = wrapt.lazy_import("http.client")
self.assertFalse("http.client" in sys.modules)
self.assertIsNotNone(client_mod.HTTPConnection)
self.assertTrue("http.client" in sys.modules)
def test_lazy_import_callable(self):
dumps = wrapt.lazy_import("json", "dumps")
self.assertTrue(callable(dumps))
result = dumps({"key": "value"})
self.assertEqual(result, '{"key": "value"}')
def test_lazy_import_iterable(self):
from collections.abc import Iterable
if "string" in sys.modules:
del sys.modules["string"]
digits0 = wrapt.lazy_import("string", "digits")
self.assertFalse(hasattr(type(digits0), "__iter__"))
self.assertTrue(callable(digits0))
# XXX Iteration does not seem to test for __iter__ being a method
# on the type, so this test is not sufficient to raise the TypeError.
# I was at some point seeing expected failures here, but not now and
# I don't know why.
# if "string" in sys.modules:
# del sys.modules["string"]
# digits1 = wrapt.lazy_import("string", "digits")
# with self.assertRaises(TypeError):
# for char in digits1:
# pass
if "string" in sys.modules:
del sys.modules["string"]
digits2 = wrapt.lazy_import("string", "digits", interface=Iterable)
self.assertTrue(hasattr(type(digits2), "__iter__"))
self.assertFalse(callable(digits2))
for char in digits2:
self.assertIn(char, "0123456789")
self.assertEqual("".join(digits2), "0123456789")
|