|
| 1 | +from collections import OrderedDict |
| 2 | +from datetime import timedelta |
| 3 | + |
| 4 | +from flytekitplugins.sleep import Sleep |
| 5 | +from flytekitplugins.sleep.task import SleepFunctionTask |
| 6 | + |
| 7 | +from flytekit import task, workflow |
| 8 | +from flytekit.configuration import Image, ImageConfig, SerializationSettings |
| 9 | +from flytekit.extend import get_serializable |
| 10 | + |
| 11 | +default_img = Image(name="default", fqn="test", tag="tag") |
| 12 | +serialization_settings = SerializationSettings( |
| 13 | + project="proj", |
| 14 | + domain="dom", |
| 15 | + version="123", |
| 16 | + image_config=ImageConfig(default_image=default_img, images=[default_img]), |
| 17 | + env={}, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +def test_task_type(): |
| 22 | + @task(task_config=Sleep()) |
| 23 | + def sleep_for(duration: timedelta) -> None: |
| 24 | + pass |
| 25 | + |
| 26 | + assert isinstance(sleep_for, SleepFunctionTask) |
| 27 | + assert sleep_for.task_type == "core-sleep" |
| 28 | + |
| 29 | + |
| 30 | +def test_serialization(): |
| 31 | + @task(task_config=Sleep()) |
| 32 | + def sleep_for(duration: timedelta) -> None: |
| 33 | + pass |
| 34 | + |
| 35 | + task_spec = get_serializable(OrderedDict(), serialization_settings, sleep_for) |
| 36 | + |
| 37 | + assert task_spec.template.type == "core-sleep" |
| 38 | + assert task_spec.template.custom == {} |
| 39 | + |
| 40 | + inputs = task_spec.template.interface.inputs |
| 41 | + assert "duration" in inputs |
| 42 | + assert len(inputs) == 1 |
| 43 | + |
| 44 | + outputs = task_spec.template.interface.outputs |
| 45 | + assert len(outputs) == 0 |
| 46 | + |
| 47 | + |
| 48 | +def test_local_execution_calls_function(): |
| 49 | + called = [] |
| 50 | + |
| 51 | + @task(task_config=Sleep()) |
| 52 | + def sleep_for(duration: timedelta) -> None: |
| 53 | + called.append(duration) |
| 54 | + |
| 55 | + sleep_for(duration=timedelta(seconds=1)) |
| 56 | + assert called == [timedelta(seconds=1)] |
| 57 | + |
| 58 | + |
| 59 | +def test_workflow_integration(): |
| 60 | + @task(task_config=Sleep()) |
| 61 | + def sleep_for(duration: timedelta) -> None: |
| 62 | + pass |
| 63 | + |
| 64 | + @workflow |
| 65 | + def wf(duration: timedelta) -> None: |
| 66 | + sleep_for(duration=duration) |
| 67 | + |
| 68 | + spec = get_serializable(OrderedDict(), serialization_settings, wf) |
| 69 | + assert len(spec.template.nodes) == 1 |
0 commit comments