egotist0 commented on code in PR #75430:
URL: https://github.com/apache/airflow/pull/75430#discussion_r4366133021
##########
providers/google/tests/unit/google/cloud/operators/test_bigquery.py:
##########
@@ -2306,6 +2306,101 @@ def
test_handle_job_error_raises_on_error_result_or_error(self, caplog):
not AIRFLOW_V_3_3_PLUS, reason="task_state_store (durable execution)
requires Airflow 3.3+"
)
class TestBigQueryInsertJobOperatorDurable:
+ @pytest.mark.parametrize("status", ["RUNNING", "success", "error",
"not_found"])
+
@mock.patch("airflow.providers.google.cloud.operators.bigquery.BigQueryHook")
+ def test_recovers_returned_location_after_worker_failure(self, mock_hook:
MagicMock, status: str) -> None:
+ values: dict = {}
+ task_store = MagicMock(spec_set=["get", "set"])
+ task_store.get.side_effect = values.get
+ task_store.set.side_effect = values.__setitem__
+ hook = mock_hook.return_value
+ hook.generate_job_id.return_value = "original-job"
+ job = MagicMock(
+ spec=QueryJob,
+ job_id="original-job",
+ location="asia-northeast3",
+ state="RUNNING",
+ error_result=None,
+ )
+ job.result.side_effect = RuntimeError("worker failed")
+ hook.insert_job.return_value = job
+ op = self._make_operator()
+ op.location = None
+
+ with pytest.raises(RuntimeError, match="worker failed"):
+ op.execute(self._context(task_store))
+
+ assert values == {
+ "bigquery_job_id": "original-job",
+ "bigquery_job_location": {"job_id": "original-job", "location":
"asia-northeast3"},
+ }
+ hook.insert_job.reset_mock()
+ job.result.side_effect = lambda **_: setattr(job, "state", "DONE")
+ job.state = "RUNNING" if status == "RUNNING" else "DONE"
+ job.error_result = "failed" if status == "error" else None
+ hook.get_job.return_value = job
+ if status == "not_found":
+ hook.get_job.side_effect = NotFound("gone")
+ hook.generate_job_id.return_value = "replacement-job"
+ hook.insert_job.return_value = MagicMock(
+ spec=QueryJob, job_id="replacement-job", location="EU",
state="DONE", error_result=None
+ )
+ op = self._make_operator()
+ op.location = "EU" if status in ("error", "not_found") else None
+
+ result = op.execute(self._context(task_store))
+
+ hook.get_job.assert_called_once_with(
+ project_id=TEST_GCP_PROJECT_ID, location="asia-northeast3",
job_id="original-job"
+ )
+ if status in ("error", "not_found"):
+ assert result == "replacement-job"
+ assert hook.insert_job.call_args.kwargs["location"] == "EU"
+ assert values["bigquery_job_location"] == {"job_id":
"replacement-job", "location": "EU"}
+ else:
+ assert result == "original-job"
+ hook.insert_job.assert_not_called()
+
+ @pytest.mark.parametrize(
+ "saved_location",
+ [
+ None,
+ {"job_id": "other-job", "location": "asia-northeast3"},
+ {"job_id": "stored-job-id", "location": None},
+ ],
+ )
+
@mock.patch("airflow.providers.google.cloud.operators.bigquery.BigQueryHook")
+ def test_recovery_falls_back_without_matching_location(
Review Comment:
Thanks for pointing this out. I've updated the tests, and all four revised
cases now fail without the fix
--
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]