test_functional.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. from __future__ import absolute_import, unicode_literals
  2. import pytest
  3. from kombu.utils.functional import lazy
  4. from celery.five import range, nextfun
  5. from celery.utils.functional import (
  6. DummyContext,
  7. fun_takes_argument,
  8. head_from_fun,
  9. firstmethod,
  10. first,
  11. maybe_list,
  12. mlazy,
  13. padlist,
  14. regen,
  15. seq_concat_seq,
  16. seq_concat_item,
  17. )
  18. def test_DummyContext():
  19. with DummyContext():
  20. pass
  21. with pytest.raises(KeyError):
  22. with DummyContext():
  23. raise KeyError()
  24. @pytest.mark.parametrize('items,n,default,expected', [
  25. (['George', 'Costanza', 'NYC'], 3, None,
  26. ['George', 'Costanza', 'NYC']),
  27. (['George', 'Costanza'], 3, None,
  28. ['George', 'Costanza', None]),
  29. (['George', 'Costanza', 'NYC'], 4, 'Earth',
  30. ['George', 'Costanza', 'NYC', 'Earth']),
  31. ])
  32. def test_padlist(items, n, default, expected):
  33. assert padlist(items, n, default=default) == expected
  34. class test_firstmethod:
  35. def test_AttributeError(self):
  36. assert firstmethod('foo')([object()]) is None
  37. def test_handles_lazy(self):
  38. class A(object):
  39. def __init__(self, value=None):
  40. self.value = value
  41. def m(self):
  42. return self.value
  43. assert 'four' == firstmethod('m')([
  44. A(), A(), A(), A('four'), A('five')])
  45. assert 'four' == firstmethod('m')([
  46. A(), A(), A(), lazy(lambda: A('four')), A('five')])
  47. def test_first():
  48. iterations = [0]
  49. def predicate(value):
  50. iterations[0] += 1
  51. if value == 5:
  52. return True
  53. return False
  54. assert first(predicate, range(10)) == 5
  55. assert iterations[0] == 6
  56. iterations[0] = 0
  57. assert first(predicate, range(10, 20)) is None
  58. assert iterations[0] == 10
  59. def test_maybe_list():
  60. assert maybe_list(1) == [1]
  61. assert maybe_list([1]) == [1]
  62. assert maybe_list(None) is None
  63. def test_mlazy():
  64. it = iter(range(20, 30))
  65. p = mlazy(nextfun(it))
  66. assert p() == 20
  67. assert p.evaluated
  68. assert p() == 20
  69. assert repr(p) == '20'
  70. class test_regen:
  71. def test_list(self):
  72. l = [1, 2]
  73. r = regen(iter(l))
  74. assert regen(l) is l
  75. assert r == l
  76. assert r == l # again
  77. assert r.__length_hint__() == 0
  78. fun, args = r.__reduce__()
  79. assert fun(*args) == l
  80. def test_gen(self):
  81. g = regen(iter(list(range(10))))
  82. assert g[7] == 7
  83. assert g[6] == 6
  84. assert g[5] == 5
  85. assert g[4] == 4
  86. assert g[3] == 3
  87. assert g[2] == 2
  88. assert g[1] == 1
  89. assert g[0] == 0
  90. assert g.data, list(range(10))
  91. assert g[8] == 8
  92. assert g[0] == 0
  93. g = regen(iter(list(range(10))))
  94. assert g[0] == 0
  95. assert g[1] == 1
  96. assert g.data == list(range(10))
  97. g = regen(iter([1]))
  98. assert g[0] == 1
  99. with pytest.raises(IndexError):
  100. g[1]
  101. assert g.data == [1]
  102. g = regen(iter(list(range(10))))
  103. assert g[-1] == 9
  104. assert g[-2] == 8
  105. assert g[-3] == 7
  106. assert g[-4] == 6
  107. assert g[-5] == 5
  108. assert g[5] == 5
  109. assert g.data == list(range(10))
  110. assert list(iter(g)) == list(range(10))
  111. class test_head_from_fun:
  112. def test_from_cls(self):
  113. class X(object):
  114. def __call__(x, y, kwarg=1): # noqa
  115. pass
  116. g = head_from_fun(X())
  117. with pytest.raises(TypeError):
  118. g(1)
  119. g(1, 2)
  120. g(1, 2, kwarg=3)
  121. def test_from_fun(self):
  122. def f(x, y, kwarg=1):
  123. pass
  124. g = head_from_fun(f)
  125. with pytest.raises(TypeError):
  126. g(1)
  127. g(1, 2)
  128. g(1, 2, kwarg=3)
  129. def test_from_fun_with_hints(self):
  130. local = {}
  131. fun = ('def f_hints(x: int, y: int, kwarg: int=1):'
  132. ' pass')
  133. try:
  134. exec(fun, {}, local)
  135. except SyntaxError:
  136. # py2
  137. return
  138. f_hints = local['f_hints']
  139. g = head_from_fun(f_hints)
  140. with pytest.raises(TypeError):
  141. g(1)
  142. g(1, 2)
  143. g(1, 2, kwarg=3)
  144. class test_fun_takes_argument:
  145. def test_starkwargs(self):
  146. assert fun_takes_argument('foo', lambda **kw: 1)
  147. def test_named(self):
  148. assert fun_takes_argument('foo', lambda a, foo, bar: 1)
  149. def fun(a, b, c, d):
  150. return 1
  151. assert fun_takes_argument('foo', fun, position=4)
  152. def test_starargs(self):
  153. assert fun_takes_argument('foo', lambda a, *args: 1)
  154. def test_does_not(self):
  155. assert not fun_takes_argument('foo', lambda a, bar, baz: 1)
  156. assert not fun_takes_argument('foo', lambda: 1)
  157. def fun(a, b, foo):
  158. return 1
  159. assert not fun_takes_argument('foo', fun, position=4)
  160. @pytest.mark.parametrize('a,b,expected', [
  161. ((1, 2, 3), [4, 5], (1, 2, 3, 4, 5)),
  162. ((1, 2), [3, 4, 5], [1, 2, 3, 4, 5]),
  163. ([1, 2, 3], (4, 5), [1, 2, 3, 4, 5]),
  164. ([1, 2], (3, 4, 5), (1, 2, 3, 4, 5)),
  165. ])
  166. def test_seq_concat_seq(a, b, expected):
  167. res = seq_concat_seq(a, b)
  168. assert type(res) is type(expected) # noqa
  169. assert res == expected
  170. @pytest.mark.parametrize('a,b,expected', [
  171. ((1, 2, 3), 4, (1, 2, 3, 4)),
  172. ([1, 2, 3], 4, [1, 2, 3, 4]),
  173. ])
  174. def test_seq_concat_item(a, b, expected):
  175. res = seq_concat_item(a, b)
  176. assert type(res) is type(expected) # noqa
  177. assert res == expected