-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
test_task.py
574 lines (435 loc) · 16.2 KB
/
test_task.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
import json
import logging
import uuid
from datetime import timedelta
from typing import Any, Union
import pytest
import prefect
from prefect.core import Edge, Flow, Parameter, Task
from prefect.engine.cache_validators import all_inputs, duration_only, never_use
from prefect.engine.result_handlers import JSONResultHandler, ResultHandler
from prefect.utilities.configuration import set_temporary_config
from prefect.utilities.tasks import task
class AddTask(Task):
def run(self, x, y=1):
return x + y
class TestCreateTask:
"""Test various Task constructors"""
def test_create_task_with_no_args(self):
"""Tasks can be created with no arguments"""
assert Task()
def test_create_task_is_not_auto_generated(self):
assert Task().auto_generated is False
def test_create_task_with_name(self):
t1 = Task()
assert t1.name == "Task"
t2 = Task(name="test")
assert t2.name == "test"
def test_create_task_with_cache_key(self):
t1 = Task()
assert t1.cache_key is None
t2 = Task(cache_key="test")
assert t2.cache_key == "test"
def test_create_task_with_slug(self):
t1 = Task()
assert t1.slug
assert uuid.UUID(t1.slug) # slug is a UUID
t2 = Task(slug="test")
assert t2.slug == "test"
def test_create_task_with_max_retries(self):
t1 = Task()
assert t1.max_retries == 0
t2 = Task(max_retries=5, retry_delay=timedelta(0))
assert t2.max_retries == 5
def test_create_task_with_retry_delay(self):
t2 = Task(retry_delay=timedelta(seconds=30))
assert t2.retry_delay == timedelta(seconds=30)
def test_create_task_with_max_retries_and_no_retry_delay(self):
with pytest.raises(ValueError):
Task(max_retries=1, retry_delay=None)
def test_create_task_with_timeout(self):
t1 = Task()
assert t1.timeout == None
with pytest.raises(TypeError):
Task(timeout=0.5)
t3 = Task(timeout=1)
assert t3.timeout == 1
with set_temporary_config({"tasks.defaults.timeout": 3}):
t4 = Task()
assert t4.timeout == 3
def test_create_task_with_trigger(self):
t1 = Task()
assert t1.trigger is prefect.triggers.all_successful
t2 = Task(trigger=prefect.triggers.all_failed)
assert t2.trigger == prefect.triggers.all_failed
def test_create_task_without_state_handler(self):
assert Task().state_handlers == []
@pytest.mark.parametrize("handlers", [[lambda *a: 1], [lambda *a: 1, lambda *a: 2]])
def test_create_task_with_state_handler(self, handlers):
assert Task(state_handlers=handlers).state_handlers == handlers
def test_create_task_with_on_failure(self):
t = Task(on_failure=lambda *args: None)
assert len(t.state_handlers) == 1
def test_create_task_illegal_handler(self):
with pytest.raises(TypeError):
Task(state_handlers=lambda *a: 1)
def test_class_instantiation_rejects_varargs(self):
with pytest.raises(ValueError):
class VarArgsTask(Task):
def run(self, x, *y):
pass
def test_class_instantiation_rejects_mapped_kwarg(self):
with pytest.raises(ValueError):
class MappedTasks(Task):
def run(self, x, mapped):
pass
with pytest.raises(ValueError):
class MappedTasks(Task):
def run(self, x, mapped=None):
pass
def test_class_instantiation_rejects_mapped_kwarg_decorator(self):
with pytest.raises(ValueError):
@task
def run(x, mapped):
pass
with pytest.raises(ValueError):
@task
def run(x, mapped=None):
pass
def test_class_instantiation_rejects_upstream_tasks_kwarg(self):
with pytest.raises(ValueError):
class UpstreamTasks(Task):
def run(self, x, upstream_tasks):
pass
with pytest.raises(ValueError):
class UpstreamTasks(Task):
def run(self, x, upstream_tasks=None):
pass
def test_class_instantiation_rejects_upstream_tasks_kwarg_decorator(self):
with pytest.raises(ValueError):
@task
def run(x, upstream_tasks):
pass
with pytest.raises(ValueError):
@task
def run(x, upstream_tasks=None):
pass
def test_class_instantiation_rejects_flow_kwarg(self):
with pytest.raises(ValueError):
class FlowTasks(Task):
def run(self, x, flow):
pass
with pytest.raises(ValueError):
class FlowTasks(Task):
def run(self, x, flow=None):
pass
def test_class_instantiation_rejects_flow_kwarg_decorator(self):
with pytest.raises(ValueError):
@task
def run(x, flow):
pass
with pytest.raises(ValueError):
@task
def run(x, flow=None):
pass
def test_class_instantiation_rejects_task_args_kwarg(self):
with pytest.raises(ValueError):
class TaskArgs(Task):
def run(self, x, task_args):
pass
with pytest.raises(ValueError):
class TaskArgs(Task):
def run(self, x, task_args=None):
pass
def test_class_instantiation_rejects_task_args_kwarg_decorator(self):
with pytest.raises(ValueError):
@task
def run(x, task_args):
pass
with pytest.raises(ValueError):
@task
def run(x, task_args=None):
pass
def test_create_task_with_and_without_cache_for(self):
t1 = Task()
assert t1.cache_validator is never_use
t2 = Task(cache_for=timedelta(days=1))
assert t2.cache_validator is duration_only
t3 = Task(cache_for=timedelta(days=1), cache_validator=all_inputs)
assert t3.cache_validator is all_inputs
def test_bad_cache_kwarg_combo(self):
with pytest.warns(UserWarning, match=".*Task will not be cached.*"):
Task(cache_validator=all_inputs)
def test_create_task_with_and_without_result_handler(self):
t1 = Task()
assert t1.result_handler is None
t2 = Task(result_handler=JSONResultHandler())
assert isinstance(t2.result_handler, ResultHandler)
assert isinstance(t2.result_handler, JSONResultHandler)
def test_create_parameter_uses_json_result_handler(self):
p = Parameter("p")
assert isinstance(p.result_handler, JSONResultHandler)
def test_create_task_with_and_without_checkpoint(self):
t = Task()
assert t.checkpoint is False
s = Task(checkpoint=True)
assert s.checkpoint is True
with set_temporary_config({"tasks.defaults.checkpoint": True}):
r = Task()
assert r.checkpoint is True
@pytest.mark.xfail(reason="UX improvement for Core")
def test_create_parameter_always_checkpoints(self):
with set_temporary_config({"tasks.defaults.checkpoint": False}):
p = Parameter("p")
assert p.checkpoint is True
def test_task_has_logger():
t = Task()
assert isinstance(t.logger, logging.Logger)
assert t.logger.name == "prefect.Task: Task"
def test_task_has_logger_with_informative_name():
t = Task(name="foo")
assert isinstance(t.logger, logging.Logger)
assert t.logger.name == "prefect.Task: foo"
def test_task_produces_no_result():
t = Task()
assert t.run() is None
def test_task_is_not_iterable():
t = Task()
with pytest.raises(TypeError):
list(t)
def test_tags_are_added_when_arguments_are_bound():
t1 = AddTask(tags=["math"])
t2 = AddTask(tags=["math"])
with prefect.context(tags=["test"]):
with Flow(name="test"):
t1.bind(1, 2)
t3 = t2(1, 2)
assert t1.tags == {"math", "test"}
assert t3.tags == {"math", "test"}
def test_tags():
t1 = Task()
assert t1.tags == set()
with pytest.raises(TypeError):
Task(tags="test")
t3 = Task(tags=["test", "test2", "test"])
assert t3.tags == set(["test", "test2"])
with prefect.context(tags=["test"]):
t4 = Task()
assert t4.tags == set(["test"])
with prefect.context(tags=["test1", "test2"]):
t5 = Task(tags=["test3"])
assert t5.tags == set(["test1", "test2", "test3"])
class TestInputsOutputs:
class add(Task):
def run(self, x, y: int = 1) -> int:
return x + y
@task
def mult(x, y: int = 1) -> int:
return x * y
def test_inputs(self):
assert self.add().inputs() == dict(
x=dict(type=Any, required=True, default=None),
y=dict(type=int, required=False, default=1),
)
def test_inputs_task_decorator(self):
with Flow("test"):
assert self.mult(x=1).inputs() == dict(
x=dict(type=Any, required=True, default=None),
y=dict(type=int, required=False, default=1),
)
def test_outputs(self):
assert self.add().outputs() == int
def test_outputs_task_decorator(self):
with Flow("test"):
assert self.mult(x=1).outputs() == int
class TestTaskCopy:
def test_copy_copies(self):
class CopyTask(Task):
class_attr = 42
def __init__(self, instance_val, **kwargs):
self.instance_val = instance_val
super().__init__(**kwargs)
def run(self, run_val):
return (run_val, self.class_attr, self.instance_val)
ct = CopyTask("username")
other = ct.copy()
assert isinstance(other, CopyTask)
assert ct is not other
assert hash(ct) != hash(other)
assert ct != other
assert other.run("pass") == ("pass", 42, "username")
def test_copy_warns_if_dependencies_in_active_flow(self):
t1 = Task()
t2 = Task()
with Flow(name="test"):
t1.set_dependencies(downstream_tasks=[t2])
with pytest.warns(UserWarning):
t1.copy()
with Flow(name="test"):
# no dependencies in this flow
t1.copy()
def test_copy_changes_slug(self):
t1 = Task(slug="test")
t2 = t1.copy()
assert t1.slug == "test"
assert t1.slug != t2.slug
def test_copy_accepts_task_args(self):
t = Task()
t2 = t.copy(name="new-task")
t3 = t.copy(**{"max_retries": 4200})
assert t2.name == "new-task"
assert t3.max_retries == 4200
def test_copy_accepts_slug_as_task_args(self):
t = Task(slug="test")
t2 = t.copy(slug="test-2")
assert t.slug == "test"
assert t2.slug == "test-2"
def test_task_has_slug():
t1 = Task()
t2 = Task()
assert t1.slug and t1.slug != t2.slug
class TestDependencies:
def test_set_downstream(self):
f = Flow(name="test")
t1 = Task()
t2 = Task()
t1.set_downstream(t2, flow=f)
assert Edge(t1, t2) in f.edges
def test_set_downstream_context(self):
with Flow(name="test") as f:
t1 = Task()
t2 = Task()
t1.set_downstream(t2)
assert Edge(t1, t2) in f.edges
def test_set_downstream_no_flow(self):
f = Flow(name="test")
t1 = Task()
t2 = Task()
with pytest.raises(ValueError, match="No Flow was passed"):
t1.set_downstream(t2)
@pytest.mark.parametrize(
"props", [{"mapped": True}, {"key": "x"}, {"key": "x", "mapped": True}]
)
def test_set_downstream_with_properties(self, props):
with Flow(name="test") as f:
t1 = Task()
t2 = Task()
t1.set_downstream(t2, **props)
assert Edge(t1, t2, **props) in f.edges
def test_set_upstream(self):
f = Flow(name="test")
t1 = Task()
t2 = Task()
t2.set_upstream(t1, flow=f)
assert Edge(t1, t2) in f.edges
def test_set_upstream_context(self):
with Flow(name="test") as f:
t1 = Task()
t2 = Task()
t2.set_upstream(t1)
assert Edge(t1, t2) in f.edges
def test_set_upstream_no_flow(self):
f = Flow(name="test")
t1 = Task()
t2 = Task()
with pytest.raises(ValueError, match="No Flow was passed"):
t2.set_upstream(t1)
@pytest.mark.parametrize(
"props", [{"mapped": True}, {"key": "x"}, {"key": "x", "mapped": True}]
)
def test_set_upstream_with_properties(self, props):
with Flow(name="test") as f:
t1 = Task()
t2 = Task()
t2.set_upstream(t1, **props)
assert Edge(t1, t2, **props) in f.edges
class TestSerialization:
def test_serialization(self):
t = Task(name="test")
s = t.serialize()
assert isinstance(s, dict)
assert s["slug"] == t.slug
assert s["type"] == "prefect.core.task.Task"
assert s["name"] == t.name
def test_subclass_serialization(self):
class NewTask(Task):
pass
s = NewTask().serialize()
assert isinstance(s, dict)
assert s["type"].endswith(".NewTask")
def test_deserialization(self):
t = Task(name="test")
s = t.serialize()
t2 = prefect.serialization.task.TaskSchema().load(s)
assert isinstance(t2, Task)
assert t2.name == t.name
def test_subclass_deserialization(self):
class NewTask(Task):
pass
t = NewTask(name="test")
s = t.serialize()
t2 = prefect.serialization.task.TaskSchema().load(s)
assert type(t2) is Task
assert not isinstance(t2, NewTask)
assert t2.name == t.name
def test_parameter_serialization(self):
p = Parameter(name="p")
serialized = p.serialize()
assert serialized["name"] == "p"
assert serialized["default"] is None
assert serialized["required"] is True
def test_parameter_deserialization(self):
p = Parameter(name="p")
serialized = p.serialize()
p2 = prefect.serialization.task.ParameterSchema().load(serialized)
assert isinstance(p2, Parameter)
assert p2.name == p.name
assert p2.required == p.required
assert p2.default == p.default
class TestTaskArgs:
def test_task_args_raises_for_non_attrs(self):
t = Task()
with Flow(name="test") as f:
with pytest.raises(AttributeError, match="foo"):
res = t(task_args={"foo": "bar"})
@pytest.mark.parametrize(
"attr,val",
[
("name", "foo-bar"),
("slug", "foo-bar"),
("max_retries", 4200),
("retry_delay", timedelta(seconds=1)),
("timeout", 12),
("skip_on_upstream_skip", False),
("cache_for", timedelta(seconds=1)),
],
)
def test_task_args_sets_new_attrs(self, attr, val):
t = Task()
with Flow(name="test") as f:
res = t(task_args={attr: val})
assert getattr(f.tasks.pop(), attr) == val
@pytest.mark.parametrize(
"attr,val",
[
("name", "foo-bar"),
("slug", "foo-bar"),
("max_retries", 4200),
("retry_delay", timedelta(seconds=1)),
("timeout", 12),
("skip_on_upstream_skip", False),
("cache_for", timedelta(seconds=1)),
],
)
def test_task_args_sets_new_attrs_on_mapped_tasks(self, attr, val):
t = Task()
with Flow(name="test") as f:
res = t.map(upstream_tasks=[1, 2, 3, 4], task_args={attr: val})
tasks = f.get_tasks(name="Task")
assert all(getattr(tt, attr) == val for tt in tasks)
def test_tags_are_appended_to_when_updating_with_task_args(self):
t = AddTask(tags=["math"])
with prefect.context(tags=["test"]):
with Flow(name="test"):
t2 = t(1, 2, task_args={"name": "test-tags", "tags": ["new-tag"]})
assert t2.tags == {"math", "test", "new-tag"}