@@ -47,7 +47,7 @@ def setup_test_cases(self):
47
47
db .clear_db_variables ()
48
48
crypto ._fernet = None
49
49
50
- @conf_vars ({("core" , "fernet_key" ): "" })
50
+ @conf_vars ({("core" , "fernet_key" ): "" , ( "core" , "unit_test_mode" ): "True" })
51
51
def test_variable_no_encryption (self , session ):
52
52
"""
53
53
Test variables without encryption
@@ -100,12 +100,13 @@ def test_variable_set_get_round_trip(self):
100
100
Variable .set ("tested_var_set_id" , "Monday morning breakfast" )
101
101
assert "Monday morning breakfast" == Variable .get ("tested_var_set_id" )
102
102
103
+ @pytest .mark .skip_if_database_isolation_mode # Does not work in db isolation mode
103
104
def test_variable_set_with_env_variable (self , caplog , session ):
104
105
caplog .set_level (logging .WARNING , logger = variable .log .name )
105
106
Variable .set (key = "key" , value = "db-value" , session = session )
106
107
with mock .patch .dict ("os.environ" , AIRFLOW_VAR_KEY = "env-value" ):
107
108
# setting value while shadowed by an env variable will generate a warning
108
- Variable .set ("key" , "new-db-value" )
109
+ Variable .set (key = "key" , value = "new-db-value" , session = session )
109
110
# value set above is not returned because the env variable value takes priority
110
111
assert "env-value" == Variable .get ("key" )
111
112
# invalidate the cache to re-evaluate value
@@ -120,6 +121,7 @@ def test_variable_set_with_env_variable(self, caplog, session):
120
121
"EnvironmentVariablesBackend"
121
122
)
122
123
124
+ @pytest .mark .skip_if_database_isolation_mode # Does not work in db isolation mode
123
125
@mock .patch ("airflow.models.variable.ensure_secrets_loaded" )
124
126
def test_variable_set_with_extra_secret_backend (self , mock_ensure_secrets , caplog , session ):
125
127
caplog .set_level (logging .WARNING , logger = variable .log .name )
@@ -137,11 +139,11 @@ def test_variable_set_with_extra_secret_backend(self, mock_ensure_secrets, caplo
137
139
"will be updated, but to read it you have to delete the conflicting variable from "
138
140
"MockSecretsBackend"
139
141
)
140
- Variable .delete ("key" )
142
+ Variable .delete (key = "key" , session = session )
141
143
142
144
def test_variable_set_get_round_trip_json (self ):
143
145
value = {"a" : 17 , "b" : 47 }
144
- Variable .set ("tested_var_set_id" , value , serialize_json = True )
146
+ Variable .set (key = "tested_var_set_id" , value = value , serialize_json = True )
145
147
assert value == Variable .get ("tested_var_set_id" , deserialize_json = True )
146
148
147
149
def test_variable_update (self , session ):
@@ -184,9 +186,9 @@ def test_get_non_existing_var_should_raise_key_error(self):
184
186
with pytest .raises (KeyError ):
185
187
Variable .get ("thisIdDoesNotExist" )
186
188
187
- def test_update_non_existing_var_should_raise_key_error (self ):
189
+ def test_update_non_existing_var_should_raise_key_error (self , session ):
188
190
with pytest .raises (KeyError ):
189
- Variable .update ("thisIdDoesNotExist" , "value" )
191
+ Variable .update (key = "thisIdDoesNotExist" , value = "value" , session = session )
190
192
191
193
def test_get_non_existing_var_with_none_default_should_return_none (self ):
192
194
assert Variable .get ("thisIdDoesNotExist" , default_var = None ) is None
@@ -197,42 +199,45 @@ def test_get_non_existing_var_should_not_deserialize_json_default(self):
197
199
"thisIdDoesNotExist" , default_var = default_value , deserialize_json = True
198
200
)
199
201
200
- def test_variable_setdefault_round_trip (self ):
202
+ @pytest .mark .skip_if_database_isolation_mode # Does not work in db isolation mode
203
+ def test_variable_setdefault_round_trip (self , session ):
201
204
key = "tested_var_setdefault_1_id"
202
205
value = "Monday morning breakfast in Paris"
203
- Variable .setdefault (key , value )
206
+ Variable .setdefault (key = key , default = value )
204
207
assert value == Variable .get (key )
205
208
206
- def test_variable_setdefault_round_trip_json (self ):
209
+ @pytest .mark .skip_if_database_isolation_mode # Does not work in db isolation mode
210
+ def test_variable_setdefault_round_trip_json (self , session ):
207
211
key = "tested_var_setdefault_2_id"
208
212
value = {"city" : "Paris" , "Happiness" : True }
209
- Variable .setdefault (key , value , deserialize_json = True )
213
+ Variable .setdefault (key = key , default = value , deserialize_json = True )
210
214
assert value == Variable .get (key , deserialize_json = True )
211
215
216
+ @pytest .mark .skip_if_database_isolation_mode # Does not work in db isolation mode
212
217
def test_variable_setdefault_existing_json (self , session ):
213
218
key = "tested_var_setdefault_2_id"
214
219
value = {"city" : "Paris" , "Happiness" : True }
215
220
Variable .set (key = key , value = value , serialize_json = True , session = session )
216
- val = Variable .setdefault (key , value , deserialize_json = True )
221
+ val = Variable .setdefault (key = key , default = value , deserialize_json = True )
217
222
# Check the returned value, and the stored value are handled correctly.
218
223
assert value == val
219
224
assert value == Variable .get (key , deserialize_json = True )
220
225
221
- def test_variable_delete (self ):
226
+ def test_variable_delete (self , session ):
222
227
key = "tested_var_delete"
223
228
value = "to be deleted"
224
229
225
230
# No-op if the variable doesn't exist
226
- Variable .delete (key )
231
+ Variable .delete (key = key , session = session )
227
232
with pytest .raises (KeyError ):
228
233
Variable .get (key )
229
234
230
235
# Set the variable
231
- Variable .set (key , value )
236
+ Variable .set (key = key , value = value , session = session )
232
237
assert value == Variable .get (key )
233
238
234
239
# Delete the variable
235
- Variable .delete (key )
240
+ Variable .delete (key = key , session = session )
236
241
with pytest .raises (KeyError ):
237
242
Variable .get (key )
238
243
@@ -276,15 +281,15 @@ def test_caching_caches(self, mock_ensure_secrets: mock.Mock):
276
281
mock_backend .get_variable .assert_called_once () # second call was not made because of cache
277
282
assert first == second
278
283
279
- def test_cache_invalidation_on_set (self ):
284
+ def test_cache_invalidation_on_set (self , session ):
280
285
with mock .patch .dict ("os.environ" , AIRFLOW_VAR_KEY = "from_env" ):
281
286
a = Variable .get ("key" ) # value is saved in cache
282
287
with mock .patch .dict ("os.environ" , AIRFLOW_VAR_KEY = "from_env_two" ):
283
288
b = Variable .get ("key" ) # value from cache is used
284
289
assert a == b
285
290
286
291
# setting a new value invalidates the cache
287
- Variable .set ("key" , "new_value" )
292
+ Variable .set (key = "key" , value = "new_value" , session = session )
288
293
289
294
c = Variable .get ("key" ) # cache should not be used
290
295
@@ -312,7 +317,6 @@ def test_masking_only_secret_values(variable_value, deserialize_json, expected_m
312
317
)
313
318
session .add (var )
314
319
session .flush ()
315
-
316
320
# Make sure we re-load it, not just get the cached object back
317
321
session .expunge (var )
318
322
_secrets_masker ().patterns = set ()
0 commit comments