From 6dd03e24bcdf7a9f764f1b776a6912034326d1d4 Mon Sep 17 00:00:00 2001 From: John Jewell Date: Thu, 18 Apr 2024 14:27:57 -0400 Subject: [PATCH] Add list class method to JobStatus Enum. More informative error message. --- florist/api/db/entities.py | 9 +++++++++ florist/api/routes/server/job.py | 4 ++-- florist/tests/integration/api/routes/server/test_job.py | 3 ++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/florist/api/db/entities.py b/florist/api/db/entities.py index dd936f36..4569779a 100644 --- a/florist/api/db/entities.py +++ b/florist/api/db/entities.py @@ -22,6 +22,15 @@ class JobStatus(Enum): FINISHED_WITH_ERROR = "FINISHED_WITH_ERROR" FINISHED_SUCCESSFULLY = "FINISHED_SUCCESSFULLY" + @classmethod + def list(cls) -> List[str]: + """ + List all the supported clients. + + :return: (List[str]) a list of valid job statuses. + """ + return [status.value for status in JobStatus] + class ClientInfo(BaseModel): """Define the information of an FL client.""" diff --git a/florist/api/routes/server/job.py b/florist/api/routes/server/job.py index 74250a44..876068c0 100644 --- a/florist/api/routes/server/job.py +++ b/florist/api/routes/server/job.py @@ -63,8 +63,8 @@ async def list_jobs_with_status(status: JobStatus, request: Request) -> List[Dic status = jsonable_encoder(status) assert isinstance(status, str) - if status not in JobStatus._member_names_: - msg = f"status is not valid. status: {status}." + if status not in JobStatus.list(): + msg = f"status {status} is not valid. Valid statuses: {JobStatus.list()}" raise HTTPException(status_code=400, detail=msg) job_db = request.app.database[JOB_DATABASE_NAME] diff --git a/florist/tests/integration/api/routes/server/test_job.py b/florist/tests/integration/api/routes/server/test_job.py index c6ea2745..45da7875 100644 --- a/florist/tests/integration/api/routes/server/test_job.py +++ b/florist/tests/integration/api/routes/server/test_job.py @@ -331,4 +331,5 @@ async def test_list_jobs_with_invalid_status(mock_request) -> None: await list_jobs_with_status("NON_EXISTENT_STATUS", mock_request) assert exception_info.value.status_code == 400 - assert "status is not valid. status: NON_EXISTENT_STATUS" in exception_info.value.detail + exc_str = f"status NON_EXISTENT_STATUS is not valid. Valid statuses: {JobStatus.list()}" + assert exc_str in exception_info.value.detail