Skip to content

Commit

Permalink
add more test
Browse files Browse the repository at this point in the history
  • Loading branch information
goFrendiAsgard committed Nov 29, 2023
1 parent 18ff3c7 commit 08cbb44
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
36 changes: 36 additions & 0 deletions test/task/test_task_execution_id.py
Original file line number Diff line number Diff line change
@@ -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
63 changes: 63 additions & 0 deletions test/task/test_task_input.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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']
Expand All @@ -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']
Expand All @@ -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']
Expand All @@ -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']
Expand Down

0 comments on commit 08cbb44

Please sign in to comment.