Add retry_utils, update docstrings, add tests
This commit is contained in:
@ -1,8 +1,7 @@
|
||||
import pytest
|
||||
|
||||
from falyx.action import Action, ChainedAction, ActionGroup, FallbackAction
|
||||
from falyx.action import Action, ChainedAction, LiteralInputAction, FallbackAction
|
||||
from falyx.execution_registry import ExecutionRegistry as er
|
||||
from falyx.hook_manager import HookManager, HookType
|
||||
from falyx.context import ExecutionContext
|
||||
|
||||
asyncio_default_fixture_loop_scope = "function"
|
||||
@ -18,6 +17,8 @@ def clean_registry():
|
||||
yield
|
||||
er.clear()
|
||||
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_action_callable():
|
||||
"""Test if Action can be created with a callable."""
|
||||
@ -33,6 +34,7 @@ async def test_action_async_callable():
|
||||
action = Action("test_action", async_callable)
|
||||
result = await action()
|
||||
assert result == "Hello, World!"
|
||||
assert str(action) == "Action(name='test_action', action=async_callable, args=(), kwargs={}, retry=False)"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_action_non_callable():
|
||||
@ -76,3 +78,28 @@ async def test_chained_action_literals(return_list, auto_inject, expected):
|
||||
|
||||
result = await chain()
|
||||
assert result == expected
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_literal_input_action():
|
||||
"""Test if LiteralInputAction can be created and used."""
|
||||
action = LiteralInputAction("Hello, World!")
|
||||
result = await action()
|
||||
assert result == "Hello, World!"
|
||||
assert action.value == "Hello, World!"
|
||||
assert str(action) == "LiteralInputAction(value='Hello, World!')"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fallback_action():
|
||||
"""Test if FallbackAction can be created and used."""
|
||||
action = FallbackAction("Fallback value")
|
||||
chain = ChainedAction(
|
||||
name="Fallback Chain",
|
||||
actions=[
|
||||
Action(name="one", action=lambda: None),
|
||||
action,
|
||||
],
|
||||
)
|
||||
result = await chain()
|
||||
assert result == "Fallback value"
|
||||
assert str(action) == "FallbackAction(fallback='Fallback value')"
|
||||
|
||||
|
@ -1,23 +1,12 @@
|
||||
import pytest
|
||||
|
||||
from falyx.action import Action, ChainedAction, ActionGroup, FallbackAction
|
||||
from falyx.action import Action, ChainedAction
|
||||
from falyx.execution_registry import ExecutionRegistry as er
|
||||
from falyx.hook_manager import HookManager, HookType
|
||||
from falyx.context import ExecutionContext
|
||||
from falyx.retry_utils import enable_retries_recursively
|
||||
|
||||
asyncio_default_fixture_loop_scope = "function"
|
||||
|
||||
# --- Helpers ---
|
||||
async def capturing_hook(context: ExecutionContext):
|
||||
context.extra["hook_triggered"] = True
|
||||
|
||||
# --- Fixtures ---
|
||||
@pytest.fixture
|
||||
def hook_manager():
|
||||
hm = HookManager()
|
||||
hm.register(HookType.BEFORE, capturing_hook)
|
||||
return hm
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def clean_registry():
|
||||
er.clear()
|
||||
@ -27,4 +16,19 @@ def clean_registry():
|
||||
def test_action_enable_retry():
|
||||
"""Test if Action can be created with retry=True."""
|
||||
action = Action("test_action", lambda: "Hello, World!", retry=True)
|
||||
assert action.retry_policy.enabled is True
|
||||
assert action.retry_policy.enabled is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_enable_retries_recursively():
|
||||
"""Test if Action can be created with retry=True."""
|
||||
action = Action("test_action", lambda: "Hello, World!")
|
||||
assert action.retry_policy.enabled is False
|
||||
|
||||
chained_action = ChainedAction(
|
||||
name="Chained Action",
|
||||
actions=[action],
|
||||
)
|
||||
|
||||
enable_retries_recursively(chained_action, policy=None)
|
||||
assert action.retry_policy.enabled is True
|
||||
|
@ -49,7 +49,8 @@ def test_command_str():
|
||||
description="Test Command",
|
||||
action=action
|
||||
)
|
||||
assert str(cmd) == "Command(key='TEST', description='Test Command' action='Action(name=test_action, action=dummy_action)')"
|
||||
print(cmd)
|
||||
assert str(cmd) == "Command(key='TEST', description='Test Command' action='Action(name='test_action', action=dummy_action, args=(), kwargs={}, retry=False)')"
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"action_factory, expected_requires_input",
|
||||
|
Reference in New Issue
Block a user