From 5faa9bed9de92c11b66afa3f47f55e25a67bdcbc Mon Sep 17 00:00:00 2001 From: jason810496 Date: Tue, 29 Oct 2024 11:37:58 +0800 Subject: [PATCH] fix: add error docs, refactor test case --- .../core_api/openapi/v1-generated.yaml | 18 ++++++++++ .../core_api/routes/public/event_logs.py | 6 +++- .../ui/openapi-gen/requests/services.gen.ts | 3 ++ airflow/ui/openapi-gen/requests/types.gen.ts | 12 +++++++ .../core_api/routes/public/test_event_logs.py | 36 ++++++++----------- 5 files changed, 52 insertions(+), 23 deletions(-) diff --git a/airflow/api_fastapi/core_api/openapi/v1-generated.yaml b/airflow/api_fastapi/core_api/openapi/v1-generated.yaml index 4b07d01399c7..3d1b1611ad8c 100644 --- a/airflow/api_fastapi/core_api/openapi/v1-generated.yaml +++ b/airflow/api_fastapi/core_api/openapi/v1-generated.yaml @@ -1526,6 +1526,24 @@ paths: application/json: schema: $ref: '#/components/schemas/EventLogResponse' + '401': + content: + application/json: + schema: + $ref: '#/components/schemas/HTTPExceptionResponse' + description: Unauthorized + '403': + content: + application/json: + schema: + $ref: '#/components/schemas/HTTPExceptionResponse' + description: Forbidden + '404': + content: + application/json: + schema: + $ref: '#/components/schemas/HTTPExceptionResponse' + description: Not Found '422': description: Validation Error content: diff --git a/airflow/api_fastapi/core_api/routes/public/event_logs.py b/airflow/api_fastapi/core_api/routes/public/event_logs.py index 012797623d89..75f12cbefb03 100644 --- a/airflow/api_fastapi/core_api/routes/public/event_logs.py +++ b/airflow/api_fastapi/core_api/routes/public/event_logs.py @@ -25,6 +25,7 @@ get_session, ) from airflow.api_fastapi.common.router import AirflowRouter +from airflow.api_fastapi.core_api.openapi.exceptions import create_openapi_http_exception_doc from airflow.api_fastapi.core_api.serializers.event_logs import ( EventLogResponse, ) @@ -33,7 +34,10 @@ event_logs_router = AirflowRouter(tags=["Event Log"], prefix="/eventLogs") -@event_logs_router.get("/{event_log_id}") +@event_logs_router.get( + "/{event_log_id}", + responses=create_openapi_http_exception_doc([401, 403, 404]), +) async def get_event_log( event_log_id: int, session: Annotated[Session, Depends(get_session)], diff --git a/airflow/ui/openapi-gen/requests/services.gen.ts b/airflow/ui/openapi-gen/requests/services.gen.ts index e7a52e1637ee..4db1e052a202 100644 --- a/airflow/ui/openapi-gen/requests/services.gen.ts +++ b/airflow/ui/openapi-gen/requests/services.gen.ts @@ -915,6 +915,9 @@ export class EventLogService { event_log_id: data.eventLogId, }, errors: { + 401: "Unauthorized", + 403: "Forbidden", + 404: "Not Found", 422: "Validation Error", }, }); diff --git a/airflow/ui/openapi-gen/requests/types.gen.ts b/airflow/ui/openapi-gen/requests/types.gen.ts index 7c84fd4a07bf..288916b3928e 100644 --- a/airflow/ui/openapi-gen/requests/types.gen.ts +++ b/airflow/ui/openapi-gen/requests/types.gen.ts @@ -1428,6 +1428,18 @@ export type $OpenApiTs = { * Successful Response */ 200: EventLogResponse; + /** + * Unauthorized + */ + 401: HTTPExceptionResponse; + /** + * Forbidden + */ + 403: HTTPExceptionResponse; + /** + * Not Found + */ + 404: HTTPExceptionResponse; /** * Validation Error */ diff --git a/tests/api_fastapi/core_api/routes/public/test_event_logs.py b/tests/api_fastapi/core_api/routes/public/test_event_logs.py index 0204fc5b8f2f..c329015b9f9e 100644 --- a/tests/api_fastapi/core_api/routes/public/test_event_logs.py +++ b/tests/api_fastapi/core_api/routes/public/test_event_logs.py @@ -88,15 +88,10 @@ def setup(self, create_task_instance, session=None) -> dict[str, Log]: owner_display_name=OWNER_DISPLAY_NAME, task_instance=task_instance, ) - session.add(normal_log) - session.add(log_with_owner) - session.add(log_with_task_instance) - session.add(log_with_owner_and_task_instance) + session.add_all( + [normal_log, log_with_owner, log_with_task_instance, log_with_owner_and_task_instance] + ) session.commit() - session.refresh(normal_log) - session.refresh(log_with_owner) - session.refresh(log_with_task_instance) - session.refresh(log_with_owner_and_task_instance) return { EVENT_NORMAL: normal_log, EVENT_WITH_OWNER: log_with_owner, @@ -163,23 +158,20 @@ def test_get_event_log(self, test_client, setup, event_log_key, expected_status_ if expected_status_code != 200: return - resp_json = response.json() - when = resp_json["when"] - logical_date = resp_json["logical_date"] expected_json = { "event_log_id": event_log_id, - "when": when, - "dag_id": None, - "task_id": None, - "run_id": None, + "when": event_log.dttm.isoformat().replace("+00:00", "Z") if event_log.dttm else None, + "dag_id": expected_body.get("dag_id"), + "task_id": expected_body.get("task_id"), + "run_id": expected_body.get("run_id"), "map_index": event_log.map_index, "try_number": event_log.try_number, - "event": None, - "logical_date": logical_date, - "owner": None, - "extra": None, + "event": expected_body.get("event"), + "logical_date": event_log.execution_date.isoformat().replace("+00:00", "Z") + if event_log.execution_date + else None, + "owner": expected_body.get("owner"), + "extra": expected_body.get("extra"), } - for key, value in expected_body.items(): - expected_json[key] = value - assert resp_json == expected_json + assert response.json() == expected_json