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 real datasource provider fixtures (#43148)

Asuka Minato6dc9de24d8ed
diff --git a/api/tests/unit_tests/core/datasource/__base/test_datasource_plugin.py b/api/tests/unit_tests/core/datasource/__base/test_datasource_plugin.py
index 15f3d7e43ce..bfa7919bda8 100644
--- a/api/tests/unit_tests/core/datasource/__base/test_datasource_plugin.py
+++ b/api/tests/unit_tests/core/datasource/__base/test_datasource_plugin.py
@@ -1,9 +1,9 @@
from collections.abc import Callable
-from unittest.mock import MagicMock
from core.datasource.__base.datasource_plugin import DatasourcePlugin
from core.datasource.__base.datasource_runtime import DatasourceRuntime
-from core.datasource.entities.datasource_entities import DatasourceEntity, DatasourceProviderType
+from core.datasource.entities.datasource_entities import DatasourceProviderType
+from tests.unit_tests.core.datasource.factories import datasource_entity
class ConcreteDatasourcePlugin(DatasourcePlugin):
@@ -19,8 +19,8 @@ class ConcreteDatasourcePlugin(DatasourcePlugin):
class TestDatasourcePlugin:
def test_init(self):
# Arrange
- entity = MagicMock(spec=DatasourceEntity)
- runtime = MagicMock(spec=DatasourceRuntime)
+ entity = datasource_entity("test_datasource")
+ runtime = DatasourceRuntime(tenant_id="tenant-1")
icon = "test-icon.png"
# Act
@@ -33,8 +33,8 @@ class TestDatasourcePlugin:
def test_datasource_provider_type(self):
# Arrange
- entity = MagicMock(spec=DatasourceEntity)
- runtime = MagicMock(spec=DatasourceRuntime)
+ entity = datasource_entity("test_datasource")
+ runtime = DatasourceRuntime(tenant_id="tenant-1")
icon = "test-icon.png"
plugin = ConcreteDatasourcePlugin(entity=entity, runtime=runtime, icon=icon)
@@ -49,31 +49,31 @@ class TestDatasourcePlugin:
def test_fork_datasource_runtime(self):
# Arrange
- mock_entity = MagicMock(spec=DatasourceEntity)
- mock_entity_copy = MagicMock(spec=DatasourceEntity)
- mock_entity.model_copy.return_value = mock_entity_copy
+ entity = datasource_entity("test_datasource")
- runtime = MagicMock(spec=DatasourceRuntime)
- new_runtime = MagicMock(spec=DatasourceRuntime)
+ runtime = DatasourceRuntime(tenant_id="tenant-1")
+ new_runtime = DatasourceRuntime(tenant_id="tenant-2", credentials={"token": "new-token"})
icon = "test-icon.png"
- plugin = ConcreteDatasourcePlugin(entity=mock_entity, runtime=runtime, icon=icon)
+ plugin = ConcreteDatasourcePlugin(entity=entity, runtime=runtime, icon=icon)
# Act
new_plugin = plugin.fork_datasource_runtime(new_runtime)
# Assert
assert isinstance(new_plugin, ConcreteDatasourcePlugin)
- assert new_plugin.entity == mock_entity_copy
- assert new_plugin.runtime == new_runtime
+ assert new_plugin.entity == entity
+ assert new_plugin.entity is not entity
+ assert new_plugin.runtime is new_runtime
assert new_plugin.icon == icon
- mock_entity.model_copy.assert_called_once()
+ assert plugin.runtime is runtime
+ assert plugin.runtime.credentials == {}
def test_get_icon_url(self, config_overrides: Callable[..., None]):
config_overrides(CONSOLE_API_URL="https://api.dify.ai")
# Arrange
- entity = MagicMock(spec=DatasourceEntity)
- runtime = MagicMock(spec=DatasourceRuntime)
+ entity = datasource_entity("test_datasource")
+ runtime = DatasourceRuntime(tenant_id="tenant-1")
icon = "test-icon.png"
tenant_id = "test-tenant-id"
diff --git a/api/tests/unit_tests/core/datasource/__base/test_datasource_provider.py b/api/tests/unit_tests/core/datasource/__base/test_datasource_provider.py
index 7e3cc722b0d..025e4a6242a 100644
--- a/api/tests/unit_tests/core/datasource/__base/test_datasource_provider.py
+++ b/api/tests/unit_tests/core/datasource/__base/test_datasource_provider.py
@@ -1,15 +1,16 @@
-from unittest.mock import MagicMock, patch
+from unittest.mock import patch
import pytest
from core.datasource.__base.datasource_plugin import DatasourcePlugin
from core.datasource.__base.datasource_provider import DatasourcePluginProviderController
from core.datasource.entities.datasource_entities import (
- DatasourceProviderEntityWithPlugin,
DatasourceProviderType,
)
from core.entities.provider_entities import ProviderConfig, ProviderConfigType
+from core.tools.entities.common_entities import I18nObject
from core.tools.errors import ToolProviderCredentialValidationError
+from tests.unit_tests.core.datasource.factories import provider_entity
class ConcreteDatasourcePluginProviderController(DatasourcePluginProviderController):
@@ -18,52 +19,50 @@ class ConcreteDatasourcePluginProviderController(DatasourcePluginProviderControl
"""
def get_datasource(self, datasource_name: str) -> DatasourcePlugin:
- return MagicMock(spec=DatasourcePlugin)
+ raise AssertionError("Credential validation must not fetch a datasource")
class TestDatasourcePluginProviderController:
def test_init(self):
# Arrange
- mock_entity = MagicMock(spec=DatasourceProviderEntityWithPlugin)
+ entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
tenant_id = "test-tenant-id"
# Act
- controller = ConcreteDatasourcePluginProviderController(entity=mock_entity, tenant_id=tenant_id)
+ controller = ConcreteDatasourcePluginProviderController(entity=entity, tenant_id=tenant_id)
# Assert
- assert controller.entity == mock_entity
+ assert controller.entity == entity
assert controller.tenant_id == tenant_id
def test_need_credentials(self):
# Arrange
- mock_entity = MagicMock(spec=DatasourceProviderEntityWithPlugin)
+ entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
tenant_id = "test-tenant-id"
- controller = ConcreteDatasourcePluginProviderController(entity=mock_entity, tenant_id=tenant_id)
+ controller = ConcreteDatasourcePluginProviderController(entity=entity, tenant_id=tenant_id)
# Case 1: credentials_schema is None
- mock_entity.credentials_schema = None
+ entity.credentials_schema = None
assert controller.need_credentials is False
# Case 2: credentials_schema is empty
- mock_entity.credentials_schema = []
+ entity.credentials_schema = []
assert controller.need_credentials is False
# Case 3: credentials_schema has items
- mock_entity.credentials_schema = [MagicMock()]
+ entity.credentials_schema = [ProviderConfig(name="api_key", type=ProviderConfigType.SECRET_INPUT)]
assert controller.need_credentials is True
@patch("core.datasource.__base.datasource_provider.PluginToolManager")
def test_validate_credentials(self, mock_manager_class):
# Arrange
mock_manager = mock_manager_class.return_value
- mock_entity = MagicMock(spec=DatasourceProviderEntityWithPlugin)
- mock_entity.identity = MagicMock()
- mock_entity.identity.name = "test-provider"
+ entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
tenant_id = "test-tenant-id"
user_id = "test-user-id"
credentials = {"api_key": "secret"}
- controller = ConcreteDatasourcePluginProviderController(entity=mock_entity, tenant_id=tenant_id)
+ controller = ConcreteDatasourcePluginProviderController(entity=entity, tenant_id=tenant_id)
# Act: Successful validation
mock_manager.validate_datasource_credentials.return_value = True
@@ -83,17 +82,17 @@ class TestDatasourcePluginProviderController:
def test_provider_type(self):
# Arrange
- mock_entity = MagicMock(spec=DatasourceProviderEntityWithPlugin)
- controller = ConcreteDatasourcePluginProviderController(entity=mock_entity, tenant_id="test")
+ entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
+ controller = ConcreteDatasourcePluginProviderController(entity=entity, tenant_id="test")
# Act & Assert
assert controller.provider_type == DatasourceProviderType.LOCAL_FILE
def test_validate_credentials_format_empty_schema(self):
# Arrange
- mock_entity = MagicMock(spec=DatasourceProviderEntityWithPlugin)
- mock_entity.credentials_schema = []
- controller = ConcreteDatasourcePluginProviderController(entity=mock_entity, tenant_id="test")
+ entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
+ entity.credentials_schema = []
+ controller = ConcreteDatasourcePluginProviderController(entity=entity, tenant_id="test")
credentials = {}
# Act & Assert (Should not raise anything)
@@ -101,11 +100,9 @@ class TestDatasourcePluginProviderController:
def test_validate_credentials_format_unknown_credential(self):
# Arrange
- mock_entity = MagicMock(spec=DatasourceProviderEntityWithPlugin)
- mock_entity.identity = MagicMock()
- mock_entity.identity.name = "test-provider"
- mock_entity.credentials_schema = []
- controller = ConcreteDatasourcePluginProviderController(entity=mock_entity, tenant_id="test")
+ entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
+ entity.credentials_schema = []
+ controller = ConcreteDatasourcePluginProviderController(entity=entity, tenant_id="test")
credentials = {"unknown": "value"}
# Act & Assert
@@ -116,13 +113,15 @@ class TestDatasourcePluginProviderController:
def test_validate_credentials_format_required_missing(self):
# Arrange
- mock_config = MagicMock(spec=ProviderConfig)
- mock_config.name = "api_key"
- mock_config.required = True
+ config = ProviderConfig(
+ name="api_key",
+ required=True,
+ type=ProviderConfigType.TEXT_INPUT,
+ )
- mock_entity = MagicMock(spec=DatasourceProviderEntityWithPlugin)
- mock_entity.credentials_schema = [mock_config]
- controller = ConcreteDatasourcePluginProviderController(entity=mock_entity, tenant_id="test")
+ entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
+ entity.credentials_schema = [config]
+ controller = ConcreteDatasourcePluginProviderController(entity=entity, tenant_id="test")
# Act & Assert
with pytest.raises(ToolProviderCredentialValidationError, match="credential api_key is required"):
@@ -130,14 +129,16 @@ class TestDatasourcePluginProviderController:
def test_validate_credentials_format_not_required_null(self):
# Arrange
- mock_config = MagicMock(spec=ProviderConfig)
- mock_config.name = "optional"
- mock_config.required = False
- mock_config.default = None
+ config = ProviderConfig(
+ name="optional",
+ required=False,
+ default=None,
+ type=ProviderConfigType.TEXT_INPUT,
+ )
- mock_entity = MagicMock(spec=DatasourceProviderEntityWithPlugin)
- mock_entity.credentials_schema = [mock_config]
- controller = ConcreteDatasourcePluginProviderController(entity=mock_entity, tenant_id="test")
+ entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
+ entity.credentials_schema = [config]
+ controller = ConcreteDatasourcePluginProviderController(entity=entity, tenant_id="test")
# Act & Assert
credentials = {"optional": None}
@@ -146,14 +147,15 @@ class TestDatasourcePluginProviderController:
def test_validate_credentials_format_type_mismatch_text(self):
# Arrange
- mock_config = MagicMock(spec=ProviderConfig)
- mock_config.name = "text_field"
- mock_config.required = True
- mock_config.type = ProviderConfigType.TEXT_INPUT
+ config = ProviderConfig(
+ name="text_field",
+ required=True,
+ type=ProviderConfigType.TEXT_INPUT,
+ )
- mock_entity = MagicMock(spec=DatasourceProviderEntityWithPlugin)
- mock_entity.credentials_schema = [mock_config]
- controller = ConcreteDatasourcePluginProviderController(entity=mock_entity, tenant_id="test")
+ entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
+ entity.credentials_schema = [config]
+ controller = ConcreteDatasourcePluginProviderController(entity=entity, tenant_id="test")
# Act & Assert
with pytest.raises(ToolProviderCredentialValidationError, match="credential text_field should be string"):
@@ -161,39 +163,39 @@ class TestDatasourcePluginProviderController:
def test_validate_credentials_format_select_validation(self):
# Arrange
- mock_option = MagicMock()
- mock_option.value = "opt1"
+ option = ProviderConfig.Option(value="opt1", label=I18nObject(en_US="Option 1"))
- mock_config = MagicMock(spec=ProviderConfig)
- mock_config.name = "select_field"
- mock_config.required = True
- mock_config.type = ProviderConfigType.SELECT
- mock_config.options = [mock_option]
+ config = ProviderConfig(
+ name="select_field",
+ required=True,
+ type=ProviderConfigType.SELECT,
+ options=[option],
+ )
- mock_entity = MagicMock(spec=DatasourceProviderEntityWithPlugin)
- mock_entity.credentials_schema = [mock_config]
- controller = ConcreteDatasourcePluginProviderController(entity=mock_entity, tenant_id="test")
+ entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
+ entity.credentials_schema = [config]
+ controller = ConcreteDatasourcePluginProviderController(entity=entity, tenant_id="test")
# Case 1: Value not string
with pytest.raises(ToolProviderCredentialValidationError, match="credential select_field should be string"):
controller.validate_credentials_format({"select_field": 123})
# Case 2: Options not list
- mock_config.options = "invalid"
+ config.options = "invalid"
with pytest.raises(
ToolProviderCredentialValidationError, match="credential select_field options should be list"
):
controller.validate_credentials_format({"select_field": "opt1"})
# Case 3: Value not in options
- mock_config.options = [mock_option]
+ config.options = [option]
with pytest.raises(ToolProviderCredentialValidationError, match="credential select_field should be one of"):
controller.validate_credentials_format({"select_field": "invalid_opt"})
def test_get_datasource_base(self):
# Arrange
- mock_entity = MagicMock(spec=DatasourceProviderEntityWithPlugin)
- controller = ConcreteDatasourcePluginProviderController(entity=mock_entity, tenant_id="test")
+ entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
+ controller = ConcreteDatasourcePluginProviderController(entity=entity, tenant_id="test")
# Act
result = DatasourcePluginProviderController.get_datasource(controller, "test")
@@ -203,14 +205,15 @@ class TestDatasourcePluginProviderController:
def test_validate_credentials_format_hits_pop(self):
# Arrange
- mock_config = MagicMock(spec=ProviderConfig)
- mock_config.name = "valid_field"
- mock_config.required = True
- mock_config.type = ProviderConfigType.TEXT_INPUT
+ config = ProviderConfig(
+ name="valid_field",
+ required=True,
+ type=ProviderConfigType.TEXT_INPUT,
+ )
- mock_entity = MagicMock(spec=DatasourceProviderEntityWithPlugin)
- mock_entity.credentials_schema = [mock_config]
- controller = ConcreteDatasourcePluginProviderController(entity=mock_entity, tenant_id="test")
+ entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
+ entity.credentials_schema = [config]
+ controller = ConcreteDatasourcePluginProviderController(entity=entity, tenant_id="test")
# Act
credentials = {"valid_field": "valid_value"}
@@ -222,14 +225,16 @@ class TestDatasourcePluginProviderController:
def test_validate_credentials_format_hits_continue(self):
# Arrange
- mock_config = MagicMock(spec=ProviderConfig)
- mock_config.name = "optional_field"
- mock_config.required = False
- mock_config.default = None
+ config = ProviderConfig(
+ name="optional_field",
+ required=False,
+ default=None,
+ type=ProviderConfigType.TEXT_INPUT,
+ )
- mock_entity = MagicMock(spec=DatasourceProviderEntityWithPlugin)
- mock_entity.credentials_schema = [mock_config]
- controller = ConcreteDatasourcePluginProviderController(entity=mock_entity, tenant_id="test")
+ entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
+ entity.credentials_schema = [config]
+ controller = ConcreteDatasourcePluginProviderController(entity=entity, tenant_id="test")
# Act
credentials = {"optional_field": None}
@@ -240,21 +245,23 @@ class TestDatasourcePluginProviderController:
def test_validate_credentials_format_default_values(self):
# Arrange
- mock_config_text = MagicMock(spec=ProviderConfig)
- mock_config_text.name = "text_def"
- mock_config_text.required = False
- mock_config_text.type = ProviderConfigType.TEXT_INPUT
- mock_config_text.default = 123 # Int default, should be converted to str
-
- mock_config_other = MagicMock(spec=ProviderConfig)
- mock_config_other.name = "other_def"
- mock_config_other.required = False
- mock_config_other.type = "OTHER"
- mock_config_other.default = "fallback"
-
- mock_entity = MagicMock(spec=DatasourceProviderEntityWithPlugin)
- mock_entity.credentials_schema = [mock_config_text, mock_config_other]
- controller = ConcreteDatasourcePluginProviderController(entity=mock_entity, tenant_id="test")
+ config_text = ProviderConfig(
+ name="text_def",
+ required=False,
+ type=ProviderConfigType.TEXT_INPUT,
+ default=123, # Int default, should be converted to str
+ )
+
+ config_other = ProviderConfig.model_construct(
+ name="other_def",
+ required=False,
+ type="OTHER",
+ default="fallback",
+ )
+
+ entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
+ entity.credentials_schema = [config_text, config_other]
+ controller = ConcreteDatasourcePluginProviderController(entity=entity, tenant_id="test")
# Act
credentials = {}
diff --git a/api/tests/unit_tests/core/datasource/factories.py b/api/tests/unit_tests/core/datasource/factories.py
new file mode 100644
index 00000000000..b98556d235b
--- /dev/null
+++ b/api/tests/unit_tests/core/datasource/factories.py
@@ -0,0 +1,35 @@
+"""Real datasource entities shared by provider and plugin tests."""
+
+from core.datasource.entities.datasource_entities import (
+ DatasourceEntity,
+ DatasourceIdentity,
+ DatasourceProviderEntityWithPlugin,
+ DatasourceProviderIdentity,
+ DatasourceProviderType,
+)
+from core.tools.entities.common_entities import I18nObject
+
+
+def datasource_entity(name: str) -> DatasourceEntity:
+ return DatasourceEntity(
+ identity=DatasourceIdentity(
+ author="test",
+ name=name,
+ label=I18nObject(en_US=name),
+ provider="test-provider",
+ ),
+ description=I18nObject(en_US="Test datasource"),
+ )
+
+
+def provider_entity(provider_type: DatasourceProviderType) -> DatasourceProviderEntityWithPlugin:
+ return DatasourceProviderEntityWithPlugin(
+ identity=DatasourceProviderIdentity(
+ author="test",
+ name="test-provider",
+ description=I18nObject(en_US="Test provider"),
+ icon="icon.svg",
+ label=I18nObject(en_US="Test"),
+ ),
+ provider_type=provider_type,
+ )
diff --git a/api/tests/unit_tests/core/datasource/local_file/test_local_file_provider.py b/api/tests/unit_tests/core/datasource/local_file/test_local_file_provider.py
index af2369ac4e4..ab98b6a7fc4 100644
--- a/api/tests/unit_tests/core/datasource/local_file/test_local_file_provider.py
+++ b/api/tests/unit_tests/core/datasource/local_file/test_local_file_provider.py
@@ -1,42 +1,40 @@
-from unittest.mock import MagicMock
-
import pytest
from core.datasource.entities.datasource_entities import (
- DatasourceProviderEntityWithPlugin,
DatasourceProviderType,
)
from core.datasource.local_file.local_file_plugin import LocalFileDatasourcePlugin
from core.datasource.local_file.local_file_provider import LocalFileDatasourcePluginProviderController
+from tests.unit_tests.core.datasource.factories import datasource_entity, provider_entity
class TestLocalFileDatasourcePluginProviderController:
def test_init(self):
# Arrange
- mock_entity = MagicMock(spec=DatasourceProviderEntityWithPlugin)
+ entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
plugin_id = "test_plugin_id"
plugin_unique_identifier = "test_plugin_unique_identifier"
tenant_id = "test_tenant_id"
# Act
controller = LocalFileDatasourcePluginProviderController(
- entity=mock_entity,
+ entity=entity,
plugin_id=plugin_id,
plugin_unique_identifier=plugin_unique_identifier,
tenant_id=tenant_id,
)
# Assert
- assert controller.entity == mock_entity
+ assert controller.entity == entity
assert controller.plugin_id == plugin_id
assert controller.plugin_unique_identifier == plugin_unique_identifier
assert controller.tenant_id == tenant_id
def test_provider_type(self):
# Arrange
- mock_entity = MagicMock(spec=DatasourceProviderEntityWithPlugin)
+ entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
controller = LocalFileDatasourcePluginProviderController(
- entity=mock_entity, plugin_id="id", plugin_unique_identifier="unique_id", tenant_id="tenant"
+ entity=entity, plugin_id="id", plugin_unique_identifier="unique_id", tenant_id="tenant"
)
# Act & Assert
@@ -44,9 +42,9 @@ class TestLocalFileDatasourcePluginProviderController:
def test_validate_credentials(self):
# Arrange
- mock_entity = MagicMock(spec=DatasourceProviderEntityWithPlugin)
+ entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
controller = LocalFileDatasourcePluginProviderController(
- entity=mock_entity, plugin_id="id", plugin_unique_identifier="unique_id", tenant_id="tenant"
+ entity=entity, plugin_id="id", plugin_unique_identifier="unique_id", tenant_id="tenant"
)
# Act & Assert
@@ -55,18 +53,16 @@ class TestLocalFileDatasourcePluginProviderController:
def test_get_datasource_success(self):
# Arrange
- mock_datasource_entity = MagicMock()
- mock_datasource_entity.identity.name = "test_datasource"
-
- mock_entity = MagicMock()
- mock_entity.datasources = [mock_datasource_entity]
- mock_entity.identity.icon = "test_icon"
+ source = datasource_entity("test_datasource")
+ entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
+ entity.datasources = [source]
+ entity.identity.icon = "test_icon"
plugin_unique_identifier = "test_plugin_unique_identifier"
tenant_id = "test_tenant_id"
controller = LocalFileDatasourcePluginProviderController(
- entity=mock_entity, plugin_id="id", plugin_unique_identifier=plugin_unique_identifier, tenant_id=tenant_id
+ entity=entity, plugin_id="id", plugin_unique_identifier=plugin_unique_identifier, tenant_id=tenant_id
)
# Act
@@ -74,21 +70,18 @@ class TestLocalFileDatasourcePluginProviderController:
# Assert
assert isinstance(datasource, LocalFileDatasourcePlugin)
- assert datasource.entity == mock_datasource_entity
+ assert datasource.entity == source
assert datasource.tenant_id == tenant_id
assert datasource.icon == "test_icon"
assert datasource.plugin_unique_identifier == plugin_unique_identifier
def test_get_datasource_not_found(self):
# Arrange
- mock_datasource_entity = MagicMock()
- mock_datasource_entity.identity.name = "other_datasource"
-
- mock_entity = MagicMock()
- mock_entity.datasources = [mock_datasource_entity]
+ entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
+ entity.datasources = [datasource_entity("other_datasource")]
controller = LocalFileDatasourcePluginProviderController(
- entity=mock_entity, plugin_id="id", plugin_unique_identifier="unique_id", tenant_id="tenant"
+ entity=entity, plugin_id="id", plugin_unique_identifier="unique_id", tenant_id="tenant"
)
# Act & Assert