Debian Codegraph @

tests/u/test_taskdef.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
20 from cylc.flow.config import WorkflowConfig
21 from cylc.flow.cycling.integer import IntegerPoint
22 from cylc.flow.cycling.iso8601 import ISO8601Point
23 from cylc.flow.taskdef import TaskTuple, generate_graph_parents
24
25
26 def test_generate_graph_parents_1(tmp_flow_config): # noqa: F811
27 """Test that parents are only generated from valid recurrences."""
28 id_ = 'pan-galactic'
29 flow_file = tmp_flow_config(
30 id_,
31 """
32 [scheduler]
33 UTC mode = True
34 [scheduling]
35 initial cycle point = 2023
36 [[graph]]
37 R1 = run_once_at_midnight
38 T00 = run_once_at_midnight[-PT0H] => every_cycle
39 T03 = run_once_at_midnight[-PT3H] => every_cycle
40 T06 = run_once_at_midnight[-PT6H] => every_cycle
41 [runtime]
42 [[every_cycle, run_once_at_midnight]]
43 """
44 )
45 cfg = WorkflowConfig(workflow=id_, fpath=flow_file, options=None)
46
47 # Each instance of every_cycle should have a parent only at T00.
48 for point in [
49 ISO8601Point('20230101T00'),
50 ISO8601Point('20230101T03'),
51 ISO8601Point('20230101T06')
52 ]:
53 assert generate_graph_parents(
54 cfg.taskdefs['every_cycle'], point, cfg.taskdefs
55 ) == [
56 ("run_once_at_midnight", ISO8601Point('20230101T0000Z'), False),
57 ]
58
59
60 def test_generate_graph_parents_2(tmp_flow_config): # noqa: F811
61 """Test inferred parents are valid w.r.t to their own recurrences."""
62 id_ = 'gargle-blaster'
63 flow_file = tmp_flow_config(
64 id_,
65 """
66 [scheduling]
67 cycling mode = integer
68 [[graph]]
69 P1 = "foo[-P1] => foo"
70 [runtime]
71 [[foo]]
72 """
73 )
74 cfg = WorkflowConfig(workflow=id_, fpath=flow_file, options=None)
75
76 # Each instance of every_cycle should have a parent only at T00.
77 assert generate_graph_parents(
78 cfg.taskdefs['foo'], IntegerPoint("1"), cfg.taskdefs
79 ) == [] # No parents at first point.
80
81 assert generate_graph_parents(
82 cfg.taskdefs['foo'], IntegerPoint("2"), cfg.taskdefs
83 ) == [
84 ("foo", IntegerPoint('1'), False),
85 ]
86
87
88 def test_generate_graph_parents__sequential(tmp_flow_config):
89 """It chooses the latest previous point when using old-style sequential."""
90 flow_file = tmp_flow_config(
91 id_ := 'gargle-blaster',
92 """
93 [scheduler]
94 allow implicit tasks = True
95 [scheduling]
96 initial cycle point = 2010-01-01
97 [[special tasks]]
98 sequential = foo
99 [[graph]]
100 P1D = foo
101 P1M = foo
102 """
103 )
104 cfg = WorkflowConfig(workflow=id_, fpath=flow_file, options=None)
105
106 parents = generate_graph_parents(
107 cfg.taskdefs['foo'], ISO8601Point('2010-02-01'), cfg.taskdefs
108 )
109 assert list(parents) == [
110 TaskTuple('foo', ISO8601Point('2010-01-31'), False),
111 ]
112
113
114 @pytest.mark.parametrize(
115 "task, point, expected",
116 [
117 param(
118 'foo',
119 IntegerPoint("1"),
120 ['0/foo'],
121 id='it.gets-prerequisites',
122 ),
123 param(
124 'multiple_pre',
125 IntegerPoint("2"),
126 ['2/food', '2/fool', '2/foolhardy', '2/foolish'],
127 id='it.gets-multiple-prerequisites',
128 ),
129 param(
130 'foo',
131 IntegerPoint("3"),
132 [],
133 id='it.only-returns-for-valid-points',
134 ),
135 param(
136 'bar',
137 IntegerPoint("2"),
138 [],
139 id='it.does-not-return-suicide-prereqs',
140 ),
141 ],
142 )
143 def test_get_prereqs(tmp_flow_config, task, point, expected): # noqa: F811
144
145 """Test that get_prereqs() returns the correct prerequisites
146 for a task."""
147 id_ = 'gargle-blaster'
148 flow_file = tmp_flow_config(
149 id_,
150 """
151 [scheduler]
152 allow implicit tasks = True
153 [scheduling]
154 final cycle point = 2
155 cycling mode = integer
156 [[graph]]
157 P1 = '''
158 foo[-P1] => foo
159 bar:fail? => !bar
160 food & fool => multiple_pre
161 foolish | foolhardy => multiple_pre
162 '''
163 """
164 )
165 cfg = WorkflowConfig(workflow=id_, fpath=flow_file, options=None)
166 taskdef = cfg.taskdefs[task]
167 point = IntegerPoint(point)
168 res = sorted([
169 condition.get_id()
170 for pre in taskdef.get_prereqs(point)
171 for condition in pre.keys()
172 ])
173 assert res == expected
174
175
176 def test_get_xtrigs(tmp_flow_config):
177 id = 'foo'
178 flow_file = tmp_flow_config(
179 id,
180 """
181 [scheduler]
182 allow implicit tasks = True
183 [scheduling]
184 initial cycle point = 1
185 final cycle point = 16
186 cycling mode = integer
187 [[xtriggers]]
188 xt_once = xrandom(1)
189 xt_every = xrandom(1)
190 xt_odd = xrandom(1)
191 xt_final = xrandom(1)
192
193 [[graph]]
194 R1 = @xt_once => foo
195 P1 = @xt_every => foo
196 P2 = @xt_odd => foo
197 R1/$ = @xt_final => foo
198 """
199 )
200 cfg = WorkflowConfig(workflow=id, fpath=flow_file, options=None)
201 taskdef = cfg.taskdefs['foo']
202 assert taskdef.get_xtrigs(IntegerPoint('1')) == {
203 'xt_once', 'xt_odd', 'xt_every'
204 }
205 assert taskdef.get_xtrigs(IntegerPoint('2')) == {'xt_every'}
206 assert taskdef.get_xtrigs(IntegerPoint('3')) == {'xt_odd', 'xt_every'}
207 assert taskdef.get_xtrigs(IntegerPoint('16')) == {'xt_final', 'xt_every'}
208
209
210 @pytest.mark.parametrize(
211 "task, point, expected",
212 [
213 param(
214 'foo',
215 IntegerPoint("1"),
216 ['foo[-P1]:succeeded'],
217 id='it.gets-triggers',
218 ),
219 param(
220 'multiple_pre',
221 IntegerPoint("2"),
222 ['food:succeeded', 'fool:succeeded',
223 'foolhardy:succeeded', 'foolish:succeeded'],
224 id='it.gets-multiple-triggers',
225 ),
226 param(
227 'foo',
228 IntegerPoint("3"),
229 [],
230 id='it.only-returns-triggers-for-valid-points',
231 ),
232 param(
233 'bar',
234 IntegerPoint("2"),
235 [],
236 id='it.does-not-return-suicide-triggers',
237 ),
238 ],
239 )
240 def test_get_triggers(tmp_flow_config, task, point, expected): # noqa: F811
241 """Test that get_triggers() returns the correct triggers for a task.
242
243 """
244 id_ = 'gargle-blaster'
245 flow_file = tmp_flow_config(
246 id_,
247 """
248 [scheduler]
249 allow implicit tasks = True
250 [scheduling]
251 final cycle point = 2
252 cycling mode = integer
253 [[graph]]
254 P1 = '''
255 foo[-P1] => foo
256 bar:fail? => !bar
257 food & fool => multiple_pre
258 foolish | foolhardy => multiple_pre
259 '''
260 """
261 )
262 cfg = WorkflowConfig(workflow=id_, fpath=flow_file, options=None)
263 taskdef = cfg.taskdefs[task]
264 point = IntegerPoint(point)
265 res = sorted([str(t) for t in taskdef.get_triggers(point)])
266 assert res == expected