Skip to content

Commit 06e0ad0

Browse files
committed
fix delete_job_endpoint and get_jobs_job_id with wrong_id
1 parent 62ba529 commit 06e0ad0

File tree

2 files changed

+48
-23
lines changed

2 files changed

+48
-23
lines changed

services/staging/rs_server_staging/main.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,20 @@ async def execute_process(req: Request, resource: str, data: ProcessMetadataMode
286286
@router.get("/jobs/{job_id}")
287287
async def get_job_status_endpoint(job_id: str = Path(..., title="The ID of the job")):
288288
"""Used to get status of processing job."""
289-
job = app.extra["process_manager"].get_job(job_id)
290-
if job:
291-
return job
292-
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail=f"Job with ID {job_id} not found")
289+
try:
290+
return app.extra["process_manager"].get_job(job_id)
291+
except JobNotFoundError:
292+
# Handle case when job_id is not found
293+
return JSONResponse(
294+
status_code=HTTP_404_NOT_FOUND,
295+
content={"title": "No Such Job", "detail": f"Job with ID {job_id} not found"},
296+
)
297+
except Exception as e: # pylint: disable=broad-exception-caught
298+
# Catch any other unexpected exceptions
299+
return JSONResponse(
300+
status_code=HTTP_500_INTERNAL_SERVER_ERROR,
301+
content={"title": "Internal Server Error", "detail": str(e)},
302+
)
293303

294304

295305
@router.get("/jobs")
@@ -305,10 +315,21 @@ async def get_jobs_endpoint():
305315
@router.delete("/jobs/{job_id}")
306316
async def delete_job_endpoint(job_id: str = Path(..., title="The ID of the job to delete")):
307317
"""Deletes a specific job from the database."""
308-
success = app.extra["process_manager"].delete_job(job_id)
309-
if success:
318+
try:
319+
app.extra["process_manager"].delete_job(job_id)
310320
return {"message": f"Job {job_id} deleted successfully"}
311-
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail=f"Job with ID {job_id} not found")
321+
except JobNotFoundError:
322+
# Handle case when job_id is not found
323+
return JSONResponse(
324+
status_code=HTTP_404_NOT_FOUND,
325+
content={"title": "No Such Job", "detail": f"Job with ID {job_id} not found"},
326+
)
327+
except Exception as e: # pylint: disable=broad-exception-caught
328+
# Catch any other unexpected exceptions
329+
return JSONResponse(
330+
status_code=HTTP_500_INTERNAL_SERVER_ERROR,
331+
content={"title": "Internal Server Error", "detail": str(e)},
332+
)
312333

313334

