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: move mocked model and tool cases to unit tests (#40708)

Asuka Minato3ecceb744ba8
diff --git a/api/tests/integration_tests/tools/__mock/http.py b/api/tests/unit_tests/core/tools/custom_tool/api_tool_http_fixture.py
similarity index 56%
rename from api/tests/integration_tests/tools/__mock/http.py
rename to api/tests/unit_tests/core/tools/custom_tool/api_tool_http_fixture.py
index fb2e3abcee7..7c0fe8bc1cc 100644
--- a/api/tests/integration_tests/tools/__mock/http.py
+++ b/api/tests/unit_tests/core/tools/custom_tool/api_tool_http_fixture.py
@@ -1,4 +1,7 @@
+"""HTTP fixture for API-tool unit tests."""
+
import json
+from collections.abc import Generator
from typing import Literal
import httpx
@@ -10,15 +13,19 @@ from core.helper import ssrf_proxy
class MockedHttp:
@staticmethod
def httpx_request(
- method: Literal["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD"], url: str, **kwargs
+ method: Literal["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD"],
+ url: str,
+ *,
+ params: dict[str, str] | None = None,
+ headers: dict[str, str] | None = None,
+ cookies: dict[str, str] | None = None,
+ data: object = None,
+ **_kwargs: object,
) -> httpx.Response:
"""
Mocked httpx.request
"""
- request = httpx.Request(
- method, url, params=kwargs.get("params"), headers=kwargs.get("headers"), cookies=kwargs.get("cookies")
- )
- data = kwargs.get("data")
+ request = httpx.Request(method, url, params=params, headers=headers, cookies=cookies)
resp = json.dumps(data).encode("utf-8") if data else b"OK"
response = httpx.Response(
status_code=200,
@@ -29,7 +36,7 @@ class MockedHttp:
@pytest.fixture
-def setup_http_mock(request, monkeypatch: pytest.MonkeyPatch):
+def setup_http_mock(monkeypatch: pytest.MonkeyPatch) -> Generator[None, None, None]:
monkeypatch.setattr(ssrf_proxy, "make_request", MockedHttp.httpx_request)
yield
monkeypatch.undo()
diff --git a/api/tests/integration_tests/tools/api_tool/test_api_tool.py b/api/tests/unit_tests/core/tools/custom_tool/test_api_tool_request_assembly.py
similarity index 90%
rename from api/tests/integration_tests/tools/api_tool/test_api_tool.py
rename to api/tests/unit_tests/core/tools/custom_tool/test_api_tool_request_assembly.py
index 9079aa7d6d2..58c3130ac55 100644
--- a/api/tests/integration_tests/tools/api_tool/test_api_tool.py
+++ b/api/tests/unit_tests/core/tools/custom_tool/test_api_tool_request_assembly.py
@@ -1,12 +1,14 @@
+import pytest
+
from core.tools.__base.tool_runtime import ToolRuntime
from core.tools.custom_tool.tool import ApiTool
from core.tools.entities.common_entities import I18nObject
from core.tools.entities.tool_bundle import ApiToolBundle
from core.tools.entities.tool_entities import ToolEntity, ToolIdentity
-pytest_plugins = ("tests.integration_tests.tools.__mock.http",)
+pytest_plugins = ("tests.unit_tests.core.tools.custom_tool.api_tool_http_fixture",)
-tool_bundle = {
+tool_bundle: dict[str, object] = {
"server_url": "http://www.example.com/{path_param}",
"method": "post",
"author": "",
@@ -32,7 +34,8 @@ parameters = {
}
-def test_api_tool(setup_http_mock):
[email protected]("setup_http_mock")
+def test_api_tool() -> None:
tool = ApiTool(
entity=ToolEntity(
identity=ToolIdentity(provider="", author="", name="", label=I18nObject(en_US="test tool")),
diff --git a/api/tests/integration_tests/workflow/nodes/test_llm.py b/api/tests/unit_tests/core/workflow/nodes/llm/test_llm_node_execution_behavior.py
similarity index 92%
rename from api/tests/integration_tests/workflow/nodes/test_llm.py
rename to api/tests/unit_tests/core/workflow/nodes/llm/test_llm_node_execution_behavior.py
index 2e9b7317f69..f8224772207 100644
--- a/api/tests/integration_tests/workflow/nodes/test_llm.py
+++ b/api/tests/unit_tests/core/workflow/nodes/llm/test_llm_node_execution_behavior.py
@@ -1,3 +1,5 @@
+"""Unit coverage for LLM node execution with model boundaries replaced by test doubles."""
+
import json
import time
import uuid
@@ -12,6 +14,7 @@ from core.model_manager import ModelInstance
from core.workflow.system_variables import build_system_variables
from extensions.ext_database import db
from graphon.enums import WorkflowNodeExecutionStatus
+from graphon.model_runtime.entities.message_entities import PromptMessage
from graphon.node_events import StreamCompletedEvent
from graphon.nodes.llm.entities import LLMNodeData
from graphon.nodes.llm.file_saver import LLMFileSaver
@@ -25,7 +28,7 @@ from tests.workflow_test_utils import build_test_graph_init_params
"""FOR MOCK FIXTURES, DO NOT REMOVE"""
-def init_llm_node(config: dict) -> LLMNode:
+def init_llm_node(config: dict[str, object]) -> LLMNode:
graph_config = {
"edges": [
{
@@ -72,7 +75,7 @@ def init_llm_node(config: dict) -> LLMNode:
graph_runtime_state = GraphRuntimeState(variable_pool=variable_pool, start_at=time.perf_counter())
prompt_message_serializer = MagicMock(spec=PromptMessageSerializerProtocol)
- prompt_message_serializer.serialize.side_effect = lambda *, model_mode, prompt_messages: [
+ prompt_message_serializer.serialize.side_effect = lambda *, prompt_messages, **_kwargs: [
message.model_dump(mode="json") for message in prompt_messages
]
llm_file_saver = MagicMock(spec=LLMFileSaver)
@@ -97,7 +100,7 @@ def _mock_db_session_close(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(db.session, "close", MagicMock())
-def test_execute_llm(monkeypatch: pytest.MonkeyPatch):
+def test_execute_llm(monkeypatch: pytest.MonkeyPatch) -> None:
node = init_llm_node(
config={
"id": "llm",
@@ -137,9 +140,9 @@ def test_execute_llm(monkeypatch: pytest.MonkeyPatch):
mock_model_instance = MagicMock(spec=ModelInstance)
mock_model_instance.provider = "openai"
mock_model_instance.model_name = "gpt-3.5-turbo"
- mock_model_instance.credentials = {}
- mock_model_instance.parameters = {}
- mock_model_instance.stop = []
+ mock_model_instance.credentials = dict[str, object]()
+ mock_model_instance.parameters = dict[str, object]()
+ mock_model_instance.stop = list[str]()
mock_model_instance.model_type_instance = MagicMock()
mock_model_instance.model_type_instance.get_model_schema.return_value = MagicMock(
model_properties={},
@@ -174,7 +177,7 @@ def test_execute_llm(monkeypatch: pytest.MonkeyPatch):
return mock_model_instance
# Mock fetch_prompt_messages to avoid database calls
- def mock_fetch_prompt_messages_1(**_kwargs):
+ def mock_fetch_prompt_messages_1(**_kwargs: object) -> tuple[list[PromptMessage], list[str]]:
from graphon.model_runtime.entities.message_entities import SystemPromptMessage, UserPromptMessage
return [
@@ -191,17 +194,16 @@ def test_execute_llm(monkeypatch: pytest.MonkeyPatch):
for item in result:
if isinstance(item, StreamCompletedEvent):
- if item.node_run_result.status != WorkflowNodeExecutionStatus.SUCCEEDED:
- print(f"Error: {item.node_run_result.error}")
- print(f"Error type: {item.node_run_result.error_type}")
- assert item.node_run_result.status == WorkflowNodeExecutionStatus.SUCCEEDED
+ assert item.node_run_result.status == WorkflowNodeExecutionStatus.SUCCEEDED, (
+ f"{item.node_run_result.error_type}: {item.node_run_result.error}"
+ )
assert item.node_run_result.process_data is not None
assert item.node_run_result.outputs is not None
assert item.node_run_result.outputs.get("text") is not None
assert item.node_run_result.outputs.get("usage", {})["total_tokens"] > 0
-def test_execute_llm_with_jinja2(monkeypatch: pytest.MonkeyPatch):
+def test_execute_llm_with_jinja2(monkeypatch: pytest.MonkeyPatch) -> None:
"""
Test execute LLM node with jinja2
"""
@@ -252,9 +254,9 @@ def test_execute_llm_with_jinja2(monkeypatch: pytest.MonkeyPatch):
mock_model_instance = MagicMock(spec=ModelInstance)
mock_model_instance.provider = "openai"
mock_model_instance.model_name = "gpt-3.5-turbo"
- mock_model_instance.credentials = {}
- mock_model_instance.parameters = {}
- mock_model_instance.stop = []
+ mock_model_instance.credentials = dict[str, object]()
+ mock_model_instance.parameters = dict[str, object]()
+ mock_model_instance.stop = list[str]()
mock_model_instance.model_type_instance = MagicMock()
mock_model_instance.model_type_instance.get_model_schema.return_value = MagicMock(
model_properties={},
@@ -289,7 +291,7 @@ def test_execute_llm_with_jinja2(monkeypatch: pytest.MonkeyPatch):
return mock_model_instance
# Mock fetch_prompt_messages to avoid database calls
- def mock_fetch_prompt_messages_2(**_kwargs):
+ def mock_fetch_prompt_messages_2(**_kwargs: object) -> tuple[list[PromptMessage], list[str]]:
from graphon.model_runtime.entities.message_entities import SystemPromptMessage, UserPromptMessage
return [
@@ -311,7 +313,7 @@ def test_execute_llm_with_jinja2(monkeypatch: pytest.MonkeyPatch):
assert "what's the weather today?" in json.dumps(item.node_run_result.process_data)
-def test_extract_json():
+def test_extract_json() -> None:
llm_texts = [
'<think>\n\n</think>{"name": "test", "age": 123', # resoning model (deepseek-r1)
'{"name":"test","age":123}', # json schema model (gpt-4o)
diff --git a/api/tests/integration_tests/workflow/nodes/test_tool.py b/api/tests/unit_tests/core/workflow/nodes/tool/test_tool_node_invocation_behavior.py
similarity index 95%
rename from api/tests/integration_tests/workflow/nodes/test_tool.py
rename to api/tests/unit_tests/core/workflow/nodes/tool/test_tool_node_invocation_behavior.py
index d3e248cc28c..243dfccf83c 100644
--- a/api/tests/integration_tests/workflow/nodes/test_tool.py
+++ b/api/tests/unit_tests/core/workflow/nodes/tool/test_tool_node_invocation_behavior.py
@@ -1,9 +1,9 @@
+"""Unit coverage for tool-node invocation with the tool boundary patched."""
+
import time
import uuid
from unittest.mock import MagicMock, patch
-import pytest
-
from core.app.entities.app_invoke_entities import InvokeFrom, UserFrom
from core.tools.utils.configuration import ToolParameterConfigurationManager
from core.workflow.node_factory import DifyNodeFactory
@@ -19,7 +19,7 @@ from graphon.runtime import GraphRuntimeState, VariablePool
from tests.workflow_test_utils import build_test_graph_init_params
-def init_tool_node(config: dict):
+def init_tool_node(config: dict[str, object]) -> ToolNode:
graph_config = {
"edges": [
{
@@ -74,7 +74,7 @@ def init_tool_node(config: dict):
return node
-def test_tool_variable_invoke(monkeypatch: pytest.MonkeyPatch):
+def test_tool_variable_invoke() -> None:
node = init_tool_node(
config={
"id": "1",
@@ -109,7 +109,7 @@ def test_tool_variable_invoke(monkeypatch: pytest.MonkeyPatch):
assert item.node_run_result.outputs.get("text") is not None
-def test_tool_mixed_invoke(monkeypatch: pytest.MonkeyPatch):
+def test_tool_mixed_invoke() -> None:
node = init_tool_node(
config={
"id": "1",