Skip to content

Commit fe282d9

Browse files
committed
get_specific_job_result_endpoint update
1 parent ba6804c commit fe282d9

File tree

2 files changed

+70
-14
lines changed

2 files changed

+70
-14
lines changed

services/staging/rs_server_staging/main.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
from fastapi import APIRouter, FastAPI, HTTPException, Path
2626
from pygeoapi.api import API
2727
from pygeoapi.process.manager.postgresql import PostgreSQLManager
28+
from pygeoapi.process.base import (
29+
JobNotFoundError,
30+
JobResultNotFoundError,
31+
ProcessorGenericError
32+
)
2833
from pygeoapi.provider.postgresql import get_engine
2934
from rs_server_common.authentication.authentication_to_external import (
3035
init_rs_server_config_yaml,
@@ -44,6 +49,7 @@
4449
HTTP_200_OK,
4550
HTTP_404_NOT_FOUND,
4651
HTTP_503_SERVICE_UNAVAILABLE,
52+
HTTP_500_INTERNAL_SERVER_ERROR,
4753
)
4854

4955
# flake8: noqa: F401
@@ -314,12 +320,25 @@ async def delete_job_endpoint(job_id: str = Path(..., title="The ID of the job t
314320
@router.get("/jobs/{job_id}/results")
315321
async def get_specific_job_result_endpoint(job_id: str = Path(..., title="The ID of the job")):
316322
"""Get result from a specific job."""
317-
# Query the database to find the job by job_id
318-
job = app.extra["process_manager"].get_job(job_id)
319-
if job:
323+
try:
324+
# Query the database to find the job by job_id
325+
job = app.extra["process_manager"].get_job(job_id)
320326
return JSONResponse(status_code=HTTP_200_OK, content=job["status"])
321-
322-
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail=f"Job with ID {job_id} not found")
327+
except JobNotFoundError:
328+
# Handle case when job_id is not found
329+
return JSONResponse(
330+
status_code=HTTP_404_NOT_FOUND,
331+
content={
332+
"title": "No Such Job",
333+
"detail": f"Job with ID {job_id} not found."
334+
}
335+
)
336+
except Exception as e:
337+
# Catch any other unexpected exceptions
338+
return JSONResponse(
339+
status_code=HTTP_500_INTERNAL_SERVER_ERROR,
340+
content={"title": "Internal Server Error", "detail": str(e)}
341+
)
323342

324343

325344
# Configure OpenTelemetry

services/staging/tests/test_staging.py

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929
HTTP_200_OK,
3030
HTTP_404_NOT_FOUND,
3131
HTTP_503_SERVICE_UNAVAILABLE,
32+
HTTP_500_INTERNAL_SERVER_ERROR,
33+
)
34+
35+
from pygeoapi.process.base import (
36+
JobNotFoundError,
37+
JobResultNotFoundError,
38+
ProcessorGenericError
3239
)
3340

3441
expected_jobs_test = [
@@ -72,6 +79,14 @@
7279
"created": "unknown",
7380
"updated": "unknown",
7481
},
82+
{
83+
"identifier": "trigger_500",
84+
"status": "error",
85+
"progress": 0.0,
86+
"message": "This job will trigger an internal server error.",
87+
"created": "unknown",
88+
"updated": "unknown",
89+
},
7590
]
7691

7792

@@ -306,20 +321,42 @@ async def test_get_job_result(
306321
"""
307322
# Mock app.extra to ensure 'db_table' exists
308323
mock_db_table = mocker.MagicMock()
309-
try:
310-
job_index = next(i for i, job in enumerate(mock_jobs) if job["identifier"] == expected_job["identifier"])
324+
325+
if expected_job["identifier"] == "non_existing":
326+
# Simulate JobNotFoundError for non-existing jobs (HTTP 404)
327+
mock_db_table.get_job.side_effect = JobNotFoundError
328+
expected_status = HTTP_404_NOT_FOUND
329+
expected_response = {
330+
"title": "No Such Job",
331+
"detail": f"Job with ID {expected_job['identifier']} not found.",
332+
}
333+
elif expected_job["identifier"] == "trigger_500":
334+
# Simulate an unexpected exception (HTTP 500)
335+
mock_db_table.get_job.side_effect = Exception("Unexpected error occurred")
336+
expected_status = HTTP_500_INTERNAL_SERVER_ERROR
337+
expected_response = {
338+
"title": "Internal Server Error",
339+
"detail": "Unexpected error occurred",
340+
}
341+
else:
342+
# Return an existing job normally (HTTP 200)
343+
job_index = next(
344+
i for i, job in enumerate(mock_jobs) if job["identifier"] == expected_job["identifier"]
345+
)
311346
mock_db_table.get_job.return_value = mock_jobs[job_index]
312-
except StopIteration:
313-
mock_db_table.get_job.return_value = []
347+
expected_status = HTTP_200_OK
348+
expected_response = expected_job["status"]
349+
314350
# Patch app.extra with the mock db_table
315351
mocker.patch.object(staging_client.app, "extra", {"process_manager": mock_db_table})
316352

317353
# Call the API
318-
response = staging_client.get(f"/jobs/{expected_job['identifier']}/results")
319-
# assert response is OK and job info match, or not found for last case
320-
assert (
321-
response.status_code == HTTP_200_OK and response.json() == expected_job["status"]
322-
) or response.status_code == HTTP_404_NOT_FOUND
354+
job_id = expected_job.get("identifier")
355+
response = staging_client.get(f"/jobs/{job_id}/results")
356+
357+
# Assert response status code and content
358+
assert response.status_code == expected_status
359+
assert response.json() == expected_response
323360

324361

325362
@pytest.mark.asyncio

0 commit comments

Comments
 (0)