1
- from celery import Celery
2
- import unittest
3
1
import mock
2
+ import unittest
4
3
5
-
6
- from flask_log_request_id .extras .celery import (
7
- RequestIDAwareTask , ctx_celery_task_get_request_id , ExecutedOutsideContext )
4
+ from celery import Celery
5
+ from flask_log_request_id .extras .celery import (ExecutedOutsideContext ,
6
+ on_before_publish_insert_request_id_header ,
7
+ ctx_celery_task_get_request_id )
8
8
9
9
10
10
class MockedTask (object ):
@@ -22,58 +22,27 @@ def apply_async(self, *args, **kwargs):
22
22
class CeleryIntegrationTestCase (unittest .TestCase ):
23
23
24
24
@mock .patch ('flask_log_request_id.extras.celery.current_request_id' )
25
- def test_mixin_injection (self , mocked_current_request_id ):
26
-
27
- patcher = mock .patch .object (RequestIDAwareTask , '__bases__' , (MockedTask ,))
28
-
29
- with patcher :
30
- patcher .is_local = True
31
-
32
- mocked_current_request_id .return_value = 15
33
- task = RequestIDAwareTask ()
34
- task .apply_async ('test' , foo = 'bar' )
35
- self .assertEqual (
36
- task .apply_async_called ['args' ],
37
- ('test' , ))
25
+ def test_enable_request_id_propagation (self , mocked_current_request_id ):
26
+ mocked_current_request_id .return_value = 15
38
27
39
- self .assertDictEqual (
40
- task .apply_async_called ['kwargs' ], {
41
- 'headers' : {'x_request_id' : 15 },
42
- 'foo' : 'bar'
43
- })
44
-
45
- @mock .patch ('flask_log_request_id.extras.celery.current_request_id' )
46
- def test_issue21_called_with_headers_None (self , mocked_current_request_id ):
47
-
48
- patcher = mock .patch .object (RequestIDAwareTask , '__bases__' , (MockedTask ,))
49
-
50
- with patcher :
51
- patcher .is_local = True
52
-
53
- mocked_current_request_id .return_value = 15
54
- task = RequestIDAwareTask ()
55
- task .apply_async ('test' , foo = 'bar' , headers = None )
56
- self .assertEqual (
57
- task .apply_async_called ['args' ],
58
- ('test' , ))
59
-
60
- self .assertDictEqual (
61
- task .apply_async_called ['kwargs' ], {
62
- 'headers' : {'x_request_id' : 15 },
63
- 'foo' : 'bar'
64
- })
28
+ headers = {}
29
+ on_before_publish_insert_request_id_header (headers = headers )
30
+ self .assertDictEqual (
31
+ {
32
+ 'x_request_id' : 15
33
+ },
34
+ headers )
65
35
66
36
@mock .patch ('flask_log_request_id.extras.celery.current_task' )
67
37
def test_ctx_fetcher_outside_context (self , mocked_current_task ):
68
-
69
38
mocked_current_task ._get_current_object .return_value = None
70
39
with self .assertRaises (ExecutedOutsideContext ):
71
40
ctx_celery_task_get_request_id ()
72
41
73
42
@mock .patch ('flask_log_request_id.extras.celery.current_task' )
74
43
def test_ctx_fetcher_inside_context (self , mocked_current_task ):
75
44
mocked_current_task ._get_current_object .return_value = True
76
- mocked_current_task .request .get .side_effect = lambda a , default : {'x_request_id' : 15 , 'other' :'bar' }[a ]
45
+ mocked_current_task .request .get .side_effect = lambda a , default : {'x_request_id' : 15 , 'other' : 'bar' }[a ]
77
46
78
47
self .assertEqual (ctx_celery_task_get_request_id (), 15 )
79
48
0 commit comments