Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat+refactor(engine): Add expression extractor + more restructuring and cleanup #634

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions tests/unit/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pydantic import SecretStr

from tracecat.contexts import RunContext, ctx_role
from tracecat.dsl.common import create_default_dsl_context
from tracecat.dsl.common import create_default_execution_context
from tracecat.dsl.models import ActionStatement, RunActionInput
from tracecat.executor.service import run_action_from_input, sync_executor_entrypoint
from tracecat.expressions.expectations import ExpectedField
Expand Down Expand Up @@ -118,7 +118,7 @@ async def test_executor_can_run_udf_with_secrets(
for_each=None,
args={"secret_key_name": "TEST_UDF_SECRET_KEY"},
),
exec_context=create_default_dsl_context(),
exec_context=create_default_execution_context(),
run_context=mock_run_context,
)

Expand Down Expand Up @@ -221,7 +221,7 @@ async def test_executor_can_run_template_action_with_secret(
for_each=None,
args={"secret_key_name": "TEST_TEMPLATE_SECRET_KEY"},
),
exec_context=create_default_dsl_context(),
exec_context=create_default_execution_context(),
run_context=mock_run_context,
)

Expand Down Expand Up @@ -260,7 +260,7 @@ def test_sync_executor_entrypoint(test_role, mock_run_context):
run_if=None,
for_each=None,
),
exec_context=create_default_dsl_context(),
exec_context=create_default_execution_context(),
run_context=mock_run_context,
)
result = sync_executor_entrypoint(input, test_role)
Expand Down
43 changes: 42 additions & 1 deletion tests/unit/test_parse.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from tracecat.expressions.functions import eval_jsonpath
from tracecat.parse import traverse_leaves
from tracecat.parse import traverse_expressions, traverse_leaves


def test_iter_dict_leaves():
Expand Down Expand Up @@ -43,3 +43,44 @@ def test_more_iter_dict_leaves():
obj4 = {"a": 1, "b": [2, 3], "c": "hello"}
expected4 = [("a", 1), ("b[0]", 2), ("b[1]", 3), ("c", "hello")]
assert list(traverse_leaves(obj4)) == expected4


def test_traverse_expressions():
# Test case 1: Single expression in string
data = {
"test": "Hello, ${{ var.name }}",
}
assert list(traverse_expressions(data)) == ["var.name"]

# Test case 2: Multiple expressions in string
data = {
"test": "This is a ${{ 1 }} or ${{ 2 }}",
}
assert list(traverse_expressions(data)) == ["1", "2"]

# Test case 3: Nested expressions in objects and lists
data = {
"test": "This is a ${{ 1 }} or ${{ 2 }}",
"list": [
"This is a ${{ 3 }} or ${{ 4 }}",
"second",
{
"test": "This is a ${{ 5 }} or ${{ 6 }}",
},
],
"data": "${{ 7 }}${{ 8 }}",
}
assert list(traverse_expressions(data)) == ["1", "2", "3", "4", "5", "6", "7", "8"]

# Test case 4: No expressions
data = {
"test": "Hello world",
"list": ["no expressions", {"test": 123}],
}
assert list(traverse_expressions(data)) == []

# Test case 5: Empty data structures
data = {}
assert list(traverse_expressions(data)) == []
data = {"test": {}, "list": []}
assert list(traverse_expressions(data)) == []
Loading