File: test_schema.py

package info (click to toggle)
cylc-flow 8.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,368 kB
  • sloc: python: 87,751; sh: 17,109; sql: 233; xml: 171; javascript: 78; lisp: 55; makefile: 11
file content (145 lines) | stat: -rw-r--r-- 4,540 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
# Copyright (C) NIWA & British Crown (Met Office) & Contributors.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from dataclasses import dataclass
from inspect import isclass
import re

import graphene
import pytest

from cylc.flow.cfgspec.workflow import SPEC as WORKFLOW_SPEC
from cylc.flow.network.schema import (
    RUNTIME_FIELD_TO_CFG_MAP,
    Mutations,
    Runtime,
    WorkflowStopMode,
    runtime_schema_to_cfg,
    sort_elements,
    SortArgs,
)
from cylc.flow.workflow_status import StopMode, WorkflowStatus


@dataclass
class DummyObject:
    value: int


@pytest.mark.parametrize(
    'elements,sort_args,expected_result',
    [
        # sort asc by key
        (
            [DummyObject(1), DummyObject(3), DummyObject(2)],
            {
                'keys': ['value'],
                'reverse': False  # NOTE: GraphQL ensures reverse is not None!
            },
            [DummyObject(1), DummyObject(2), DummyObject(3)]
        ),
        # sort desc by key
        (
            [DummyObject(1), DummyObject(3), DummyObject(2)],
            {
                'keys': ['value'],
                'reverse': True
            },
            [DummyObject(3), DummyObject(2), DummyObject(1)]
        ),
        # raise error when no keys given
        (
            [DummyObject(1), DummyObject(3), DummyObject(2)],
            {
                'keys': [],
                'reverse': True
            },
            ValueError
        ),
        # raise error when any of the keys given are not in the schema
        (
            [DummyObject(1), DummyObject(3), DummyObject(2)],
            {
                'keys': ['value', 'river_name'],
                'reverse': True
            },
            ValueError
        )
    ]
)
def test_sort_args(elements, sort_args, expected_result):
    """Test the sorting function used by the schema."""
    sort = SortArgs()
    sort.keys = sort_args['keys']
    sort.reverse = sort_args['reverse']
    args = {
        'sort': sort
    }
    if isclass(expected_result):
        with pytest.raises(expected_result):
            sort_elements(elements, args)
    else:
        sort_elements(elements, args)
        assert elements == expected_result


def test_runtime_field_to_cfg_map():
    """Ensure the Runtime type's fields can be mapped back to the workflow
    config."""
    assert set(RUNTIME_FIELD_TO_CFG_MAP) == set(Runtime._meta.fields)
    for cfg_name in RUNTIME_FIELD_TO_CFG_MAP.values():
        assert WORKFLOW_SPEC.get('runtime', '__MANY__', cfg_name)


@pytest.mark.parametrize('runtime_dict,expected', [
    pytest.param(
        {'run_mode': 'Skip'}, {'run mode': 'skip'}, id='edit-runtime'
    ),
    pytest.param(
        {'run mode': 'skip'}, {'run mode': 'skip'}, id='broadcast'
    ),
])
def test_runtime_schema_to_cfg(runtime_dict, expected):
    """Test this function can handle Edit Runtime submitted values as well
    as normal broadcast values."""
    assert runtime_schema_to_cfg(runtime_dict) == expected


@pytest.mark.parametrize('mutation', (
    pytest.param(attr, id=name)
    for name, attr in Mutations.__dict__.items()
    if isinstance(attr, graphene.Field)
))
def test_mutations_valid_for(mutation):
    """Check that all mutations have a "Valid for" in their description.

    This is needed by the UI to disable mutations that are not valid for the
    workflow state.
    """
    match = re.search(
        r'Valid for:\s(.*)\sworkflows.', mutation.description
    )
    assert match
    valid_states = set(match.group(1).split(', '))
    assert valid_states
    assert not valid_states.difference(i.value for i in WorkflowStatus)


@pytest.mark.parametrize('wflow_stop_mode', list(WorkflowStopMode))
def test_stop_mode_enum(wflow_stop_mode):
    """Check that WorkflowStopMode is a subset of StopMode."""
    assert StopMode(wflow_stop_mode.value)
    assert wflow_stop_mode.description