From e9354de34f622189210ee1f3bb763f74d476b367 Mon Sep 17 00:00:00 2001 From: Marie Backman Date: Wed, 28 Feb 2024 12:03:50 -0500 Subject: [PATCH 1/2] fix error thrown when task.task_class is None and add unit test --- src/workflow_app/workflow/states.py | 2 +- .../workflow/tests/test_states.py | 24 +++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/workflow_app/workflow/states.py b/src/workflow_app/workflow/states.py index e4ca05a3..6e1ba87a 100644 --- a/src/workflow_app/workflow/states.py +++ b/src/workflow_app/workflow/states.py @@ -53,7 +53,7 @@ def _call_db_task(self, task_data, headers, message): :param message: JSON-encoded message content """ task_def = json.loads(task_data) - if "task_class" in task_def and len(task_def["task_class"].strip()) > 0: + if "task_class" in task_def and (task_def["task_class"] is not None) and len(task_def["task_class"].strip()) > 0: try: toks = task_def["task_class"].strip().split(".") module = ".".join(toks[: len(toks) - 1]) diff --git a/src/workflow_app/workflow/tests/test_states.py b/src/workflow_app/workflow/tests/test_states.py index 9d1397f5..899fda85 100644 --- a/src/workflow_app/workflow/tests/test_states.py +++ b/src/workflow_app/workflow/tests/test_states.py @@ -26,10 +26,26 @@ def test_call_db_task(self): message = "test_msg" sa._call_db_task(task_data, headers, message) - def test_call(self): - # NOTE: the decorator logged_action is preventing unittest mock from - # isolating the functionality for unit test, hence skipping - pass + @mock.patch("workflow.states.transactions.get_task") + def test_call(self, mock_get_task): + # NOTE: skipping testing of importing a task class as this option is not currently used + from workflow.states import StateAction + + mock_connection = mock.Mock() + sa = StateAction(connection=mock_connection, use_db_task=True) + headers = {"destination": "test", "message-id": "test-0"} + message = '{"facility": "SNS", "instrument": "arcs", "ipts": "IPTS-5", "run_number": 3, "data_file": "test"}' + + # test with task class: null + mock_get_task.return_value = '{"task_class": null, "task_queues": ["QUEUE-0", "QUEUE-1"]}' + sa(headers, message) + assert mock_connection.send.call_count == 2 # one per task queue + original_call_count = mock_connection.send.call_count + + # test with task class: empty string + mock_get_task.return_value = '{"task_class": "", "task_queues": ["QUEUE-0", "QUEUE-1"]}' + sa(headers, message) + assert mock_connection.send.call_count - original_call_count == 2 # one per task queue @mock.patch("workflow.database.transactions.add_status_entry") def test_send(self, mockAddStatusEntry): From 69c4d49ab001f39227c57a5601df420a585845e1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 28 Feb 2024 17:12:34 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/workflow_app/workflow/states.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/workflow_app/workflow/states.py b/src/workflow_app/workflow/states.py index 6e1ba87a..d4deef0f 100644 --- a/src/workflow_app/workflow/states.py +++ b/src/workflow_app/workflow/states.py @@ -53,7 +53,11 @@ def _call_db_task(self, task_data, headers, message): :param message: JSON-encoded message content """ task_def = json.loads(task_data) - if "task_class" in task_def and (task_def["task_class"] is not None) and len(task_def["task_class"].strip()) > 0: + if ( + "task_class" in task_def + and (task_def["task_class"] is not None) + and len(task_def["task_class"].strip()) > 0 + ): try: toks = task_def["task_class"].strip().split(".") module = ".".join(toks[: len(toks) - 1])