diff --git a/doc/changelog.d/233.miscellaneous.md b/doc/changelog.d/233.miscellaneous.md new file mode 100644 index 00000000..0503d541 --- /dev/null +++ b/doc/changelog.d/233.miscellaneous.md @@ -0,0 +1 @@ +Fix/bug-in-job-status \ No newline at end of file diff --git a/src/ansys/conceptev/core/ocm.py b/src/ansys/conceptev/core/ocm.py index 9b2e3492..df004f59 100644 --- a/src/ansys/conceptev/core/ocm.py +++ b/src/ansys/conceptev/core/ocm.py @@ -252,8 +252,13 @@ def get_status(job_info: dict, token: str) -> str: headers={"Authorization": token}, ) processed_response = process_response(response) - initial_status = processed_response["jobStatus"][-1]["jobStatus"] - return initial_status + if "finalStatus" in processed_response and processed_response["finalStatus"] is not None: + status = processed_response["finalStatus"].upper() + elif "lastStatus" in processed_response and processed_response["lastStatus"] is not None: + status = processed_response["lastStatus"].upper() + else: + raise ResponseError(f"Failed to get job status {processed_response}.") + return status def get_project_ids(name: str, account_id: str, token: str) -> dict: diff --git a/src/ansys/conceptev/core/progress.py b/src/ansys/conceptev/core/progress.py index 30acdddd..fa219b89 100644 --- a/src/ansys/conceptev/core/progress.py +++ b/src/ansys/conceptev/core/progress.py @@ -38,9 +38,9 @@ else: import async_timeout -STATUS_COMPLETE = "complete" +STATUS_COMPLETE = "COMPLETED" STATUS_FINISHED = "FINISHED" -STATUS_ERROR = "failed" +STATUS_ERROR = "FAILED" OCM_SOCKET_URL = settings.ocm_socket_url JOB_TIMEOUT = settings.job_timeout ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) @@ -62,7 +62,7 @@ def get_status(message: str, job_id: str): if message_type == "status": status = message_data.get("status", None) print(f"Status:{status}") - return status + return status.upper() elif message_type == "progress": progress = message_data.get("progress", None) print(f"Progress:{progress}") diff --git a/tests/test_app.py b/tests/test_app.py index a88f4876..d46d0ea2 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -27,6 +27,13 @@ from ansys.conceptev.core import app from ansys.conceptev.core.auth import AnsysIDAuth +from ansys.conceptev.core.exceptions import ResponseError +from ansys.conceptev.core.progress import ( + STATUS_COMPLETE, + STATUS_ERROR, + STATUS_FINISHED, + check_status, +) from ansys.conceptev.core.settings import settings conceptev_url = settings.conceptev_url @@ -352,7 +359,9 @@ def test_read_results(httpx_mock: HTTPXMock, client: httpx.Client): url=ocm_url + "/user/details", method="post", json={"userId": "user_123"} ) httpx_mock.add_response( - url=ocm_url + "/job/load", method="post", json={"jobStatus": [{"jobStatus": "complete"}]} + url=ocm_url + "/job/load", + method="post", + json={"finalStatus": "COMPLETED", "jobStatus": [{"jobStatus": "complete"}]}, ) results = app.read_results(client, example_job_info) assert example_results == results @@ -493,3 +502,44 @@ def test_get_concept(httpx_mock: HTTPXMock, client: httpx.Client): "requirements": [{"name": "reequirements"}], "architecture": {"name": "architecture"}, } + + +statuses = [STATUS_COMPLETE, STATUS_FINISHED, STATUS_ERROR, None] + + +@pytest.mark.parametrize("last_status", statuses) +@pytest.mark.parametrize("final_status", statuses) +def test_returns_final_status_when_present(mocker, final_status, last_status): + job_info = {"job_id": "123"} + token = "token" + mock_response = mocker.Mock() + mock_response.status_code = 200 + mock_response.json.return_value = {} + if final_status is not None: + mock_response.json.return_value["finalStatus"] = final_status + if last_status is not None: + mock_response.json.return_value["lastStatus"] = last_status + mocker.patch("httpx.post", return_value=mock_response) + + if final_status is None and last_status is None: + with pytest.raises(ResponseError) as exc: + result = app.get_status(job_info, token) + return True + else: + result = app.get_status(job_info, token) + assert result in [final_status, last_status] + + +@pytest.mark.parametrize( + "result,expected", + [(STATUS_COMPLETE, True), (STATUS_FINISHED, True), (STATUS_ERROR, False), (None, False)], +) +def test_check_status(result, expected): + if expected: + assert check_status(result) + elif result is STATUS_ERROR: + with pytest.raises(Exception) as exc: + check_status(result) + assert "Job Failed" in str(exc.value) if result == STATUS_ERROR else True + else: + assert not check_status(result)