lohitkolluri commented on code in PR #67900:
URL: https://github.com/apache/airflow/pull/67900#discussion_r3507318648


##########
airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_backfills.py:
##########
@@ -353,6 +354,135 @@ def test_create_backfill_with_depends_on_past(
                     == "Dag has tasks for which depends_on_past=True. You must 
set reprocess behavior to reprocess completed or reprocess failed."
                 )
 
+    def test_create_backfill_database_locked(self, session, dag_maker, 
test_client):
+        """SQLite 'database is locked' during backfill creation returns HTTP 
503."""
+        with dag_maker(session=session, dag_id="TEST_DAG_1", schedule="0 * * * 
*") as dag:
+            EmptyOperator(task_id="mytask")
+        session.scalars(select(DagModel)).all()
+        session.commit()
+
+        from_date = pendulum.parse("2024-01-01")
+        to_date = pendulum.parse("2024-02-01")
+
+        data = {
+            "dag_id": dag.dag_id,
+            "from_date": to_iso(from_date),
+            "to_date": to_iso(to_date),
+            "max_active_runs": 5,
+            "run_backwards": False,
+            "dag_run_conf": {},
+        }
+
+        with mock.patch(
+            
"airflow.api_fastapi.core_api.routes.public.backfills._create_backfill",
+            side_effect=OperationalError("statement", "params", "database is 
locked"),
+        ):
+            response = test_client.post("/backfills", json=data)
+
+        # OperationalError with "database is locked" should return 503
+        assert response.status_code == 503
+        assert "database is locked" in response.json()["detail"].lower()
+
+    class TestCreateBackfillDryRun:
+        """Tests for the create_backfill dry_run endpoint."""
+
+        def test_create_backfill_dry_run_database_locked(self, session, 
dag_maker, test_client):
+            """SQLite 'database is locked' during dry-run returns 503."""
+            with dag_maker(session=session, dag_id="TEST_DAG_1", schedule="0 * 
* * *") as dag:
+                EmptyOperator(task_id="mytask")
+            session.scalars(select(DagModel)).all()
+            session.commit()
+
+            from_date = pendulum.parse("2024-01-01")
+            to_date = pendulum.parse("2024-02-01")
+
+            data = {
+                "dag_id": dag.dag_id,
+                "from_date": to_iso(from_date),
+                "to_date": to_iso(to_date),
+                "max_active_runs": 5,
+                "run_backwards": False,
+                "dag_run_conf": {},
+            }
+
+            with mock.patch(
+                
"airflow.api_fastapi.core_api.routes.public.backfills._do_dry_run",
+                side_effect=OperationalError("statement", "params", "database 
is locked"),
+            ):
+                response = test_client.post("/backfills/dry_run", json=data)
+
+            # OperationalError with "database is locked" should return 503
+            assert response.status_code == 503
+            assert "database is locked" in response.json()["detail"].lower()
+
+    def test_create_backfill_cleans_up_orphan_on_lock_error(self, session, 
dag_maker):

Review Comment:
   Added `test_create_backfill_cleanup_removes_partial_dag_runs` for this, and 
kept the existing orphan test focused on the bare `Backfill` case.
   
   The new test creates two partial `DagRun`s (`backfill_id` set, 
`BACKFILL_JOB`) using `dag_maker.create_dagrun`, which also creates real 
`TaskInstance`s. It then adds the corresponding `BackfillDagRun` entries, runs 
`_cleanup_partial_backfill`, and verifies that `Backfill`, `DagRun`, 
`BackfillDagRun`, and `TaskInstance` are all removed.
   
   This covers the cascade delete path: removing the `DagRun`s also removes the 
associated `TaskInstance`s. Tested locally and it passes.



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