test_functional.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. def test_from_fun_with_hints(self):
  132. local = {}
  133. fun = ('def f_hints(x: int, y: int, kwarg: int=1):'
  134. ' pass')
  135. try:
  136. exec(fun, {}, local)
  137. except SyntaxError:
  138. # py2
  139. return
  140. f_hints = local['f_hints']
  141. g = head_from_fun(f_hints)
  142. with pytest.raises(TypeError):
  143. g(1)
  144. g(1, 2)
  145. g(1, 2, kwarg=3)
  146. @skip.unless_python3()
  147. def test_from_fun_forced_kwargs(self):
  148. local = {}
  149. fun = ('def f_kwargs(*, a, b="b", c=None):'
  150. ' return')
  151. try:
  152. exec(fun, {}, local)
  153. except SyntaxError:
  154. # Python 2.
  155. return
  156. f_kwargs = local['f_kwargs']
  157. g = head_from_fun(f_kwargs)
  158. with pytest.raises(TypeError):
  159. g(1)
  160. g(a=1)
  161. g(a=1, b=2)
  162. g(a=1, b=2, c=3)
  163. class test_fun_takes_argument:
  164. def test_starkwargs(self):
  165. assert fun_takes_argument('foo', lambda **kw: 1)
  166. def test_named(self):
  167. assert fun_takes_argument('foo', lambda a, foo, bar: 1)
  168. def fun(a, b, c, d):
  169. return 1
  170. assert fun_takes_argument('foo', fun, position=4)
  171. def test_starargs(self):
  172. assert fun_takes_argument('foo', lambda a, *args: 1)
  173. def test_does_not(self):
  174. assert not fun_takes_argument('foo', lambda a, bar, baz: 1)
  175. assert not fun_takes_argument('foo', lambda: 1)
  176. def fun(a, b, foo):
  177. return 1
  178. assert not fun_takes_argument('foo', fun, position=4)
  179. @pytest.mark.parametrize('a,b,expected', [
  180. ((1, 2, 3), [4, 5], (1, 2, 3, 4, 5)),
  181. ((1, 2), [3, 4, 5], [1, 2, 3, 4, 5]),
  182. ([1, 2, 3], (4, 5), [1, 2, 3, 4, 5]),
  183. ([1, 2], (3, 4, 5), (1, 2, 3, 4, 5)),
  184. ])
  185. def test_seq_concat_seq(a, b, expected):
  186. res = seq_concat_seq(a, b)
  187. assert type(res) is type(expected) # noqa
  188. assert res == expected
  189. @pytest.mark.parametrize('a,b,expected', [
  190. ((1, 2, 3), 4, (1, 2, 3, 4)),
  191. ([1, 2, 3], 4, [1, 2, 3, 4]),
  192. ])
  193. def test_seq_concat_item(a, b, expected):
  194. res = seq_concat_item(a, b)
  195. assert type(res) is type(expected) # noqa
  196. assert res == expected
  197. class StarKwargsCallable(object):
  198. def __call__(self, **kwargs):
  199. return 1
  200. class StarArgsStarKwargsCallable(object):
  201. def __call__(self, *args, **kwargs):
  202. return 1
  203. class StarArgsCallable(object):
  204. def __call__(self, *args):
  205. return 1
  206. class ArgsCallable(object):
  207. def __call__(self, a, b):
  208. return 1
  209. class ArgsStarKwargsCallable(object):
  210. def __call__(self, a, b, **kwargs):
  211. return 1
  212. class test_fun_accepts_kwargs:
  213. @pytest.mark.parametrize('fun', [
  214. lambda a, b, **kwargs: 1,
  215. lambda *args, **kwargs: 1,
  216. lambda foo=1, **kwargs: 1,
  217. StarKwargsCallable(),
  218. StarArgsStarKwargsCallable(),
  219. ArgsStarKwargsCallable(),
  220. ])
  221. def test_accepts(self, fun):
  222. assert fun_accepts_kwargs(fun)
  223. @pytest.mark.parametrize('fun', [
  224. lambda a: 1,
  225. lambda a, b: 1,
  226. lambda *args: 1,
  227. lambda a, kw1=1, kw2=2: 1,
  228. StarArgsCallable(),
  229. ArgsCallable(),
  230. ])
  231. def test_rejects(self, fun):
  232. assert not fun_accepts_kwargs(fun)