Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

test: use validated workflow invocation entities (#43176)

Asuka Minato0fa381f3e736
diff --git a/api/tests/unit_tests/core/app/apps/advanced_chat/test_app_runner_input_moderation.py b/api/tests/unit_tests/core/app/apps/advanced_chat/test_app_runner_input_moderation.py
index bb1c2cf8573..5e420ed6c5f 100644
--- a/api/tests/unit_tests/core/app/apps/advanced_chat/test_app_runner_input_moderation.py
+++ b/api/tests/unit_tests/core/app/apps/advanced_chat/test_app_runner_input_moderation.py
@@ -9,6 +9,7 @@ from sqlalchemy.engine import Engine
from sqlalchemy.orm import Session, sessionmaker
import core.app.apps.advanced_chat.app_runner as module
+from core.app.app_config.entities import WorkflowUIBasedAppConfig
from core.app.apps.advanced_chat.app_runner import AdvancedChatAppRunner
from core.app.entities.app_invoke_entities import AdvancedChatAppGenerateEntity, InvokeFrom
from core.app.entities.queue_entities import QueueAnnotationReplyEvent, QueueStopEvent
@@ -53,10 +54,9 @@ def build_runner(sqlite_session: Session):
created_by=str(uuid4()),
)
- mock_app_config = MagicMock()
- mock_app_config.app_id = app_id
- mock_app_config.workflow_id = workflow_id
- mock_app_config.tenant_id = tenant_id
+ app_config = WorkflowUIBasedAppConfig(
+ app_id=app_id, workflow_id=workflow_id, tenant_id=tenant_id, app_mode=AppMode.ADVANCED_CHAT
+ )
app = App(
id=app_id,
@@ -72,20 +72,17 @@ def build_runner(sqlite_session: Session):
sqlite_session.add(app)
sqlite_session.commit()
- gen = MagicMock(spec=AdvancedChatAppGenerateEntity)
- gen.app_config = mock_app_config
- gen.inputs = {"q": "raw"}
- gen.query = "raw-query"
- gen.files = []
- gen.user_id = str(uuid4())
- gen.invoke_from = InvokeFrom.SERVICE_API
- gen.workflow_run_id = str(uuid4())
- gen.task_id = str(uuid4())
- gen.call_depth = 0
- gen.single_iteration_run = None
- gen.single_loop_run = None
- gen.extras = {}
- gen.trace_manager = None
+ gen = AdvancedChatAppGenerateEntity(
+ app_config=app_config,
+ inputs={"q": "raw"},
+ query="raw-query",
+ files=[],
+ user_id=str(uuid4()),
+ invoke_from=InvokeFrom.SERVICE_API,
+ workflow_run_id=str(uuid4()),
+ task_id=str(uuid4()),
+ stream=True,
+ )
runner = AdvancedChatAppRunner(
application_generate_entity=gen,
diff --git a/api/tests/unit_tests/core/app/apps/common/test_workflow_response_converter_truncation.py b/api/tests/unit_tests/core/app/apps/common/test_workflow_response_converter_truncation.py
index ecf58070655..e9780c817ce 100644
--- a/api/tests/unit_tests/core/app/apps/common/test_workflow_response_converter_truncation.py
+++ b/api/tests/unit_tests/core/app/apps/common/test_workflow_response_converter_truncation.py
@@ -7,7 +7,6 @@ from collections.abc import Mapping
from dataclasses import dataclass
from datetime import UTC, datetime
from typing import Any
-from unittest.mock import Mock
import pytest
@@ -35,20 +34,25 @@ from models.model import AppMode
class TestWorkflowResponseConverter:
"""Test truncation in WorkflowResponseConverter."""
- def create_mock_generate_entity(self) -> WorkflowAppGenerateEntity:
- """Create a mock WorkflowAppGenerateEntity."""
- mock_entity = Mock(spec=WorkflowAppGenerateEntity)
- mock_app_config = Mock()
- mock_app_config.tenant_id = "test-tenant-id"
- mock_entity.invoke_from = InvokeFrom.WEB_APP
- mock_entity.app_config = mock_app_config
- mock_entity.inputs = {}
- return mock_entity
+ def create_generate_entity(self) -> WorkflowAppGenerateEntity:
+ """Create a validated workflow invocation for response conversion."""
+ return WorkflowAppGenerateEntity(
+ app_config=WorkflowUIBasedAppConfig(
+ tenant_id="test-tenant-id", app_id="app", workflow_id="wf-id", app_mode=AppMode.WORKFLOW
+ ),
+ task_id="task",
+ workflow_execution_id="initial-run-id",
+ user_id="test-user-id",
+ invoke_from=InvokeFrom.WEB_APP,
+ inputs={},
+ files=[],
+ stream=True,
+ )
def create_workflow_response_converter(self) -> WorkflowResponseConverter:
"""Create a WorkflowResponseConverter for testing."""
- mock_entity = self.create_mock_generate_entity()
+ entity = self.create_generate_entity()
mock_user = Account(
name="Test User",
@@ -57,7 +61,7 @@ class TestWorkflowResponseConverter:
system_variables = build_system_variables(workflow_id="wf-id", workflow_execution_id="initial-run-id")
return WorkflowResponseConverter(
- application_generate_entity=mock_entity,
+ application_generate_entity=entity,
user=mock_user,
system_variables=system_variables,
)
diff --git a/api/tests/unit_tests/core/app/apps/test_workflow_app_runner_single_node.py b/api/tests/unit_tests/core/app/apps/test_workflow_app_runner_single_node.py
index baa831ee689..ce0018b8b4f 100644
--- a/api/tests/unit_tests/core/app/apps/test_workflow_app_runner_single_node.py
+++ b/api/tests/unit_tests/core/app/apps/test_workflow_app_runner_single_node.py
@@ -1,13 +1,13 @@
from __future__ import annotations
import json
-from typing import Any
from unittest.mock import MagicMock, patch
import pytest
from pydantic import ValidationError
-from core.app.apps.base_app_queue_manager import AppQueueManager
+from core.app.app_config.entities import WorkflowUIBasedAppConfig
+from core.app.apps.workflow.app_queue_manager import WorkflowAppQueueManager
from core.app.apps.workflow.app_runner import WorkflowAppRunner
from core.app.apps.workflow_app_runner import WorkflowBasedAppRunner
from core.app.entities.app_invoke_entities import InvokeFrom, WorkflowAppGenerateEntity
@@ -28,6 +28,13 @@ def _make_graph_state():
return MagicMock(), variable_pool, GraphRuntimeState(variable_pool=variable_pool, start_at=0.0)
+def queue_manager() -> WorkflowAppQueueManager:
+ return WorkflowAppQueueManager(
+ task_id="task-id", user_id="user", invoke_from=InvokeFrom.SERVICE_API, app_mode=AppMode.WORKFLOW
+ )
+
+
@pytest.mark.parametrize(
("single_iteration_run", "single_loop_run"),
[
@@ -36,28 +43,25 @@ def _make_graph_state():
],
)
def test_run_uses_single_node_execution_branch(
- single_iteration_run: Any,
- single_loop_run: Any,
+ single_iteration_run: WorkflowAppGenerateEntity.SingleIterationRunEntity | None,
+ single_loop_run: WorkflowAppGenerateEntity.SingleLoopRunEntity | None,
+ queue_manager: WorkflowAppQueueManager,
) -> None:
- app_config = MagicMock()
- app_config.app_id = "app"
- app_config.tenant_id = "tenant"
- app_config.workflow_id = "workflow"
- app_config.app_mode = AppMode.WORKFLOW
-
- app_generate_entity = MagicMock(spec=WorkflowAppGenerateEntity)
- app_generate_entity.app_config = app_config
- app_generate_entity.inputs = {}
- app_generate_entity.files = []
- app_generate_entity.user_id = "user"
- app_generate_entity.invoke_from = InvokeFrom.SERVICE_API
- app_generate_entity.workflow_execution_id = "execution-id"
- app_generate_entity.task_id = "task-id"
- app_generate_entity.call_depth = 0
- app_generate_entity.trace_manager = None
- app_generate_entity.extras = {"trace_session_id": "session-1"}
- app_generate_entity.single_iteration_run = single_iteration_run
- app_generate_entity.single_loop_run = single_loop_run
+ app_generate_entity = WorkflowAppGenerateEntity(
+ app_config=WorkflowUIBasedAppConfig(
+ app_id="app", tenant_id="tenant", workflow_id="workflow", app_mode=AppMode.WORKFLOW
+ ),
+ inputs={},
+ files=[],
+ user_id="user",
+ invoke_from=InvokeFrom.SERVICE_API,
+ workflow_execution_id="execution-id",
+ task_id="task-id",
+ stream=True,
+ extras={"trace_session_id": "session-1"},
+ single_iteration_run=single_iteration_run,
+ single_loop_run=single_loop_run,
+ )
workflow = Workflow(
tenant_id="tenant",
@@ -71,7 +75,7 @@ def test_run_uses_single_node_execution_branch(
runner = WorkflowAppRunner(
application_generate_entity=app_generate_entity,
- queue_manager=MagicMock(spec=AppQueueManager),
+ queue_manager=queue_manager,
variable_loader=MagicMock(),
workflow=workflow,
system_user_id="system-user",
@@ -118,9 +122,9 @@ def test_run_uses_single_node_execution_branch(
assert entry_kwargs["graph_runtime_state"] is graph_runtime_state
-def test_single_node_run_validates_target_node_config() -> None:
+def test_single_node_run_validates_target_node_config(queue_manager: WorkflowAppQueueManager) -> None:
runner = WorkflowBasedAppRunner(
- queue_manager=MagicMock(spec=AppQueueManager),
+ queue_manager=queue_manager,
variable_loader=MagicMock(),
app_id="app",
)
@@ -155,25 +159,19 @@ def test_single_node_run_validates_target_node_config() -> None:
)
-def test_run_adds_inputs_with_snippet_compatible_start_aliases() -> None:
- app_config = MagicMock()
- app_config.app_id = "app"
- app_config.tenant_id = "tenant"
- app_config.workflow_id = "workflow"
-
- app_generate_entity = MagicMock(spec=WorkflowAppGenerateEntity)
- app_generate_entity.app_config = app_config
- app_generate_entity.inputs = {"question": "hello"}
- app_generate_entity.files = []
- app_generate_entity.user_id = "user"
- app_generate_entity.invoke_from = InvokeFrom.SERVICE_API
- app_generate_entity.workflow_execution_id = "execution-id"
- app_generate_entity.task_id = "task-id"
- app_generate_entity.call_depth = 0
- app_generate_entity.trace_manager = None
- app_generate_entity.extras = {}
- app_generate_entity.single_iteration_run = None
- app_generate_entity.single_loop_run = None
+def test_run_adds_inputs_with_snippet_compatible_start_aliases(queue_manager: WorkflowAppQueueManager) -> None:
+ app_generate_entity = WorkflowAppGenerateEntity(
+ app_config=WorkflowUIBasedAppConfig(
+ app_id="app", tenant_id="tenant", workflow_id="workflow", app_mode=AppMode.WORKFLOW
+ ),
+ inputs={"question": "hello"},
+ files=[],
+ user_id="user",
+ invoke_from=InvokeFrom.SERVICE_API,
+ workflow_execution_id="execution-id",
+ task_id="task-id",
+ stream=True,
+ )
workflow = Workflow(
tenant_id="tenant",
@@ -188,7 +186,7 @@ def test_run_adds_inputs_with_snippet_compatible_start_aliases() -> None:
runner = WorkflowAppRunner(
application_generate_entity=app_generate_entity,
- queue_manager=MagicMock(spec=AppQueueManager),
+ queue_manager=queue_manager,
variable_loader=MagicMock(),
workflow=workflow,
system_user_id="system-user",