2323import pytest
2424
2525from airflow .models import DAG , Connection
26- from airflow .providers .common .compat .sdk import AirflowException , TaskDeferred , timezone
26+ from airflow .providers .common .compat .sdk import TaskDeferred , timezone
2727from airflow .providers .dbt .cloud .hooks .dbt import DbtCloudHook , DbtCloudJobRunException , DbtCloudJobRunStatus
2828from airflow .providers .dbt .cloud .operators .dbt import (
2929 DbtCloudGetJobRunArtifactOperator ,
@@ -199,7 +199,7 @@ def test_execute_failed_before_getting_deferred(
199199 def test_dbt_run_job_op_async (self , mock_trigger_job_run , mock_dbt_hook , mock_job_run_status , status ):
200200 """
201201 Asserts that a task is deferred and an DbtCloudRunJobTrigger will be fired
202- when the DbtCloudRunJobOperator has deferrable param set to True
202+ when the DbtCloudRunJobOperator has deferrable param set to True.
203203 """
204204 mock_job_run_status .return_value = status
205205 dbt_op = DbtCloudRunJobOperator (
@@ -215,6 +215,40 @@ def test_dbt_run_job_op_async(self, mock_trigger_job_run, mock_dbt_hook, mock_jo
215215 dbt_op .execute (MagicMock ())
216216 assert isinstance (exc .value .trigger , DbtCloudRunJobTrigger ), "Trigger is not a DbtCloudRunJobTrigger"
217217
218+ def test_execute_complete_timeout_without_run_id (self ):
219+ """
220+ Verify that when a deferrable dbt job emits a timeout event with no run_id,
221+ the operator cancels the job and fails.
222+ """
223+
224+ operator = DbtCloudRunJobOperator (
225+ task_id = TASK_ID ,
226+ dbt_cloud_conn_id = ACCOUNT_ID_CONN ,
227+ job_id = JOB_ID ,
228+ dag = self .dag ,
229+ deferrable = True ,
230+ )
231+
232+ # Pretend the job was already triggered.
233+ operator .run_id = None
234+
235+ # Mock the hook so we can assert cancellation.
236+ operator .hook = MagicMock ()
237+
238+ timeout_event = {
239+ "status" : "timeout" ,
240+ "run_id" : None ,
241+ "message" : "Job run timed out." ,
242+ }
243+
244+ with pytest .raises (DbtCloudJobRunException ):
245+ operator .execute_complete (
246+ context = self .mock_context ,
247+ event = timeout_event ,
248+ )
249+
250+ operator .hook .cancel_job_run .assert_not_called ()
251+
218252 def test_execute_complete_timeout_cancels_job (self ):
219253 """
220254 Verify that when a deferrable dbt job emits a timeout event,
@@ -240,7 +274,45 @@ def test_execute_complete_timeout_cancels_job(self):
240274 "message" : "Job run timed out." ,
241275 }
242276
243- with pytest .raises (AirflowException , match = "has timed out" ):
277+ with pytest .raises (DbtCloudJobRunException , match = "has timed out" ):
278+ operator .execute_complete (
279+ context = self .mock_context ,
280+ event = timeout_event ,
281+ )
282+
283+ operator .hook .cancel_job_run .assert_called_once_with (
284+ account_id = operator .account_id ,
285+ run_id = RUN_ID ,
286+ )
287+
288+ def test_execute_complete_timeout_cancel_job_does_not_mask_original_error (self ):
289+ """
290+ Verify that when a deferrable dbt job is cancelled after a timeout event is received,
291+ the original error is not masked.
292+ """
293+ operator = DbtCloudRunJobOperator (
294+ task_id = TASK_ID ,
295+ dbt_cloud_conn_id = ACCOUNT_ID_CONN ,
296+ job_id = JOB_ID ,
297+ dag = self .dag ,
298+ deferrable = True ,
299+ )
300+
301+ # Pretend the job was already triggered.
302+ operator .run_id = RUN_ID
303+
304+ # Mock the hook so we can assert cancellation.
305+ operator .hook = MagicMock ()
306+
307+ operator .hook .cancel_job_run .side_effect = Exception ("Cancellation failed" )
308+
309+ timeout_event = {
310+ "status" : "timeout" ,
311+ "run_id" : RUN_ID ,
312+ "message" : "Job run timed out." ,
313+ }
314+
315+ with pytest .raises (DbtCloudJobRunException , match = "has timed out" ):
244316 operator .execute_complete (
245317 context = self .mock_context ,
246318 event = timeout_event ,
@@ -690,6 +762,61 @@ def test_custom_trigger_reason(self, mock_run_job, conn_id, account_id):
690762 additional_run_config = self .config ["additional_run_config" ],
691763 )
692764
765+ def test_on_kill_cancels_job_and_confirms_success (self ):
766+ operator = DbtCloudRunJobOperator (
767+ task_id = TASK_ID ,
768+ dbt_cloud_conn_id = ACCOUNT_ID_CONN ,
769+ job_id = JOB_ID ,
770+ dag = self .dag ,
771+ )
772+
773+ operator .run_id = RUN_ID
774+ operator .hook = MagicMock ()
775+
776+ # Simulate successful cancellation confirmation.
777+ operator .hook .wait_for_job_run_status .return_value = True
778+
779+ operator .on_kill ()
780+
781+ operator .hook .cancel_job_run .assert_called_once_with (
782+ account_id = operator .account_id ,
783+ run_id = RUN_ID ,
784+ )
785+
786+ operator .hook .wait_for_job_run_status .assert_called_once_with (
787+ run_id = RUN_ID ,
788+ account_id = operator .account_id ,
789+ expected_statuses = DbtCloudJobRunStatus .CANCELLED .value ,
790+ check_interval = operator .check_interval ,
791+ timeout = operator .timeout ,
792+ )
793+
794+ def test_on_kill_best_effort_cancellation_does_not_raise (self ):
795+ operator = DbtCloudRunJobOperator (
796+ task_id = TASK_ID ,
797+ dbt_cloud_conn_id = ACCOUNT_ID_CONN ,
798+ job_id = JOB_ID ,
799+ dag = self .dag ,
800+ )
801+
802+ operator .run_id = RUN_ID
803+ operator .hook = MagicMock ()
804+
805+ # Simulate cancellation failure.
806+ operator .hook .cancel_job_run .side_effect = Exception ("Cancellation failed" )
807+
808+ # Simulate confirmation also failing (normal path).
809+ operator .hook .wait_for_job_run_status .side_effect = DbtCloudJobRunException ("Still running" )
810+
811+ operator .on_kill ()
812+
813+ operator .hook .cancel_job_run .assert_called_once_with (
814+ account_id = operator .account_id ,
815+ run_id = RUN_ID ,
816+ )
817+
818+ operator .hook .wait_for_job_run_status .assert_called_once ()
819+
693820 @pytest .mark .parametrize (
694821 ("conn_id" , "account_id" ),
695822 [(ACCOUNT_ID_CONN , None ), (NO_ACCOUNT_ID_CONN , ACCOUNT_ID )],
0 commit comments