23
23
from unittest .mock import call , Mock , patch
24
24
25
25
import pytest
26
- from flask import g
27
26
28
27
from superset import app
29
28
from superset .utils import decorators
@@ -89,7 +88,10 @@ def my_func(response: ResponseValues, *args: Any, **kwargs: Any) -> str:
89
88
mock .assert_called_once_with (expected_result , 1 )
90
89
91
90
92
- def test_context_decorator () -> None :
91
+ @patch ("superset.utils.decorators.g" )
92
+ def test_context_decorator (flask_g_mock ) -> None :
93
+ flask_g_mock .logs_context = {}
94
+
93
95
@decorators .logs_context ()
94
96
def myfunc (* args , ** kwargs ) -> str :
95
97
return "test"
@@ -99,46 +101,85 @@ def myfunc(*args, **kwargs) -> str:
99
101
def myfunc_with_kwargs (* args , ** kwargs ) -> str :
100
102
return "test"
101
103
104
+ @decorators .logs_context (bad_context = 1 )
105
+ def myfunc_with_dissallowed_kwargs (* args , ** kwargs ) -> str :
106
+ return "test"
107
+
108
+ @decorators .logs_context (
109
+ context_func = lambda * args , ** kwargs : {"slice_id" : kwargs ["chart_id" ]}
110
+ )
111
+ def myfunc_with_context (* args , ** kwargs ) -> str :
112
+ return "test"
113
+
102
114
# should not add any data to the global.logs_context scope
103
115
myfunc (1 , 1 )
104
- assert g .logs_context == {}
116
+ assert flask_g_mock .logs_context == {}
105
117
106
118
# should add dashboard_id to the global.logs_context scope
107
119
myfunc (1 , 1 , dashboard_id = 1 )
108
- assert g .logs_context == {"dashboard_id" : 1 }
109
- g .logs_context = {}
120
+ assert flask_g_mock .logs_context == {"dashboard_id" : 1 }
121
+ # reset logs_context
122
+ flask_g_mock .logs_context = {}
110
123
111
124
# should add slice_id to the global.logs_context scope
112
125
myfunc (1 , 1 , slice_id = 1 )
113
- assert g .logs_context == {"slice_id" : 1 }
114
- g .logs_context = {}
126
+ assert flask_g_mock .logs_context == {"slice_id" : 1 }
127
+ # reset logs_context
128
+ flask_g_mock .logs_context = {}
115
129
116
130
# should add execution_id to the global.logs_context scope
117
131
myfunc (1 , 1 , execution_id = 1 )
118
- assert g .logs_context == {"execution_id" : 1 }
119
- g .logs_context = {}
132
+ assert flask_g_mock .logs_context == {"execution_id" : 1 }
133
+ # reset logs_context
134
+ flask_g_mock .logs_context = {}
120
135
121
136
# should add all three to the global.logs_context scope
122
137
myfunc (1 , 1 , dashboard_id = 1 , slice_id = 1 , execution_id = 1 )
123
- assert g .logs_context == {"dashboard_id" : 1 , "slice_id" : 1 , "execution_id" : 1 }
124
- g .logs_context = {}
138
+ assert flask_g_mock .logs_context == {
139
+ "dashboard_id" : 1 ,
140
+ "slice_id" : 1 ,
141
+ "execution_id" : 1 ,
142
+ }
143
+ # reset logs_context
144
+ flask_g_mock .logs_context = {}
125
145
126
146
# should overwrite existing values in the global.logs_context scope
127
- g .logs_context = {"dashboard_id" : 2 , "slice_id" : 2 , "execution_id" : 2 }
147
+ flask_g_mock .logs_context = {"dashboard_id" : 2 , "slice_id" : 2 , "execution_id" : 2 }
128
148
myfunc (1 , 1 , dashboard_id = 3 , slice_id = 3 , execution_id = 3 )
129
- assert g .logs_context == {"dashboard_id" : 3 , "slice_id" : 3 , "execution_id" : 3 }
130
- g .logs_context = {}
149
+ assert flask_g_mock .logs_context == {
150
+ "dashboard_id" : 3 ,
151
+ "slice_id" : 3 ,
152
+ "execution_id" : 3 ,
153
+ }
154
+ # reset logs_context
155
+ flask_g_mock .logs_context = {}
156
+
157
+ # should not add a value that does not exist in the global.logs_context scope
158
+ myfunc_with_dissallowed_kwargs ()
159
+ assert flask_g_mock .logs_context == {}
160
+ # reset logs_context
161
+ flask_g_mock .logs_context = {}
131
162
132
163
# should be able to add values to the decorator function directly
133
164
myfunc_with_kwargs ()
134
- assert g .logs_context ["dashboard_id" ] == 1
135
- assert g .logs_context ["slice_id" ] == 1
136
- assert isinstance (g .logs_context ["execution_id" ], uuid .UUID )
137
- g .logs_context = {}
165
+ assert flask_g_mock .logs_context ["dashboard_id" ] == 1
166
+ assert flask_g_mock .logs_context ["slice_id" ] == 1
167
+ assert isinstance (flask_g_mock .logs_context ["execution_id" ], uuid .UUID )
168
+ # reset logs_context
169
+ flask_g_mock .logs_context = {}
138
170
139
171
# should be able to add values to the decorator function directly
140
- # and overwrite with any kwargs passed in
172
+ # and it will overwrite any kwargs passed into the decorated function
141
173
myfunc_with_kwargs (execution_id = 4 )
142
174
143
- assert g .logs_context == {"dashboard_id" : 1 , "slice_id" : 1 , "execution_id" : 4 }
144
- g .logs_context = {}
175
+ assert flask_g_mock .logs_context ["dashboard_id" ] == 1
176
+ assert flask_g_mock .logs_context ["slice_id" ] == 1
177
+ assert isinstance (flask_g_mock .logs_context ["execution_id" ], uuid .UUID )
178
+ # reset logs_context
179
+ flask_g_mock .logs_context = {}
180
+
181
+ # should be able to pass a callable context to the decorator
182
+ myfunc_with_context (chart_id = 1 )
183
+ assert flask_g_mock .logs_context == {"slice_id" : 1 }
184
+ # reset logs_context
185
+ flask_g_mock .logs_context = {}
0 commit comments