test_local.py 8.8 KB

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