From f100adf0ea65155f0c549396f6be44f58e1642c2 Mon Sep 17 00:00:00 2001 From: Asuka Minato Date: Fri, 2 Oct 2026 13:53:31 +0000 Subject: [PATCH] test: use concrete workflow composition dependencies (#43210) --- .../core/workflow/test_node_factory.py | 11 +++--- .../core/workflow/test_node_runtime.py | 6 ++-- .../test_human_input_delivery_test_service.py | 34 ++++++++++++------- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/api/tests/unit_tests/core/workflow/test_node_factory.py b/api/tests/unit_tests/core/workflow/test_node_factory.py index f5c7b022740..43788877965 100644 --- a/api/tests/unit_tests/core/workflow/test_node_factory.py +++ b/api/tests/unit_tests/core/workflow/test_node_factory.py @@ -1,6 +1,6 @@ from collections.abc import Mapping from types import SimpleNamespace -from unittest.mock import MagicMock, Mock, patch, sentinel +from unittest.mock import MagicMock, patch, sentinel import pytest from sqlalchemy import Engine @@ -28,6 +28,7 @@ from graphon.nodes.parameter_extractor.entities import ParameterExtractorNodeDat from graphon.variables.segments import ArrayObjectSegment, ObjectSegment, StringSegment from models.base import TypeBase from models.model import AppMode, Conversation, ConversationFromSource +from tests.unit_tests.core.model_fixtures import make_model_instance @pytest.fixture @@ -563,7 +564,7 @@ class TestDifyNodeFactoryCreateNode: }, } ) - wrapped_model_instance = MagicMock(spec=DifyPreparedLLM) + wrapped_model_instance = DifyPreparedLLM(make_model_instance(provider="openai", model="gpt-4")) memory = sentinel.memory factory._build_model_instance_for_llm_node = MagicMock(return_value=sentinel.model_instance) factory._build_memory_for_llm_node = MagicMock(return_value=memory) @@ -592,7 +593,7 @@ class TestDifyNodeFactoryCreateNode: request_metadata={"app_id": "app-id"}, ) assert kwargs["model_instance"] is wrapped_model_instance - assert kwargs["polling_finalizer"] is wrapped_model_instance.finalize_llm_polling + assert kwargs["polling_finalizer"] == wrapped_model_instance.finalize_llm_polling def test_resolve_llm_model_reference_uses_shared_model_and_parameters(self, factory): node_data = LLMNodeData.model_validate( @@ -721,7 +722,7 @@ class TestDifyNodeFactoryCreateNode: plugin_runtime = PluginModelRuntime( tenant_id="tenant-id", user_id="user-id", - client=Mock(spec=PluginModelClient), + client=PluginModelClient(), plugin_service=PluginService, ) model_instance = _ModelInstanceStub( @@ -749,7 +750,7 @@ class TestDifyNodeFactoryCreateNode: plugin_runtime = PluginModelRuntime( tenant_id="tenant-id", user_id="user-id", - client=Mock(spec=PluginModelClient), + client=PluginModelClient(), plugin_service=PluginService, ) model_instance = _ModelInstanceStub( diff --git a/api/tests/unit_tests/core/workflow/test_node_runtime.py b/api/tests/unit_tests/core/workflow/test_node_runtime.py index a8ca5de4ac7..5c4262da297 100644 --- a/api/tests/unit_tests/core/workflow/test_node_runtime.py +++ b/api/tests/unit_tests/core/workflow/test_node_runtime.py @@ -319,7 +319,7 @@ def test_dify_prepared_polling_llm_delegates_to_plugin_runtime() -> None: plugin_runtime = PluginModelRuntime( tenant_id="tenant-id", user_id="user-id", - client=Mock(spec=PluginModelClient), + client=PluginModelClient(), plugin_service=PluginService, ) plugin_runtime.start_llm_polling = Mock(return_value=polling_result) # type: ignore[method-assign] @@ -384,7 +384,7 @@ def test_dify_prepared_polling_llm_commits_successful_reservation() -> None: plugin_runtime = PluginModelRuntime( tenant_id="tenant-id", user_id="user-id", - client=Mock(spec=PluginModelClient), + client=PluginModelClient(), plugin_service=PluginService, ) plugin_runtime.start_llm_polling = Mock(return_value=running_result) # type: ignore[method-assign] @@ -418,7 +418,7 @@ def test_dify_prepared_polling_llm_releases_previous_reservation_on_restart() -> plugin_runtime = PluginModelRuntime( tenant_id="tenant-id", user_id="user-id", - client=Mock(spec=PluginModelClient), + client=PluginModelClient(), plugin_service=PluginService, ) plugin_runtime.start_llm_polling = Mock(return_value=running_result) # type: ignore[method-assign] diff --git a/api/tests/unit_tests/services/test_human_input_delivery_test_service.py b/api/tests/unit_tests/services/test_human_input_delivery_test_service.py index 8929d49702f..541fcdce391 100644 --- a/api/tests/unit_tests/services/test_human_input_delivery_test_service.py +++ b/api/tests/unit_tests/services/test_human_input_delivery_test_service.py @@ -8,6 +8,7 @@ from uuid import uuid4 import pytest from flask import Flask +from pytest_mock import MockerFixture from sqlalchemy.engine import Engine from sqlalchemy.orm import Session @@ -73,7 +74,9 @@ class TestDeliveryTestRegistry: handler.send_test.return_value = DeliveryTestResult(status=DeliveryTestStatus.OK) registry = DeliveryTestRegistry([handler]) - context = MagicMock(spec=DeliveryTestContext) + context = DeliveryTestContext( + tenant_id="t1", app_id="a1", node_id="n1", node_title="title", rendered_content="content" + ) method = MagicMock() result = registry.dispatch(context=context, method=method) @@ -87,7 +90,9 @@ class TestDeliveryTestRegistry: handler.supports.return_value = False registry = DeliveryTestRegistry([handler]) - context = MagicMock(spec=DeliveryTestContext) + context = DeliveryTestContext( + tenant_id="t1", app_id="a1", node_id="n1", node_title="title", rendered_content="content" + ) method = MagicMock() with pytest.raises(DeliveryTestUnsupportedError, match="Delivery method does not support test send."): @@ -105,21 +110,26 @@ class TestDeliveryTestRegistry: assert isinstance(registry._handlers[0], EmailDeliveryTestHandler) -def test_human_input_delivery_test_service(): - registry = MagicMock(spec=DeliveryTestRegistry) +def test_human_input_delivery_test_service(mocker: MockerFixture, sqlite_engine: Engine): + handler = EmailDeliveryTestHandler(session_factory=sqlite_engine) + send_test = mocker.patch.object(handler, "send_test", return_value=DeliveryTestResult(status=DeliveryTestStatus.OK)) + registry = DeliveryTestRegistry([handler]) + dispatch = mocker.spy(registry, "dispatch") service = HumanInputDeliveryTestService(registry=registry) - context = MagicMock(spec=DeliveryTestContext) - method = MagicMock() + context = DeliveryTestContext( + tenant_id="t1", app_id="a1", node_id="n1", node_title="title", rendered_content="content" + ) + method = EmailDeliveryMethod(config=_make_valid_email_config()) - service.send_test(context=context, method=method) - registry.dispatch.assert_called_once_with(context=context, method=method) + assert service.send_test(context=context, method=method).status == DeliveryTestStatus.OK + dispatch.assert_called_once_with(context=context, method=method) + send_test.assert_called_once_with(context=context, method=method) class TestEmailDeliveryTestHandler: - def test_init_with_engine(self): - engine = MagicMock(spec=Engine) - handler = EmailDeliveryTestHandler(session_factory=engine) - assert handler._session_factory.kw["bind"] == engine + def test_init_with_engine(self, sqlite_engine: Engine): + handler = EmailDeliveryTestHandler(session_factory=sqlite_engine) + assert handler._session_factory.kw["bind"] is sqlite_engine def test_supports(self, sqlite_engine: Engine) -> None: handler = EmailDeliveryTestHandler(session_factory=sqlite_engine)