Debian Codegraph @

tests/u/network/test_graphql.py

raw
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 import pytest
18 from pytest import param
19 from graphql import (
20 TypeInfo,
21 TypeInfoVisitor,
22 parse,
23 visit
24 )
25
26 from cylc.flow.data_messages_pb2 import PbTaskProxy, PbPrerequisite
27 from cylc.flow.network.graphql import (
28 CylcVisitor, null_setter, strip_null, async_next, NULL_VALUE, grow_tree
29 )
30 from cylc.flow.network.schema import schema
31
32
33 TASK_PROXY_PREREQS = PbTaskProxy()
34 TASK_PROXY_PREREQS.prerequisites.append(PbPrerequisite(expression="foo"))
35
36
37 @pytest.mark.parametrize(
38 'query,'
39 'variables,'
40 'search_arg,'
41 'expected_result',
42 [
43 pytest.param(
44 '''
45 query ($workflowID: ID) {
46 workflows (ids: [$workflowID]) {
47 id
48 }
49 }
50 ''',
51 {
52 'workflowID': 'cylc|workflow'
53 },
54 {
55 'ids': ['cylc|workflow'],
56 },
57 True,
58 id="simple query with correct variables"
59 ),
60 pytest.param(
61 '''
62 query ($workflowID: ID) {
63 ...WorkflowData
64 }
65 fragment WorkflowData on workflows {
66 workflows (ids: [$workflowID]) {
67 id
68 }
69 }
70 ''',
71 {
72 'workflowID': 'cylc|workflow'
73 },
74 {
75 'ids': ['cylc|workflow'],
76 },
77 True,
78 id="query with a fragment and correct variables"
79 ),
80 pytest.param(
81 '''
82 query ($workflowID: ID) {
83 workflows (ids: [$workflowID]) {
84 id
85 }
86 }
87 ''',
88 {
89 'workflowID': 'cylc|workflow'
90 },
91 {
92 'ids': None,
93 },
94 False,
95 id="correct variable definition, but missing variable in "
96 "provided values"
97 ),
98 pytest.param(
99 '''
100 query ($workflowID: ID) {
101 workflows (ids: [$workflowID]) {
102 id
103 }
104 }
105 ''',
106 {
107 'workflowID': 'cylc|workflow'
108 },
109 {
110 'idfsdf': ['cylc|workflow'],
111 },
112 False,
113 id="correct variable definition, but wrong search argument"
114 )
115 ]
116 )
117 def test_query_variables(
118 query: str,
119 variables: dict,
120 search_arg: dict,
121 expected_result: bool,
122 ):
123 """Test that query variables are parsed and found correctly.
124
125 Args:
126 query: a valid GraphQL query (using our schema)
127 variables: map with variable values for the query
128 search_arg: argument and value to search for
129 expected_result: was the argument and value found
130 """
131 document = parse(query)
132 type_info = TypeInfo(schema.graphql_schema)
133 cylc_visitor = CylcVisitor(
134 type_info,
135 variables,
136 search_arg
137 )
138 visit(
139 document,
140 TypeInfoVisitor(
141 type_info,
142 cylc_visitor
143 ),
144 None
145 )
146
147 assert expected_result == cylc_visitor.arg_flag
148
149
150 @pytest.mark.parametrize(
151 'pre_result,'
152 'expected_result',
153 [
154 (
155 'foo',
156 'foo'
157 ),
158 (
159 [],
160 NULL_VALUE
161 ),
162 (
163 {},
164 NULL_VALUE
165 ),
166 (
167 TASK_PROXY_PREREQS.prerequisites,
168 TASK_PROXY_PREREQS.prerequisites
169 ),
170 (
171 PbTaskProxy().prerequisites,
172 NULL_VALUE
173 )
174 ]
175 )
176 def test_null_setter(pre_result, expected_result):
177 """Test the null setting of different data types/results."""
178 post_result = null_setter(pre_result)
179 assert post_result == expected_result
180
181
182 @pytest.mark.parametrize(
183 'pre_result,'
184 'expected_result',
185 [
186 (
187 'foo',
188 'foo'
189 ),
190 (
191 [NULL_VALUE],
192 []
193 ),
194 (
195 {'nothing': NULL_VALUE},
196 {},
197 ),
198 (
199 TASK_PROXY_PREREQS.prerequisites,
200 TASK_PROXY_PREREQS.prerequisites
201 ),
202 ]
203 )
204 async def test_strip_null(pre_result, expected_result):
205 """Test the null stripping of different result data/types."""
206 # non-async
207 post_result = async_next(strip_null, pre_result)
208 assert post_result == expected_result
209
210 async def async_result(result):
211 return result
212
213 # async
214 async_post_result = async_next(strip_null, async_result(pre_result))
215 assert await async_post_result == expected_result
216
217
218 @pytest.mark.parametrize(
219 'expect, tree, path, leaves',
220 [
221 param(
222 {'foo': {'bar': {'baz': {}}}}, {}, ['foo', 'bar', 'baz'], None,
223 id='fill-empty-tree'
224 ),
225 param(
226 {'bar': {'baz': {}}}, {'bar': {'baz': {}}}, ['bar', 'baz'], None,
227 id='keep-full-tree'
228 ),
229 param(
230 {'foo': {'bar': {}, 'qux': {}}},
231 {'foo': {'bar': {}}},
232 ['foo', 'qux'],
233 None,
234 id='add-new-branch'
235 ),
236 param(
237 {'leaves': {'foo': {}}}, {}, [], {'foo': {}},
238 id='add-leaves'
239 )
240 ]
241 )
242 def test_grow_tree(expect, tree, path, leaves):
243 grow_tree(tree, path, leaves)
244 assert tree == expect