Skip to content

Commit c49a99b

Browse files
github-actions[bot]henry3260
authored andcommitted
[v3-2-test] airflow-ctl: fix variable import to correctly handle falsy values (#64362) (#64448)
(cherry picked from commit a908450) Co-authored-by: Henry Chen <henryhenry0512@gmail.com>
1 parent af4718d commit c49a99b

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

airflow-ctl/src/airflowctl/ctl/commands/variable_command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def import_(args, api_client=NEW_API_CLIENT) -> list[str]:
5151
vars_to_update = []
5252
for k, v in var_json.items():
5353
value, description = v, None
54-
if isinstance(v, dict) and v.get("value"):
54+
if isinstance(v, dict) and "value" in v:
5555
value, description = v["value"], v.get("description")
5656

5757
vars_to_update.append(

airflow-ctl/tests/airflow_ctl/ctl/commands/test_variable_command.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,37 @@ def test_import_success(self, api_client_maker, tmp_path, monkeypatch):
8383
)
8484
assert response == [self.key]
8585

86+
@pytest.mark.parametrize(
87+
"falsy_value",
88+
[
89+
"",
90+
0,
91+
False,
92+
],
93+
ids=["empty_string", "zero", "false"],
94+
)
95+
def test_import_falsy_values(self, api_client_maker, tmp_path, monkeypatch, falsy_value):
96+
"""Test that falsy values (empty string, 0, False) are correctly imported."""
97+
api_client = api_client_maker(
98+
path="/api/v2/variables",
99+
response_json=self.bulk_response_success.model_dump(),
100+
expected_http_status_code=200,
101+
kind=ClientKind.CLI,
102+
)
103+
104+
monkeypatch.chdir(tmp_path)
105+
expected_json_path = tmp_path / self.export_file_name
106+
variable_file = {
107+
self.key: {"value": falsy_value, "description": "test falsy value"},
108+
}
109+
110+
expected_json_path.write_text(json.dumps(variable_file))
111+
response = variable_command.import_(
112+
self.parser.parse_args(["variables", "import", expected_json_path.as_posix()]),
113+
api_client=api_client,
114+
)
115+
assert response == [self.key]
116+
86117
def test_import_error(self, api_client_maker, tmp_path, monkeypatch):
87118
api_client = api_client_maker(
88119
path="/api/v2/variables",

0 commit comments

Comments
 (0)