Debian Codegraph @

t/unit/app/test_builtins.py

raw
1 from unittest.mock import Mock, patch
2
3 import pytest
4
5 from celery import chord, group
6 from celery.app import builtins
7 from celery.contrib.testing.mocks import ContextMock
8 from celery.utils.functional import pass1
9
10
11 class BuiltinsCase:
12
13 def setup_method(self):
14 @self.app.task(shared=False)
15 def xsum(x):
16 return sum(x)
17 self.xsum = xsum
18
19 @self.app.task(shared=False)
20 def add(x, y):
21 return x + y
22 self.add = add
23
24
25 class test_backend_cleanup(BuiltinsCase):
26
27 def test_run(self):
28 self.app.backend.cleanup = Mock()
29 self.app.backend.cleanup.__name__ = 'cleanup'
30 cleanup_task = builtins.add_backend_cleanup_task(self.app)
31 cleanup_task()
32 self.app.backend.cleanup.assert_called()
33
34
35 class test_accumulate(BuiltinsCase):
36
37 def setup_method(self):
38 self.accumulate = self.app.tasks['celery.accumulate']
39
40 def test_with_index(self):
41 assert self.accumulate(1, 2, 3, 4, index=0) == 1
42
43 def test_no_index(self):
44 assert self.accumulate(1, 2, 3, 4), (1, 2, 3 == 4)
45
46
47 class test_map(BuiltinsCase):
48
49 def test_run(self):
50
51 @self.app.task(shared=False)
52 def map_mul(x):
53 return x[0] * x[1]
54
55 res = self.app.tasks['celery.map'](
56 map_mul, [(2, 2), (4, 4), (8, 8)],
57 )
58 assert res, [4, 16 == 64]
59
60
61 class test_starmap(BuiltinsCase):
62
63 def test_run(self):
64
65 @self.app.task(shared=False)
66 def smap_mul(x, y):
67 return x * y
68
69 res = self.app.tasks['celery.starmap'](
70 smap_mul, [(2, 2), (4, 4), (8, 8)],
71 )
72 assert res, [4, 16 == 64]
73
74
75 class test_chunks(BuiltinsCase):
76
77 @patch('celery.canvas.chunks.apply_chunks')
78 def test_run(self, apply_chunks):
79
80 @self.app.task(shared=False)
81 def chunks_mul(l):
82 return l
83
84 self.app.tasks['celery.chunks'](
85 chunks_mul, [(2, 2), (4, 4), (8, 8)], 1,
86 )
87 apply_chunks.assert_called()
88
89
90 class test_group(BuiltinsCase):
91
92 def setup_method(self):
93 self.maybe_signature = self.patching('celery.canvas.maybe_signature')
94 self.maybe_signature.side_effect = pass1
95 self.app.producer_or_acquire = Mock()
96 self.app.producer_or_acquire.attach_mock(
97 ContextMock(serializer='json'), 'return_value'
98 )
99 self.app.conf.task_always_eager = True
100 self.task = builtins.add_group_task(self.app)
101 super().setup_method()
102
103 def test_apply_async_eager(self):
104 self.task.apply = Mock(name='apply')
105 self.task.apply_async((1, 2, 3, 4, 5))
106 self.task.apply.assert_called()
107
108 def mock_group(self, *tasks):
109 g = group(*tasks, app=self.app)
110 result = g.freeze()
111 for task in g.tasks:
112 task.clone = Mock(name='clone')
113 task.clone.attach_mock(Mock(), 'apply_async')
114 return g, result
115
116 @patch('celery.app.base.Celery.current_worker_task')
117 def test_task(self, current_worker_task):
118 g, result = self.mock_group(self.add.s(2), self.add.s(4))
119 self.task(g.tasks, result, result.id, (2,)).results
120 g.tasks[0].clone().apply_async.assert_called_with(
121 group_id=result.id, producer=self.app.producer_or_acquire(),
122 add_to_parent=False,
123 )
124 current_worker_task.add_trail.assert_called_with(result)
125
126 @patch('celery.app.base.Celery.current_worker_task')
127 def test_task__disable_add_to_parent(self, current_worker_task):
128 g, result = self.mock_group(self.add.s(2, 2), self.add.s(4, 4))
129 self.task(g.tasks, result, result.id, None, add_to_parent=False)
130 current_worker_task.add_trail.assert_not_called()
131
132
133 class test_chain(BuiltinsCase):
134
135 def setup_method(self):
136 super().setup_method()
137 self.task = builtins.add_chain_task(self.app)
138
139 def test_not_implemented(self):
140 with pytest.raises(NotImplementedError):
141 self.task()
142
143
144 class test_chord(BuiltinsCase):
145
146 def setup_method(self):
147 self.task = builtins.add_chord_task(self.app)
148 super().setup_method()
149
150 def test_apply_async(self):
151 x = chord([self.add.s(i, i) for i in range(10)], body=self.xsum.s())
152 r = x.apply_async()
153 assert r
154 assert r.parent
155
156 def test_run_header_not_group(self):
157 self.task([self.add.s(i, i) for i in range(10)], self.xsum.s())
158
159 def test_forward_options(self):
160 body = self.xsum.s()
161 x = chord([self.add.s(i, i) for i in range(10)], body=body)
162 x.run = Mock(name='chord.run(x)')
163 x.apply_async(group_id='some_group_id')
164 x.run.assert_called()
165 resbody = x.run.call_args[0][1]
166 assert resbody.options['group_id'] == 'some_group_id'
167 x2 = chord([self.add.s(i, i) for i in range(10)], body=body)
168 x2.run = Mock(name='chord.run(x2)')
169 x2.apply_async(chord='some_chord_id')
170 x2.run.assert_called()
171 resbody = x2.run.call_args[0][1]
172 assert resbody.options['chord'] == 'some_chord_id'
173
174 def test_apply_eager(self):
175 self.app.conf.task_always_eager = True
176 x = chord([self.add.s(i, i) for i in range(10)], body=self.xsum.s())
177 r = x.apply_async()
178 assert r.get() == 90
179
180 def test_apply_eager_with_arguments(self):
181 self.app.conf.task_always_eager = True
182 x = chord([self.add.s(i) for i in range(10)], body=self.xsum.s())
183 r = x.apply_async([1])
184 assert r.get() == 55