Lee-W commented on code in PR #69915:
URL: https://github.com/apache/airflow/pull/69915#discussion_r3628297860
##########
airflow-ctl/src/airflowctl/ctl/commands/dag_command.py:
##########
@@ -108,53 +108,61 @@ def next_execution(args, api_client=NEW_API_CLIENT) ->
dict | None:
return result
-def _parse_logical_date(value: str) -> datetime.datetime | None:
- """Parse an ISO-formatted logical date."""
+def _get_dag_run_by_run_id(api_client, dag_id: str, run_id: str) ->
DAGRunResponse:
+ """Get a Dag run by its run ID."""
+ try:
+ return api_client.dag_runs.get(dag_id=dag_id, dag_run_id=run_id,
suppress_error_log=True)
+ except ServerResponseError as e:
+ if e.response.status_code != 404:
+ raise
+ rich.print(f"[red]Dag run {run_id!r} of Dag {dag_id!r} not found[/red]")
+ sys.exit(1)
Review Comment:
```suggestion
except ServerResponseError as e:
if e.response.status_code != 404:
raise
rich.print(f"[red]Dag run {run_id!r} of Dag {dag_id!r} not
found[/red]")
sys.exit(1)
```
##########
airflow-ctl/tests/airflow_ctl/ctl/commands/test_dag_command.py:
##########
@@ -262,67 +267,91 @@ def test_state_by_logical_date(self, capsys):
logical_date_lte=logical_date,
order_by="-id",
limit=1,
+ suppress_error_log=True,
)
+ api_client.dag_runs.get.assert_not_called()
@pytest.mark.parametrize(
- ("value", "expected_list_kwargs"),
+ "extra_args",
[
- pytest.param("missing_run", {"dag_id": dag_id, "limit": 1},
id="run-id"),
- pytest.param(
- "2025-01-01T00:00:00+00:00",
- {
- "dag_id": dag_id,
- "logical_date_gte": datetime.datetime(2025, 1, 1,
tzinfo=datetime.timezone.utc),
- "logical_date_lte": datetime.datetime(2025, 1, 1,
tzinfo=datetime.timezone.utc),
- "order_by": "-id",
- "limit": 1,
- },
- id="logical-date",
- ),
+ [],
+ ["test_run", "--logical-date", "2025-01-01T00:00:00+00:00"],
],
+ ids=["neither", "both"],
)
- @mock.patch("rich.print")
- def test_state_missing_run_prints_message(self, mock_rich_print, value,
expected_list_kwargs):
+ def test_state_requires_exactly_one_of_run_id_and_logical_date(self,
extra_args, capsys):
api_client = mock.MagicMock()
- api_client.dag_runs.get.side_effect = _server_error(404)
- api_client.dag_runs.list.return_value.dag_runs = []
- dag_command.state(
- self.parser.parse_args(["dags", "state", self.dag_id, value]),
- api_client=api_client,
+ with pytest.raises(SystemExit, match="1"):
+ dag_command.state(
+ self.parser.parse_args(["dags", "state", self.dag_id,
*extra_args]),
+ api_client=api_client,
+ )
+
+ api_client.dag_runs.get.assert_not_called()
+ assert _normalize_rich_output(capsys.readouterr().out) == (
+ "Provide either run_id or --logical-date, but not both"
)
- mock_rich_print.assert_called_once_with("[yellow]No matching Dag run
found.[/yellow]")
-
api_client.dag_runs.list.assert_called_once_with(**expected_list_kwargs)
+ @pytest.mark.parametrize(
+ ("logical_date", "expected_message"),
+ [
+ ("not-a-date", "Invalid --logical-date: 'not-a-date'"),
+ ("2025-01-01T00:00:00", "--logical-date must include a timezone
offset"),
+ ],
+ ids=["unparsable", "naive"],
+ )
+ def test_state_rejects_bad_logical_date(self, logical_date,
expected_message, capsys):
+ api_client = mock.MagicMock()
- def test_state_missing_dag_propagates_api_error(self):
+ with pytest.raises(SystemExit, match="1"):
+ dag_command.state(
+ self.parser.parse_args(["dags", "state", self.dag_id,
"--logical-date", logical_date]),
+ api_client=api_client,
+ )
+
+ api_client.dag_runs.list.assert_not_called()
+ assert _normalize_rich_output(capsys.readouterr().out) ==
expected_message
+
+ @pytest.mark.parametrize("list_failure", ["no_matching_run",
"dag_not_found_404"])
+ def test_state_dag_run_not_found_by_logical_date(self, list_failure,
capsys):
api_client = mock.MagicMock()
- api_client.dag_runs.get.side_effect = _server_error(404)
- api_client.dag_runs.list.side_effect = error = _server_error(404)
+ if list_failure == "no_matching_run":
+ api_client.dag_runs.list.return_value.dag_runs = []
+ else:
+ api_client.dag_runs.list.side_effect = _make_server_error(404)
- with pytest.raises(ServerResponseError) as ctx:
+ with pytest.raises(SystemExit, match="1"):
Review Comment:
what is this `match="1"` for ?
##########
airflow-ctl/src/airflowctl/ctl/commands/dag_command.py:
##########
@@ -108,53 +108,61 @@ def next_execution(args, api_client=NEW_API_CLIENT) ->
dict | None:
return result
-def _parse_logical_date(value: str) -> datetime.datetime | None:
- """Parse an ISO-formatted logical date."""
+def _get_dag_run_by_run_id(api_client, dag_id: str, run_id: str) ->
DAGRunResponse:
+ """Get a Dag run by its run ID."""
+ try:
+ return api_client.dag_runs.get(dag_id=dag_id, dag_run_id=run_id,
suppress_error_log=True)
+ except ServerResponseError as e:
+ if e.response.status_code != 404:
+ raise
+ rich.print(f"[red]Dag run {run_id!r} of Dag {dag_id!r} not found[/red]")
+ sys.exit(1)
+
+
+def _get_dag_run_by_logical_date(api_client, dag_id: str, value: str) ->
DAGRunResponse:
Review Comment:
```suggestion
def _get_dag_run_by_logical_date(api_client: Client, dag_id: str, value:
str) -> DAGRunResponse:
```
##########
airflow-ctl/src/airflowctl/ctl/commands/dag_command.py:
##########
@@ -108,53 +108,61 @@ def next_execution(args, api_client=NEW_API_CLIENT) ->
dict | None:
return result
-def _parse_logical_date(value: str) -> datetime.datetime | None:
- """Parse an ISO-formatted logical date."""
+def _get_dag_run_by_run_id(api_client, dag_id: str, run_id: str) ->
DAGRunResponse:
+ """Get a Dag run by its run ID."""
+ try:
+ return api_client.dag_runs.get(dag_id=dag_id, dag_run_id=run_id,
suppress_error_log=True)
+ except ServerResponseError as e:
+ if e.response.status_code != 404:
+ raise
+ rich.print(f"[red]Dag run {run_id!r} of Dag {dag_id!r} not found[/red]")
+ sys.exit(1)
Review Comment:
these 2 lines work as part of the 404 handling. grouping the logic together
will make it easier to understand
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]