mirror of
https://github.com/deepset-ai/haystack.git
synced 2025-11-25 14:36:05 +00:00
* add draft of WriteToStore and basic test * add DocumentWriter implementation * draft unit and integration tests * add release note * mock Store in unit tests * pylint * Update haystack/preview/components/writers/document_writer.py Co-authored-by: Daria Fokina <daria.fokina@deepset.ai> * Remove unnecessary test * Rework DocumentWriter to support new Component I/O definition --------- Co-authored-by: Daria Fokina <daria.fokina@deepset.ai> Co-authored-by: Silvano Cerza <silvanocerza@gmail.com>
37 lines
1.3 KiB
Python
37 lines
1.3 KiB
Python
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
|
|
from haystack.preview import Document
|
|
from haystack.preview.components.writers.document_writer import DocumentWriter
|
|
from haystack.preview.document_stores import DuplicatePolicy
|
|
from test.preview.components.base import BaseTestComponent
|
|
|
|
|
|
class TestDocumentWriter(BaseTestComponent):
|
|
@pytest.mark.unit
|
|
def test_run(self):
|
|
writer = DocumentWriter()
|
|
documents = [
|
|
Document(content="This is the text of a document."),
|
|
Document(content="This is the text of another document."),
|
|
]
|
|
|
|
mocked_document_store = MagicMock()
|
|
mocked_document_store.__haystack_document_store__ = True
|
|
writer.document_store = mocked_document_store
|
|
writer.run(documents=documents)
|
|
|
|
mocked_document_store.write_documents.assert_called_once_with(documents=documents, policy=DuplicatePolicy.FAIL)
|
|
|
|
@pytest.mark.unit
|
|
def test_run_without_store(self):
|
|
writer = DocumentWriter()
|
|
documents = [Document(content="test")]
|
|
with pytest.raises(
|
|
ValueError,
|
|
match="DocumentWriter needs a DocumentStore to run: set the DocumentStore instance to the "
|
|
"self.document_store attribute",
|
|
):
|
|
writer.run(documents=documents)
|