-
Notifications
You must be signed in to change notification settings - Fork 245
/
test_conditions.py
469 lines (367 loc) · 13 KB
/
test_conditions.py
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import typing
from collections import OrderedDict
import mock
import pytest
import flytekit.configuration
from flytekit import task, workflow
from flytekit.configuration import Image, ImageConfig, SerializationSettings
from flytekit.core.condition import conditional
from flytekit.models.core.workflow import Node
from flytekit.tools.translator import get_serializable
default_img = Image(name="default", fqn="test", tag="tag")
serialization_settings = SerializationSettings(
project="project",
domain="domain",
version="version",
env=None,
image_config=ImageConfig(default_image=default_img, images=[default_img]),
)
@task
def five() -> int:
return 5
@task
def square(n: float) -> float:
"""
Parameters:
n (float): name of the parameter for the task will be derived from the name of the input variable
the type will be automatically deduced to be Types.Integer
Return:
float: The label for the output will be automatically assigned and type will be deduced from the annotation
"""
return n * n
@task
def double(n: float) -> float:
"""
Parameters:
n (float): name of the parameter for the task will be derived from the name of the input variable
the type will be automatically deduced to be Types.Integer
Return:
float: The label for the output will be automatically assigned and type will be deduced from the annotation
"""
return 2 * n
def test_condition_else_fail():
@workflow
def multiplier_2(my_input: float) -> float:
return (
conditional("fractions")
.if_((my_input > 0.1) & (my_input < 1.0))
.then(double(n=my_input))
.elif_((my_input > 1.0) & (my_input < 10.0))
.then(square(n=my_input))
.else_()
.fail("The input must be between 0 and 10")
)
with pytest.raises(ValueError):
multiplier_2(my_input=10.0)
def test_condition_else_int():
@workflow
def multiplier_3(my_input: int) -> float:
return (
conditional("fractions")
.if_((my_input >= 0) & (my_input < 1.0))
.then(double(n=my_input))
.elif_((my_input > 1.0) & (my_input < 10.0))
.then(square(n=my_input))
.else_()
.fail("The input must be between 0 and 10")
)
assert multiplier_3(my_input=0) == 0
def test_condition_sub_workflows():
@task
def sum_div_sub(a: int, b: int) -> typing.NamedTuple("Outputs", sum=int, div=int, sub=int):
return a + b, a / b, a - b
@task
def sum_sub(a: int, b: int) -> typing.NamedTuple("Outputs", sum=int, sub=int):
return a + b, a - b
@workflow
def sub_wf(a: int, b: int) -> typing.Tuple[int, int]:
return sum_sub(a=a, b=b)
@workflow
def math_ops(a: int, b: int) -> typing.Tuple[int, int]:
# Flyte will only make `sum` and `sub` available as outputs because they are common between all branches
sum, sub = (
conditional("noDivByZero")
.if_(a > b)
.then(sub_wf(a=a, b=b))
.else_()
.fail("Only positive results are allowed")
)
return sum, sub
x, y = math_ops(a=3, b=2)
assert x == 5
assert y == 1
def test_condition_tuple_branches():
@task
def sum_sub(a: int, b: int) -> typing.NamedTuple("Outputs", sum=int, sub=int):
return a + b, a - b
@workflow
def math_ops(a: int, b: int) -> typing.Tuple[int, int]:
add, sub = (
conditional("noDivByZero")
.if_(a > b)
.then(sum_sub(a=a, b=b))
.else_()
.fail("Only positive results are allowed")
)
return add, sub
x, y = math_ops(a=3, b=2)
assert x == 5
assert y == 1
wf_spec = get_serializable(OrderedDict(), serialization_settings, math_ops)
assert len(wf_spec.template.nodes) == 1
assert (
wf_spec.template.nodes[0].branch_node.if_else.case.then_node.task_node.reference_id.name
== "tests.flytekit.unit.core.test_conditions.sum_sub"
)
def test_condition_unary_bool():
@task
def return_true() -> bool:
return True
@workflow
def failed() -> int:
return 10
@workflow
def success() -> int:
return 20
with pytest.raises(AssertionError):
@workflow
def decompose_unary() -> int:
result = return_true()
return conditional("test").if_(result).then(success()).else_().then(failed())
with pytest.raises(AssertionError):
@workflow
def decompose_none() -> int:
return conditional("test").if_(None).then(success()).else_().then(failed())
with pytest.raises(AssertionError):
@workflow
def decompose_is() -> int:
result = return_true()
return conditional("test").if_(result is True).then(success()).else_().then(failed())
@workflow
def decompose() -> int:
result = return_true()
return conditional("test").if_(result.is_true()).then(success()).else_().then(failed())
assert decompose() == 20
def test_subworkflow_condition_serialization():
"""Test that subworkflows are correctly extracted from serialized workflows with condiationals."""
@task
def t() -> int:
return 5
@workflow
def wf1() -> int:
return t()
@workflow
def wf2() -> int:
return t()
@workflow
def wf3() -> int:
return t()
@workflow
def wf4() -> int:
return t()
@workflow
def ifelse_branching(x: int) -> int:
return conditional("simple branching test").if_(x == 2).then(wf1()).else_().then(wf2())
@workflow
def ifelse_branching_fail(x: int) -> int:
return conditional("simple branching test").if_(x == 2).then(wf1()).else_().fail("failed")
@workflow
def if_elif_else_branching(x: int) -> int:
return ( # noqa
conditional("test")
.if_(x == 2)
.then(wf1())
.elif_(x == 3)
.then(wf2())
.elif_(x == 4)
.then(wf3())
.else_()
.then(wf4())
)
@workflow
def wf5() -> int:
return t()
@workflow
def nested_branching(x: int) -> int:
return conditional("nested test").if_(x == 2).then(ifelse_branching(x=x)).else_().then(wf5())
default_img = Image(name="default", fqn="test", tag="tag")
serialization_settings = flytekit.configuration.SerializationSettings(
project="project",
domain="domain",
version="version",
env=None,
image_config=ImageConfig(default_image=default_img, images=[default_img]),
)
fmt = "tests.flytekit.unit.core.test_conditions.{}"
for wf, expected_subworkflows in [
(ifelse_branching, [fmt.format(x) for x in ("wf1", "wf2")]),
(ifelse_branching_fail, [fmt.format(x) for x in ("wf1",)]),
(if_elif_else_branching, [fmt.format(x) for x in ("wf1", "wf2", "wf3", "wf4")]),
(nested_branching, [fmt.format(x) for x in ("ifelse_branching", "wf1", "wf2", "wf5")]),
]:
wf_spec = get_serializable(OrderedDict(), serialization_settings, wf)
subworkflows = wf_spec.sub_workflows
for sub_wf in subworkflows:
assert sub_wf.id.name in expected_subworkflows
assert len(subworkflows) == len(expected_subworkflows)
def test_subworkflow_condition():
@task
def t() -> int:
return 5
@workflow
def wf1() -> int:
return t()
@workflow
def branching(x: int) -> int:
return conditional("test").if_(x == 2).then(t()).else_().then(wf1())
assert branching(x=2) == 5
assert branching(x=3) == 5
def test_no_output_condition():
@task
def t():
...
@workflow
def wf1():
t()
@workflow
def branching(x: int):
return conditional("test").if_(x == 2).then(t()).else_().then(wf1())
assert branching(x=2) is None
def test_subworkflow_condition_named_tuple():
nt = typing.NamedTuple("SampleNamedTuple", b=int, c=str)
@task
def t() -> nt:
return nt(5, "foo")
@workflow
def wf1() -> nt:
return nt(3, "bar")
@workflow
def branching(x: int) -> nt:
return conditional("test").if_(x == 2).then(t()).else_().then(wf1())
assert branching(x=2) == (5, "foo")
assert branching(x=3) == (3, "bar")
def test_subworkflow_condition_single_named_tuple():
nt = typing.NamedTuple("SampleNamedTuple", b=int)
@task
def t() -> nt:
return nt(
5,
)
@workflow
def wf1() -> nt:
return t()
@workflow
def branching(x: int) -> int:
return conditional("test").if_(x == 2).then(t().b).else_().then(wf1().b)
assert branching(x=2) == 5
@mock.patch.object(five, "execute")
def test_call_counts(five_mock):
five_mock.return_value = 5
@workflow
def if_elif_else_branching(x: int) -> int:
return (
conditional("test")
.if_(x == 2)
.then(five())
.elif_(x == 3)
.then(five())
.elif_(x == 4)
.then(five())
.else_()
.then(five())
)
res = if_elif_else_branching(x=2)
assert res == 5
assert five_mock.call_count == 1
def test_nested_condition():
@workflow
def multiplier_2(my_input: float) -> float:
return (
conditional("fractions")
.if_((my_input > 0.1) & (my_input < 1.0))
.then(
conditional("inner_fractions")
.if_(my_input < 0.5)
.then(double(n=my_input))
.else_()
.fail("Only <0.5 allowed")
)
.elif_((my_input > 1.0) & (my_input < 10.0))
.then(square(n=my_input))
.else_()
.fail("The input must be between 0 and 10")
)
srz_wf = get_serializable(OrderedDict(), serialization_settings, multiplier_2)
assert len(srz_wf.template.nodes) == 1
fractions_branch = srz_wf.template.nodes[0]
assert isinstance(fractions_branch, Node)
assert fractions_branch.id == "n0"
assert fractions_branch.branch_node is not None
if_else_b = fractions_branch.branch_node.if_else
assert if_else_b is not None
assert if_else_b.case is not None
assert if_else_b.case.then_node is not None
inner_fractions_node = if_else_b.case.then_node
assert inner_fractions_node.id == "n0"
assert inner_fractions_node.branch_node.if_else.case.then_node.task_node is not None
assert inner_fractions_node.branch_node.if_else.case.then_node.id == "n0"
# Ensure other cases exist
assert len(if_else_b.other) == 1
assert if_else_b.other[0].then_node.task_node is not None
assert if_else_b.other[0].then_node.id == "n1"
with pytest.raises(ValueError):
multiplier_2(my_input=0.5)
res = multiplier_2(my_input=0.3)
assert res == 0.6
res = multiplier_2(my_input=5.0)
assert res == 25
with pytest.raises(ValueError):
multiplier_2(my_input=10.0)
def test_nested_condition_2():
@workflow
def multiplier_2(my_input: float) -> float:
return (
conditional("fractions")
.if_((my_input > 0.1) & (my_input < 1.0))
.then(
conditional("inner_fractions")
.if_(my_input < 0.5)
.then(double(n=my_input))
.elif_((my_input > 0.5) & (my_input < 0.7))
.then(square(n=my_input))
.else_()
.fail("Only <0.7 allowed")
)
.elif_((my_input > 1.0) & (my_input < 10.0))
.then(square(n=my_input))
.else_()
.then(double(n=my_input))
)
srz_wf = get_serializable(OrderedDict(), serialization_settings, multiplier_2)
assert len(srz_wf.template.nodes) == 1
fractions_branch = srz_wf.template.nodes[0]
assert isinstance(fractions_branch, Node)
assert fractions_branch.id == "n0"
assert fractions_branch.branch_node is not None
if_else_b = fractions_branch.branch_node.if_else
assert if_else_b is not None
assert if_else_b.case is not None
assert if_else_b.case.then_node is not None
inner_fractions_node = if_else_b.case.then_node
assert inner_fractions_node.id == "n0"
assert inner_fractions_node.branch_node.if_else.case.then_node.task_node is not None
assert inner_fractions_node.branch_node.if_else.case.then_node.id == "n0"
assert len(inner_fractions_node.branch_node.if_else.other) == 1
assert inner_fractions_node.branch_node.if_else.other[0].then_node.id == "n1"
# Ensure other cases exist
assert len(if_else_b.other) == 1
assert if_else_b.other[0].then_node.task_node is not None
assert if_else_b.other[0].then_node.id == "n1"
with pytest.raises(ValueError):
multiplier_2(my_input=0.7)
res = multiplier_2(my_input=0.3)
assert res == 0.6
res = multiplier_2(my_input=5.0)
assert res == 25
res = multiplier_2(my_input=10.0)
assert res == 20