Debian Codegraph @

tests/u/tui/test_util.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
18 from datetime import (
19 datetime,
20 timedelta
21 )
22 from unittest.mock import Mock
23
24 import pytest
25
26 from cylc.flow.tui.util import (
27 JOB_ICON,
28 TASK_ICONS,
29 render_node,
30 compute_tree,
31 get_task_icon
32 )
33 from cylc.flow.wallclock import (
34 get_time_string,
35 get_current_time_string
36 )
37
38
39 def test_render_node__job_info():
40 """It renders job information nodes."""
41 assert render_node(
42 None,
43 {'a': 1, 'b': 2},
44 'job_info'
45 ) == [
46 'a 1\n',
47 'b 2'
48 ]
49
50
51 def test_render_node__job():
52 """It renders job nodes."""
53 assert render_node(
54 None,
55 {'state': 'succeeded', 'submitNum': 1},
56 'job'
57 ) == [
58 '#01 ',
59 [('job_succeeded', JOB_ICON)]
60 ]
61
62
63 def test_render_node__task__succeeded():
64 """It renders tasks."""
65 node = Mock()
66 node.get_child_node = lambda _: None
67 assert render_node(
68 node,
69 {
70 'name': 'foo',
71 'state': 'succeeded',
72 'isHeld': False,
73 'isQueued': False,
74 'isRunahead': False,
75 'flowNums': '[1]',
76 },
77 'task'
78 ) == [
79 ('body', TASK_ICONS['succeeded']),
80 ' ',
81 ('body', 'foo'),
82 ]
83
84
85 def test_render_node__task__running():
86 """It renders running tasks."""
87 child = Mock()
88 child.get_value = lambda: {'data': {
89 'startedTime': get_current_time_string(),
90 'state': 'running'
91 }}
92 node = Mock()
93 node.get_child_node = lambda _: child
94 assert render_node(
95 node,
96 {
97 'name': 'foo',
98 'state': 'running',
99 'isHeld': False,
100 'isQueued': False,
101 'isRunahead': False,
102 'flowNums': '[1]',
103 'task': {'meanElapsedTime': 100}
104 },
105 'task'
106 ) == [
107 ('body', TASK_ICONS['running']),
108 ' ',
109 ('job_running', JOB_ICON),
110 ' ',
111 ('body', 'foo'),
112 ]
113
114
115 def test_render_node__family():
116 """It renders families."""
117 assert render_node(
118 None,
119 {
120 'state': 'succeeded',
121 'isHeld': False,
122 'isQueued': False,
123 'isRunahead': False,
124 'id': 'myid'
125 },
126 'family'
127 ) == [
128 [('body', TASK_ICONS['succeeded'])],
129 ' ',
130 'myid'
131 ]
132
133
134 def test_render_node__cycle_point():
135 """It renders cycle points."""
136 assert render_node(
137 None,
138 {'id': 'myid'},
139 'cycle_point'
140 ) == 'myid'
141
142
143 @pytest.mark.parametrize(
144 'status,is_held,is_queued,is_runahead,start_offset,mean_time,expected',
145 [
146 # task states
147 ('waiting', False, False, False, None, None, ['○']),
148 ('submitted', False, False, False, None, None, ['⊙']),
149 ('running', False, False, False, None, None, ['⊙']),
150 ('succeeded', False, False, False, None, None, ['●']),
151 ('submit-failed', False, False, False, None, None, ['⊘']),
152 ('failed', False, False, False, None, None, ['⊗']),
153 # progress indicator
154 ('running', False, False, False, 0, 100, ['⊙']),
155 ('running', False, False, False, 25, 100, ['◔']),
156 ('running', False, False, False, 50, 100, ['◑']),
157 ('running', False, False, False, 75, 100, ['◕']),
158 ('running', False, False, False, 100, 100, ['◕']),
159 # is-held modifier
160 ('waiting', True, False, False, None, None, ['\u030E', '○']),
161 # is-queued modifier
162 ('waiting', False, True, False, None, None, ['\u033F', '○']),
163 # is-runahead modifier
164 ('waiting', False, False, True, None, None, ['\u0340', '○']),
165 ]
166 )
167 def test_get_task_icon(
168 status, is_held, is_queued, is_runahead, start_offset, mean_time,
169 expected):
170 """It renders task icons."""
171 start_time = None
172 if start_offset is not None:
173 start_time = get_time_string(
174 datetime.utcnow() - timedelta(seconds=start_offset)
175 )
176 assert (
177 (
178 get_task_icon(
179 status,
180 is_held=is_held,
181 is_queued=is_queued,
182 is_runahead=is_runahead,
183 colour='custom',
184 start_time=start_time,
185 mean_time=mean_time,
186 )
187 )
188 ) == [('custom', char) for char in expected]
189
190
191 def test_compute_tree():
192 """It computes a tree in the right structure for urwid.
193
194 Note this test doesn't use full data or propper ids because it's
195 purpose is not to test the GraphQL interface but to ensure the
196 assumptions made by compute_tree check out.
197
198 """
199 tree = compute_tree({
200 'workflows': [{
201 'id': 'workflow id',
202 'port': 1234,
203 'cyclePoints': [
204 {
205 'id': '1/family-suffix',
206 'cyclePoint': '1'
207 }
208 ],
209 'familyProxies': [
210 { # top level family
211 'name': 'FOO',
212 'id': '1/FOO',
213 'cyclePoint': '1',
214 'firstParent': {'name': 'root', 'id': '1/root'}
215 },
216 { # nested family
217 'name': 'FOOT',
218 'id': '1/FOOT',
219 'cyclePoint': '1',
220 'firstParent': {'name': 'FOO', 'id': '1/FOO'}
221 },
222 ],
223 'taskProxies': [
224 { # top level task
225 'name': 'pub',
226 'id': '1/pub',
227 'firstParent': {'name': 'root', 'id': '1/root'},
228 'cyclePoint': '1',
229 'jobs': []
230 },
231 { # child task (belongs to family)
232 'name': 'fan',
233 'id': '1/fan',
234 'firstParent': {'name': 'fan', 'id': '1/fan'},
235 'cyclePoint': '1',
236 'jobs': []
237 },
238 { # nested child task (belongs to incestuous family)
239 'name': 'fool',
240 'id': '1/fool',
241 'firstParent': {'name': 'FOOT', 'id': '1/FOOT'},
242 'cyclePoint': '1',
243 'jobs': []
244 },
245 { # a task which has jobs
246 'name': 'worker',
247 'id': '1/worker',
248 'firstParent': {'name': 'root', 'id': '1/root'},
249 'cyclePoint': '1',
250 'jobs': [
251 {'id': '1/worker/03', 'submitNum': '3'},
252 {'id': '1/worker/02', 'submitNum': '2'},
253 {'id': '1/worker/01', 'submitNum': '1'}
254 ]
255 }
256 ]
257 }]
258 })
259
260 # the root node
261 assert tree['type_'] == 'root'
262 assert tree['id_'] == 'root'
263 assert len(tree['children']) == 1
264
265 # the workflow node
266 workflow = tree['children'][0]
267 assert workflow['type_'] == 'workflow'
268 assert workflow['id_'] == 'workflow id'
269 assert set(workflow['data']) == {
270 # whatever if present on the node should end up in data
271 'cyclePoints',
272 'familyProxies',
273 'id',
274 'port',
275 'taskProxies'
276 }
277 assert len(workflow['children']) == 1
278
279 # the cycle point node
280 cycle = workflow['children'][0]
281 assert cycle['type_'] == 'cycle'
282 assert cycle['id_'] == '//1'
283 assert list(cycle['data']) == [
284 'id',
285 'cyclePoint'
286 ]
287 assert len(cycle['children']) == 3
288 assert [
289 node['id_']
290 for node in cycle['children']
291 ] == [
292 # test alphabetical sorting
293 '1/FOO',
294 '1/pub',
295 '1/worker'
296 ]
297
298 # test family node
299 family = cycle['children'][0]
300 assert family['type_'] == 'family'
301 assert family['id_'] == '1/FOO'
302 assert list(family['data']) == [
303 'name',
304 'id',
305 'cyclePoint',
306 'firstParent'
307 ]
308 assert len(family['children']) == 1
309
310 # test nested family
311 nested_family = family['children'][0]
312 assert nested_family['type_'] == 'family'
313 assert nested_family['id_'] == '1/FOOT'
314 assert list(nested_family['data']) == [
315 'name',
316 'id',
317 'cyclePoint',
318 'firstParent'
319 ]
320 assert len(nested_family['children']) == 1
321
322 # test task
323 task = nested_family['children'][0]
324 assert task['type_'] == 'task'
325 assert task['id_'] == '1/fool'
326 assert list(task['data']) == [
327 'name',
328 'id',
329 'firstParent',
330 'cyclePoint',
331 'jobs'
332 ]
333 assert len(task['children']) == 0
334
335 # test task with jobs
336 task = cycle['children'][-1]
337 assert [ # test sorting
338 job['id_']
339 for job in task['children']
340 ] == [
341 '1/worker/03',
342 '1/worker/02',
343 '1/worker/01'
344 ]
345
346 # test job
347 job = task['children'][0]
348 assert job['type_'] == 'job'
349 assert job['id_'] == '1/worker/03'
350 assert list(job['data']) == [
351 'id',
352 'submitNum'
353 ]
354 assert len(job['children']) == 1
355
356 # test job info
357 job_info = job['children'][0]
358 assert job_info['type_'] == 'job_info'
359 assert job_info['id_'] == '1/worker/03_info'
360 assert list(job_info['data']) == [
361 'id',
362 'submitNum'
363 ]
364 assert len(job_info['children']) == 0