Debian Codegraph @

tests/u/test_id.py

raw
1 # THIS FILE IS PART OF THE CYLC SUITE 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 """Test the Cylc universal identifier system."""
17
18 import pytest
19
20 from cylc.flow.cycling.integer import IntegerPoint
21 from cylc.flow.cycling.iso8601 import ISO8601Point
22 from cylc.flow.id import (
23 LEGACY_CYCLE_SLASH_TASK,
24 LEGACY_TASK_DOT_CYCLE,
25 RELATIVE_ID,
26 UNIVERSAL_ID,
27 Tokens,
28 quick_relative_id,
29 )
30
31
32 @pytest.mark.parametrize(
33 'identifier',
34 [
35 '',
36 '~',
37 '~user//cycle'
38 '~flow:state',
39 'flow:flow_sel:flow_sel',
40 ]
41 )
42 def test_univseral_id_illegal(identifier):
43 """Test illegal formats of the universal identifier."""
44 assert UNIVERSAL_ID.match(identifier) is None
45
46
47 @pytest.mark.parametrize(
48 'identifier',
49 [
50 '~user',
51 '~user/',
52 '~user/workflow',
53 '~user/workflow//',
54 '~user/workflow:workflow_sel',
55 '~user/workflow:workflow_sel//',
56 '~user/workflow:workflow_sel//cycle',
57 '~user/workflow:workflow_sel//cycle/',
58 '~user/workflow:workflow_sel//cycle:cycle_sel',
59 '~user/workflow:workflow_sel//cycle:cycle_sel/',
60 '~user/workflow:workflow_sel//cycle:cycle_sel/task',
61 '~user/workflow:workflow_sel//cycle:cycle_sel/task/',
62 '~user/workflow:workflow_sel//cycle:cycle_sel/task:task_sel',
63 '~user/workflow:workflow_sel//cycle:cycle_sel/task:task_sel/',
64 '~user/workflow:workflow_sel//cycle:cycle_sel/task:task_sel/job',
65 (
66 '~user/workflow:workflow_sel//cycle:cycle_sel/task:task_sel/job'
67 ':job_sel'
68 ),
69 'workflow',
70 'workflow//',
71 'workflow:workflow_sel',
72 'workflow:workflow_sel//',
73 'workflow:workflow_sel//cycle',
74 'workflow:workflow_sel//cycle/',
75 'workflow:workflow_sel//cycle:cycle_sel',
76 'workflow:workflow_sel//cycle:cycle_sel/',
77 'workflow:workflow_sel//cycle:cycle_sel/task',
78 'workflow:workflow_sel//cycle:cycle_sel/task/',
79 'workflow:workflow_sel//cycle:cycle_sel/task:task_sel',
80 'workflow:workflow_sel//cycle:cycle_sel/task:task_sel/',
81 'workflow:workflow_sel//cycle:cycle_sel/task:task_sel/job',
82 'workflow:workflow_sel//cycle:cycle_sel/task:task_sel/job:job_sel'
83 ]
84 )
85 def test_universal_id_matches(identifier):
86 """test every legal format of the universal identifier."""
87 # fmt: off
88 expected_tokens = {
89 'user':
90 'user' if 'user' in identifier else None,
91 'workflow':
92 'workflow' if 'workflow' in identifier else None,
93 'workflow_sel':
94 'workflow_sel' if 'workflow_sel' in identifier else None,
95 'cycle':
96 'cycle' if 'cycle' in identifier else None,
97 'cycle_sel':
98 'cycle_sel' if 'cycle_sel' in identifier else None,
99 'task':
100 'task' if 'task' in identifier else None,
101 'task_sel':
102 'task_sel' if 'task_sel' in identifier else None,
103 'job':
104 'job' if 'job' in identifier else None,
105 'job_sel':
106 'job_sel' if 'job_sel' in identifier else None
107 }
108 # fmt: on
109 match = UNIVERSAL_ID.match(identifier)
110 assert match
111 assert match.groupdict() == expected_tokens
112
113
114 @pytest.mark.parametrize(
115 'identifier',
116 [
117 '~user/a/b/c',
118 '~user/a/b/c//',
119 '~user/a/b/c:workflow_sel',
120 '~user/a/b/c:workflow_sel//',
121 '~user/a/b/c:workflow_sel//cycle',
122 '~user/a/b/c:workflow_sel//cycle/',
123 '~user/a/b/c:workflow_sel//cycle:cycle_sel',
124 '~user/a/b/c:workflow_sel//cycle:cycle_sel/',
125 '~user/a/b/c:workflow_sel//cycle:cycle_sel/task',
126 '~user/a/b/c:workflow_sel//cycle:cycle_sel/task/',
127 '~user/a/b/c:workflow_sel//cycle:cycle_sel/task:task_sel',
128 '~user/a/b/c:workflow_sel//cycle:cycle_sel/task:task_sel/',
129 '~user/a/b/c:workflow_sel//cycle:cycle_sel/task:task_sel/job',
130 (
131 '~user/a/b/c:workflow_sel//cycle:cycle_sel/task:task_sel/job'
132 ':job_sel'
133 ),
134 'a/b/c',
135 'a/b/c//',
136 'a/b/c:workflow_sel',
137 'a/b/c:workflow_sel//',
138 'a/b/c:workflow_sel//cycle',
139 'a/b/c:workflow_sel//cycle/',
140 'a/b/c:workflow_sel//cycle:cycle_sel',
141 'a/b/c:workflow_sel//cycle:cycle_sel/',
142 'a/b/c:workflow_sel//cycle:cycle_sel/task',
143 'a/b/c:workflow_sel//cycle:cycle_sel/task/',
144 'a/b/c:workflow_sel//cycle:cycle_sel/task:task_sel',
145 'a/b/c:workflow_sel//cycle:cycle_sel/task:task_sel/',
146 'a/b/c:workflow_sel//cycle:cycle_sel/task:task_sel/job',
147 'a/b/c:workflow_sel//cycle:cycle_sel/task:task_sel/job:job_sel'
148 ]
149 )
150 def test_universal_id_matches_hierarchical(identifier):
151 """Test the UID with hierarchical workflow IDs."""
152 # fmt: off
153 expected_tokens = {
154 'user':
155 'user' if 'user' in identifier else None,
156 'workflow':
157 'a/b/c', # the hierarchical workflow ID
158 'workflow_sel':
159 'workflow_sel' if 'workflow_sel' in identifier else None,
160 'cycle':
161 'cycle' if 'cycle' in identifier else None,
162 'cycle_sel':
163 'cycle_sel' if 'cycle_sel' in identifier else None,
164 'task':
165 'task' if 'task' in identifier else None,
166 'task_sel':
167 'task_sel' if 'task_sel' in identifier else None,
168 'job':
169 'job' if 'job' in identifier else None,
170 'job_sel':
171 'job_sel' if 'job_sel' in identifier else None
172 }
173 # fmt: on
174 match = UNIVERSAL_ID.match(identifier)
175 assert match
176 assert match.groupdict() == expected_tokens
177
178
179 @pytest.mark.parametrize(
180 'identifier',
181 [
182 '',
183 '~',
184 ':',
185 'workflow//cycle',
186 'task:task_sel:task_sel',
187 'cycle/task'
188 '//',
189 '//~',
190 '//:',
191 '//workflow//cycle',
192 '//cycle/task:task_sel:task_sel'
193 ]
194 )
195 def test_relative_id_illegal(identifier):
196 """Test illegal formats of the universal identifier."""
197 assert RELATIVE_ID.match(identifier) is None
198
199
200 @pytest.mark.parametrize(
201 'identifier',
202 [
203 '//cycle',
204 '//cycle/',
205 '//cycle:cycle_sel',
206 '//cycle:cycle_sel/',
207 '//cycle:cycle_sel/task',
208 '//cycle:cycle_sel/task/',
209 '//cycle:cycle_sel/task:task_sel',
210 '//cycle:cycle_sel/task:task_sel/',
211 '//cycle:cycle_sel/task:task_sel/job',
212 '//cycle:cycle_sel/task:task_sel/job:job_sel',
213 ]
214 )
215 def test_relative_id_matches(identifier):
216 """test every legal format of the relative identifier."""
217 expected_tokens = {
218 'cycle': 'cycle' if 'cycle' in identifier else None,
219 'cycle_sel': 'cycle_sel' if 'cycle_sel' in identifier else None,
220 'task': 'task' if 'task' in identifier else None,
221 'task_sel': 'task_sel' if 'task_sel' in identifier else None,
222 'job': 'job' if 'job' in identifier else None,
223 'job_sel': 'job_sel' if 'job_sel' in identifier else None
224 }
225 match = RELATIVE_ID.match(identifier)
226 assert match
227 assert match.groupdict() == expected_tokens
228
229
230 @pytest.mark.parametrize(
231 'identifier',
232 [
233 '',
234 '~',
235 '/',
236 ':',
237 'task.cycle', # the first digit of the cycle should be a number
238 '//task.123', # don't match the new format
239 'task.cycle/job',
240 'task:task_sel.123' # selector should suffix the cycle
241 ]
242 )
243 def test_legacy_task_dot_cycle_illegal(identifier):
244 """Test illegal formats of the legacy task.cycle identifier."""
245 assert LEGACY_TASK_DOT_CYCLE.match(identifier) is None
246
247
248 @pytest.mark.parametrize(
249 'identifier,expected_tokens',
250 [
251 (
252 'task.1', # integer cycles can be one character long
253 {'task': 'task', 'cycle': '1', 'task_sel': None}
254 ),
255 (
256 't.a.s.k.123',
257 {'task': 't.a.s.k', 'cycle': '123', 'task_sel': None}
258 ),
259 (
260 'task.123:task_sel',
261 {'task': 'task', 'cycle': '123', 'task_sel': 'task_sel'}
262 ),
263 ]
264 )
265 def test_legacy_task_dot_cycle_matches(identifier, expected_tokens):
266 match = LEGACY_TASK_DOT_CYCLE.match(identifier)
267 assert match
268 assert match.groupdict() == expected_tokens
269
270
271 @pytest.mark.parametrize(
272 'identifier',
273 [
274 '',
275 '~',
276 '/',
277 ':',
278 'cycle/task', # the first digit of the cycle should be a number
279 '//123/task', # don't match the new format
280 'cycle/task/job'
281 ]
282 )
283 def test_legacy_cycle_slash_task_illegal(identifier):
284 """Test illegal formats of the legacy cycle/task identifier."""
285 assert LEGACY_CYCLE_SLASH_TASK.match(identifier) is None
286
287
288 @pytest.mark.parametrize(
289 'identifier,expected_tokens',
290 [
291 (
292 '123/task',
293 {'task': 'task', 'cycle': '123', 'task_sel': None}
294 ),
295 (
296 '123/t.a.s.k',
297 {'task': 't.a.s.k', 'cycle': '123', 'task_sel': None}
298 ),
299 (
300 '123/task:task_sel',
301 {'task': 'task', 'cycle': '123', 'task_sel': 'task_sel'}
302 )
303 ]
304 )
305 def test_legacy_cycle_slash_task_matches(identifier, expected_tokens):
306 match = LEGACY_CYCLE_SLASH_TASK.match(identifier)
307 assert match
308 assert match.groupdict() == expected_tokens
309
310
311 def test_tokens():
312 # tested mainly in doctests
313
314 Tokens('a')
315 with pytest.raises(ValueError):
316 Tokens('a', 'b')
317
318 Tokens(cycle='a')
319 with pytest.raises(ValueError):
320 Tokens(foo='a')
321
322 Tokens().duplicate(cycle='a')
323 with pytest.raises(ValueError):
324 Tokens(foo='a')
325
326 # test equality
327 assert Tokens('a') == Tokens('a')
328 assert Tokens('a') != Tokens('b')
329 assert Tokens('a', relative=True) == Tokens('a', relative=True)
330 assert Tokens('a', relative=True) != Tokens('b', relative=True)
331 assert Tokens() != Tokens('a')
332 assert Tokens(workflow='a') == Tokens('a')
333
334 # test equality with non Tokens objects
335 assert Tokens('a') != 'a'
336 assert not Tokens('a') == 'a'
337 assert Tokens('a') != 1
338 assert not Tokens('a') == 1
339
340 tokens = Tokens('a//b')
341 new_tokens = tokens.duplicate(cycle='c', task='d')
342 assert new_tokens == Tokens('a//c/d')
343 with pytest.raises(Exception):
344 tokens.update({'foo': 'c'})
345 with pytest.raises(Exception):
346 tokens['cycle'] = 'a'
347
348 # test gt/lt
349 assert sorted(
350 tokens.id
351 for tokens in [
352 Tokens('~u/c'),
353 Tokens('~u/b//1'),
354 Tokens('~u/a'),
355 Tokens('~u/b'),
356 Tokens('~u/b//2'),
357 ]
358 ) == ['~u/a', '~u/b', '~u/b//1', '~u/b//2', '~u/c']
359
360
361 def test_no_look_behind():
362 """Ensure the UID pattern does not use lookbehinds.
363
364 Ideally this pattern should be work for both cylc-flow and
365 cylc-ui.
366
367 2022-01-11:
368 * Lookbehind support is at ~75%
369 * https://caniuse.com/js-regexp-lookbehind
370
371 """
372 assert '?<=' not in UNIVERSAL_ID.pattern
373 assert '?<!' not in UNIVERSAL_ID.pattern
374
375
376 def test_workflow_property():
377 """The workflow property should provide the workflow part of the ID.
378
379 The relevant selectors should be preserved
380 """
381 assert (
382 Tokens('~u/w:ws//c:cs/t:ts/j:js').workflow
383 ) == (
384 Tokens('~u/w:ws')
385 )
386
387
388 def test_task_property():
389 """The task property should provide the task part of the ID.
390
391 The relevant selectors should be preserved
392 """
393 assert (
394 Tokens('~u/w:ws//c:cs/t:ts/j:js').task
395 ) == (
396 Tokens('//c:cs/t:ts/j:js', relative=True)
397 )
398
399
400 @pytest.mark.parametrize('submit_num, expected', [
401 (2, 'c/t/02'),
402 (20, 'c/t/20'),
403 (200, 'c/t/200'),
404 ])
405 def test_relative_id_job_fmt(submit_num: int, expected: str):
406 """Job part of tokens should be formatted with a leading zero.
407
408 This is important for job log dirs etc.
409 """
410 assert Tokens(f'~u/w//c/t/{submit_num}').relative_id == expected
411 # Check passing int to job arg even though it is proscribed:
412 assert Tokens(cycle='c', task='t', job=submit_num).relative_id == expected
413
414
415 def test_relative_id_excludes_selector():
416 """Relative ID should not include selectors."""
417 assert Tokens('~u/w//c/t:ts/2:js').relative_id == 'c/t/02'
418
419
420 @pytest.mark.parametrize('cycle, expected', [
421 ('2000', '2000/foo'),
422 (2001, '2001/foo'),
423 (IntegerPoint('3'), '3/foo'),
424 # NOTE: ISO8601Points are not standardised by this function:
425 (ISO8601Point('2002'), '2002/foo'),
426 ])
427 def test_quick_relative_id(cycle, expected):
428 assert quick_relative_id(cycle, 'foo') == expected