Skip to content

Commit

Permalink
fix CLI unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhenye-Na committed Oct 9, 2023
1 parent 9b98b2a commit 9e43737
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
11 changes: 10 additions & 1 deletion airflow/cli/cli_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,15 @@ def string_lower_type(val):
ARG_DEFAULT = Arg(
("-d", "--default"), metavar="VAL", default=None, help="Default value returned if variable does not exist"
)
ARG_VAR_DESCRIPTION = Arg(
(
"-desc",
"--description",
),
default=None,
required=False,
help="Variable description, optional when setting a variable",
)
ARG_DESERIALIZE_JSON = Arg(("-j", "--json"), help="Deserialize JSON variable", action="store_true")
ARG_SERIALIZE_JSON = Arg(("-j", "--json"), help="Serialize JSON variable", action="store_true")
ARG_VAR_IMPORT = Arg(("file",), help="Import variables from JSON file")
Expand Down Expand Up @@ -1442,7 +1451,7 @@ class GroupCommand(NamedTuple):
name="set",
help="Set variable",
func=lazy_load_command("airflow.cli.commands.variable_command.variables_set"),
args=(ARG_VAR, ARG_VAR_VALUE, ARG_SERIALIZE_JSON, ARG_VERBOSE),
args=(ARG_VAR, ARG_VAR_VALUE, ARG_VAR_DESCRIPTION, ARG_SERIALIZE_JSON, ARG_VERBOSE),
),
ActionCommand(
name="delete",
Expand Down
2 changes: 1 addition & 1 deletion airflow/cli/commands/variable_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def variables_get(args):
@cli_utils.action_cli
@providers_configuration_loaded
def variables_set(args):
"""Create new variable with a given name and value."""
"""Create new variable with a given name, value and description."""
Variable.set(args.key, args.value, args.description or None, serialize_json=args.json)
print(f"Variable {args.key} created")

Expand Down
15 changes: 14 additions & 1 deletion tests/cli/commands/test_variable_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,23 @@ def teardown_method(self):

def test_variables_set(self):
"""Test variable_set command"""
variable_command.variables_set(self.parser.parse_args(["variables", "set", "foo", "bar"]))
assert Variable.get("foo") is not None
with pytest.raises(KeyError):
Variable.get("foo1")

def test_variables_set_with_description(self):
"""Test variable_set command with optional description argument"""
variable_command.variables_set(
self.parser.parse_args(["variables", "set", "foo", "bar", "foo_bar_description"])
self.parser.parse_args(["variables", "set", "foo", "bar", "--desc", "foo_bar_description"])
)
assert Variable.get("foo") is not None

variable_command.variables_set(
self.parser.parse_args(["variables", "set", "bar", "foo", "--description", "bar_foo_description"])
)
assert Variable.get("bar") is not None

with pytest.raises(KeyError):
Variable.get("foo1")

Expand Down

0 comments on commit 9e43737

Please sign in to comment.