test_functional.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. from __future__ import absolute_import, unicode_literals
  2. import pytest
  3. from kombu.utils.functional import lazy
  4. from case import skip
  5. from celery.five import range, nextfun
  6. from celery.utils.functional import (
  7. DummyContext,
  8. fun_accepts_kwargs,
  9. fun_takes_argument,
  10. head_from_fun,
  11. firstmethod,
  12. first,
  13. maybe_list,
  14. mlazy,
  15. padlist,
  16. regen,
  17. seq_concat_seq,
  18. seq_concat_item,
  19. )
  20. def test_DummyContext():
  21. with DummyContext():
  22. pass
  23. with pytest.raises(KeyError):
  24. with DummyContext():
  25. raise KeyError()
  26. @pytest.mark.parametrize('items,n,default,expected', [
  27. (['George', 'Costanza', 'NYC'], 3, None,
  28. ['George', 'Costanza', 'NYC']),
  29. (['George', 'Costanza'], 3, None,
  30. ['George', 'Costanza', None]),
  31. (['George', 'Costanza', 'NYC'], 4, 'Earth',
  32. ['George', 'Costanza', 'NYC', 'Earth']),
  33. ])
  34. def test_padlist(items, n, default, expected):
  35. assert padlist(items, n, default=default) == expected
  36. class test_firstmethod:
  37. def test_AttributeError(self):
  38. assert firstmethod('foo')([object()]) is None
  39. def test_handles_lazy(self):
  40. class A(object):
  41. def __init__(self, value=None):
  42. self.value = value
  43. def m(self):
  44. return self.value
  45. assert 'four' == firstmethod('m')([
  46. A(), A(), A(), A('four'), A('five')])
  47. assert 'four' == firstmethod('m')([
  48. A(), A(), A(), lazy(lambda: A('four')), A('five')])
  49. def test_first():
  50. iterations = [0]
  51. def predicate(value):
  52. iterations[0] += 1
  53. if value == 5:
  54. return True
  55. return False
  56. assert first(predicate, range(10)) == 5
  57. assert iterations[0] == 6
  58. iterations[0] = 0
  59. assert first(predicate, range(10, 20)) is None
  60. assert iterations[0] == 10
  61. def test_maybe_list():
  62. assert maybe_list(1) == [1]
  63. assert maybe_list([1]) == [1]
  64. assert maybe_list(None) is None
  65. def test_mlazy():
  66. it = iter(range(20, 30))
  67. p = mlazy(nextfun(it))
  68. assert p() == 20
  69. assert p.evaluated
  70. assert p() == 20
  71. assert repr(p) == '20'
  72. class test_regen:
  73. def test_list(self):
  74. l = [1, 2]
  75. r = regen(iter(l))
  76. assert regen(l) is l
  77. assert r == l
  78. assert r == l # again
  79. assert r.__length_hint__() == 0
  80. fun, args = r.__reduce__()
  81. assert fun(*args) == l
  82. def test_gen(self):
  83. g = regen(iter(list(range(10))))
  84. assert g[7] == 7
  85. assert g[6] == 6
  86. assert g[5] == 5
  87. assert g[4] == 4
  88. assert g[3] == 3
  89. assert g[2] == 2
  90. assert g[1] == 1
  91. assert g[0] == 0
  92. assert g.data, list(range(10))
  93. assert g[8] == 8
  94. assert g[0] == 0
  95. g = regen(iter(list(range(10))))
  96. assert g[0] == 0
  97. assert g[1] == 1
  98. assert g.data == list(range(10))
  99. g = regen(iter([1]))
  100. assert g[0] == 1
  101. with pytest.raises(IndexError):
  102. g[1]
  103. assert g.data == [1]
  104. g = regen(iter(list(range(10))))
  105. assert g[-1] == 9
  106. assert g[-2] == 8
  107. assert g[-3] == 7
  108. assert g[-4] == 6
  109. assert g[-5] == 5
  110. assert g[5] == 5
  111. assert g.data == list(range(10))
  112. assert list(iter(g)) == list(range(10))
  113. class test_head_from_fun:
  114. def test_from_cls(self):
  115. class X(object):
  116. def __call__(x, y, kwarg=1): # noqa
  117. pass
  118. g = head_from_fun(X())
  119. with pytest.raises(TypeError):
  120. g(1)
  121. g(1, 2)
  122. g(1, 2, kwarg=3)
  123. def test_from_fun(self):
  124. def f(x, y, kwarg=1):
  125. pass
  126. g = head_from_fun(f)
  127. with pytest.raises(TypeError):
  128. g(1)
  129. g(1, 2)
  130. g(1, 2, kwarg=3)
  131. @skip.unless_python3()
  132. def test_regression_3678(self):
  133. local = {}
  134. fun = ('def f(foo, *args, bar="", **kwargs):'
  135. ' return foo, args, bar')
  136. exec(fun, {}, local)
  137. g = head_from_fun(local['f'])
  138. g(1)
  139. g(1, 2, 3, 4, bar=100)
  140. with pytest.raises(TypeError):
  141. g(bar=100)
  142. @skip.unless_python3()
  143. def test_from_fun_with_hints(self):
  144. local = {}
  145. fun = ('def f_hints(x: int, y: int, kwarg: int=1):'
  146. ' pass')
  147. exec(fun, {}, local)
  148. f_hints = local['f_hints']
  149. g = head_from_fun(f_hints)
  150. with pytest.raises(TypeError):
  151. g(1)
  152. g(1, 2)
  153. g(1, 2, kwarg=3)
  154. @skip.unless_python3()
  155. def test_from_fun_forced_kwargs(self):
  156. local = {}
  157. fun = ('def f_kwargs(*, a, b="b", c=None):'
  158. ' return')
  159. exec(fun, {}, local)
  160. f_kwargs = local['f_kwargs']
  161. g = head_from_fun(f_kwargs)
  162. with pytest.raises(TypeError):
  163. g(1)
  164. g(a=1)
  165. g(a=1, b=2)
  166. g(a=1, b=2, c=3)
  167. def test_classmethod(self):
  168. class A(object):
  169. @classmethod
  170. def f(cls, x):
  171. return x
  172. fun = head_from_fun(A.f, bound=False)
  173. assert fun(A, 1) == 1
  174. fun = head_from_fun(A.f, bound=True)
  175. assert fun(1) == 1
  176. class test_fun_takes_argument:
  177. def test_starkwargs(self):
  178. assert fun_takes_argument('foo', lambda **kw: 1)
  179. def test_named(self):
  180. assert fun_takes_argument('foo', lambda a, foo, bar: 1)
  181. def fun(a, b, c, d):
  182. return 1
  183. assert fun_takes_argument('foo', fun, position=4)
  184. def test_starargs(self):
  185. assert fun_takes_argument('foo', lambda a, *args: 1)
  186. def test_does_not(self):
  187. assert not fun_takes_argument('foo', lambda a, bar, baz: 1)
  188. assert not fun_takes_argument('foo', lambda: 1)
  189. def fun(a, b, foo):
  190. return 1
  191. assert not fun_takes_argument('foo', fun, position=4)
  192. @pytest.mark.parametrize('a,b,expected', [
  193. ((1, 2, 3), [4, 5], (1, 2, 3, 4, 5)),
  194. ((1, 2), [3, 4, 5], [1, 2, 3, 4, 5]),
  195. ([1, 2, 3], (4, 5), [1, 2, 3, 4, 5]),
  196. ([1, 2], (3, 4, 5), (1, 2, 3, 4, 5)),
  197. ])
  198. def test_seq_concat_seq(a, b, expected):
  199. res = seq_concat_seq(a, b)
  200. assert type(res) is type(expected) # noqa
  201. assert res == expected
  202. @pytest.mark.parametrize('a,b,expected', [
  203. ((1, 2, 3), 4, (1, 2, 3, 4)),
  204. ([1, 2, 3], 4, [1, 2, 3, 4]),
  205. ])
  206. def test_seq_concat_item(a, b, expected):
  207. res = seq_concat_item(a, b)
  208. assert type(res) is type(expected) # noqa
  209. assert res == expected
  210. class StarKwargsCallable(object):
  211. def __call__(self, **kwargs):
  212. return 1
  213. class StarArgsStarKwargsCallable(object):
  214. def __call__(self, *args, **kwargs):
  215. return 1
  216. class StarArgsCallable(object):
  217. def __call__(self, *args):
  218. return 1
  219. class ArgsCallable(object):
  220. def __call__(self, a, b):
  221. return 1
  222. class ArgsStarKwargsCallable(object):
  223. def __call__(self, a, b, **kwargs):
  224. return 1
  225. class test_fun_accepts_kwargs:
  226. @pytest.mark.parametrize('fun', [
  227. lambda a, b, **kwargs: 1,
  228. lambda *args, **kwargs: 1,
  229. lambda foo=1, **kwargs: 1,
  230. StarKwargsCallable(),
  231. StarArgsStarKwargsCallable(),
  232. ArgsStarKwargsCallable(),
  233. ])
  234. def test_accepts(self, fun):
  235. assert fun_accepts_kwargs(fun)
  236. @pytest.mark.parametrize('fun', [
  237. lambda a: 1,
  238. lambda a, b: 1,
  239. lambda *args: 1,
  240. lambda a, kw1=1, kw2=2: 1,
  241. StarArgsCallable(),
  242. ArgsCallable(),
  243. ])
  244. def test_rejects(self, fun):
  245. assert not fun_accepts_kwargs(fun)