chore: import upstream snapshot with attribution
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
Docker image release / Build base image (push) Has been cancelled
Sync docs with Docusaurus / sync (push) Has been cancelled
Tests / Check if changed (push) Has been cancelled
Tests / format (push) Has been cancelled
Tests / check-imports (push) Has been cancelled
Tests / Unit / macos-latest (push) Has been cancelled
Tests / Unit / ubuntu-latest (push) Has been cancelled
Tests / Unit / windows-latest (push) Has been cancelled
Tests / mypy (push) Has been cancelled
Tests / Integration / ubuntu-latest (push) Has been cancelled
Tests / Integration / macos-latest (push) Has been cancelled
Tests / Integration / windows-latest (push) Has been cancelled
Tests / notify-slack-on-failure (push) Has been cancelled
Tests / Mark tests as completed (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
Docker image release / Build base image (push) Has been cancelled
Sync docs with Docusaurus / sync (push) Has been cancelled
Tests / Check if changed (push) Has been cancelled
Tests / format (push) Has been cancelled
Tests / check-imports (push) Has been cancelled
Tests / Unit / macos-latest (push) Has been cancelled
Tests / Unit / ubuntu-latest (push) Has been cancelled
Tests / Unit / windows-latest (push) Has been cancelled
Tests / mypy (push) Has been cancelled
Tests / Integration / ubuntu-latest (push) Has been cancelled
Tests / Integration / macos-latest (push) Has been cancelled
Tests / Integration / windows-latest (push) Has been cancelled
Tests / notify-slack-on-failure (push) Has been cancelled
Tests / Mark tests as completed (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from haystack import Document
|
||||
from haystack.components.caching.cache_checker import CacheChecker
|
||||
from haystack.testing.factory import document_store_class
|
||||
|
||||
|
||||
class TestCacheCheckerAsync:
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_async_invalid_docstore(self):
|
||||
mocked_docstore_class = document_store_class("MockedDocumentStore")
|
||||
checker = CacheChecker(document_store=mocked_docstore_class(), cache_field="url")
|
||||
|
||||
with pytest.raises(TypeError, match="does not provide async support"):
|
||||
await checker.run_async(items=["https://example.com/1"])
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_async(self, in_memory_doc_store):
|
||||
documents = [
|
||||
Document(content="doc1", meta={"url": "https://example.com/1"}),
|
||||
Document(content="doc2", meta={"url": "https://example.com/2"}),
|
||||
Document(content="doc3", meta={"url": "https://example.com/1"}),
|
||||
Document(content="doc4", meta={"url": "https://example.com/2"}),
|
||||
]
|
||||
in_memory_doc_store.write_documents(documents)
|
||||
checker = CacheChecker(in_memory_doc_store, cache_field="url")
|
||||
results = await checker.run_async(items=["https://example.com/1", "https://example.com/5"])
|
||||
assert results == {"hits": [documents[0], documents[2]], "misses": ["https://example.com/5"]}
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_async_all_hits(self, in_memory_doc_store):
|
||||
documents = [
|
||||
Document(content="doc1", meta={"url": "https://example.com/1"}),
|
||||
Document(content="doc2", meta={"url": "https://example.com/2"}),
|
||||
]
|
||||
in_memory_doc_store.write_documents(documents)
|
||||
checker = CacheChecker(in_memory_doc_store, cache_field="url")
|
||||
results = await checker.run_async(items=["https://example.com/1", "https://example.com/2"])
|
||||
assert results["hits"] == documents
|
||||
assert results["misses"] == []
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_async_all_misses(self, in_memory_doc_store):
|
||||
checker = CacheChecker(in_memory_doc_store, cache_field="url")
|
||||
results = await checker.run_async(items=["https://example.com/1", "https://example.com/2"])
|
||||
assert results["hits"] == []
|
||||
assert results["misses"] == ["https://example.com/1", "https://example.com/2"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_async_filters_syntax(self):
|
||||
mock_store = MagicMock()
|
||||
mock_store.filter_documents_async = AsyncMock(return_value=[])
|
||||
checker = CacheChecker(document_store=mock_store, cache_field="url")
|
||||
await checker.run_async(items=["https://example.com/1"])
|
||||
expected_filters = {"field": "url", "operator": "==", "value": "https://example.com/1"}
|
||||
mock_store.filter_documents_async.assert_awaited_once_with(filters=expected_filters)
|
||||
@@ -0,0 +1,94 @@
|
||||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from haystack import Document
|
||||
from haystack.components.caching.cache_checker import CacheChecker
|
||||
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
||||
from haystack.testing.factory import document_store_class
|
||||
|
||||
|
||||
class TestCacheChecker:
|
||||
def test_to_dict(self):
|
||||
mocked_docstore_class = document_store_class("MockedDocumentStore")
|
||||
component = CacheChecker(document_store=mocked_docstore_class(), cache_field="url")
|
||||
data = component.to_dict()
|
||||
assert data == {
|
||||
"type": "haystack.components.caching.cache_checker.CacheChecker",
|
||||
"init_parameters": {
|
||||
"document_store": {"type": "haystack.testing.factory.MockedDocumentStore", "init_parameters": {}},
|
||||
"cache_field": "url",
|
||||
},
|
||||
}
|
||||
|
||||
def test_to_dict_with_custom_init_parameters(self):
|
||||
mocked_docstore_class = document_store_class("MockedDocumentStore")
|
||||
component = CacheChecker(document_store=mocked_docstore_class(), cache_field="my_url_field")
|
||||
data = component.to_dict()
|
||||
assert data == {
|
||||
"type": "haystack.components.caching.cache_checker.CacheChecker",
|
||||
"init_parameters": {
|
||||
"document_store": {"type": "haystack.testing.factory.MockedDocumentStore", "init_parameters": {}},
|
||||
"cache_field": "my_url_field",
|
||||
},
|
||||
}
|
||||
|
||||
def test_from_dict(self):
|
||||
data = {
|
||||
"type": "haystack.components.caching.cache_checker.CacheChecker",
|
||||
"init_parameters": {
|
||||
"document_store": {
|
||||
"type": "haystack.document_stores.in_memory.document_store.InMemoryDocumentStore",
|
||||
"init_parameters": {},
|
||||
},
|
||||
"cache_field": "my_url_field",
|
||||
},
|
||||
}
|
||||
component = CacheChecker.from_dict(data)
|
||||
assert isinstance(component.document_store, InMemoryDocumentStore)
|
||||
assert component.cache_field == "my_url_field"
|
||||
|
||||
def test_from_dict_without_docstore(self):
|
||||
data = {"type": "haystack.components.caching.cache_checker.CacheChecker", "init_parameters": {}}
|
||||
with pytest.raises(
|
||||
TypeError, match="missing 2 required positional arguments: 'document_store' and 'cache_field'"
|
||||
):
|
||||
CacheChecker.from_dict(data)
|
||||
|
||||
def test_from_dict_nonexisting_docstore(self):
|
||||
# Use a type whose module passes the deserialization allowlist (haystack.*) but cannot be
|
||||
# resolved, so we still exercise the "import failed" code path rather than the allowlist gate.
|
||||
data = {
|
||||
"type": "haystack.components.caching.cache_checker.CacheChecker",
|
||||
"init_parameters": {
|
||||
"document_store": {"type": "haystack.does.not.exist.DocumentStore", "init_parameters": {}}
|
||||
},
|
||||
}
|
||||
with pytest.raises(
|
||||
ImportError, match=r"Failed to deserialize 'document_store':.*haystack\.does\.not\.exist\.DocumentStore"
|
||||
):
|
||||
CacheChecker.from_dict(data)
|
||||
|
||||
def test_run(self, in_memory_doc_store):
|
||||
documents = [
|
||||
Document(content="doc1", meta={"url": "https://example.com/1"}),
|
||||
Document(content="doc2", meta={"url": "https://example.com/2"}),
|
||||
Document(content="doc3", meta={"url": "https://example.com/1"}),
|
||||
Document(content="doc4", meta={"url": "https://example.com/2"}),
|
||||
]
|
||||
in_memory_doc_store.write_documents(documents)
|
||||
checker = CacheChecker(in_memory_doc_store, cache_field="url")
|
||||
results = checker.run(items=["https://example.com/1", "https://example.com/5"])
|
||||
assert results == {"hits": [documents[0], documents[2]], "misses": ["https://example.com/5"]}
|
||||
|
||||
def test_filters_syntax(self):
|
||||
mocked_docstore_class = document_store_class("MockedDocumentStore")
|
||||
mocked_docstore_class.filter_documents = MagicMock()
|
||||
checker = CacheChecker(document_store=mocked_docstore_class(), cache_field="url")
|
||||
checker.run(items=["https://example.com/1"])
|
||||
valid_filters_syntax = {"field": "url", "operator": "==", "value": "https://example.com/1"}
|
||||
mocked_docstore_class.filter_documents.assert_any_call(filters=valid_filters_syntax)
|
||||
Reference in New Issue
Block a user