diff --git a/test/task/test_task_execution_id.py b/test/task/test_task_execution_id.py new file mode 100644 index 00000000..549b109a --- /dev/null +++ b/test/task/test_task_execution_id.py @@ -0,0 +1,36 @@ +from zrb.task.task import Task +from zrb.task.cmd_task import CmdTask + + +def test_task_execution_id_cannot_be_set_twice(): + task = Task( + name='task', + run=lambda *args, **kwargs: kwargs.get('_task').get_execution_id() + ) + task._set_execution_id('execution_id_1') + task._set_execution_id('execution_id_2') + task._set_execution_id('execution_id_3') + function = task.to_function() + result = function() + assert result == 'execution_id_1' + + +def test_consistent_task_upstream_execution_id(): + task_upstream_1 = Task( + name='task-upstream-1', + run=lambda *args, **kwargs: kwargs.get('_task').get_execution_id() + ) + task_upstream_2 = CmdTask( + name='task-upstream-2', + cmd='echo $_ZRB_EXECUTION_ID' + ) + task = Task( + name='task', + upstreams=[ + task_upstream_1, task_upstream_2 + ], + return_upstream_result=True + ) + function = task.to_function() + result = function() + assert result[0] == result[1].output diff --git a/test/task/test_task_input.py b/test/task/test_task_input.py index 6f9234f7..c66b3769 100644 --- a/test/task/test_task_input.py +++ b/test/task/test_task_input.py @@ -1,4 +1,5 @@ from zrb.task.task import Task +from zrb.task.cmd_task import CmdTask from zrb.task_input.str_input import StrInput @@ -21,6 +22,21 @@ def _run(*args, **kwargs) -> str: assert result == 'hello Dumbledore, your favorite drink is Elixir' +def test_cmd_task_input(): + task = CmdTask( + name='hello-name', + inputs=[ + StrInput(name='name'), + StrInput(name='favorite-drink') + ], + cmd='echo hello $_INPUT_NAME, your favorite drink is $_INPUT_FAVORITE_DRINK', # noqa + retry=0 + ) + function = task.to_function() + result = function(name='Dumbledore', favorite_drink='Elixir') + assert result.output == 'hello Dumbledore, your favorite drink is Elixir' + + def test_task_input_with_default_value(): def _run(*args, **kwargs) -> str: name = kwargs['name'] @@ -40,6 +56,21 @@ def _run(*args, **kwargs) -> str: assert result == 'hello Nicholas Flamel, your favorite drink is Elixir' +def test_cmd_task_input_with_default_value(): + task = CmdTask( + name='hello-name', + inputs=[ + StrInput(name='name', default='Nicholas Flamel'), + StrInput(name='favorite-drink', default='Elixir') + ], + cmd='echo hello $_INPUT_NAME, your favorite drink is $_INPUT_FAVORITE_DRINK', # noqa + retry=0 + ) + function = task.to_function() + result = function() + assert result.output == 'hello Nicholas Flamel, your favorite drink is Elixir' + + def test_task_input_with_jinja_value(): def _run(*args, **kwargs) -> str: name = kwargs['name'] @@ -59,6 +90,21 @@ def _run(*args, **kwargs) -> str: assert result == 'hello Nicholas Flamel, aka Nicholas Flamel' +def test_cmd_task_input_with_jinja_value(): + task = CmdTask( + name='hello-name', + inputs=[ + StrInput(name='name', default='Nicholas Flamel'), + StrInput(name='alias', default='{{input.name}}') + ], + cmd='echo hello $_INPUT_NAME, aka $_INPUT_ALIAS', + retry=0 + ) + function = task.to_function() + result = function() + assert result.output == 'hello Nicholas Flamel, aka Nicholas Flamel' + + def test_task_input_with_should_not_be_rendered_jinja_value(): def _run(*args, **kwargs) -> str: name = kwargs['name'] @@ -80,6 +126,23 @@ def _run(*args, **kwargs) -> str: assert result == 'hello Nicholas Flamel, aka {{input.name}}' +def test_cmd_task_input_with_should_not_be_rendered_jinja_value(): + task = CmdTask( + name='hello-name', + inputs=[ + StrInput(name='name', default='Nicholas Flamel'), + StrInput( + name='alias', default='{{input.name}}', should_render=False + ) + ], + cmd='echo hello $_INPUT_NAME, aka $_INPUT_ALIAS', + retry=0 + ) + function = task.to_function() + result = function() + assert result.output == 'hello Nicholas Flamel, aka {{input.name}}' + + def test_task_input_with_jinja_value_and_partial_custom_kwargs(): def _run(*args, **kwargs) -> str: name = kwargs['name']