kaxil commented on code in PR #43602:
URL: https://github.com/apache/airflow/pull/43602#discussion_r1828116232


##########
tests/api_fastapi/execution_api/routes/test_task_instance.py:
##########
@@ -0,0 +1,192 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from __future__ import annotations
+
+from unittest import mock
+
+import pytest
+from sqlalchemy import select
+from sqlalchemy.exc import SQLAlchemyError
+
+from airflow.models.taskinstance import TaskInstance
+from airflow.utils.state import State
+
+from tests_common.test_utils.db import clear_db_runs
+
+pytestmark = pytest.mark.db_test
+
+
+class TestTIUpdateState:
+    def setup_method(self):
+        clear_db_runs()
+
+    def teardown_method(self):
+        clear_db_runs()
+
+    def test_ti_update_state_to_running(self, client, session, 
create_task_instance):
+        """
+        Test that the Task Instance state is updated to running when the Task 
Instance is in a state where it can be
+        marked as running.
+        """
+        test_client = client(apps="execution")
+        ti = create_task_instance(task_id="test_ti_update_state_to_running", 
state=State.QUEUED)
+
+        session.commit()
+
+        response = test_client.patch(
+            f"/execution/task_instance/{ti.id}/state",
+            json={
+                "state": "running",
+                "hostname": "random-hostname",
+                "unixname": "random-unixname",
+                "pid": 100,
+                "start_date": "2024-10-31T12:00:00Z",
+            },
+        )
+
+        assert response.status_code == 204
+        assert response.text == ""
+
+        assert session.scalar(select(TaskInstance.state).where(TaskInstance.id 
== ti.id)) == State.RUNNING
+
+    def test_ti_update_state_conflict_if_not_queued(self, client, session, 
create_task_instance):
+        """
+        Test that a 409 error is returned when the Task Instance is not in a 
state where it can be marked as
+        running. In this case, the Task Instance is first in NONE state so it 
cannot be marked as running.
+        """
+        ti = create_task_instance(
+            task_id="test_ti_update_state_conflict_if_not_queued",
+            state=State.NONE,
+        )
+        session.commit()
+        test_client = client(apps="execution")
+
+        response = test_client.patch(
+            f"/execution/task_instance/{ti.id}/state",
+            json={
+                "state": "running",
+                "hostname": "random-hostname",
+                "unixname": "random-unixname",
+                "pid": 100,
+                "start_date": "2024-10-31T12:00:00Z",
+            },
+        )
+
+        assert response.status_code == 409
+        assert response.json() == {
+            "detail": {
+                "message": "TI was not in a state where it could be marked as 
running",
+                "previous_state": State.NONE,
+                "reason": "invalid_state",
+            }
+        }
+
+        assert session.scalar(select(TaskInstance.state).where(TaskInstance.id 
== ti.id)) == State.NONE
+
+    @pytest.mark.parametrize(
+        "state, end_date, expected_state",
+        [
+            (State.SUCCESS, "2024-10-31T12:00:00Z", State.SUCCESS),
+            (State.FAILED, "2024-10-31T12:05:00Z", State.FAILED),
+            (State.SKIPPED, "2024-10-31T12:05:00Z", State.SKIPPED),
+            (State.SKIPPED, None, State.SKIPPED),
+            (State.UPSTREAM_FAILED, None, State.UPSTREAM_FAILED),
+            (State.REMOVED, None, State.REMOVED),
+        ],
+    )
+    def test_ti_update_state_to_terminal(
+        self, client, session, create_task_instance, state, end_date, 
expected_state
+    ):
+        ti = create_task_instance(
+            task_id="test_ti_update_state_to_terminal",
+            state=State.RUNNING,
+        )
+        session.commit()
+        test_client = client(apps="execution")
+
+        payload = {"state": state}
+        if end_date:
+            payload["end_date"] = end_date
+
+        response = test_client.patch(
+            f"/execution/task_instance/{ti.id}/state",
+            json=payload,
+        )
+
+        assert response.status_code == 204
+        assert response.text == ""
+
+        assert session.scalar(select(TaskInstance.state).where(TaskInstance.id 
== ti.id)) == expected_state
+
+        if expected_state == State.SKIPPED and not end_date:
+            assert (
+                
session.scalar(select(TaskInstance.end_date).where(TaskInstance.id == ti.id)) 
== ti.start_date
+            )
+        elif expected_state in [State.UPSTREAM_FAILED, State.REMOVED]:
+            assert 
session.scalar(select(TaskInstance.end_date).where(TaskInstance.id == ti.id)) 
is None
+
+    def test_ti_update_state_not_found(self, client, session):
+        """
+        Test that a 404 error is returned when the Task Instance does not 
exist.
+        """
+        task_instance_id = "0182e924-0f1e-77e6-ab50-e977118bc139"
+
+        # Assert that the Task Instance does not exist
+        assert session.scalar(select(TaskInstance.id).where(TaskInstance.id == 
task_instance_id)) is None
+        session.commit()

Review Comment:
   Updated



-- 
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]

Reply via email to