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.

refactor(api): enable strict typing for core datasource tests (#43886)

Eddy ZHANGdf13ccd51a3c
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 bfa7919bda8..0cf48bb30d2 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
@@ -17,7 +17,7 @@ class ConcreteDatasourcePlugin(DatasourcePlugin):
class TestDatasourcePlugin:
- def test_init(self):
+ def test_init(self) -> None:
# Arrange
entity = datasource_entity("test_datasource")
runtime = DatasourceRuntime(tenant_id="tenant-1")
@@ -31,7 +31,7 @@ class TestDatasourcePlugin:
assert plugin.runtime == runtime
assert plugin.icon == icon
- def test_datasource_provider_type(self):
+ def test_datasource_provider_type(self) -> None:
# Arrange
entity = datasource_entity("test_datasource")
runtime = DatasourceRuntime(tenant_id="tenant-1")
@@ -47,7 +47,7 @@ class TestDatasourcePlugin:
assert provider_type == DatasourceProviderType.LOCAL_FILE
assert base_provider_type == DatasourceProviderType.LOCAL_FILE
- def test_fork_datasource_runtime(self):
+ def test_fork_datasource_runtime(self) -> None:
# Arrange
entity = datasource_entity("test_datasource")
@@ -69,7 +69,7 @@ class TestDatasourcePlugin:
assert plugin.runtime is runtime
assert plugin.runtime.credentials == {}
- def test_get_icon_url(self, config_overrides: Callable[..., None]):
+ def test_get_icon_url(self, config_overrides: Callable[..., None]) -> None:
config_overrides(CONSOLE_API_URL="https://api.dify.ai")
# Arrange
entity = datasource_entity("test_datasource")
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 025e4a6242a..9059f1e1815 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,4 +1,4 @@
-from unittest.mock import patch
+from unittest.mock import MagicMock, patch
import pytest
@@ -23,7 +23,7 @@ class ConcreteDatasourcePluginProviderController(DatasourcePluginProviderControl
class TestDatasourcePluginProviderController:
- def test_init(self):
+ def test_init(self) -> None:
# Arrange
entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
tenant_id = "test-tenant-id"
@@ -35,26 +35,26 @@ class TestDatasourcePluginProviderController:
assert controller.entity == entity
assert controller.tenant_id == tenant_id
- def test_need_credentials(self):
+ @pytest.mark.parametrize(
+ ("credentials_schema", "expected"),
+ [
+ (None, False),
+ ([], False),
+ ([ProviderConfig(name="api_key", type=ProviderConfigType.SECRET_INPUT)], True),
+ ],
+ ids=["schema_none", "schema_empty", "schema_with_items"],
+ )
+ def test_need_credentials(self, credentials_schema: list[ProviderConfig] | None, expected: bool) -> None:
# Arrange
entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
- tenant_id = "test-tenant-id"
- controller = ConcreteDatasourcePluginProviderController(entity=entity, tenant_id=tenant_id)
-
- # Case 1: credentials_schema is None
- entity.credentials_schema = None
- assert controller.need_credentials is False
+ entity.credentials_schema = credentials_schema # pyrefly: ignore[bad-assignment]
+ controller = ConcreteDatasourcePluginProviderController(entity=entity, tenant_id="test-tenant-id")
- # Case 2: credentials_schema is empty
- entity.credentials_schema = []
- assert controller.need_credentials is False
-
- # Case 3: credentials_schema has items
- entity.credentials_schema = [ProviderConfig(name="api_key", type=ProviderConfigType.SECRET_INPUT)]
- assert controller.need_credentials is True
+ # Act & Assert
+ assert controller.need_credentials is expected
@patch("core.datasource.__base.datasource_provider.PluginToolManager")
- def test_validate_credentials(self, mock_manager_class):
+ def test_validate_credentials(self, mock_manager_class: MagicMock) -> None:
# Arrange
mock_manager = mock_manager_class.return_value
entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
@@ -80,7 +80,7 @@ class TestDatasourcePluginProviderController:
with pytest.raises(ToolProviderCredentialValidationError, match="Invalid credentials"):
controller._validate_credentials(user_id, credentials)
- def test_provider_type(self):
+ def test_provider_type(self) -> None:
# Arrange
entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
controller = ConcreteDatasourcePluginProviderController(entity=entity, tenant_id="test")
@@ -88,7 +88,7 @@ class TestDatasourcePluginProviderController:
# Act & Assert
assert controller.provider_type == DatasourceProviderType.LOCAL_FILE
- def test_validate_credentials_format_empty_schema(self):
+ def test_validate_credentials_format_empty_schema(self) -> None:
# Arrange
entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
entity.credentials_schema = []
@@ -98,7 +98,7 @@ class TestDatasourcePluginProviderController:
# Act & Assert (Should not raise anything)
controller.validate_credentials_format(credentials)
- def test_validate_credentials_format_unknown_credential(self):
+ def test_validate_credentials_format_unknown_credential(self) -> None:
# Arrange
entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
entity.credentials_schema = []
@@ -111,7 +111,7 @@ class TestDatasourcePluginProviderController:
):
controller.validate_credentials_format(credentials)
- def test_validate_credentials_format_required_missing(self):
+ def test_validate_credentials_format_required_missing(self) -> None:
# Arrange
config = ProviderConfig(
name="api_key",
@@ -127,7 +127,7 @@ class TestDatasourcePluginProviderController:
with pytest.raises(ToolProviderCredentialValidationError, match="credential api_key is required"):
controller.validate_credentials_format({})
- def test_validate_credentials_format_not_required_null(self):
+ def test_validate_credentials_format_not_required_null(self) -> None:
# Arrange
config = ProviderConfig(
name="optional",
@@ -141,11 +141,11 @@ class TestDatasourcePluginProviderController:
controller = ConcreteDatasourcePluginProviderController(entity=entity, tenant_id="test")
# Act & Assert
- credentials = {"optional": None}
+ credentials: dict[str, object] = {"optional": None}
controller.validate_credentials_format(credentials)
assert credentials["optional"] is None
- def test_validate_credentials_format_type_mismatch_text(self):
+ def test_validate_credentials_format_type_mismatch_text(self) -> None:
# Arrange
config = ProviderConfig(
name="text_field",
@@ -161,7 +161,7 @@ class TestDatasourcePluginProviderController:
with pytest.raises(ToolProviderCredentialValidationError, match="credential text_field should be string"):
controller.validate_credentials_format({"text_field": 123})
- def test_validate_credentials_format_select_validation(self):
+ def test_validate_credentials_format_select_validation(self) -> None:
# Arrange
option = ProviderConfig.Option(value="opt1", label=I18nObject(en_US="Option 1"))
@@ -181,7 +181,7 @@ class TestDatasourcePluginProviderController:
controller.validate_credentials_format({"select_field": 123})
# Case 2: Options not list
- config.options = "invalid"
+ config.options = "invalid" # pyrefly: ignore[bad-assignment]
with pytest.raises(
ToolProviderCredentialValidationError, match="credential select_field options should be list"
):
@@ -192,7 +192,7 @@ class TestDatasourcePluginProviderController:
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):
+ def test_get_datasource_base(self) -> None:
# Arrange
entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
controller = ConcreteDatasourcePluginProviderController(entity=entity, tenant_id="test")
@@ -203,7 +203,7 @@ class TestDatasourcePluginProviderController:
# Assert
assert result is None
- def test_validate_credentials_format_hits_pop(self):
+ def test_validate_credentials_format_hits_pop(self) -> None:
# Arrange
config = ProviderConfig(
name="valid_field",
@@ -223,7 +223,7 @@ class TestDatasourcePluginProviderController:
assert "valid_field" in credentials
assert credentials["valid_field"] == "valid_value"
- def test_validate_credentials_format_hits_continue(self):
+ def test_validate_credentials_format_hits_continue(self) -> None:
# Arrange
config = ProviderConfig(
name="optional_field",
@@ -237,13 +237,13 @@ class TestDatasourcePluginProviderController:
controller = ConcreteDatasourcePluginProviderController(entity=entity, tenant_id="test")
# Act
- credentials = {"optional_field": None}
+ credentials: dict[str, object] = {"optional_field": None}
controller.validate_credentials_format(credentials)
# Assert
assert credentials["optional_field"] is None
- def test_validate_credentials_format_default_values(self):
+ def test_validate_credentials_format_default_values(self) -> None:
# Arrange
config_text = ProviderConfig(
name="text_def",
diff --git a/api/tests/unit_tests/core/datasource/__base/test_datasource_runtime.py b/api/tests/unit_tests/core/datasource/__base/test_datasource_runtime.py
index 2bca9155e9d..3990146b676 100644
--- a/api/tests/unit_tests/core/datasource/__base/test_datasource_runtime.py
+++ b/api/tests/unit_tests/core/datasource/__base/test_datasource_runtime.py
@@ -4,7 +4,7 @@ from core.datasource.entities.datasource_entities import DatasourceInvokeFrom
class TestDatasourceRuntime:
- def test_init(self):
+ def test_init(self) -> None:
runtime = DatasourceRuntime(
tenant_id="test-tenant",
datasource_id="test-ds",
@@ -17,7 +17,7 @@ class TestDatasourceRuntime:
assert runtime.datasource_id == "test-ds"
assert runtime.credentials["key"] == "val"
- def test_fake_datasource_runtime(self):
+ def test_fake_datasource_runtime(self) -> None:
# This covers the FakeDatasourceRuntime class and its __init__
runtime = FakeDatasourceRuntime()
assert runtime.tenant_id == "fake_tenant_id"
diff --git a/api/tests/unit_tests/core/datasource/entities/test_api_entities.py b/api/tests/unit_tests/core/datasource/entities/test_api_entities.py
index 9855b4040ab..88e5c4357f0 100644
--- a/api/tests/unit_tests/core/datasource/entities/test_api_entities.py
+++ b/api/tests/unit_tests/core/datasource/entities/test_api_entities.py
@@ -3,7 +3,7 @@ from core.datasource.entities.datasource_entities import DatasourceParameter
from core.tools.entities.common_entities import I18nObject
-def test_datasource_api_entity():
+def test_datasource_api_entity() -> None:
label = I18nObject(en_US="label", zh_Hans="标签")
description = I18nObject(en_US="desc", zh_Hans="描述")
@@ -20,7 +20,7 @@ def test_datasource_api_entity():
assert entity.output_schema is None
-def test_datasource_provider_api_entity_defaults():
+def test_datasource_provider_api_entity_defaults() -> None:
description = I18nObject(en_US="desc", zh_Hans="描述")
label = I18nObject(en_US="label", zh_Hans="标签")
@@ -37,7 +37,7 @@ def test_datasource_provider_api_entity_defaults():
assert entity.labels == []
-def test_datasource_provider_api_entity_convert_none_to_empty_list():
+def test_datasource_provider_api_entity_convert_none_to_empty_list() -> None:
description = I18nObject(en_US="desc", zh_Hans="描述")
label = I18nObject(en_US="label", zh_Hans="标签")
@@ -56,7 +56,7 @@ def test_datasource_provider_api_entity_convert_none_to_empty_list():
assert entity.datasources == []
-def test_datasource_provider_api_entity_to_dict():
+def test_datasource_provider_api_entity_to_dict() -> None:
description = I18nObject(en_US="desc", zh_Hans="描述")
label = I18nObject(en_US="label", zh_Hans="标签")
@@ -100,7 +100,7 @@ def test_datasource_provider_api_entity_to_dict():
assert result["datasources"][0]["parameters"][0]["type"] == "files"
-def test_datasource_provider_api_entity_to_dict_no_params():
+def test_datasource_provider_api_entity_to_dict_no_params() -> None:
description = I18nObject(en_US="desc", zh_Hans="描述")
label = I18nObject(en_US="label", zh_Hans="标签")
@@ -123,7 +123,7 @@ def test_datasource_provider_api_entity_to_dict_no_params():
assert result["datasources"][0]["parameters"] is None
-def test_datasource_provider_api_entity_to_dict_other_param_type():
+def test_datasource_provider_api_entity_to_dict_other_param_type() -> None:
description = I18nObject(en_US="desc", zh_Hans="描述")
label = I18nObject(en_US="label", zh_Hans="标签")
diff --git a/api/tests/unit_tests/core/datasource/entities/test_common_entities.py b/api/tests/unit_tests/core/datasource/entities/test_common_entities.py
index 0ee4928105c..559014341b1 100644
--- a/api/tests/unit_tests/core/datasource/entities/test_common_entities.py
+++ b/api/tests/unit_tests/core/datasource/entities/test_common_entities.py
@@ -1,7 +1,7 @@
from core.datasource.entities.common_entities import I18nObject
-def test_i18n_object_fallback():
+def test_i18n_object_fallback() -> None:
# Only en_US provided
obj = I18nObject(en_US="Hello")
assert obj.en_US == "Hello"
@@ -17,7 +17,7 @@ def test_i18n_object_fallback():
assert obj.ja_JP == "Hello"
-def test_i18n_object_all_fields():
+def test_i18n_object_all_fields() -> None:
obj = I18nObject(en_US="Hello", zh_Hans="你好", pt_BR="Olá", ja_JP="こんにちは")
assert obj.en_US == "Hello"
assert obj.zh_Hans == "你好"
@@ -25,7 +25,7 @@ def test_i18n_object_all_fields():
assert obj.ja_JP == "こんにちは"
-def test_i18n_object_to_dict():
+def test_i18n_object_to_dict() -> None:
obj = I18nObject(en_US="Hello", zh_Hans="你好", pt_BR="Olá", ja_JP="こんにちは")
expected_dict = {"en_US": "Hello", "zh_Hans": "你好", "pt_BR": "Olá", "ja_JP": "こんにちは"}
assert obj.to_dict() == expected_dict
diff --git a/api/tests/unit_tests/core/datasource/local_file/test_local_file_plugin.py b/api/tests/unit_tests/core/datasource/local_file/test_local_file_plugin.py
index 374b7b9cd31..1ce21c6b5f2 100644
--- a/api/tests/unit_tests/core/datasource/local_file/test_local_file_plugin.py
+++ b/api/tests/unit_tests/core/datasource/local_file/test_local_file_plugin.py
@@ -7,7 +7,7 @@ from tests.unit_tests.core.datasource.factories import datasource_entity
class TestLocalFileDatasourcePlugin:
- def test_init(self):
+ def test_init(self) -> None:
# Arrange
entity = datasource_entity("test_name")
runtime = DatasourceRuntime(tenant_id="test_tenant")
@@ -31,7 +31,7 @@ class TestLocalFileDatasourcePlugin:
assert plugin.runtime == runtime
assert plugin.icon == icon
- def test_datasource_provider_type(self):
+ def test_datasource_provider_type(self) -> None:
# Arrange
entity = datasource_entity("test_name")
runtime = DatasourceRuntime(tenant_id="test_tenant")
@@ -42,7 +42,7 @@ class TestLocalFileDatasourcePlugin:
# Act & Assert
assert plugin.datasource_provider_type() == DatasourceProviderType.LOCAL_FILE
- def test_get_icon_url(self):
+ def test_get_icon_url(self) -> None:
# Arrange
entity = datasource_entity("test_name")
runtime = DatasourceRuntime(tenant_id="test_tenant")
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 ab98b6a7fc4..aa6ca328f18 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
@@ -9,7 +9,7 @@ from tests.unit_tests.core.datasource.factories import datasource_entity, provid
class TestLocalFileDatasourcePluginProviderController:
- def test_init(self):
+ def test_init(self) -> None:
# Arrange
entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
plugin_id = "test_plugin_id"
@@ -30,7 +30,7 @@ class TestLocalFileDatasourcePluginProviderController:
assert controller.plugin_unique_identifier == plugin_unique_identifier
assert controller.tenant_id == tenant_id
- def test_provider_type(self):
+ def test_provider_type(self) -> None:
# Arrange
entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
controller = LocalFileDatasourcePluginProviderController(
@@ -40,7 +40,7 @@ class TestLocalFileDatasourcePluginProviderController:
# Act & Assert
assert controller.provider_type == DatasourceProviderType.LOCAL_FILE
- def test_validate_credentials(self):
+ def test_validate_credentials(self) -> None:
# Arrange
entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
controller = LocalFileDatasourcePluginProviderController(
@@ -51,7 +51,7 @@ class TestLocalFileDatasourcePluginProviderController:
# Should not raise any exception
controller._validate_credentials("user_id", {"key": "value"})
- def test_get_datasource_success(self):
+ def test_get_datasource_success(self) -> None:
# Arrange
source = datasource_entity("test_datasource")
entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
@@ -75,7 +75,7 @@ class TestLocalFileDatasourcePluginProviderController:
assert datasource.icon == "test_icon"
assert datasource.plugin_unique_identifier == plugin_unique_identifier
- def test_get_datasource_not_found(self):
+ def test_get_datasource_not_found(self) -> None:
# Arrange
entity = provider_entity(DatasourceProviderType.LOCAL_FILE)
entity.datasources = [datasource_entity("other_datasource")]
diff --git a/api/tests/unit_tests/core/datasource/online_document/test_online_document_provider.py b/api/tests/unit_tests/core/datasource/online_document/test_online_document_provider.py
index 1a110dd70fd..f09e10adcc3 100644
--- a/api/tests/unit_tests/core/datasource/online_document/test_online_document_provider.py
+++ b/api/tests/unit_tests/core/datasource/online_document/test_online_document_provider.py
@@ -9,7 +9,7 @@ from tests.unit_tests.core.datasource.factories import datasource_entity, provid
class TestOnlineDocumentDatasourcePluginProviderController:
- def test_init(self):
+ def test_init(self) -> None:
# Arrange
entity = provider_entity(DatasourceProviderType.ONLINE_DOCUMENT)
plugin_id = "test_plugin_id"
@@ -30,7 +30,7 @@ class TestOnlineDocumentDatasourcePluginProviderController:
assert controller.plugin_unique_identifier == plugin_unique_identifier
assert controller.tenant_id == tenant_id
- def test_provider_type(self):
+ def test_provider_type(self) -> None:
# Arrange
entity = provider_entity(DatasourceProviderType.ONLINE_DOCUMENT)
controller = OnlineDocumentDatasourcePluginProviderController(
@@ -40,7 +40,7 @@ class TestOnlineDocumentDatasourcePluginProviderController:
# Assert
assert controller.provider_type == DatasourceProviderType.ONLINE_DOCUMENT
- def test_get_datasource_success(self):
+ def test_get_datasource_success(self) -> None:
# Arrange
source = datasource_entity("target_datasource")
@@ -69,7 +69,7 @@ class TestOnlineDocumentDatasourcePluginProviderController:
assert result.plugin_unique_identifier == plugin_unique_identifier
assert result.runtime.tenant_id == tenant_id
- def test_get_datasource_not_found(self):
+ def test_get_datasource_not_found(self) -> None:
# Arrange
source = datasource_entity("other_datasource")
diff --git a/api/tests/unit_tests/core/datasource/online_drive/test_online_drive_provider.py b/api/tests/unit_tests/core/datasource/online_drive/test_online_drive_provider.py
index 480dd13e575..557b89fe3a7 100644
--- a/api/tests/unit_tests/core/datasource/online_drive/test_online_drive_provider.py
+++ b/api/tests/unit_tests/core/datasource/online_drive/test_online_drive_provider.py
@@ -7,7 +7,7 @@ from tests.unit_tests.core.datasource.factories import datasource_entity, provid
class TestOnlineDriveDatasourcePluginProviderController:
- def test_init(self):
+ def test_init(self) -> None:
# Arrange
entity = provider_entity(DatasourceProviderType.ONLINE_DRIVE)
plugin_id = "test_plugin_id"
@@ -28,7 +28,7 @@ class TestOnlineDriveDatasourcePluginProviderController:
assert controller.plugin_unique_identifier == plugin_unique_identifier
assert controller.tenant_id == tenant_id
- def test_provider_type(self):
+ def test_provider_type(self) -> None:
# Arrange
entity = provider_entity(DatasourceProviderType.ONLINE_DRIVE)
controller = OnlineDriveDatasourcePluginProviderController(
@@ -38,7 +38,7 @@ class TestOnlineDriveDatasourcePluginProviderController:
# Act & Assert
assert controller.provider_type == DatasourceProviderType.ONLINE_DRIVE
- def test_get_datasource_success(self):
+ def test_get_datasource_success(self) -> None:
# Arrange
source = datasource_entity("test_datasource")
@@ -64,7 +64,7 @@ class TestOnlineDriveDatasourcePluginProviderController:
assert datasource.plugin_unique_identifier == plugin_unique_identifier
assert datasource.runtime.tenant_id == tenant_id
- def test_get_datasource_not_found(self):
+ def test_get_datasource_not_found(self) -> None:
# Arrange
source = datasource_entity("other_datasource")
diff --git a/api/tests/unit_tests/core/datasource/test_errors.py b/api/tests/unit_tests/core/datasource/test_errors.py
index 7b7e2536dd2..887974305bf 100644
--- a/api/tests/unit_tests/core/datasource/test_errors.py
+++ b/api/tests/unit_tests/core/datasource/test_errors.py
@@ -12,48 +12,48 @@ from core.datasource.errors import (
class TestErrors:
- def test_datasource_provider_not_found_error(self):
+ def test_datasource_provider_not_found_error(self) -> None:
error = DatasourceProviderNotFoundError("Provider not found")
assert str(error) == "Provider not found"
assert isinstance(error, ValueError)
- def test_datasource_not_found_error(self):
+ def test_datasource_not_found_error(self) -> None:
error = DatasourceNotFoundError("Datasource not found")
assert str(error) == "Datasource not found"
assert isinstance(error, ValueError)
- def test_datasource_parameter_validation_error(self):
+ def test_datasource_parameter_validation_error(self) -> None:
error = DatasourceParameterValidationError("Validation failed")
assert str(error) == "Validation failed"
assert isinstance(error, ValueError)
- def test_datasource_provider_credential_validation_error(self):
+ def test_datasource_provider_credential_validation_error(self) -> None:
error = DatasourceProviderCredentialValidationError("Credential validation failed")
assert str(error) == "Credential validation failed"
assert isinstance(error, ValueError)
- def test_datasource_not_supported_error(self):
+ def test_datasource_not_supported_error(self) -> None:
error = DatasourceNotSupportedError("Not supported")
assert str(error) == "Not supported"
assert isinstance(error, ValueError)
- def test_datasource_invoke_error(self):
+ def test_datasource_invoke_error(self) -> None:
error = DatasourceInvokeError("Invoke error")
assert str(error) == "Invoke error"
assert isinstance(error, ValueError)
- def test_datasource_api_schema_error(self):
+ def test_datasource_api_schema_error(self) -> None:
error = DatasourceApiSchemaError("API schema error")
assert str(error) == "API schema error"
assert isinstance(error, ValueError)
- def test_datasource_engine_invoke_error(self):
+ def test_datasource_engine_invoke_error(self) -> None:
meta = DatasourceInvokeMeta.error_instance("Invoke failed")
error = DatasourceEngineInvokeError(meta=meta)
assert error.meta == meta
assert isinstance(error, Exception)
- def test_datasource_engine_invoke_error_init(self):
+ def test_datasource_engine_invoke_error_init(self) -> None:
# Test initialization with meta
meta = DatasourceInvokeMeta(time_cost=1.5, error="Engine failed")
error = DatasourceEngineInvokeError(meta=meta)
diff --git a/api/tests/unit_tests/core/datasource/utils/test_message_transformer.py b/api/tests/unit_tests/core/datasource/utils/test_message_transformer.py
index 20bf200bdeb..3e61fb806f0 100644
--- a/api/tests/unit_tests/core/datasource/utils/test_message_transformer.py
+++ b/api/tests/unit_tests/core/datasource/utils/test_message_transformer.py
@@ -1,4 +1,5 @@
-from unittest.mock import patch
+from collections.abc import Generator
+from unittest.mock import MagicMock, patch
import pytest
@@ -8,8 +9,17 @@ from graphon.file import File, FileTransferMethod, FileType
from models.tools import ToolFile
+def _stream(messages: list[DatasourceMessage]) -> Generator[DatasourceMessage]:
+ yield from messages
+
+
+def _text(message: DatasourceMessage) -> str:
+ assert isinstance(message.message, DatasourceMessage.TextMessage)
+ return message.message.text
+
+
class TestDatasourceFileMessageTransformer:
- def test_transform_text_and_link_messages(self):
+ def test_transform_text_and_link_messages(self) -> None:
# Setup
messages = [
DatasourceMessage(
@@ -24,20 +34,22 @@ class TestDatasourceFileMessageTransformer:
# Execute
result = list(
DatasourceFileMessageTransformer.transform_datasource_invoke_messages(
- messages=iter(messages), user_id="user1", tenant_id="tenant1"
+ messages=_stream(messages), user_id="user1", tenant_id="tenant1"
)
)
# Verify
assert len(result) == 2
assert result[0].type == DatasourceMessage.MessageType.TEXT
- assert result[0].message.text == "hello"
+ assert _text(result[0]) == "hello"
assert result[1].type == DatasourceMessage.MessageType.LINK
- assert result[1].message.text == "https://example.com"
+ assert _text(result[1]) == "https://example.com"
@patch("core.datasource.utils.message_transformer.ToolFileManager")
@patch("core.datasource.utils.message_transformer.guess_extension")
- def test_transform_image_message_success(self, mock_guess_ext, mock_tool_file_manager_cls):
+ def test_transform_image_message_success(
+ self, mock_guess_ext: MagicMock, mock_tool_file_manager_cls: MagicMock
+ ) -> None:
# Setup
mock_manager = mock_tool_file_manager_cls.return_value
mock_tool_file = ToolFile(
@@ -62,21 +74,21 @@ class TestDatasourceFileMessageTransformer:
# Execute
result = list(
DatasourceFileMessageTransformer.transform_datasource_invoke_messages(
- messages=iter(messages), user_id="user1", tenant_id="tenant1", conversation_id="conv1"
+ messages=_stream(messages), user_id="user1", tenant_id="tenant1", conversation_id="conv1"
)
)
# Verify
assert len(result) == 1
assert result[0].type == DatasourceMessage.MessageType.IMAGE_LINK
- assert result[0].message.text == "/files/datasources/file_id_123.png"
+ assert _text(result[0]) == "/files/datasources/file_id_123.png"
assert result[0].meta == {"some": "meta"}
mock_manager.create_file_by_url.assert_called_once_with(
user_id="user1", tenant_id="tenant1", file_url="https://example.com/image.png", conversation_id="conv1"
)
@patch("core.datasource.utils.message_transformer.ToolFileManager")
- def test_transform_image_message_failure(self, mock_tool_file_manager_cls):
+ def test_transform_image_message_failure(self, mock_tool_file_manager_cls: MagicMock) -> None:
# Setup
mock_manager = mock_tool_file_manager_cls.return_value
mock_manager.create_file_by_url.side_effect = Exception("Download failed")
@@ -91,19 +103,21 @@ class TestDatasourceFileMessageTransformer:
# Execute
result = list(
DatasourceFileMessageTransformer.transform_datasource_invoke_messages(
- messages=iter(messages), user_id="user1", tenant_id="tenant1"
+ messages=_stream(messages), user_id="user1", tenant_id="tenant1"
)
)
# Verify
assert len(result) == 1
assert result[0].type == DatasourceMessage.MessageType.TEXT
- assert "Failed to download image" in result[0].message.text
- assert "Download failed" in result[0].message.text
+ assert "Failed to download image" in _text(result[0])
+ assert "Download failed" in _text(result[0])
@patch("core.datasource.utils.message_transformer.ToolFileManager")
@patch("core.datasource.utils.message_transformer.guess_extension")
- def test_transform_blob_message_image(self, mock_guess_ext, mock_tool_file_manager_cls):
+ def test_transform_blob_message_image(
+ self, mock_guess_ext: MagicMock, mock_tool_file_manager_cls: MagicMock
+ ) -> None:
# Setup
mock_manager = mock_tool_file_manager_cls.return_value
mock_tool_file = ToolFile(
@@ -129,22 +143,22 @@ class TestDatasourceFileMessageTransformer:
# Execute
result = list(
DatasourceFileMessageTransformer.transform_datasource_invoke_messages(
- messages=iter(messages), user_id="user1", tenant_id="tenant1"
+ messages=_stream(messages), user_id="user1", tenant_id="tenant1"
)
)
# Verify
assert len(result) == 1
assert result[0].type == DatasourceMessage.MessageType.IMAGE_LINK
- assert result[0].message.text == "/files/datasources/blob_id_456.jpg"
+ assert _text(result[0]) == "/files/datasources/blob_id_456.jpg"
mock_manager.create_file_by_raw.assert_called_once()
@patch("core.datasource.utils.message_transformer.ToolFileManager")
@patch("core.datasource.utils.message_transformer.guess_extension")
@patch("core.datasource.utils.message_transformer.guess_type")
def test_transform_blob_message_binary_guess_mimetype(
- self, mock_guess_type, mock_guess_ext, mock_tool_file_manager_cls
- ):
+ self, mock_guess_type: MagicMock, mock_guess_ext: MagicMock, mock_tool_file_manager_cls: MagicMock
+ ) -> None:
# Setup
mock_manager = mock_tool_file_manager_cls.return_value
mock_tool_file = ToolFile(
@@ -171,16 +185,16 @@ class TestDatasourceFileMessageTransformer:
# Execute
result = list(
DatasourceFileMessageTransformer.transform_datasource_invoke_messages(
- messages=iter(messages), user_id="user1", tenant_id="tenant1"
+ messages=_stream(messages), user_id="user1", tenant_id="tenant1"
)
)
# Verify
assert len(result) == 1
assert result[0].type == DatasourceMessage.MessageType.BINARY_LINK
- assert result[0].message.text == "/files/datasources/blob_id_789.pdf"
+ assert _text(result[0]) == "/files/datasources/blob_id_789.pdf"
- def test_transform_blob_message_invalid_type(self):
+ def test_transform_blob_message_invalid_type(self) -> None:
# Setup
messages = [
DatasourceMessage(
@@ -192,11 +206,11 @@ class TestDatasourceFileMessageTransformer:
with pytest.raises(ValueError, match="unexpected message type"):
list(
DatasourceFileMessageTransformer.transform_datasource_invoke_messages(
- messages=iter(messages), user_id="user1", tenant_id="tenant1"
+ messages=_stream(messages), user_id="user1", tenant_id="tenant1"
)
)
- def test_transform_file_tool_file_image(self):
+ def test_transform_file_tool_file_image(self) -> None:
# Setup
file = File(
transfer_method=FileTransferMethod.TOOL_FILE,
@@ -216,16 +230,16 @@ class TestDatasourceFileMessageTransformer:
# Execute
result = list(
DatasourceFileMessageTransformer.transform_datasource_invoke_messages(
- messages=iter(messages), user_id="user1", tenant_id="tenant1"
+ messages=_stream(messages), user_id="user1", tenant_id="tenant1"
)
)
# Verify
assert len(result) == 1
assert result[0].type == DatasourceMessage.MessageType.IMAGE_LINK
- assert result[0].message.text == "/files/datasources/related_123.png"
+ assert _text(result[0]) == "/files/datasources/related_123.png"
- def test_transform_file_tool_file_binary(self):
+ def test_transform_file_tool_file_binary(self) -> None:
# Setup
file = File(
transfer_method=FileTransferMethod.TOOL_FILE,
@@ -245,16 +259,16 @@ class TestDatasourceFileMessageTransformer:
# Execute
result = list(
DatasourceFileMessageTransformer.transform_datasource_invoke_messages(
- messages=iter(messages), user_id="user1", tenant_id="tenant1"
+ messages=_stream(messages), user_id="user1", tenant_id="tenant1"
)
)
# Verify
assert len(result) == 1
assert result[0].type == DatasourceMessage.MessageType.LINK
- assert result[0].message.text == "/files/datasources/related_456.txt"
+ assert _text(result[0]) == "/files/datasources/related_456.txt"
- def test_transform_file_other_transfer_method(self):
+ def test_transform_file_other_transfer_method(self) -> None:
# Setup
file = File(
transfer_method=FileTransferMethod.REMOTE_URL,
@@ -272,7 +286,7 @@ class TestDatasourceFileMessageTransformer:
# Execute
result = list(
DatasourceFileMessageTransformer.transform_datasource_invoke_messages(
- messages=iter(messages), user_id="user1", tenant_id="tenant1"
+ messages=_stream(messages), user_id="user1", tenant_id="tenant1"
)
)
@@ -280,7 +294,7 @@ class TestDatasourceFileMessageTransformer:
assert len(result) == 1
assert result[0] == msg
- def test_transform_other_message_type(self):
+ def test_transform_other_message_type(self) -> None:
# JSON type is yielded by the default 'else' block or the 'yield message' at the end
msg = DatasourceMessage(
type=DatasourceMessage.MessageType.JSON, message=DatasourceMessage.JsonMessage(json_object={"k": "v"})
@@ -290,7 +304,7 @@ class TestDatasourceFileMessageTransformer:
# Execute
result = list(
DatasourceFileMessageTransformer.transform_datasource_invoke_messages(
- messages=iter(messages), user_id="user1", tenant_id="tenant1"
+ messages=_stream(messages), user_id="user1", tenant_id="tenant1"
)
)
@@ -298,7 +312,7 @@ class TestDatasourceFileMessageTransformer:
assert len(result) == 1
assert result[0] == msg
- def test_get_datasource_file_url(self):
+ def test_get_datasource_file_url(self) -> None:
# Test with extension
url = DatasourceFileMessageTransformer.get_datasource_file_url("file1", ".jpg")
assert url == "/files/datasources/file1.jpg"
@@ -307,7 +321,7 @@ class TestDatasourceFileMessageTransformer:
url = DatasourceFileMessageTransformer.get_datasource_file_url("file2", None)
assert url == "/files/datasources/file2.bin"
- def test_transform_blob_message_no_meta_filename(self):
+ def test_transform_blob_message_no_meta_filename(self) -> None:
# This tests line 70 where filename might be None
with patch("core.datasource.utils.message_transformer.ToolFileManager") as mock_tool_file_manager_cls:
mock_manager = mock_tool_file_manager_cls.return_value
@@ -332,16 +346,16 @@ class TestDatasourceFileMessageTransformer:
result = list(
DatasourceFileMessageTransformer.transform_datasource_invoke_messages(
- messages=iter(messages), user_id="user1", tenant_id="tenant1"
+ messages=_stream(messages), user_id="user1", tenant_id="tenant1"
)
)
assert len(result) == 1
assert result[0].type == DatasourceMessage.MessageType.BINARY_LINK
- assert result[0].message.text == "/files/datasources/blob_id_no_name.bin"
+ assert _text(result[0]) == "/files/datasources/blob_id_no_name.bin"
@patch("core.datasource.utils.message_transformer.ToolFileManager")
- def test_transform_image_message_not_text_message(self, mock_tool_file_manager_cls):
+ def test_transform_image_message_not_text_message(self, mock_tool_file_manager_cls: MagicMock) -> None:
# This tests line 24-26 where it checks if message is instance of TextMessage
messages = [
DatasourceMessage(
@@ -352,7 +366,7 @@ class TestDatasourceFileMessageTransformer:
# Execute
result = list(
DatasourceFileMessageTransformer.transform_datasource_invoke_messages(
- messages=iter(messages), user_id="user1", tenant_id="tenant1"
+ messages=_stream(messages), user_id="user1", tenant_id="tenant1"
)
)
diff --git a/api/tests/unit_tests/core/datasource/website_crawl/test_website_crawl_plugin.py b/api/tests/unit_tests/core/datasource/website_crawl/test_website_crawl_plugin.py
index 82d00533cd9..c388b1ed46a 100644
--- a/api/tests/unit_tests/core/datasource/website_crawl/test_website_crawl_plugin.py
+++ b/api/tests/unit_tests/core/datasource/website_crawl/test_website_crawl_plugin.py
@@ -5,6 +5,7 @@ import pytest
from core.datasource.__base.datasource_runtime import DatasourceRuntime
from core.datasource.entities.datasource_entities import (
+ DatasourceEntity,
DatasourceProviderType,
WebsiteCrawlMessage,
)
@@ -14,19 +15,19 @@ from tests.unit_tests.core.datasource.factories import datasource_entity
class TestWebsiteCrawlDatasourcePlugin:
@pytest.fixture
- def entity(self):
+ def entity(self) -> DatasourceEntity:
entity = datasource_entity("test_name")
entity.identity.provider = "test-provider"
entity.identity.name = "test-name"
return entity
@pytest.fixture
- def runtime(self):
+ def runtime(self) -> DatasourceRuntime:
runtime = DatasourceRuntime(tenant_id="test_tenant")
runtime.credentials = {"api_key": "test-key"}
return runtime
- def test_init(self, entity, runtime):
+ def test_init(self, entity: DatasourceEntity, runtime: DatasourceRuntime) -> None:
# Arrange
tenant_id = "test-tenant-id"
icon = "test-icon"
@@ -48,7 +49,7 @@ class TestWebsiteCrawlDatasourcePlugin:
assert plugin.runtime == runtime
assert plugin.icon == icon
- def test_datasource_provider_type(self, entity, runtime):
+ def test_datasource_provider_type(self, entity: DatasourceEntity, runtime: DatasourceRuntime) -> None:
# Arrange
plugin = WebsiteCrawlDatasourcePlugin(
entity=entity, runtime=runtime, tenant_id="test", icon="test", plugin_unique_identifier="test"
@@ -57,7 +58,7 @@ class TestWebsiteCrawlDatasourcePlugin:
# Act & Assert
assert plugin.datasource_provider_type() == DatasourceProviderType.WEBSITE_CRAWL
- def test_get_website_crawl(self, entity, runtime):
+ def test_get_website_crawl(self, entity: DatasourceEntity, runtime: DatasourceRuntime) -> None:
# Arrange
plugin = WebsiteCrawlDatasourcePlugin(
entity=entity,
diff --git a/api/tests/unit_tests/core/datasource/website_crawl/test_website_crawl_provider.py b/api/tests/unit_tests/core/datasource/website_crawl/test_website_crawl_provider.py
index 78891e089a9..3c988f224b4 100644
--- a/api/tests/unit_tests/core/datasource/website_crawl/test_website_crawl_provider.py
+++ b/api/tests/unit_tests/core/datasource/website_crawl/test_website_crawl_provider.py
@@ -2,6 +2,7 @@ import pytest
from core.datasource.__base.datasource_runtime import DatasourceRuntime
from core.datasource.entities.datasource_entities import (
+ DatasourceProviderEntityWithPlugin,
DatasourceProviderType,
)
from core.datasource.website_crawl.website_crawl_plugin import WebsiteCrawlDatasourcePlugin
@@ -11,13 +12,13 @@ from tests.unit_tests.core.datasource.factories import datasource_entity, provid
class TestWebsiteCrawlDatasourcePluginProviderController:
@pytest.fixture
- def entity(self):
+ def entity(self) -> DatasourceProviderEntityWithPlugin:
entity = provider_entity(DatasourceProviderType.WEBSITE_CRAWL)
entity.datasources = []
entity.identity.icon = "test-icon"
return entity
- def test_init(self, entity):
+ def test_init(self, entity: DatasourceProviderEntityWithPlugin) -> None:
# Arrange
plugin_id = "test-plugin-id"
plugin_unique_identifier = "test-unique-id"
@@ -37,7 +38,7 @@ class TestWebsiteCrawlDatasourcePluginProviderController:
assert controller.plugin_unique_identifier == plugin_unique_identifier
assert controller.tenant_id == tenant_id
- def test_provider_type(self, entity):
+ def test_provider_type(self, entity: DatasourceProviderEntityWithPlugin) -> None:
# Arrange
controller = WebsiteCrawlDatasourcePluginProviderController(
entity=entity, plugin_id="test", plugin_unique_identifier="test", tenant_id="test"
@@ -46,7 +47,7 @@ class TestWebsiteCrawlDatasourcePluginProviderController:
# Act & Assert
assert controller.provider_type == DatasourceProviderType.WEBSITE_CRAWL
- def test_get_datasource_success(self, entity):
+ def test_get_datasource_success(self, entity: DatasourceProviderEntityWithPlugin) -> None:
# Arrange
datasource_name = "test-datasource"
tenant_id = "test-tenant-id"
@@ -71,7 +72,7 @@ class TestWebsiteCrawlDatasourcePluginProviderController:
assert result.icon == "test-icon"
assert result.plugin_unique_identifier == plugin_unique_identifier
- def test_get_datasource_not_found(self, entity):
+ def test_get_datasource_not_found(self, entity: DatasourceProviderEntityWithPlugin) -> None:
# Arrange
datasource_name = "non-existent"
entity.datasources = []
diff --git a/api/tests/unit_tests/pyrefly.toml b/api/tests/unit_tests/pyrefly.toml
index e946054c7e6..56d90609a20 100644
--- a/api/tests/unit_tests/pyrefly.toml
+++ b/api/tests/unit_tests/pyrefly.toml
@@ -315,26 +315,13 @@ project-excludes = [
"core/callback_handler/test_agent_tool_callback_handler.py",
"core/callback_handler/test_index_tool_callback_handler.py",
"core/callback_handler/test_workflow_tool_callback_handler.py",
- "core/datasource/__base/test_datasource_plugin.py",
- "core/datasource/__base/test_datasource_provider.py",
- "core/datasource/__base/test_datasource_runtime.py",
- "core/datasource/entities/test_api_entities.py",
- "core/datasource/entities/test_common_entities.py",
"core/datasource/entities/test_datasource_entities.py",
- "core/datasource/local_file/test_local_file_plugin.py",
- "core/datasource/local_file/test_local_file_provider.py",
"core/datasource/online_document/test_online_document_plugin.py",
- "core/datasource/online_document/test_online_document_provider.py",
"core/datasource/online_drive/test_online_drive_plugin.py",
- "core/datasource/online_drive/test_online_drive_provider.py",
"core/datasource/test_datasource_file_manager.py",
"core/datasource/test_datasource_manager.py",
- "core/datasource/test_errors.py",
"core/datasource/test_notion_provider.py",
"core/datasource/test_website_crawl.py",
- "core/datasource/utils/test_message_transformer.py",
- "core/datasource/website_crawl/test_website_crawl_plugin.py",
- "core/datasource/website_crawl/test_website_crawl_provider.py",
"core/entities/test_entities_mcp_provider.py",
"core/entities/test_entities_provider_configuration.py",
"core/extension/test_api_based_extension_requestor.py",