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 upload streams and document values (#43167)

Asuka Minato1beefc2b5035
diff --git a/api/tests/test_containers_integration_tests/services/test_hit_testing_service.py b/api/tests/test_containers_integration_tests/services/test_hit_testing_service.py
index 73a79f1ae50..249a8f98cf8 100644
--- a/api/tests/test_containers_integration_tests/services/test_hit_testing_service.py
+++ b/api/tests/test_containers_integration_tests/services/test_hit_testing_service.py
@@ -160,14 +160,14 @@ class TestHitTestingService:
self, mock_format: MagicMock, db_session_with_containers: Session
) -> None:
query = "test query"
- mock_doc = MagicMock(spec=Document)
+ document = Document(page_content="Test document content")
mock_record = MagicMock()
mock_record.model_dump.return_value = {"content": "formatted content"}
mock_format.return_value = [mock_record]
response = _RetrieveResponse.model_validate(
- HitTestingService.compact_retrieve_response(query, [mock_doc], session=db_session_with_containers)
+ HitTestingService.compact_retrieve_response(query, [document], session=db_session_with_containers)
)
assert response.query.content == query
@@ -175,7 +175,7 @@ class TestHitTestingService:
assert response.records[0].content == "formatted content"
mock_format.assert_called_once()
assert mock_format.call_args.args[0] is not db_session_with_containers
- assert mock_format.call_args.args[1] == [mock_doc]
+ assert mock_format.call_args.args[1] == [document]
def test_compact_external_retrieve_response_should_return_records_for_external_provider(
self, db_session_with_containers: Session
diff --git a/api/tests/unit_tests/core/rag/docstore/test_dataset_docstore.py b/api/tests/unit_tests/core/rag/docstore/test_dataset_docstore.py
index 07f7a87372a..37e80d937a2 100644
--- a/api/tests/unit_tests/core/rag/docstore/test_dataset_docstore.py
+++ b/api/tests/unit_tests/core/rag/docstore/test_dataset_docstore.py
@@ -5,8 +5,6 @@ Tests cover all public methods and error paths of the DatasetDocumentStore class
which provides document storage and retrieval functionality for datasets in the RAG system.
"""
-from unittest.mock import MagicMock
-
import pytest
from sqlalchemy import func, select
from sqlalchemy.orm import Session
@@ -284,8 +282,8 @@ class TestDatasetDocumentStoreAddDocuments(_UsesSQLiteSession):
def test_add_documents_with_none_metadata(self, sqlite_session: Session):
"""Test that document with None metadata raises ValueError."""
- document = MagicMock(spec=Document)
- document.metadata = None
+ # Bypass model validation to exercise the store's legacy malformed-metadata guard.
+ document = Document.model_construct(page_content="Test content", metadata=None)
store = DatasetDocumentStore(dataset=_dataset(), user_id=USER_ID, document_id=DOCUMENT_ID)
with pytest.raises(ValueError, match="metadata must be a dict"):
diff --git a/api/tests/unit_tests/core/rag/indexing/processor/test_qa_index_processor.py b/api/tests/unit_tests/core/rag/indexing/processor/test_qa_index_processor.py
index 57cbc443b59..5e943e1b4d8 100644
--- a/api/tests/unit_tests/core/rag/indexing/processor/test_qa_index_processor.py
+++ b/api/tests/unit_tests/core/rag/indexing/processor/test_qa_index_processor.py
@@ -1,9 +1,10 @@
import logging
+from contextlib import closing
+from io import BytesIO
from types import SimpleNamespace
from typing import Any
from unittest.mock import Mock, patch
-import pandas as pd
import pytest
from flask import Flask
from sqlalchemy import event
@@ -183,40 +184,26 @@ class TestQAIndexProcessor:
assert mock_format.call_count == 2
def test_format_by_template_validates_file_type(self, processor: QAIndexProcessor) -> None:
- not_csv_file = Mock(spec=FileStorage)
- not_csv_file.filename = "qa.txt"
-
- with pytest.raises(ValueError, match="Only CSV files"):
- processor.format_by_template(not_csv_file)
+ with closing(FileStorage(stream=BytesIO(b"not csv"), filename="qa.txt")) as file:
+ with pytest.raises(ValueError, match="Only CSV files"):
+ processor.format_by_template(file)
def test_format_by_template_parses_csv_rows(self, processor: QAIndexProcessor) -> None:
- csv_file = Mock(spec=FileStorage)
- csv_file.filename = "qa.csv"
- dataframe = pd.DataFrame([["Q1", "A1"], ["Q2", "A2"]])
-
- with patch("core.rag.index_processor.processor.qa_index_processor.pd.read_csv", return_value=dataframe):
- docs = processor.format_by_template(csv_file)
+ with closing(FileStorage(stream=BytesIO(b"question,answer\nQ1,A1\nQ2,A2\n"), filename="qa.csv")) as file:
+ docs = processor.format_by_template(file)
assert [doc.page_content for doc in docs] == ["Q1", "Q2"]
assert [doc.metadata["answer"] for doc in docs] == ["A1", "A2"]
def test_format_by_template_raises_on_empty_csv(self, processor: QAIndexProcessor) -> None:
- csv_file = Mock(spec=FileStorage)
- csv_file.filename = "qa.csv"
-
- with patch("core.rag.index_processor.processor.qa_index_processor.pd.read_csv", return_value=pd.DataFrame()):
+ with closing(FileStorage(stream=BytesIO(b"question,answer\n"), filename="qa.csv")) as file:
with pytest.raises(ValueError, match="empty"):
- processor.format_by_template(csv_file)
+ processor.format_by_template(file)
def test_format_by_template_raises_on_invalid_csv(self, processor: QAIndexProcessor) -> None:
- csv_file = Mock(spec=FileStorage)
- csv_file.filename = "qa.csv"
-
- with patch(
- "core.rag.index_processor.processor.qa_index_processor.pd.read_csv", side_effect=Exception("bad csv")
- ):
- with pytest.raises(ValueError, match="bad csv"):
- processor.format_by_template(csv_file)
+ with closing(FileStorage(stream=BytesIO(b'question,answer\n"unterminated'), filename="qa.csv")) as file:
+ with pytest.raises(ValueError, match="Error tokenizing data"):
+ processor.format_by_template(file)
def test_load_creates_vectors_for_high_quality_dataset(
self, processor: QAIndexProcessor, dataset: Dataset, sqlite_session: Session
diff --git a/api/tests/unit_tests/services/hit_service.py b/api/tests/unit_tests/services/hit_service.py
index 6ef10ad0726..c576d6fa646 100644
--- a/api/tests/unit_tests/services/hit_service.py
+++ b/api/tests/unit_tests/services/hit_service.py
@@ -88,23 +88,12 @@ class HitTestingTestDataFactory:
return user
@staticmethod
- def create_document_mock(
+ def create_document(
content: str = "Test document content",
metadata: dict[str, Any] | None = None,
- **kwargs,
- ) -> Mock:
- """
- Create a mock Document from core.rag.models.document.
-
- Args:
- content: Document content/text
- metadata: Optional metadata dictionary
- **kwargs: Additional attributes to set on the mock
-
- Returns:
- Mock object configured as a Document instance
- """
- return Mock(spec=Document, page_content=content, metadata=metadata or {}, **kwargs)
+ ) -> Document:
+ """Create a document; None means that the document has no metadata."""
+ return Document(page_content=content, metadata=metadata if metadata is not None else {})
@staticmethod
def create_retrieval_record_mock(
@@ -155,8 +144,8 @@ class TestHitTestingServiceRetrieve:
external_retrieval_model = {}
documents = [
- HitTestingTestDataFactory.create_document_mock(content="Doc 1"),
- HitTestingTestDataFactory.create_document_mock(content="Doc 2"),
+ HitTestingTestDataFactory.create_document(content="Doc 1"),
+ HitTestingTestDataFactory.create_document(content="Doc 2"),
]
mock_records = [
@@ -212,7 +201,7 @@ class TestHitTestingServiceRetrieve:
}
external_retrieval_model = {}
- documents = [HitTestingTestDataFactory.create_document_mock()]
+ documents = [HitTestingTestDataFactory.create_document()]
mock_records = [HitTestingTestDataFactory.create_retrieval_record_mock()]
with (
@@ -268,7 +257,7 @@ class TestHitTestingServiceRetrieve:
None,
)
- documents = [HitTestingTestDataFactory.create_document_mock()]
+ documents = [HitTestingTestDataFactory.create_document()]
mock_records = [HitTestingTestDataFactory.create_retrieval_record_mock()]
with (
@@ -358,7 +347,7 @@ class TestHitTestingServiceRetrieve:
retrieval_model = None
external_retrieval_model = {}
- documents = [HitTestingTestDataFactory.create_document_mock()]
+ documents = [HitTestingTestDataFactory.create_document()]
mock_records = [HitTestingTestDataFactory.create_retrieval_record_mock()]
with (
@@ -566,8 +555,8 @@ class TestHitTestingServiceCompactRetrieveResponse:
# Arrange
query = "test query"
documents = [
- HitTestingTestDataFactory.create_document_mock(content="Doc 1"),
- HitTestingTestDataFactory.create_document_mock(content="Doc 2"),
+ HitTestingTestDataFactory.create_document(content="Doc 1"),
+ HitTestingTestDataFactory.create_document(content="Doc 2"),
]
mock_records = [
diff --git a/api/tests/unit_tests/services/test_audio_service.py b/api/tests/unit_tests/services/test_audio_service.py
index d545709d7ac..1ce0aa53270 100644
--- a/api/tests/unit_tests/services/test_audio_service.py
+++ b/api/tests/unit_tests/services/test_audio_service.py
@@ -32,7 +32,7 @@ Tests available voice retrieval:
## Testing Approach
-- **Isolation Strategy**: External dependencies (ModelManager and FileStorage) are mocked,
+- **Isolation Strategy**: ModelManager is mocked; uploads use real FileStorage byte streams,
while database paths use isolated in-memory SQLite sessions
- **Factory Pattern**: AudioServiceTestDataFactory provides consistent test data
- **Fixtures**: Mock objects are configured per test method
@@ -56,7 +56,8 @@ Tests available voice retrieval:
import json
from collections.abc import Generator
from decimal import Decimal
-from unittest.mock import MagicMock, Mock, patch
+from io import BytesIO
+from unittest.mock import MagicMock, patch
from uuid import uuid4
import pytest
@@ -224,32 +225,13 @@ class AudioServiceTestDataFactory:
return config
@staticmethod
- def create_file_storage_mock(
+ def create_file_storage(
filename: str = "test.mp3",
mimetype: str = "audio/mp3",
content: bytes = b"fake audio content",
- **kwargs: object,
- ) -> Mock:
- """
- Create a mock FileStorage object.
-
- Args:
- filename: Name of the file
- mimetype: MIME type of the file
- content: File content as bytes
- **kwargs: Additional attributes to set on the mock
-
- Returns:
- Mock FileStorage object with specified attributes
- """
- file = Mock(spec=FileStorage)
- file.filename = filename
- file.mimetype = mimetype
- file.stream = Mock()
- file.stream.read = Mock(return_value=content)
- for key, value in kwargs.items():
- setattr(file, key, value)
- return file
+ ) -> FileStorage:
+ """Create an upload with an in-memory byte stream and the requested MIME type."""
+ return FileStorage(stream=BytesIO(content), filename=filename, content_type=mimetype)
@pytest.fixture
@@ -316,7 +298,7 @@ class TestAudioServiceASR:
mode=AppMode.CHAT,
app_model_config=app_model_config,
)
- file = factory.create_file_storage_mock()
+ file = factory.create_file_storage()
# Mock ModelManager
mock_model_manager = mock_model_manager_class.return_value
@@ -347,7 +329,7 @@ class TestAudioServiceASR:
# Arrange
app_model_config = factory.create_app_model_config_mock(speech_to_text_dict={"enabled": True})
app = factory.create_app_mock(mode=AppMode.CHAT, app_model_config=app_model_config)
- file = factory.create_file_storage_mock(filename="audio.m4a", mimetype="audio/x-m4a")
+ file = factory.create_file_storage(filename="audio.m4a", mimetype="audio/x-m4a")
mock_model_instance = MagicMock()
mock_model_instance.invoke_speech2text.return_value = "M4A transcript"
mock_model_manager_class.return_value.get_default_model_instance.return_value = mock_model_instance
@@ -370,7 +352,7 @@ class TestAudioServiceASR:
mode=AppMode.ADVANCED_CHAT,
workflow=workflow,
)
- file = factory.create_file_storage_mock()
+ file = factory.create_file_storage()
# Mock ModelManager
mock_model_manager = mock_model_manager_class.return_value
@@ -391,7 +373,7 @@ class TestAudioServiceASR:
factory: AudioServiceTestDataFactory,
) -> None:
app = factory.create_app_mock(mode=AppMode.AGENT)
- file = factory.create_file_storage_mock()
+ file = factory.create_file_storage()
agent_soul = AgentSoulConfig.model_validate({"app_features": {"speech_to_text": {"enabled": True}}})
agent = Agent(
tenant_id=app.tenant_id,
@@ -433,7 +415,7 @@ class TestAudioServiceASR:
) -> None:
app_model_config = factory.create_app_model_config_mock(speech_to_text_dict={"enabled": True})
app = factory.create_app_mock(mode=AppMode.AGENT, app_model_config=app_model_config)
- file = factory.create_file_storage_mock()
+ file = factory.create_file_storage()
mock_model_instance = MagicMock()
mock_model_instance.invoke_speech2text.return_value = "Legacy Agent transcript"
mock_model_manager_class.return_value.get_default_model_instance.return_value = mock_model_instance
@@ -447,7 +429,7 @@ class TestAudioServiceASR:
self, mock_model_manager_class: MagicMock, factory: AudioServiceTestDataFactory
) -> None:
app = factory.create_app_mock(mode=AppMode.AGENT)
- file = factory.create_file_storage_mock()
+ file = factory.create_file_storage()
agent_soul = AgentSoulConfig.model_validate({"app_features": {"speech_to_text": {"enabled": True}}})
mock_model_instance = MagicMock()
mock_model_instance.invoke_speech2text.return_value = "Agent transcript"
@@ -482,7 +464,7 @@ class TestAudioServiceASR:
self, factory: AudioServiceTestDataFactory, agent_soul: AgentSoulConfig
) -> None:
app = factory.create_app_mock(mode=AppMode.AGENT)
- file = factory.create_file_storage_mock()
+ file = factory.create_file_storage()
with pytest.raises(SpeechToTextDisabledServiceError):
AudioService.transcript_agent_asr(app_model=app, agent_soul=agent_soul, file=file, session=self.session)
@@ -493,7 +475,7 @@ class TestAudioServiceASR:
) -> None:
app_model_config = factory.create_app_model_config_mock(speech_to_text_dict={"enabled": True})
app = factory.create_app_mock(mode=AppMode.AGENT, app_model_config=app_model_config)
- file = factory.create_file_storage_mock()
+ file = factory.create_file_storage()
mock_model_instance = MagicMock()
mock_model_instance.invoke_speech2text.return_value = "Legacy feature transcript"
mock_model_manager_class.return_value.get_default_model_instance.return_value = mock_model_instance
@@ -512,7 +494,7 @@ class TestAudioServiceASR:
) -> None:
app_model_config = factory.create_app_model_config_mock(speech_to_text_dict={"enabled": True})
app = factory.create_app_mock(mode=AppMode.AGENT, app_model_config=app_model_config)
- file = factory.create_file_storage_mock()
+ file = factory.create_file_storage()
agent_soul = AgentSoulConfig.model_validate({"app_features": {"speech_to_text": {"enabled": False}}})
with pytest.raises(SpeechToTextDisabledServiceError):
@@ -528,7 +510,7 @@ class TestAudioServiceASR:
mode=AppMode.CHAT,
app_model_config=app_model_config,
)
- file = factory.create_file_storage_mock()
+ file = factory.create_file_storage()
# Act & Assert
with pytest.raises(SpeechToTextDisabledServiceError):
@@ -544,7 +526,7 @@ class TestAudioServiceASR:
mode=AppMode.WORKFLOW,
workflow=workflow,
)
- file = factory.create_file_storage_mock()
+ file = factory.create_file_storage()
# Act & Assert
with pytest.raises(SpeechToTextDisabledServiceError):
@@ -557,7 +539,7 @@ class TestAudioServiceASR:
mode=AppMode.WORKFLOW,
workflow=None,
)
- file = factory.create_file_storage_mock()
+ file = factory.create_file_storage()
# Act & Assert
with pytest.raises(SpeechToTextDisabledServiceError):
@@ -584,7 +566,7 @@ class TestAudioServiceASR:
mode=AppMode.CHAT,
app_model_config=app_model_config,
)
- file = factory.create_file_storage_mock(mimetype="video/mp4")
+ file = factory.create_file_storage(mimetype="video/mp4")
# Act & Assert
with pytest.raises(UnsupportedAudioTypeServiceError):
@@ -600,7 +582,7 @@ class TestAudioServiceASR:
)
# Create file larger than 30MB
large_content = b"x" * (31 * 1024 * 1024)
- file = factory.create_file_storage_mock(content=large_content)
+ file = factory.create_file_storage(content=large_content)
# Act & Assert
with pytest.raises(AudioTooLargeServiceError, match="Audio size larger than 30 mb"):
@@ -617,7 +599,7 @@ class TestAudioServiceASR:
mode=AppMode.CHAT,
app_model_config=app_model_config,
)
- file = factory.create_file_storage_mock()
+ file = factory.create_file_storage()
# Mock ModelManager to return None
mock_model_manager = mock_model_manager_class.return_value