mirror of
https://github.com/langgenius/dify.git
synced 2026-10-04 13:47:55 +08:00
test: use concrete workflow composition dependencies (#43210)
This commit is contained in:
1 parent
63094c91ba
commit
f100adf0ea
3 files changed
+31
-20
No files matched your search
@@ -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(
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user