|
29 | 29 | HTTP_200_OK, |
30 | 30 | HTTP_404_NOT_FOUND, |
31 | 31 | HTTP_503_SERVICE_UNAVAILABLE, |
| 32 | + HTTP_500_INTERNAL_SERVER_ERROR, |
| 33 | +) |
| 34 | + |
| 35 | +from pygeoapi.process.base import ( |
| 36 | + JobNotFoundError, |
| 37 | + JobResultNotFoundError, |
| 38 | + ProcessorGenericError |
32 | 39 | ) |
33 | 40 |
|
34 | 41 | expected_jobs_test = [ |
|
72 | 79 | "created": "unknown", |
73 | 80 | "updated": "unknown", |
74 | 81 | }, |
| 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 | + }, |
75 | 90 | ] |
76 | 91 |
|
77 | 92 |
|
@@ -306,20 +321,42 @@ async def test_get_job_result( |
306 | 321 | """ |
307 | 322 | # Mock app.extra to ensure 'db_table' exists |
308 | 323 | 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 | + ) |
311 | 346 | 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 | + |
314 | 350 | # Patch app.extra with the mock db_table |
315 | 351 | mocker.patch.object(staging_client.app, "extra", {"process_manager": mock_db_table}) |
316 | 352 |
|
317 | 353 | # 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 |
323 | 360 |
|
324 | 361 |
|
325 | 362 | @pytest.mark.asyncio |
|
0 commit comments