| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
from dataclasses import dataclass |
| 18 |
from inspect import isclass |
| 19 |
import re |
| 20 |
|
| 21 |
import graphene |
| 22 |
import pytest |
| 23 |
|
| 24 |
from cylc.flow.cfgspec.workflow import SPEC as WORKFLOW_SPEC |
| 25 |
from cylc.flow.network.schema import ( |
| 26 |
RUNTIME_FIELD_TO_CFG_MAP, |
| 27 |
Mutations, |
| 28 |
Runtime, |
| 29 |
WorkflowStopMode, |
| 30 |
runtime_schema_to_cfg, |
| 31 |
sort_elements, |
| 32 |
SortArgs, |
| 33 |
) |
| 34 |
from cylc.flow.workflow_status import StopMode, WorkflowStatus |
| 35 |
|
| 36 |
|
| 37 |
@dataclass |
| 38 |
class DummyObject: |
| 39 |
value: int |
| 40 |
|
| 41 |
|
| 42 |
@pytest.mark.parametrize( |
| 43 |
'elements,sort_args,expected_result', |
| 44 |
[ |
| 45 |
# sort asc by key |
| 46 |
( |
| 47 |
[DummyObject(1), DummyObject(3), DummyObject(2)], |
| 48 |
{ |
| 49 |
'keys': ['value'], |
| 50 |
'reverse': False # NOTE: GraphQL ensures reverse is not None! |
| 51 |
}, |
| 52 |
[DummyObject(1), DummyObject(2), DummyObject(3)] |
| 53 |
), |
| 54 |
# sort desc by key |
| 55 |
( |
| 56 |
[DummyObject(1), DummyObject(3), DummyObject(2)], |
| 57 |
{ |
| 58 |
'keys': ['value'], |
| 59 |
'reverse': True |
| 60 |
}, |
| 61 |
[DummyObject(3), DummyObject(2), DummyObject(1)] |
| 62 |
), |
| 63 |
# raise error when no keys given |
| 64 |
( |
| 65 |
[DummyObject(1), DummyObject(3), DummyObject(2)], |
| 66 |
{ |
| 67 |
'keys': [], |
| 68 |
'reverse': True |
| 69 |
}, |
| 70 |
ValueError |
| 71 |
), |
| 72 |
# raise error when any of the keys given are not in the schema |
| 73 |
( |
| 74 |
[DummyObject(1), DummyObject(3), DummyObject(2)], |
| 75 |
{ |
| 76 |
'keys': ['value', 'river_name'], |
| 77 |
'reverse': True |
| 78 |
}, |
| 79 |
ValueError |
| 80 |
) |
| 81 |
] |
| 82 |
) |
| 83 |
def test_sort_args(elements, sort_args, expected_result): |
| 84 |
"""Test the sorting function used by the schema.""" |
| 85 |
sort = SortArgs() |
| 86 |
sort.keys = sort_args['keys'] |
| 87 |
sort.reverse = sort_args['reverse'] |
| 88 |
args = { |
| 89 |
'sort': sort |
| 90 |
} |
| 91 |
if isclass(expected_result): |
| 92 |
with pytest.raises(expected_result): |
| 93 |
sort_elements(elements, args) |
| 94 |
else: |
| 95 |
sort_elements(elements, args) |
| 96 |
assert elements == expected_result |
| 97 |
|
| 98 |
|
| 99 |
def test_runtime_field_to_cfg_map(): |
| 100 |
"""Ensure the Runtime type's fields can be mapped back to the workflow |
| 101 |
config.""" |
| 102 |
assert set(RUNTIME_FIELD_TO_CFG_MAP) == set(Runtime._meta.fields) |
| 103 |
for cfg_name in RUNTIME_FIELD_TO_CFG_MAP.values(): |
| 104 |
assert WORKFLOW_SPEC.get('runtime', '__MANY__', cfg_name) |
| 105 |
|
| 106 |
|
| 107 |
@pytest.mark.parametrize('runtime_dict,expected', [ |
| 108 |
pytest.param( |
| 109 |
{'run_mode': 'Skip'}, {'run mode': 'skip'}, id='edit-runtime' |
| 110 |
), |
| 111 |
pytest.param( |
| 112 |
{'run mode': 'skip'}, {'run mode': 'skip'}, id='broadcast' |
| 113 |
), |
| 114 |
]) |
| 115 |
def test_runtime_schema_to_cfg(runtime_dict, expected): |
| 116 |
"""Test this function can handle Edit Runtime submitted values as well |
| 117 |
as normal broadcast values.""" |
| 118 |
assert runtime_schema_to_cfg(runtime_dict) == expected |
| 119 |
|
| 120 |
|
| 121 |
@pytest.mark.parametrize('mutation', ( |
| 122 |
pytest.param(attr, id=name) |
| 123 |
for name, attr in Mutations.__dict__.items() |
| 124 |
if isinstance(attr, graphene.Field) |
| 125 |
)) |
| 126 |
def test_mutations_valid_for(mutation): |
| 127 |
"""Check that all mutations have a "Valid for" in their description. |
| 128 |
|
| 129 |
This is needed by the UI to disable mutations that are not valid for the |
| 130 |
workflow state. |
| 131 |
""" |
| 132 |
match = re.search( |
| 133 |
r'Valid for:\s(.*)\sworkflows.', mutation.description |
| 134 |
) |
| 135 |
assert match |
| 136 |
valid_states = set(match.group(1).split(', ')) |
| 137 |
assert valid_states |
| 138 |
assert not valid_states.difference(i.value for i in WorkflowStatus) |
| 139 |
|
| 140 |
|
| 141 |
@pytest.mark.parametrize('wflow_stop_mode', list(WorkflowStopMode)) |
| 142 |
def test_stop_mode_enum(wflow_stop_mode): |
| 143 |
"""Check that WorkflowStopMode is a subset of StopMode.""" |
| 144 |
assert StopMode(wflow_stop_mode.value) |
| 145 |
assert wflow_stop_mode.description |