Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.d/233.miscellaneous.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix/bug-in-job-status
9 changes: 7 additions & 2 deletions src/ansys/conceptev/core/ocm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions src/ansys/conceptev/core/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}")
Expand Down
52 changes: 51 additions & 1 deletion tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Loading