test_functional.py 4.7 KB

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