|
17 | 17 | # under the License.
|
18 | 18 |
|
19 | 19 | import unittest
|
| 20 | +from unittest import mock |
20 | 21 |
|
21 | 22 | from cryptography.fernet import Fernet
|
22 | 23 | from parameterized import parameterized
|
23 | 24 |
|
24 | 25 | from airflow import settings
|
25 |
| -from airflow.models import Variable, crypto |
| 26 | +from airflow.models import Variable, crypto, variable |
26 | 27 | from tests.test_utils import db
|
27 | 28 | from tests.test_utils.config import conf_vars
|
28 | 29 |
|
@@ -89,6 +90,23 @@ def test_variable_set_get_round_trip(self):
|
89 | 90 | Variable.set("tested_var_set_id", "Monday morning breakfast")
|
90 | 91 | self.assertEqual("Monday morning breakfast", Variable.get("tested_var_set_id"))
|
91 | 92 |
|
| 93 | + def test_variable_set_with_env_variable(self): |
| 94 | + Variable.set("key", "db-value") |
| 95 | + with self.assertLogs(variable.log) as log_context: |
| 96 | + with mock.patch.dict('os.environ', AIRFLOW_VAR_KEY="env-value"): |
| 97 | + Variable.set("key", "new-db-value") |
| 98 | + self.assertEqual("env-value", Variable.get("key")) |
| 99 | + self.assertEqual("new-db-value", Variable.get("key")) |
| 100 | + |
| 101 | + self.assertEqual( |
| 102 | + log_context.records[0].message, |
| 103 | + ( |
| 104 | + 'You have the environment variable AIRFLOW_VAR_KEY defined, which takes precedence over ' |
| 105 | + 'reading from the database. The value will be saved, but to read it you have to delete ' |
| 106 | + 'the environment variable.' |
| 107 | + ), |
| 108 | + ) |
| 109 | + |
92 | 110 | def test_variable_set_get_round_trip_json(self):
|
93 | 111 | value = {"a": 17, "b": 47}
|
94 | 112 | Variable.set("tested_var_set_id", value, serialize_json=True)
|
|
0 commit comments