test_local.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. from __future__ import absolute_import, unicode_literals
  2. import sys
  3. import pytest
  4. from case import Mock, skip
  5. from celery.five import PY3, long_t, python_2_unicode_compatible, string
  6. from celery.local import PromiseProxy, Proxy, maybe_evaluate, try_import
  7. class test_try_import:
  8. def test_imports(self):
  9. assert try_import(__name__)
  10. def test_when_default(self):
  11. default = object()
  12. assert try_import('foobar.awqewqe.asdwqewq', default) is default
  13. class test_Proxy:
  14. def test_std_class_attributes(self):
  15. assert Proxy.__name__ == 'Proxy'
  16. assert Proxy.__module__ == 'celery.local'
  17. assert isinstance(Proxy.__doc__, str)
  18. def test_doc(self):
  19. def real():
  20. pass
  21. x = Proxy(real, __doc__='foo')
  22. assert x.__doc__ == 'foo'
  23. def test_name(self):
  24. def real():
  25. """real function"""
  26. return 'REAL'
  27. x = Proxy(lambda: real, name='xyz')
  28. assert x.__name__ == 'xyz'
  29. y = Proxy(lambda: real)
  30. assert y.__name__ == 'real'
  31. assert x.__doc__ == 'real function'
  32. assert x.__class__ == type(real)
  33. assert x.__dict__ == real.__dict__
  34. assert repr(x) == repr(real)
  35. assert x.__module__
  36. def test_get_current_local(self):
  37. x = Proxy(lambda: 10)
  38. object.__setattr__(x, '_Proxy_local', Mock())
  39. assert x._get_current_object()
  40. def test_bool(self):
  41. class X(object):
  42. def __bool__(self):
  43. return False
  44. __nonzero__ = __bool__
  45. x = Proxy(lambda: X())
  46. assert not x
  47. def test_slots(self):
  48. class X(object):
  49. __slots__ = ()
  50. x = Proxy(X)
  51. with pytest.raises(AttributeError):
  52. x.__dict__
  53. @skip.if_python3()
  54. def test_unicode(self):
  55. @python_2_unicode_compatible
  56. class X(object):
  57. def __unicode__(self):
  58. return 'UNICODE'
  59. __str__ = __unicode__
  60. def __repr__(self):
  61. return 'REPR'
  62. x = Proxy(lambda: X())
  63. assert string(x) == 'UNICODE'
  64. del(X.__unicode__)
  65. del(X.__str__)
  66. assert string(x) == 'REPR'
  67. def test_dir(self):
  68. class X(object):
  69. def __dir__(self):
  70. return ['a', 'b', 'c']
  71. x = Proxy(lambda: X())
  72. assert dir(x) == ['a', 'b', 'c']
  73. class Y(object):
  74. def __dir__(self):
  75. raise RuntimeError()
  76. y = Proxy(lambda: Y())
  77. assert dir(y) == []
  78. def test_getsetdel_attr(self):
  79. class X(object):
  80. a = 1
  81. b = 2
  82. c = 3
  83. def __dir__(self):
  84. return ['a', 'b', 'c']
  85. v = X()
  86. x = Proxy(lambda: v)
  87. assert x.__members__ == ['a', 'b', 'c']
  88. assert x.a == 1
  89. assert x.b == 2
  90. assert x.c == 3
  91. setattr(x, 'a', 10)
  92. assert x.a == 10
  93. del(x.a)
  94. assert x.a == 1
  95. def test_dictproxy(self):
  96. v = {}
  97. x = Proxy(lambda: v)
  98. x['foo'] = 42
  99. assert x['foo'] == 42
  100. assert len(x) == 1
  101. assert 'foo' in x
  102. del(x['foo'])
  103. with pytest.raises(KeyError):
  104. x['foo']
  105. assert iter(x)
  106. def test_listproxy(self):
  107. v = []
  108. x = Proxy(lambda: v)
  109. x.append(1)
  110. x.extend([2, 3, 4])
  111. assert x[0] == 1
  112. assert x[:-1] == [1, 2, 3]
  113. del(x[-1])
  114. assert x[:-1] == [1, 2]
  115. x[0] = 10
  116. assert x[0] == 10
  117. assert 10 in x
  118. assert len(x) == 3
  119. assert iter(x)
  120. x[0:2] = [1, 2]
  121. del(x[0:2])
  122. assert str(x)
  123. if sys.version_info[0] < 3:
  124. assert x.__cmp__(object()) == -1
  125. def test_complex_cast(self):
  126. class O(object):
  127. def __complex__(self):
  128. return complex(10.333)
  129. o = Proxy(O)
  130. assert o.__complex__() == complex(10.333)
  131. def test_index(self):
  132. class O(object):
  133. def __index__(self):
  134. return 1
  135. o = Proxy(O)
  136. assert o.__index__() == 1
  137. def test_coerce(self):
  138. class O(object):
  139. def __coerce__(self, other):
  140. return self, other
  141. o = Proxy(O)
  142. assert o.__coerce__(3)
  143. def test_int(self):
  144. assert Proxy(lambda: 10) + 1 == Proxy(lambda: 11)
  145. assert Proxy(lambda: 10) - 1 == Proxy(lambda: 9)
  146. assert Proxy(lambda: 10) * 2 == Proxy(lambda: 20)
  147. assert Proxy(lambda: 10) ** 2 == Proxy(lambda: 100)
  148. assert Proxy(lambda: 20) / 2 == Proxy(lambda: 10)
  149. assert Proxy(lambda: 20) // 2 == Proxy(lambda: 10)
  150. assert Proxy(lambda: 11) % 2 == Proxy(lambda: 1)
  151. assert Proxy(lambda: 10) << 2 == Proxy(lambda: 40)
  152. assert Proxy(lambda: 10) >> 2 == Proxy(lambda: 2)
  153. assert Proxy(lambda: 10) ^ 7 == Proxy(lambda: 13)
  154. assert Proxy(lambda: 10) | 40 == Proxy(lambda: 42)
  155. assert Proxy(lambda: 10) != Proxy(lambda: -11)
  156. assert Proxy(lambda: 10) != Proxy(lambda: -10)
  157. assert Proxy(lambda: -10) == Proxy(lambda: -10)
  158. assert Proxy(lambda: 10) < Proxy(lambda: 20)
  159. assert Proxy(lambda: 20) > Proxy(lambda: 10)
  160. assert Proxy(lambda: 10) >= Proxy(lambda: 10)
  161. assert Proxy(lambda: 10) <= Proxy(lambda: 10)
  162. assert Proxy(lambda: 10) == Proxy(lambda: 10)
  163. assert Proxy(lambda: 20) != Proxy(lambda: 10)
  164. assert Proxy(lambda: 100).__divmod__(30)
  165. assert Proxy(lambda: 100).__truediv__(30)
  166. assert abs(Proxy(lambda: -100))
  167. x = Proxy(lambda: 10)
  168. x -= 1
  169. assert x == 9
  170. x = Proxy(lambda: 9)
  171. x += 1
  172. assert x == 10
  173. x = Proxy(lambda: 10)
  174. x *= 2
  175. assert x == 20
  176. x = Proxy(lambda: 20)
  177. x /= 2
  178. assert x == 10
  179. x = Proxy(lambda: 10)
  180. x %= 2
  181. assert x == 0
  182. x = Proxy(lambda: 10)
  183. x <<= 3
  184. assert x == 80
  185. x = Proxy(lambda: 80)
  186. x >>= 4
  187. assert x == 5
  188. x = Proxy(lambda: 5)
  189. x ^= 1
  190. assert x == 4
  191. x = Proxy(lambda: 4)
  192. x **= 4
  193. assert x == 256
  194. x = Proxy(lambda: 256)
  195. x //= 2
  196. assert x == 128
  197. x = Proxy(lambda: 128)
  198. x |= 2
  199. assert x == 130
  200. x = Proxy(lambda: 130)
  201. x &= 10
  202. assert x == 2
  203. x = Proxy(lambda: 10)
  204. assert type(x.__float__()) == float
  205. assert type(x.__int__()) == int
  206. if not PY3:
  207. assert type(x.__long__()) == long_t
  208. assert hex(x)
  209. assert oct(x)
  210. def test_hash(self):
  211. class X(object):
  212. def __hash__(self):
  213. return 1234
  214. assert hash(Proxy(lambda: X())) == 1234
  215. def test_call(self):
  216. class X(object):
  217. def __call__(self):
  218. return 1234
  219. assert Proxy(lambda: X())() == 1234
  220. def test_context(self):
  221. class X(object):
  222. entered = exited = False
  223. def __enter__(self):
  224. self.entered = True
  225. return 1234
  226. def __exit__(self, *exc_info):
  227. self.exited = True
  228. v = X()
  229. x = Proxy(lambda: v)
  230. with x as val:
  231. assert val == 1234
  232. assert x.entered
  233. assert x.exited
  234. def test_reduce(self):
  235. class X(object):
  236. def __reduce__(self):
  237. return 123
  238. x = Proxy(lambda: X())
  239. assert x.__reduce__() == 123
  240. class test_PromiseProxy:
  241. def test_only_evaluated_once(self):
  242. class X(object):
  243. attr = 123
  244. evals = 0
  245. def __init__(self):
  246. self.__class__.evals += 1
  247. p = PromiseProxy(X)
  248. assert p.attr == 123
  249. assert p.attr == 123
  250. assert X.evals == 1
  251. def test_callbacks(self):
  252. source = Mock(name='source')
  253. p = PromiseProxy(source)
  254. cbA = Mock(name='cbA')
  255. cbB = Mock(name='cbB')
  256. cbC = Mock(name='cbC')
  257. p.__then__(cbA, p)
  258. p.__then__(cbB, p)
  259. assert not p.__evaluated__()
  260. assert object.__getattribute__(p, '__pending__')
  261. assert repr(p)
  262. assert p.__evaluated__()
  263. with pytest.raises(AttributeError):
  264. object.__getattribute__(p, '__pending__')
  265. cbA.assert_called_with(p)
  266. cbB.assert_called_with(p)
  267. assert p.__evaluated__()
  268. p.__then__(cbC, p)
  269. cbC.assert_called_with(p)
  270. with pytest.raises(AttributeError):
  271. object.__getattribute__(p, '__pending__')
  272. def test_maybe_evaluate(self):
  273. x = PromiseProxy(lambda: 30)
  274. assert not x.__evaluated__()
  275. assert maybe_evaluate(x) == 30
  276. assert maybe_evaluate(x) == 30
  277. assert maybe_evaluate(30) == 30
  278. assert x.__evaluated__()