314335
@router.get("/jobs/{job_id}/results")
@@ -322,7 +343,7 @@ async def get_specific_job_result_endpoint(job_id: str = Path(..., title="The ID
322343
# Handle case when job_id is not found
323344
return JSONResponse(
324345
status_code=HTTP_404_NOT_FOUND,
325-
content={"title": "No Such Job", "detail": f"Job with ID {job_id} not found."},
346+
content={"title": "No Such Job", "detail": f"Job with ID {job_id} not found"},
326347
)
327348
except Exception as e: # pylint: disable=broad-exception-caught
328349
# Catch any other unexpected exceptions

services/staging/tests/test_staging.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ async def test_get_jobs_endpoint(mocker, set_db_env_var, staging_client): # pyl
226226
(
227227
{"identifier": "non_existing_id"},
228228
HTTP_404_NOT_FOUND,
229-
{"title": "No Such Job", "detail": "Job with ID non_existing_id not found."},
229+
{"title": "No Such Job", "detail": "Job with ID non_existing_id not found"},
230230
),
231231
(
232232
{"identifier": "trigger_500"},
@@ -266,14 +266,14 @@ async def test_get_job(
266266
# Mock app.extra to ensure 'db_table' exists
267267
mock_db_table = mocker.MagicMock()
268268

269+
# Simulate JobNotFoundError for non-existing jobs (HTTP 404)
269270
if expected_status == HTTP_404_NOT_FOUND:
270-
# Simulate JobNotFoundError for non-existing jobs (HTTP 404)
271271
mock_db_table.get_job.side_effect = JobNotFoundError
272+
# Simulate an unexpected exception (HTTP 500)
272273
elif expected_status == HTTP_500_INTERNAL_SERVER_ERROR:
273-
# Simulate an unexpected exception (HTTP 500)
274274
mock_db_table.get_job.side_effect = Exception("Unexpected error occurred")
275+
# Return an existing job normally (HTTP 200)
275276
else:
276-
# Return an existing job normally (HTTP 200)
277277
mock_db_table.get_job.return_value = next(
278278
job for job in mock_jobs if job["identifier"] == expected_job["identifier"]
279279
)
@@ -296,7 +296,7 @@ async def test_get_job(
296296
(
297297
{"identifier": "non_existing_id"},
298298
HTTP_404_NOT_FOUND,
299-
{"title": "No Such Job", "detail": "Job with ID non_existing_id not found."},
299+
{"title": "No Such Job", "detail": "Job with ID non_existing_id not found"},
300300
),
301301
(
302302
{"identifier": "trigger_500"},
@@ -336,14 +336,14 @@ async def test_get_job_result(
336336
# Mock app.extra to ensure 'db_table' exists
337337
mock_db_table = mocker.MagicMock()
338338

339+
# Simulate JobNotFoundError for non-existing jobs (HTTP 404)
339340
if expected_status == HTTP_404_NOT_FOUND:
340-
# Simulate JobNotFoundError for non-existing jobs (HTTP 404)
341341
mock_db_table.get_job.side_effect = JobNotFoundError
342+
# Simulate an unexpected exception (HTTP 500)
342343
elif expected_status == HTTP_500_INTERNAL_SERVER_ERROR:
343-
# Simulate an unexpected exception (HTTP 500)
344344
mock_db_table.get_job.side_effect = Exception("Unexpected error occurred")
345+
# Return an existing job normally (HTTP 200)
345346
else:
346-
# Return an existing job normally (HTTP 200)
347347
mock_db_table.get_job.return_value = next(
348348
job for job in mock_jobs if job["identifier"] == expected_job["identifier"]
349349
)
@@ -367,7 +367,7 @@ async def test_get_job_result(
367367
(
368368
{"identifier": "non_existing_id"},
369369
HTTP_404_NOT_FOUND,
370-
{"title": "No Such Job", "detail": "Job with ID non_existing_id not found."},
370+
{"title": "No Such Job", "detail": "Job with ID non_existing_id not found"},
371371
),
372372
(
373373
{"identifier": "trigger_500"},
@@ -399,25 +399,29 @@ async def test_delete_job_endpoint(
399399
Args:
400400
mocker: A mocker object used to create mocks and patches for testing.
401401
staging_client: A test client for making requests to the FastAPI application.
402+
mock_jobs: Fixture used to mock output of tiny db jobs
402403
expected_job (dict): The expected job dictionary containing job_id,
403404
status, progress, and message for the job to be deleted.
405+
expected_status: response HTTP status code
406+
expected_response: response body (JSON object)
404407
405408
Assertions:
406409
- Asserts that the response status code is 200 if the job is successfully deleted.
407410
- Asserts that the response status code is 404 if the job does not exist.
411+
- Asserts that the response status code is 500 if other exception occurs.
408412
"""
409413
# Mock app.extra to ensure 'db_table' exists
410414
mock_db_table = mocker.MagicMock()
411415

416+
# Simulate JobNotFoundError for non-existing jobs (HTTP 404)
412417
if expected_status == HTTP_404_NOT_FOUND:
413-
# Simulate JobNotFoundError for non-existing jobs (HTTP 404)
414-
mock_db_table.get_job.side_effect = JobNotFoundError
418+
mock_db_table.delete_job.side_effect = JobNotFoundError
419+
# Simulate an unexpected exception (HTTP 500)
415420
elif expected_status == HTTP_500_INTERNAL_SERVER_ERROR:
416-
# Simulate an unexpected exception (HTTP 500)
417-
mock_db_table.get_job.side_effect = Exception("Unexpected error occurred")
421+
mock_db_table.delete_job.side_effect = Exception("Unexpected error occurred")
422+
# Return an existing job normally (HTTP 200)
418423
else:
419-
# Return an existing job normally (HTTP 200)
420-
mock_db_table.get_job.return_value = next(
424+
mock_db_table.delete_job.return_value = next(
421425
job for job in mock_jobs if job["identifier"] == expected_job["identifier"]
422426
)
423427

0 commit comments

Comments
 (0)