From d4dc150dca0abf6b771672a3eebaa004e63c252e Mon Sep 17 00:00:00 2001 From: Tim Pillinger <26465611+wxtim@users.noreply.github.com> Date: Wed, 12 Jun 2024 09:54:07 +0100 Subject: [PATCH] add tests for workflow state xtrigger validation function fix checking for non-int args. --- cylc/flow/xtriggers/workflow_state.py | 4 +-- tests/unit/xtriggers/test_workflow_state.py | 37 +++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/cylc/flow/xtriggers/workflow_state.py b/cylc/flow/xtriggers/workflow_state.py index ca0a96a9aed..a55d5038886 100644 --- a/cylc/flow/xtriggers/workflow_state.py +++ b/cylc/flow/xtriggers/workflow_state.py @@ -114,9 +114,7 @@ def validate(args: Dict[str, Any]): raise WorkflowConfigError( "Full ID needed: workflow//cycle/task[:selector].") - try: - int(args["flow_num"]) - except ValueError: + if not isinstance(args["flow_num"], int): raise WorkflowConfigError("flow_num must be an integer.") diff --git a/tests/unit/xtriggers/test_workflow_state.py b/tests/unit/xtriggers/test_workflow_state.py index ce00fde0c6b..1935ec581e8 100644 --- a/tests/unit/xtriggers/test_workflow_state.py +++ b/tests/unit/xtriggers/test_workflow_state.py @@ -22,11 +22,13 @@ import pytest from cylc.flow.dbstatecheck import output_fallback_msg +from cylc.flow.exceptions import WorkflowConfigError from cylc.flow.rundb import CylcWorkflowDAO from cylc.flow.workflow_files import WorkflowFiles from cylc.flow.xtriggers.workflow_state import ( _workflow_state_backcompat, workflow_state, + validate, ) from cylc.flow.xtriggers.suite_state import suite_state @@ -260,3 +262,38 @@ def test__workflow_state_backcompat(tmp_run_dir: 'Callable'): assert satisfied satisfied, _ = func(id_, 'arrakis', '2012', message='lisan al-gaib') assert satisfied + + +def test_validate_ok(): + """Validate returns ok with valid args.""" + validate({ + 'workflow_task_id': 'foo//1/bar', + 'offset': 'PT1H', + 'flow_num': 44, + }) + + +@pytest.mark.parametrize( + 'id_', (('foo//1'),) +) +def test_validate_fail_bad_id(id_): + """Validation failure for bad id""" + with pytest.raises(WorkflowConfigError, match='Full ID needed'): + validate({ + 'workflow_task_id': id_, + 'offset': 'PT1H', + 'flow_num': 44, + }) + + +@pytest.mark.parametrize( + 'flow_num', ((4.25260), ('Belguim')) +) +def test_validate_fail_non_int_flow(flow_num): + """Validate failur for non integer flow numbers.""" + with pytest.raises(WorkflowConfigError, match='must be an integer'): + validate({ + 'workflow_task_id': 'foo//1/bar', + 'offset': 'PT1H', + 'flow_num': flow_num, + })