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