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


##########
tests/api_fastapi/execution_api/routes/test_task_instance.py:
##########
@@ -0,0 +1,232 @@
+# 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 import timezone
+from airflow.utils.state import State
+
+from tests_common.test_utils.db import clear_db_runs
+
+pytestmark = pytest.mark.db_test
+
+
+DEFAULT_START_DATE = timezone.parse("2024-10-31T11:00:00Z")
+DEFAULT_END_DATE = timezone.parse("2024-10-31T12:00:00Z")
+
+
+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.
+        """
+
+        ti = create_task_instance(
+            task_id="test_ti_update_state_to_running",
+            state=State.QUEUED,
+            session=session,
+        )
+
+        session.commit()
+
+        response = 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 == ""
+
+        # Refresh the Task Instance from the database so that we can check the 
updated values
+        session.refresh(ti)
+        assert ti.state == State.RUNNING
+        assert ti.hostname == "random-hostname"
+        assert ti.unixname == "random-unixname"
+        assert ti.pid == 100
+        assert ti.start_date.isoformat() == "2024-10-31T12:00:00+00:00"
+
+    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()
+
+        response = 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", "expected_end_date"),
+        [
+            # For SUCCESS & FAILED states, end_date is required
+            (State.SUCCESS, DEFAULT_END_DATE, State.SUCCESS, DEFAULT_END_DATE),
+            (State.FAILED, DEFAULT_END_DATE, State.FAILED, DEFAULT_END_DATE),
+            # When state is SKIPPED, end_date is optional because
+            # 1) a user can run a task
+            #   and raise AirflowSkipException within the task without 
specifying the end_date
+            # 2) a task can be skipped by the scheduler without specifying the 
end_date
+            (State.SKIPPED, DEFAULT_END_DATE, State.SKIPPED, DEFAULT_END_DATE),
+            (State.SKIPPED, None, State.SKIPPED, None),

Review Comment:
   Scheduler won't be hitting this API server though.



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