test_local.py 8.5 KB

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