SameerMesiah97 commented on code in PR #77633:
URL: https://github.com/apache/airflow/pull/77633#discussion_r4712181392
##########
airflow-core/tests/unit/api_fastapi/execution_api/versions/head/test_xcoms.py:
##########
@@ -1841,6 +1841,33 @@ def test_xcom_dag_result(self, client,
create_task_instance, authenticate_as, se
dag_result = XComModelV2.get_for_attempt(ti.id, "return_value",
session=session).dag_result
assert dag_result is True
+ def test_set_xcom_value_error_returns_rfc9457(self, client,
create_task_instance, authenticate_as, session):
+ """
+ When XComModel.set_for_attempt raises ValueError the endpoint must
return a 404
+ response whose ``detail`` field is an RFC 9457-compliant dict
containing the
+ ``reason`` and ``message`` keys, not a bare string.
+
+ Regression test for issue #70044.
+ """
+ ti = create_task_instance()
+ session.commit()
+ authenticate_as(ti)
+
+ with patch(
+
"airflow.api_fastapi.execution_api.routes.xcoms.XComModel.set_for_attempt",
+ side_effect=ValueError("unknown task instance id"),
+ ):
+ response = client.post(
+
f"/execution/xcoms/{ti.dag_id}/{ti.run_id}/{ti.task_id}/xcom_1",
+ json='"value"',
+ )
+
+ assert response.status_code == 404
+ detail = response.json()["detail"]
+ assert isinstance(detail, dict), f"detail must be a dict (RFC 9457),
got {type(detail)}: {detail!r}"
+ assert detail["reason"] == "not_found"
+ assert detail["message"] == "unknown task instance id"
Review Comment:
1) Could be less RFC standard specific
2) Docstring is unnecessarily long
3) Individual assertions could be consolidated.
Please see the suggestion below:
```
def test_set_xcom_handles_value_error(
self, client, create_task_instance, authenticate_as, session
):
"""Test that setting an XCom returns a 404 response when a ValueError is
raised."""
ti = create_task_instance()
session.commit()
authenticate_as(ti)
with patch(
"airflow.api_fastapi.execution_api.routes.xcoms.XComModel.set_for_attempt",
side_effect=ValueError("unknown task instance id"),
):
response = client.post(
f"/execution/xcoms/{ti.dag_id}/{ti.run_id}/{ti.task_id}/xcom_1",
json="value",
)
assert response.status_code == 404
assert response.json()["detail"] == {
"reason": "not_found",
"message": "unknown task instance id",
}
```
--
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]