test_local.py 8.8 KB

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