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): improve vector provider test type coverage (#44775)

Eddy ZHANGaa59e823352f
diff --git a/api/providers/vdb/vdb-baidu/tests/unit_tests/src/dify_vdb_baidu/test_baidu_vector.py b/api/providers/vdb/vdb-baidu/tests/unit_tests/src/dify_vdb_baidu/test_baidu_vector.py
index 5e5fe6780b5..ef0e17c2c30 100644
--- a/api/providers/vdb/vdb-baidu/tests/unit_tests/src/dify_vdb_baidu/test_baidu_vector.py
+++ b/api/providers/vdb/vdb-baidu/tests/unit_tests/src/dify_vdb_baidu/test_baidu_vector.py
@@ -1,17 +1,21 @@
import importlib
import json
import sys
-from types import SimpleNamespace
-from unittest.mock import MagicMock, patch
+from collections.abc import Generator
+from contextlib import contextmanager
+from dataclasses import dataclass
+from types import ModuleType, SimpleNamespace
+from typing import override
import pytest
from core.rag.datasource.vdb.vector_integration_test_support import FakeModule
+from core.rag.embedding.embedding_base import Embeddings
from core.rag.models.document import Document
from models.dataset import Dataset
-def _build_fake_pymochow_modules():
+def _build_fake_pymochow_modules() -> dict[str, FakeModule]:
pymochow = FakeModule("pymochow")
pymochow.__path__ = []
pymochow_auth = FakeModule("pymochow.auth")
@@ -27,13 +31,13 @@ def _build_fake_pymochow_modules():
pymochow_model_table = FakeModule("pymochow.model.table")
class _SimpleObject:
- def __init__[**P](self, *args: P.args, **kwargs: P.kwargs):
+ def __init__[**P](self, *args: P.args, **kwargs: P.kwargs) -> None:
self.args = args
for key, value in kwargs.items():
setattr(self, key, value)
class ServerError(Exception):
- def __init__(self, code):
+ def __init__(self, code: int) -> None:
super().__init__(f"server error {code}")
self.code = code
@@ -123,7 +127,7 @@ def _build_fake_pymochow_modules():
@pytest.fixture
-def baidu_module(monkeypatch: pytest.MonkeyPatch):
+def baidu_module(monkeypatch: pytest.MonkeyPatch) -> ModuleType:
for name, module in _build_fake_pymochow_modules().items():
monkeypatch.setitem(sys.modules, name, module)
import dify_vdb_baidu.baidu_vector as module
@@ -131,7 +135,146 @@ def baidu_module(monkeypatch: pytest.MonkeyPatch):
return importlib.reload(module)
-def test_baidu_config_validation(baidu_module):
+@dataclass(frozen=True)
+class IndexInfo:
+ """The index description pymochow returns from ``describe_index``."""
+
+ state: str
+
+
+@dataclass(frozen=True)
+class DatabaseInfo:
+ """A database entry pymochow returns from ``list_databases``."""
+
+ database_name: str
+
+
+class FakeTable:
+ """A pymochow table that records what the vector store sends to it."""
+
+ def __init__(self, state: str = "NORMAL") -> None:
+ self.state = state
+ self.query_result: object = None
+ self.upserted_rows: list[list[object]] = []
+ self.delete_filters: list[str] = []
+ self.rebuilt_indexes: list[str] = []
+ self.described_indexes: list[str] = []
+
+ def upsert(self, *, rows: list[object]) -> None:
+ self.upserted_rows.append(rows)
+
+ def query(self, *, primary_key: dict[str, str]) -> object:
+ return self.query_result
+
+ def delete(self, *, filter: str) -> None:
+ self.delete_filters.append(filter)
+
+ def search(self, *, anns: object, projections: list[str], retrieve_vector: bool) -> str:
+ return "vector_result"
+
+ def bm25_search(self, *, request: object, projections: list[str]) -> str:
+ return "bm25_result"
+
+ def rebuild_index(self, index_name: str) -> None:
+ self.rebuilt_indexes.append(index_name)
+
+ def describe_index(self, index_name: str) -> IndexInfo:
+ self.described_indexes.append(index_name)
+ return IndexInfo(state="NORMAL")
+
+
+class FakeDatabase:
+ """A pymochow database holding one table, which records the table-level calls it receives."""
+
+ def __init__(self, table: FakeTable | None = None, *, table_error: Exception | None = None) -> None:
+ self._table = table or FakeTable()
+ self.table_error = table_error
+ self.drop_error: Exception | None = None
+ self.opened_tables: list[str] = []
+ self.dropped_tables: list[str] = []
+ self.created_tables: list[str] = []
+
+ def table(self, table_name: str) -> FakeTable:
+ self.opened_tables.append(table_name)
+ if self.table_error:
+ raise self.table_error
+ return self._table
+
+ def drop_table(self, *, table_name: str) -> None:
+ self.dropped_tables.append(table_name)
+ if self.drop_error:
+ raise self.drop_error
+
+ def create_table(
+ self, *, table_name: str, replication: int, partition: object, schema: object, description: str
+ ) -> None:
+ self.created_tables.append(table_name)
+
+ def describe_table(self, table_name: str) -> FakeTable:
+ return self._table
+
+
+class FakeClient:
+ """A MochowClient that knows a fixed set of databases."""
+
+ def __init__(self, database_names: list[str], *, create_error: Exception | None = None) -> None:
+ self._database_names = database_names
+ self._create_error = create_error
+ self.created_databases: list[str] = []
+
+ def list_databases(self) -> list[DatabaseInfo]:
+ return [DatabaseInfo(database_name=name) for name in self._database_names]
+
+ def create_database(self, *, database_name: str) -> None:
+ self.created_databases.append(database_name)
+ if self._create_error:
+ raise self._create_error
+
+ def database(self, database_name: str) -> str:
+ return f"database:{database_name}"
+
+
+class FakeRedis:
+ """The parts of ``redis_client`` that table creation uses, returning one cached value for every key."""
+
+ def __init__(self, cached: object = None) -> None:
+ self.cached = cached
+ self.lock_names: list[str] = []
+ self.set_calls: list[tuple[str, object, int | None]] = []
+
+ @contextmanager
+ def lock(self, name: str, timeout: int) -> Generator[None]:
+ self.lock_names.append(name)
+ yield
+
+ def get(self, key: str) -> object:
+ return self.cached
+
+ def set(self, key: str, value: object, ex: int | None = None) -> None:
+ self.set_calls.append((key, value, ex))
+
+
+class UnusedEmbeddings(Embeddings):
+ """Embeddings the factory only passes on to the vector store."""
+
+ @override
+ def embed_documents(self, texts: list[str]) -> list[list[float]]:
+ raise NotImplementedError
+
+ @override
+ def embed_multimodal_documents(self, multimodel_documents: list[dict[str, object]]) -> list[list[float]]:
+ raise NotImplementedError
+
+ @override
+ def embed_query(self, text: str) -> list[float]:
+ raise NotImplementedError
+
+ @override
+ def embed_multimodal_query(self, multimodel_document: dict[str, object]) -> list[float]:
+ raise NotImplementedError
+
+
+def test_baidu_config_validation(baidu_module: ModuleType) -> None:
values = {
"endpoint": "https://example.com",
"account": "account",
@@ -153,7 +296,7 @@ def test_baidu_config_validation(baidu_module):
baidu_module.BaiduConfig.model_validate(invalid)
-def test_get_search_result_handles_metadata_and_threshold(baidu_module):
+def test_get_search_result_handles_metadata_and_threshold(baidu_module: ModuleType) -> None:
vector = baidu_module.BaiduVector.__new__(baidu_module.BaiduVector)
response = SimpleNamespace(
rows=[
@@ -171,92 +314,96 @@ def test_get_search_result_handles_metadata_and_threshold(baidu_module):
assert docs[1].page_content == "doc3"
-def test_delete_by_ids_and_delete_by_metadata_field(baidu_module):
+def test_delete_by_ids_and_delete_by_metadata_field(baidu_module: ModuleType) -> None:
+ table = FakeTable()
+ db = FakeDatabase(table)
vector = baidu_module.BaiduVector.__new__(baidu_module.BaiduVector)
- table = MagicMock()
- vector._db = MagicMock()
- vector._db.table.return_value = table
+ vector._db = db
vector._collection_name = "collection_1"
vector.delete_by_ids([])
- table.delete.assert_not_called()
+ assert db.opened_tables == []
vector.delete_by_ids(["id1", "id2"])
- table.delete.assert_called_once()
-
- table.delete.reset_mock()
vector.delete_by_metadata_field("source", 'abc"def')
- delete_filter = table.delete.call_args.kwargs["filter"]
- assert delete_filter == 'metadata["source"] = "abc\\"def"'
+
+ assert table.delete_filters == ["id IN('id1', 'id2')", 'metadata["source"] = "abc\\"def"']
+ assert db.opened_tables == ["collection_1", "collection_1"]
-def test_delete_handles_table_not_exist_error_and_raises_for_other_codes(baidu_module):
+def test_delete_handles_table_not_exist_error_and_raises_for_other_codes(baidu_module: ModuleType) -> None:
+ db = FakeDatabase()
vector = baidu_module.BaiduVector.__new__(baidu_module.BaiduVector)
vector._collection_name = "collection_1"
- vector._db = MagicMock()
+ vector._db = db
- vector._db.drop_table.side_effect = baidu_module.ServerError(baidu_module.ServerErrCode.TABLE_NOT_EXIST)
+ db.drop_error = baidu_module.ServerError(baidu_module.ServerErrCode.TABLE_NOT_EXIST)
vector.delete()
+ assert db.dropped_tables == ["collection_1"]
- vector._db.drop_table.side_effect = baidu_module.ServerError(9999)
+ db.drop_error = baidu_module.ServerError(9999)
with pytest.raises(baidu_module.ServerError):
vector.delete()
-def test_init_database_uses_existing_or_creates_when_missing(baidu_module):
+def test_init_database_uses_existing_or_creates_when_missing(baidu_module: ModuleType) -> None:
vector = baidu_module.BaiduVector.__new__(baidu_module.BaiduVector)
- vector._client = MagicMock()
vector._client_config = SimpleNamespace(database="my_db")
- vector._client.list_databases.return_value = [SimpleNamespace(database_name="my_db")]
- vector._client.database.return_value = "existing_db"
- assert vector._init_database() == "existing_db"
+ existing = FakeClient(["my_db"])
+ vector._client = existing
+ assert vector._init_database() == "database:my_db"
+ assert existing.created_databases == []
- vector._client.list_databases.return_value = []
- vector._client.database.return_value = "created_db"
- vector._client.create_database.side_effect = None
- assert vector._init_database() == "created_db"
+ missing = FakeClient([])
+ vector._client = missing
+ assert vector._init_database() == "database:my_db"
+ assert missing.created_databases == ["my_db"]
- vector._client.create_database.side_effect = baidu_module.ServerError(baidu_module.ServerErrCode.DB_ALREADY_EXIST)
- assert vector._init_database() == "created_db"
+ vector._client = FakeClient([], create_error=baidu_module.ServerError(baidu_module.ServerErrCode.DB_ALREADY_EXIST))
+ assert vector._init_database() == "database:my_db"
-def test_table_existed_checks_table_access(baidu_module):
+def test_table_existed_checks_table_access(baidu_module: ModuleType) -> None:
+ db = FakeDatabase()
vector = baidu_module.BaiduVector.__new__(baidu_module.BaiduVector)
vector._collection_name = "collection_1"
- vector._db = MagicMock()
- vector._db.table.return_value = MagicMock()
+ vector._db = db
assert vector._table_existed() is True
- vector._db.table.side_effect = baidu_module.ServerError(baidu_module.ServerErrCode.TABLE_NOT_EXIST)
+ db.table_error = baidu_module.ServerError(baidu_module.ServerErrCode.TABLE_NOT_EXIST)
assert vector._table_existed() is False
- vector._db.table.side_effect = baidu_module.ServerError(9999)
+ db.table_error = baidu_module.ServerError(9999)
with pytest.raises(baidu_module.ServerError):
vector._table_existed()
-def test_search_methods_delegate_to_database_table(baidu_module):
+def test_search_methods_delegate_to_database_table(baidu_module: ModuleType, monkeypatch: pytest.MonkeyPatch) -> None:
vector = baidu_module.BaiduVector.__new__(baidu_module.BaiduVector)
vector._collection_name = "collection_1"
- vector._db = MagicMock()
- vector._get_search_res = MagicMock(return_value=[Document(page_content="doc", metadata={"doc_id": "1"})])
+ vector._db = FakeDatabase()
+ documents = [Document(page_content="doc", metadata={"doc_id": "1"})]
+ parsed: list[tuple[object, float]] = []
- table = MagicMock()
- vector._db.table.return_value = table
- table.search.return_value = "vector_result"
- table.bm25_search.return_value = "bm25_result"
+ def get_search_res(res: object, score_threshold: float) -> list[Document]:
+ parsed.append((res, score_threshold))
+ return documents
+
+ monkeypatch.setattr(vector, "_get_search_res", get_search_res)
result1 = vector.search_by_vector([0.1, 0.2], top_k=3, document_ids_filter=["doc-1"], score_threshold=0.2)
result2 = vector.search_by_full_text("query", top_k=3, document_ids_filter=["doc-1"], score_threshold=0.2)
- assert result1 == vector._get_search_res.return_value
- assert result2 == vector._get_search_res.return_value
- assert vector._get_search_res.call_count == 2
+ assert result1 == documents
+ assert result2 == documents
+ assert parsed == [("vector_result", 0.2), ("bm25_result", 0.2)]
-def test_factory_initializes_collection_name_and_index_struct(baidu_module, monkeypatch: pytest.MonkeyPatch):
+def test_factory_initializes_collection_name_and_index_struct(
+ baidu_module: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
factory = baidu_module.BaiduVectorFactory()
dataset = Dataset(id="dataset-1")
monkeypatch.setattr(baidu_module.Dataset, "gen_collection_name_by_id", lambda _id: "AUTO_COLLECTION")
@@ -272,20 +419,25 @@ def test_factory_initializes_collection_name_and_index_struct(baidu_module, monk
monkeypatch.setattr(baidu_module.dify_config, "BAIDU_VECTOR_DB_AUTO_BUILD_ROW_COUNT_INCREMENT", 500)
monkeypatch.setattr(baidu_module.dify_config, "BAIDU_VECTOR_DB_AUTO_BUILD_ROW_COUNT_INCREMENT_RATIO", 0.05)
monkeypatch.setattr(baidu_module.dify_config, "BAIDU_VECTOR_DB_REBUILD_INDEX_TIMEOUT_IN_SECONDS", 300)
+ vector_kwargs: list[dict[str, object]] = []
+
+ def build_vector(**kwargs: object) -> str:
+ vector_kwargs.append(kwargs)
+ return "vector"
- with patch.object(baidu_module, "BaiduVector", return_value="vector") as vector_cls:
- result = factory.init_vector(dataset, attributes=[], embeddings=MagicMock(), session=None)
+ monkeypatch.setattr(baidu_module, "BaiduVector", build_vector)
+ result = factory.init_vector(dataset, attributes=[], embeddings=UnusedEmbeddings(), session=None)
assert result == "vector"
- assert vector_cls.call_args.kwargs["collection_name"] == "auto_collection"
+ assert [kwargs["collection_name"] for kwargs in vector_kwargs] == ["auto_collection"]
assert dataset.index_struct is not None
-def test_init_get_type_to_index_struct_and_create_delegate(baidu_module, monkeypatch: pytest.MonkeyPatch):
- init_client = MagicMock(return_value="client")
- init_database = MagicMock(return_value="database")
- monkeypatch.setattr(baidu_module.BaiduVector, "_init_client", init_client)
- monkeypatch.setattr(baidu_module.BaiduVector, "_init_database", init_database)
+def test_init_get_type_to_index_struct_and_create_delegate(
+ baidu_module: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ monkeypatch.setattr(baidu_module.BaiduVector, "_init_client", lambda _self, _config: "client")
+ monkeypatch.setattr(baidu_module.BaiduVector, "_init_database", lambda _self: "database")
config = baidu_module.BaiduConfig(
endpoint="https://example.com",
@@ -300,20 +452,19 @@ def test_init_get_type_to_index_struct_and_create_delegate(baidu_module, monkeyp
assert vector._client == "client"
assert vector._db == "database"
- vector._create_table = MagicMock()
- vector.add_texts = MagicMock()
+ calls: list[tuple[str, object]] = []
+ monkeypatch.setattr(vector, "_create_table", lambda dimension: calls.append(("create_table", dimension)))
+ monkeypatch.setattr(vector, "add_texts", lambda texts, embeddings: calls.append(("add_texts", (texts, embeddings))))
docs = [Document(page_content="p1", metadata={"doc_id": "d1"})]
vector.create(docs, [[0.1, 0.2]])
- vector._create_table.assert_called_once_with(2)
- vector.add_texts.assert_called_once_with(docs, [[0.1, 0.2]])
+ assert calls == [("create_table", 2), ("add_texts", (docs, [[0.1, 0.2]]))]
-def test_add_texts_batches_rows(baidu_module):
+def test_add_texts_batches_rows(baidu_module: ModuleType) -> None:
+ table = FakeTable()
vector = baidu_module.BaiduVector.__new__(baidu_module.BaiduVector)
vector._collection_name = "collection_1"
- table = MagicMock()
- vector._db = MagicMock()
- vector._db.table.return_value = table
+ vector._db = FakeDatabase(table)
docs = [
Document(page_content="doc-1", metadata={"doc_id": "id-1", "document_id": "doc-1"}),
@@ -321,17 +472,14 @@ def test_add_texts_batches_rows(baidu_module):
]
vector.add_texts(docs, [[0.1, 0.2], [0.3, 0.4]])
- assert table.upsert.call_count == 1
- inserted_rows = table.upsert.call_args.kwargs["rows"]
- assert len(inserted_rows) == 2
+ assert [len(rows) for rows in table.upserted_rows] == [2]
-def test_add_texts_batches_more_than_batch_size(baidu_module):
+def test_add_texts_batches_more_than_batch_size(baidu_module: ModuleType) -> None:
+ table = FakeTable()
vector = baidu_module.BaiduVector.__new__(baidu_module.BaiduVector)
vector._collection_name = "collection_1"
- table = MagicMock()
- vector._db = MagicMock()
- vector._db.table.return_value = table
+ vector._db = FakeDatabase(table)
docs = [
Document(page_content=f"doc-{idx}", metadata={"doc_id": f"id-{idx}", "document_id": f"doc-{idx}"})
@@ -341,29 +489,26 @@ def test_add_texts_batches_more_than_batch_size(baidu_module):
vector.add_texts(docs, embeddings)
- assert table.upsert.call_count == 2
- assert len(table.upsert.call_args_list[0].kwargs["rows"]) == 1000
- assert len(table.upsert.call_args_list[1].kwargs["rows"]) == 1
+ assert [len(rows) for rows in table.upserted_rows] == [1000, 1]
-def test_text_exists_returns_false_when_query_code_is_not_success(baidu_module):
+def test_text_exists_returns_false_when_query_code_is_not_success(baidu_module: ModuleType) -> None:
+ table = FakeTable()
vector = baidu_module.BaiduVector.__new__(baidu_module.BaiduVector)
vector._collection_name = "collection_1"
- table = MagicMock()
- vector._db = MagicMock()
- vector._db.table.return_value = table
+ vector._db = FakeDatabase(table)
- table.query.return_value = SimpleNamespace(code=0)
+ table.query_result = SimpleNamespace(code=0)
assert vector.text_exists("id-1") is True
- table.query.return_value = SimpleNamespace(code=1)
+ table.query_result = SimpleNamespace(code=1)
assert vector.text_exists("id-1") is False
- table.query.return_value = None
+ table.query_result = None
assert vector.text_exists("id-1") is False
-def test_get_search_result_handles_invalid_metadata_json(baidu_module):
+def test_get_search_result_handles_invalid_metadata_json(baidu_module: ModuleType) -> None:
vector = baidu_module.BaiduVector.__new__(baidu_module.BaiduVector)
response = SimpleNamespace(rows=[{"row": {"page_content": "doc1", "metadata": "{bad json"}, "score": 0.7}])
@@ -374,14 +519,7 @@ def test_get_search_result_handles_invalid_metadata_json(baidu_module):
assert "document_id" not in docs[0].metadata
-def test_init_client_constructs_configuration_and_client(baidu_module, monkeypatch: pytest.MonkeyPatch):
- credentials = MagicMock(return_value="credentials")
- configuration = MagicMock(return_value="configuration")
- client_cls = MagicMock(return_value="client")
- monkeypatch.setattr(baidu_module, "BceCredentials", credentials)
- monkeypatch.setattr(baidu_module, "Configuration", configuration)
- monkeypatch.setattr(baidu_module, "MochowClient", client_cls)
-
+def test_init_client_constructs_configuration_and_client(baidu_module: ModuleType) -> None:
vector = baidu_module.BaiduVector.__new__(baidu_module.BaiduVector)
config = SimpleNamespace(
account="account",
@@ -392,28 +530,25 @@ def test_init_client_constructs_configuration_and_client(baidu_module, monkeypat
client = vector._init_client(config)
- assert client == "client"
- credentials.assert_called_once_with("account", "key")
- configuration.assert_called_once_with(
- credentials="credentials",
- endpoint="https://endpoint",
- connection_timeout_in_mills=12_345,
- )
- client_cls.assert_called_once_with("configuration")
+ # The fake SDK classes keep their constructor arguments, so the client exposes the whole chain.
+ (configuration,) = client.args
+ assert configuration.credentials.args == ("account", "key")
+ assert configuration.endpoint == "https://endpoint"
+ assert configuration.connection_timeout_in_mills == 12_345
-def test_init_database_raises_for_unknown_create_database_error(baidu_module):
+def test_init_database_raises_for_unknown_create_database_error(baidu_module: ModuleType) -> None:
vector = baidu_module.BaiduVector.__new__(baidu_module.BaiduVector)
- vector._client = MagicMock()
+ vector._client = FakeClient([], create_error=baidu_module.ServerError(9999))
vector._client_config = SimpleNamespace(database="my_db")
- vector._client.list_databases.return_value = []
- vector._client.create_database.side_effect = baidu_module.ServerError(9999)
with pytest.raises(baidu_module.ServerError):
vector._init_database()
-def test_create_table_handles_cache_and_validation_paths(baidu_module, monkeypatch: pytest.MonkeyPatch):
+def test_create_table_handles_cache_and_validation_paths(
+ baidu_module: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
vector = baidu_module.BaiduVector.__new__(baidu_module.BaiduVector)
vector._collection_name = "collection_1"
vector._client_config = SimpleNamespace(
@@ -427,47 +562,40 @@ def test_create_table_handles_cache_and_validation_paths(baidu_module, monkeypat
replicas=1,
shard=1,
)
- vector._db = MagicMock()
- table = MagicMock()
- table.state = baidu_module.TableState.NORMAL
- vector._db.describe_table.return_value = table
- vector._table_existed = MagicMock(return_value=False)
- vector.delete = MagicMock()
-
- lock = MagicMock()
- lock.__enter__.return_value = None
- lock.__exit__.return_value = None
- monkeypatch.setattr(baidu_module.redis_client, "lock", MagicMock(return_value=lock))
- monkeypatch.setattr(baidu_module.redis_client, "set", MagicMock())
+ table = FakeTable()
+ db = FakeDatabase(table)
+ vector._db = db
+ fake_redis = FakeRedis(cached=1)
+ monkeypatch.setattr(baidu_module, "redis_client", fake_redis)
monkeypatch.setattr(baidu_module.time, "sleep", lambda _s: None)
- monkeypatch.setattr(vector, "_wait_for_index_ready", MagicMock())
# Cached table skips all work.
- monkeypatch.setattr(baidu_module.redis_client, "get", MagicMock(return_value=1))
vector._create_table(3)
- vector._db.create_table.assert_not_called()
+ assert db.opened_tables == []
# Existing table also skips creation.
- monkeypatch.setattr(baidu_module.redis_client, "get", MagicMock(return_value=None))
- vector._table_existed.return_value = True
+ fake_redis.cached = None
vector._create_table(3)
- vector._db.create_table.assert_not_called()
+ assert db.opened_tables == ["collection_1"]
+ assert db.created_tables == []
# Create table when cache is empty and table does not exist.
- vector._table_existed.return_value = False
+ db.table_error = baidu_module.ServerError(baidu_module.ServerErrCode.TABLE_NOT_EXIST)
vector._create_table(3)
- vector._db.create_table.assert_called_once()
- baidu_module.redis_client.set.assert_called_once_with("vector_indexing_collection_1", 1, ex=3600)
- table.rebuild_index.assert_called_once_with(vector.vector_index)
- vector._wait_for_index_ready.assert_called_once_with(table, 3600)
+ assert db.dropped_tables == ["collection_1"]
+ assert db.created_tables == ["collection_1"]
+ assert fake_redis.set_calls == [("vector_indexing_collection_1", 1, 3600)]
+ assert table.rebuilt_indexes == [vector.vector_index]
+ assert table.described_indexes == [vector.vector_index]
+ assert fake_redis.lock_names == ["vector_indexing_lock_collection_1"] * 3
-def test_create_table_raises_for_invalid_index_or_metric(baidu_module, monkeypatch: pytest.MonkeyPatch):
+def test_create_table_raises_for_invalid_index_or_metric(
+ baidu_module: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
vector = baidu_module.BaiduVector.__new__(baidu_module.BaiduVector)
vector._collection_name = "collection_1"
- vector._db = MagicMock()
- vector._table_existed = MagicMock(return_value=False)
- vector.delete = MagicMock()
+ vector._db = FakeDatabase(table_error=baidu_module.ServerError(baidu_module.ServerErrCode.TABLE_NOT_EXIST))
vector._client_config = SimpleNamespace(
index_type="INVALID",
metric_type="IP",
@@ -479,12 +607,7 @@ def test_create_table_raises_for_invalid_index_or_metric(baidu_module, monkeypat
replicas=1,
shard=1,
)
-
- lock = MagicMock()
- lock.__enter__.return_value = None
- lock.__exit__.return_value = None
- monkeypatch.setattr(baidu_module.redis_client, "lock", MagicMock(return_value=lock))
- monkeypatch.setattr(baidu_module.redis_client, "get", MagicMock(return_value=None))
+ monkeypatch.setattr(baidu_module, "redis_client", FakeRedis())
with pytest.raises(ValueError, match="unsupported index_type"):
vector._create_table(3)
@@ -495,7 +618,9 @@ def test_create_table_raises_for_invalid_index_or_metric(baidu_module, monkeypat
vector._create_table(3)
-def test_create_table_raises_timeout_if_table_never_becomes_normal(baidu_module, monkeypatch: pytest.MonkeyPatch):
+def test_create_table_raises_timeout_if_table_never_becomes_normal(
+ baidu_module: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
vector = baidu_module.BaiduVector.__new__(baidu_module.BaiduVector)
vector._collection_name = "collection_1"
vector._client_config = SimpleNamespace(
@@ -509,26 +634,21 @@ def test_create_table_raises_timeout_if_table_never_becomes_normal(baidu_module,
replicas=1,
shard=1,
)
- vector._db = MagicMock()
- vector._db.describe_table.return_value = SimpleNamespace(state="CREATING")
- vector._table_existed = MagicMock(return_value=False)
- vector.delete = MagicMock()
-
- lock = MagicMock()
- lock.__enter__.return_value = None
- lock.__exit__.return_value = None
- monkeypatch.setattr(baidu_module.redis_client, "lock", MagicMock(return_value=lock))
- monkeypatch.setattr(baidu_module.redis_client, "get", MagicMock(return_value=None))
+ vector._db = FakeDatabase(
+ FakeTable(state="CREATING"),
+ table_error=baidu_module.ServerError(baidu_module.ServerErrCode.TABLE_NOT_EXIST),
+ )
+ monkeypatch.setattr(baidu_module, "redis_client", FakeRedis())
monkeypatch.setattr(baidu_module.time, "sleep", lambda _s: None)
- monkeypatch.setattr(baidu_module.time, "time", MagicMock(side_effect=[0, 301]))
+ monkeypatch.setattr(baidu_module.time, "time", iter([0, 301]).__next__)
with pytest.raises(TimeoutError, match="Table creation timeout"):
vector._create_table(3)
def test_factory_uses_existing_collection_prefix_when_index_struct_exists(
- baidu_module, monkeypatch: pytest.MonkeyPatch
-):
+ baidu_module: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
factory = baidu_module.BaiduVectorFactory()
dataset = Dataset(
id="dataset-1", index_struct=json.dumps({"vector_store": {"class_prefix": "EXISTING_COLLECTION"}})
@@ -545,9 +665,14 @@ def test_factory_uses_existing_collection_prefix_when_index_struct_exists(
monkeypatch.setattr(baidu_module.dify_config, "BAIDU_VECTOR_DB_AUTO_BUILD_ROW_COUNT_INCREMENT", 500)
monkeypatch.setattr(baidu_module.dify_config, "BAIDU_VECTOR_DB_AUTO_BUILD_ROW_COUNT_INCREMENT_RATIO", 0.05)
monkeypatch.setattr(baidu_module.dify_config, "BAIDU_VECTOR_DB_REBUILD_INDEX_TIMEOUT_IN_SECONDS", 300)
+ vector_kwargs: list[dict[str, object]] = []
+
+ def build_vector(**kwargs: object) -> str:
+ vector_kwargs.append(kwargs)
+ return "vector"
- with patch.object(baidu_module, "BaiduVector", return_value="vector") as vector_cls:
- result = factory.init_vector(dataset, attributes=[], embeddings=MagicMock(), session=None)
+ monkeypatch.setattr(baidu_module, "BaiduVector", build_vector)
+ result = factory.init_vector(dataset, attributes=[], embeddings=UnusedEmbeddings(), session=None)
assert result == "vector"
- assert vector_cls.call_args.kwargs["collection_name"] == "existing_collection"
+ assert [kwargs["collection_name"] for kwargs in vector_kwargs] == ["existing_collection"]
diff --git a/api/providers/vdb/vdb-chroma/tests/unit_tests/src/dify_vdb_chroma/test_chroma_vector.py b/api/providers/vdb/vdb-chroma/tests/unit_tests/src/dify_vdb_chroma/test_chroma_vector.py
index acb18f606c4..a6524b10196 100644
--- a/api/providers/vdb/vdb-chroma/tests/unit_tests/src/dify_vdb_chroma/test_chroma_vector.py
+++ b/api/providers/vdb/vdb-chroma/tests/unit_tests/src/dify_vdb_chroma/test_chroma_vector.py
@@ -2,6 +2,7 @@ import importlib
import json
import sys
from collections import UserDict
+from types import ModuleType
from unittest.mock import MagicMock, patch
import pytest
@@ -11,28 +12,28 @@ from core.rag.models.document import Document
from models.dataset import Dataset
-def _build_fake_chroma_modules():
+def _build_fake_chroma_modules() -> FakeModule:
chroma = FakeModule("chromadb")
chroma.DEFAULT_TENANT = "default_tenant"
chroma.DEFAULT_DATABASE = "default_database"
class Settings:
- def __init__(self, **kwargs):
+ def __init__(self, **kwargs: object) -> None:
for key, value in kwargs.items():
setattr(self, key, value)
- class QueryResult(UserDict):
+ class QueryResult(UserDict[str, object]):
pass
class _Collection:
- def __init__(self):
+ def __init__(self) -> None:
self.upsert = MagicMock()
self.delete = MagicMock()
self.query = MagicMock()
self.get = MagicMock(return_value={})
class _Client:
- def __init__(self, **kwargs):
+ def __init__(self, **kwargs: object) -> None:
self.kwargs = kwargs
self.collection = _Collection()
self.get_or_create_collection = MagicMock(return_value=self.collection)
@@ -45,7 +46,7 @@ def _build_fake_chroma_modules():
@pytest.fixture
-def chroma_module(monkeypatch: pytest.MonkeyPatch):
+def chroma_module(monkeypatch: pytest.MonkeyPatch) -> ModuleType:
fake_chroma = _build_fake_chroma_modules()
monkeypatch.setitem(sys.modules, "chromadb", fake_chroma)
import dify_vdb_chroma.chroma_vector as module
@@ -53,7 +54,7 @@ def chroma_module(monkeypatch: pytest.MonkeyPatch):
return importlib.reload(module)
-def test_chroma_config_to_params_builds_expected_payload(chroma_module):
+def test_chroma_config_to_params_builds_expected_payload(chroma_module: ModuleType) -> None:
config = chroma_module.ChromaConfig(
host="localhost",
port=8000,
@@ -74,7 +75,9 @@ def test_chroma_config_to_params_builds_expected_payload(chroma_module):
assert params["settings"].chroma_client_auth_credentials == "credentials"
-def test_create_collection_uses_redis_lock_and_cache(chroma_module, monkeypatch: pytest.MonkeyPatch):
+def test_create_collection_uses_redis_lock_and_cache(
+ chroma_module: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
lock = MagicMock()
lock.__enter__.return_value = None
lock.__exit__.return_value = None
@@ -92,7 +95,7 @@ def test_create_collection_uses_redis_lock_and_cache(chroma_module, monkeypatch:
chroma_module.redis_client.set.assert_called_once()
-def test_create_with_empty_texts_is_noop(chroma_module):
+def test_create_with_empty_texts_is_noop(chroma_module: ModuleType) -> None:
vector = chroma_module.ChromaVector(
collection_name="collection_1",
config=chroma_module.ChromaConfig(host="localhost", port=8000, tenant="t", database="d"),
@@ -101,7 +104,7 @@ def test_create_with_empty_texts_is_noop(chroma_module):
vector._client.get_or_create_collection.assert_not_called()
-def test_create_with_texts_creates_collection_and_upserts(chroma_module):
+def test_create_with_texts_creates_collection_and_upserts(chroma_module: ModuleType) -> None:
vector = chroma_module.ChromaVector(
collection_name="collection_1",
config=chroma_module.ChromaConfig(host="localhost", port=8000, tenant="t", database="d"),
@@ -113,7 +116,7 @@ def test_create_with_texts_creates_collection_and_upserts(chroma_module):
vector._client.collection.upsert.assert_called_once()
-def test_delete_methods_and_text_exists(chroma_module):
+def test_delete_methods_and_text_exists(chroma_module: ModuleType) -> None:
vector = chroma_module.ChromaVector(
collection_name="collection_1",
config=chroma_module.ChromaConfig(host="localhost", port=8000, tenant="t", database="d"),
@@ -130,24 +133,25 @@ def test_delete_methods_and_text_exists(chroma_module):
vector._client.collection.get.return_value = {"ids": ["id-1"]}
assert vector.text_exists("id-1") is True
- vector._client.collection.get.return_value = {}
+ vector._client.collection.get.return_value = dict[str, list[str]]()
assert vector.text_exists("id-2") is False
vector.delete()
vector._client.delete_collection.assert_called_once_with("collection_1")
-def test_search_by_vector_handles_empty_results(chroma_module):
+def test_search_by_vector_handles_empty_results(chroma_module: ModuleType) -> None:
vector = chroma_module.ChromaVector(
collection_name="collection_1",
config=chroma_module.ChromaConfig(host="localhost", port=8000, tenant="t", database="d"),
)
- vector._client.collection.query.return_value = {"ids": [], "documents": [], "metadatas": [], "distances": []}
+ empty_result: dict[str, list[object]] = {"ids": [], "documents": [], "metadatas": [], "distances": []}
+ vector._client.collection.query.return_value = empty_result
assert vector.search_by_vector([0.1, 0.2], top_k=2) == []
-def test_search_by_vector_applies_score_threshold_and_sorting(chroma_module):
+def test_search_by_vector_applies_score_threshold_and_sorting(chroma_module: ModuleType) -> None:
vector = chroma_module.ChromaVector(
collection_name="collection_1",
config=chroma_module.ChromaConfig(host="localhost", port=8000, tenant="t", database="d"),
@@ -166,7 +170,7 @@ def test_search_by_vector_applies_score_threshold_and_sorting(chroma_module):
assert docs[0].metadata["score"] == 0.9
-def test_search_by_full_text_returns_empty_list(chroma_module):
+def test_search_by_full_text_returns_empty_list(chroma_module: ModuleType) -> None:
vector = chroma_module.ChromaVector(
collection_name="collection_1",
config=chroma_module.ChromaConfig(host="localhost", port=8000, tenant="t", database="d"),
@@ -174,7 +178,9 @@ def test_search_by_full_text_returns_empty_list(chroma_module):
assert vector.search_by_full_text("query") == []
-def test_factory_init_vector_uses_existing_or_generated_collection(chroma_module, monkeypatch: pytest.MonkeyPatch):
+def test_factory_init_vector_uses_existing_or_generated_collection(
+ chroma_module: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
factory = chroma_module.ChromaVectorFactory()
dataset_with_index = Dataset(
id="dataset-1", index_struct=json.dumps({"vector_store": {"class_prefix": "EXISTING"}})
diff --git a/api/providers/vdb/vdb-huawei-cloud/tests/unit_tests/src/dify_vdb_huawei_cloud/test_huawei_cloud_vector.py b/api/providers/vdb/vdb-huawei-cloud/tests/unit_tests/src/dify_vdb_huawei_cloud/test_huawei_cloud_vector.py
index acefb2d9745..1c8ee619f74 100644
--- a/api/providers/vdb/vdb-huawei-cloud/tests/unit_tests/src/dify_vdb_huawei_cloud/test_huawei_cloud_vector.py
+++ b/api/providers/vdb/vdb-huawei-cloud/tests/unit_tests/src/dify_vdb_huawei_cloud/test_huawei_cloud_vector.py
@@ -1,7 +1,8 @@
import importlib
import json
import sys
-from types import SimpleNamespace
+from types import ModuleType, SimpleNamespace
+from typing import TYPE_CHECKING
from unittest.mock import MagicMock, patch
import pytest
@@ -11,12 +12,15 @@ from core.rag.datasource.vdb.vector_integration_test_support import FakeModule
from core.rag.models.document import Document
from models.dataset import Dataset
+if TYPE_CHECKING:
+ from dify_vdb_huawei_cloud.huawei_cloud_vector import HuaweiCloudVectorConfig
-def _build_fake_elasticsearch_modules():
+
+def _build_fake_elasticsearch_modules() -> dict[str, FakeModule]:
elasticsearch = FakeModule("elasticsearch")
class Elasticsearch:
- def __init__(self, **kwargs):
+ def __init__(self, **kwargs: object) -> None:
self.kwargs = kwargs
self.index = MagicMock()
self.exists = MagicMock(return_value=False)
@@ -31,7 +35,7 @@ def _build_fake_elasticsearch_modules():
@pytest.fixture
-def huawei_module(monkeypatch: pytest.MonkeyPatch):
+def huawei_module(monkeypatch: pytest.MonkeyPatch) -> ModuleType:
for name, module in _build_fake_elasticsearch_modules().items():
monkeypatch.setitem(sys.modules, name, module)
@@ -40,17 +44,17 @@ def huawei_module(monkeypatch: pytest.MonkeyPatch):
return importlib.reload(module)
-def _config(module):
+def _config(module: ModuleType) -> "HuaweiCloudVectorConfig":
return module.HuaweiCloudVectorConfig(hosts="http://localhost:9200", username="user", password="pass")
-def test_create_ssl_context(huawei_module):
+def test_create_ssl_context(huawei_module: ModuleType) -> None:
ctx = huawei_module.create_ssl_context()
assert ctx.check_hostname is False
assert ctx.verify_mode == huawei_module.ssl.CERT_NONE
-def test_huawei_config_validation_and_params(huawei_module):
+def test_huawei_config_validation_and_params(huawei_module: ModuleType) -> None:
with pytest.raises(ValidationError, match="HOSTS is required"):
huawei_module.HuaweiCloudVectorConfig.model_validate({"hosts": ""})
@@ -64,7 +68,7 @@ def test_huawei_config_validation_and_params(huawei_module):
assert "basic_auth" not in params
-def test_init_get_type_and_add_texts(huawei_module):
+def test_init_get_type_and_add_texts(huawei_module: ModuleType) -> None:
vector = huawei_module.HuaweiCloudVector("COLLECTION", _config(huawei_module))
assert vector._collection_name == "collection"
@@ -82,7 +86,7 @@ def test_init_get_type_and_add_texts(huawei_module):
vector._client.indices.refresh.assert_called_once_with(index="collection")
-def test_crud_methods(huawei_module):
+def test_crud_methods(huawei_module: ModuleType) -> None:
vector = huawei_module.HuaweiCloudVector("collection", _config(huawei_module))
vector._client.exists.return_value = True
@@ -99,7 +103,7 @@ def test_crud_methods(huawei_module):
vector.delete_by_ids.assert_called_once_with(["id-1"])
vector.delete_by_ids.reset_mock()
- vector._client.search.return_value = {"hits": {"hits": []}}
+ vector._client.search.return_value = {"hits": {"hits": list[dict[str, object]]()}}
vector.delete_by_metadata_field("doc_id", "x")
vector.delete_by_ids.assert_not_called()
@@ -107,7 +111,7 @@ def test_crud_methods(huawei_module):
vector._client.indices.delete.assert_called_once_with(index="collection")
-def test_search_by_vector_and_full_text(huawei_module):
+def test_search_by_vector_and_full_text(huawei_module: ModuleType) -> None:
vector = huawei_module.HuaweiCloudVector("collection", _config(huawei_module))
vector._client.search.return_value = {
"hits": {
@@ -157,9 +161,11 @@ def test_search_by_vector_and_full_text(huawei_module):
assert docs[0].page_content == "text-hit"
-def test_search_by_vector_skips_hits_without_metadata(huawei_module, monkeypatch: pytest.MonkeyPatch):
+def test_search_by_vector_skips_hits_without_metadata(
+ huawei_module: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
class FakeDocument:
- def __init__(self, page_content, vector, metadata):
+ def __init__(self, page_content: str, vector: list[float], metadata: dict[str, object]) -> None:
self.page_content = page_content
self.vector = vector
self.metadata = None
@@ -187,7 +193,7 @@ def test_search_by_vector_skips_hits_without_metadata(huawei_module, monkeypatch
assert docs == []
-def test_create_and_create_collection_paths(huawei_module, monkeypatch: pytest.MonkeyPatch):
+def test_create_and_create_collection_paths(huawei_module: ModuleType, monkeypatch: pytest.MonkeyPatch) -> None:
lock = MagicMock()
lock.__enter__.return_value = None
lock.__exit__.return_value = None
@@ -220,7 +226,7 @@ def test_create_and_create_collection_paths(huawei_module, monkeypatch: pytest.M
huawei_module.redis_client.set.assert_called_once()
-def test_huawei_factory_branches(huawei_module, monkeypatch: pytest.MonkeyPatch):
+def test_huawei_factory_branches(huawei_module: ModuleType, monkeypatch: pytest.MonkeyPatch) -> None:
factory = huawei_module.HuaweiCloudVectorFactory()
dataset_with_index = Dataset(
id="dataset-1", index_struct=json.dumps({"vector_store": {"class_prefix": "EXISTING_COLLECTION"}})
diff --git a/api/providers/vdb/vdb-tidb-on-qdrant/tests/unit_tests/src/dify_vdb_tidb_on_qdrant/test_tidb_on_qdrant_vector.py b/api/providers/vdb/vdb-tidb-on-qdrant/tests/unit_tests/src/dify_vdb_tidb_on_qdrant/test_tidb_on_qdrant_vector.py
index 46302234c02..d680e0ea8aa 100644
--- a/api/providers/vdb/vdb-tidb-on-qdrant/tests/unit_tests/src/dify_vdb_tidb_on_qdrant/test_tidb_on_qdrant_vector.py
+++ b/api/providers/vdb/vdb-tidb-on-qdrant/tests/unit_tests/src/dify_vdb_tidb_on_qdrant/test_tidb_on_qdrant_vector.py
@@ -1,5 +1,4 @@
-from typing import Protocol, cast
-from unittest.mock import patch
+from dataclasses import dataclass
import httpx
import pytest
@@ -14,23 +13,79 @@ from core.rag.rerank.entity.weight import KeywordSetting, VectorSetting, Weights
from core.rag.rerank.weight_rerank import WeightRerankRunner
-class ScrollMock(Protocol):
- side_effect: object
- call_count: int
+@dataclass(frozen=True)
+class ScrollRequest:
+ """The arguments of one ``QdrantClient.scroll`` call."""
+
+ collection_name: str
+ scroll_filter: rest.Filter
+ limit: int
+ with_payload: bool
+ with_vectors: bool
+
+
+@dataclass(frozen=True)
+class DeleteRequest:
+ """The arguments of one ``QdrantClient.delete`` call."""
+
+ collection_name: str
+ points_selector: rest.FilterSelector
+
+
+class FakeQdrantClient:
+ """A Qdrant client that serves queued scroll pages and records scroll and delete requests."""
+
+ def __init__(self) -> None:
+ self.scroll_pages: list[list[rest.Record]] = []
+ self.scroll_requests: list[ScrollRequest] = []
+ self.delete_error: Exception | None = None
+ self.delete_requests: list[DeleteRequest] = []
+
+ def scroll(
+ self, *, collection_name: str, scroll_filter: rest.Filter, limit: int, with_payload: bool, with_vectors: bool
+ ) -> tuple[list[rest.Record], None]:
+ self.scroll_requests.append(ScrollRequest(collection_name, scroll_filter, limit, with_payload, with_vectors))
+ # Once the queued pages run out, every further keyword has no matches.
+ return (self.scroll_pages.pop(0) if self.scroll_pages else []), None
+
+ def delete(self, *, collection_name: str, points_selector: rest.FilterSelector) -> None:
+ self.delete_requests.append(DeleteRequest(collection_name, points_selector))
+ if self.delete_error:
+ raise self.delete_error
+
+
+def _text_filter(text: str) -> rest.Filter:
+ return rest.Filter(must=[rest.FieldCondition(key="page_content", match=rest.MatchText(text=text))])
+
+
+def _doc_id_selector(ids: list[str]) -> rest.FilterSelector:
+ return rest.FilterSelector(
+ filter=rest.Filter(must=[rest.FieldCondition(key="metadata.doc_id", match=rest.MatchAny(any=ids))])
+ )
+
+
+def fake_client(monkeypatch: pytest.MonkeyPatch) -> FakeQdrantClient:
+ client = FakeQdrantClient()
+ monkeypatch.setattr(
+ "dify_vdb_tidb_on_qdrant.tidb_on_qdrant_vector.qdrant_client.QdrantClient", lambda **_params: client
+ )
+ return client
class TestTidbOnQdrantVectorFullTextSearch:
@pytest.fixture
- def vector(self):
- with patch("dify_vdb_tidb_on_qdrant.tidb_on_qdrant_vector.qdrant_client.QdrantClient"):
- return TidbOnQdrantVector(
- collection_name="test_collection",
- group_id="test_group",
- config=TidbOnQdrantConfig(endpoint="http://localhost:6333"),
- )
+ def vector(self, fake_client: FakeQdrantClient) -> TidbOnQdrantVector:
+ vector = TidbOnQdrantVector(
+ collection_name="test_collection",
+ group_id="test_group",
+ config=TidbOnQdrantConfig(endpoint="http://localhost:6333"),
+ )
+ assert vector._client is fake_client
+ return vector
@pytest.fixture
- def points(self):
+ def points(self) -> list[rest.Record]:
return [
rest.Record(
id=index,
@@ -41,96 +96,102 @@ class TestTidbOnQdrantVectorFullTextSearch:
]
@pytest.mark.parametrize("query", ["apple", "量子计算", "量子コンピューター"])
- def test_sends_text_and_document_filters(self, vector, points, query):
+ def test_sends_text_and_document_filters(
+ self, fake_client: FakeQdrantClient, vector: TidbOnQdrantVector, points: list[rest.Record], query: str
+ ) -> None:
# CJK cases verify transport only; tokenization must be tested against the TiDB gateway.
+ assert points[0].payload is not None
points[0].payload["page_content"] = query
- vector._client.scroll.return_value = ([points[0]], None)
+ fake_client.scroll_pages = [[points[0]]]
documents = vector.search_by_full_text(query, top_k=3, document_ids_filter=["document-1"])
- vector._client.scroll.assert_called_once_with(
- collection_name="test_collection",
- scroll_filter=rest.Filter(
- must=[
- rest.FieldCondition(key="metadata.document_id", match=rest.MatchAny(any=["document-1"])),
- rest.FieldCondition(key="page_content", match=rest.MatchText(text=query)),
- ]
- ),
- limit=3,
- with_payload=True,
- with_vectors=True,
- )
+ assert fake_client.scroll_requests == [
+ ScrollRequest(
+ collection_name="test_collection",
+ scroll_filter=rest.Filter(
+ must=[
+ rest.FieldCondition(key="metadata.document_id", match=rest.MatchAny(any=["document-1"])),
+ rest.FieldCondition(key="page_content", match=rest.MatchText(text=query)),
+ ]
+ ),
+ limit=3,
+ with_payload=True,
+ with_vectors=True,
+ )
+ ]
assert documents[0].page_content == query
assert documents[0].vector == [0.0, 1.0]
assert "score" not in documents[0].metadata
@pytest.mark.parametrize("top_k", [1, 2, 3, 4])
- def test_merges_keyword_matches_and_respects_top_k(self, vector, points, top_k):
- vector._client.scroll.side_effect = [([points[0], points[1]][:top_k], None), (points[1:][:top_k], None)]
+ def test_merges_keyword_matches_and_respects_top_k(
+ self, fake_client: FakeQdrantClient, vector: TidbOnQdrantVector, points: list[rest.Record], top_k: int
+ ) -> None:
+ fake_client.scroll_pages = [[points[0], points[1]][:top_k], points[1:][:top_k]]
documents = vector.search_by_full_text(" apple\tapple banana ", top_k=top_k)
assert [doc.metadata["doc_id"] for doc in documents] == ["1", "2", "3"][:top_k]
- calls = vector._client.scroll.call_args_list
- keywords = [call.kwargs["scroll_filter"].must[0].match.text for call in calls]
- assert keywords == ["apple", "banana"]
+ assert [request.scroll_filter for request in fake_client.scroll_requests] == [
+ _text_filter("apple"),
+ _text_filter("banana"),
+ ]
def test_later_keywords_are_not_starved_by_first_keyword(
- self, vector: TidbOnQdrantVector, points: list[rest.Record]
+ self, fake_client: FakeQdrantClient, vector: TidbOnQdrantVector, points: list[rest.Record]
) -> None:
- scroll = cast(ScrollMock, vector._client.scroll)
- scroll.side_effect = [([points[0], points[1]], None), ([points[2]], None)]
+ fake_client.scroll_pages = [[points[0], points[1]], [points[2]]]
documents = vector.search_by_full_text("common rare", top_k=2)
assert [doc.metadata["doc_id"] for doc in documents] == ["1", "3"]
- assert scroll.call_count == 2
+ assert len(fake_client.scroll_requests) == 2
def test_overlapping_keyword_matches_are_deduplicated(
- self, vector: TidbOnQdrantVector, points: list[rest.Record]
+ self, fake_client: FakeQdrantClient, vector: TidbOnQdrantVector, points: list[rest.Record]
) -> None:
- cast(ScrollMock, vector._client.scroll).side_effect = [
- ([points[0], points[1]], None),
- ([points[0], points[2]], None),
- ]
+ fake_client.scroll_pages = [[points[0], points[1]], [points[0], points[2]]]
documents = vector.search_by_full_text("common rare", top_k=3)
assert [doc.metadata["doc_id"] for doc in documents] == ["1", "3", "2"]
def test_stops_after_exhausting_duplicate_keyword_matches(
- self, vector: TidbOnQdrantVector, points: list[rest.Record]
+ self, fake_client: FakeQdrantClient, vector: TidbOnQdrantVector, points: list[rest.Record]
) -> None:
- scroll = cast(ScrollMock, vector._client.scroll)
- scroll.side_effect = [([points[0], points[1]], None), ([points[0]], None)]
+ fake_client.scroll_pages = [[points[0], points[1]], [points[0]]]
documents = vector.search_by_full_text("common rare", top_k=4)
assert [doc.metadata["doc_id"] for doc in documents] == ["1", "2"]
- assert scroll.call_count == 2
-
- def test_no_match_does_not_fall_back_to_unfiltered_scroll(self, vector):
- vector._client.scroll.return_value = ([], None)
+ assert len(fake_client.scroll_requests) == 2
+ def test_no_match_does_not_fall_back_to_unfiltered_scroll(
+ self, fake_client: FakeQdrantClient, vector: TidbOnQdrantVector
+ ) -> None:
assert vector.search_by_full_text("absentword") == []
- vector._client.scroll.assert_called_once()
- assert vector._client.scroll.call_args.kwargs["scroll_filter"] == rest.Filter(
- must=[rest.FieldCondition(key="page_content", match=rest.MatchText(text="absentword"))]
- )
+ assert [request.scroll_filter for request in fake_client.scroll_requests] == [_text_filter("absentword")]
@pytest.mark.parametrize(("query", "top_k"), [("", 2), (" \t\n", 2), ("apple", 0), ("apple", -1)])
- def test_empty_query_or_nonpositive_limit_does_not_scroll(self, vector, query, top_k):
+ def test_empty_query_or_nonpositive_limit_does_not_scroll(
+ self, fake_client: FakeQdrantClient, vector: TidbOnQdrantVector, query: str, top_k: int
+ ) -> None:
assert vector.search_by_full_text(query, top_k=top_k) == []
- vector._client.scroll.assert_not_called()
-
- def test_limits_keyword_requests(self, vector):
- vector._client.scroll.return_value = ([], None)
+ assert fake_client.scroll_requests == []
+ def test_limits_keyword_requests(self, fake_client: FakeQdrantClient, vector: TidbOnQdrantVector) -> None:
assert vector.search_by_full_text(" ".join(f"word{index}" for index in range(12))) == []
- assert vector._client.scroll.call_count == 10
-
- def test_keyword_only_hit_gets_a_weighted_rerank_score(self, vector, points):
- vector._client.scroll.return_value = ([points[0]], None)
+ assert len(fake_client.scroll_requests) == 10
+
+ def test_keyword_only_hit_gets_a_weighted_rerank_score(
+ self,
+ fake_client: FakeQdrantClient,
+ vector: TidbOnQdrantVector,
+ points: list[rest.Record],
+ monkeypatch: pytest.MonkeyPatch,
+ ) -> None:
+ fake_client.scroll_pages = [[points[0]]]
documents = vector.search_by_full_text("apple")
runner = WeightRerankRunner(
tenant_id="tenant-1",
@@ -141,11 +202,13 @@ class TestTidbOnQdrantVectorFullTextSearch:
keyword_setting=KeywordSetting(keyword_weight=0.7),
),
)
- with (
- patch.object(runner, "_calculate_keyword_score", return_value=[0.8]),
- patch("core.rag.rerank.weight_rerank.CacheEmbedding.get_cached_query_embedding", return_value=[1.0, 0.0]),
- ):
- reranked = runner.run("apple", documents, score_threshold=0.5, top_n=1)
+ monkeypatch.setattr(runner, "_calculate_keyword_score", lambda _query, _documents: [0.8])
+ monkeypatch.setattr(
+ "core.rag.rerank.weight_rerank.CacheEmbedding.get_cached_query_embedding",
+ lambda _provider, _model, _text: [1.0, 0.0],
+ )
+
+ reranked = runner.run("apple", documents, score_threshold=0.5, top_n=1)
assert len(reranked) == 1
# The vectors are orthogonal: only the keyword contribution should count.
@@ -156,164 +219,118 @@ class TestTidbOnQdrantVectorDeleteByIds:
"""Unit tests for TidbOnQdrantVector.delete_by_ids method."""
@pytest.fixture
- def vector_instance(self):
+ def vector_instance(self, fake_client: FakeQdrantClient) -> TidbOnQdrantVector:
"""Create a TidbOnQdrantVector instance for testing."""
config = TidbOnQdrantConfig(
endpoint="http://localhost:6333",
api_key="test_api_key",
)
- with patch("dify_vdb_tidb_on_qdrant.tidb_on_qdrant_vector.qdrant_client.QdrantClient"):
- vector = TidbOnQdrantVector(
- collection_name="test_collection",
- group_id="test_group",
- config=config,
- )
- return vector
+ vector = TidbOnQdrantVector(
+ collection_name="test_collection",
+ group_id="test_group",
+ config=config,
+ )
+ assert vector._client is fake_client
+ return vector
- def test_delete_by_ids_with_multiple_ids(self, vector_instance):
+ def test_delete_by_ids_with_multiple_ids(
+ self, fake_client: FakeQdrantClient, vector_instance: TidbOnQdrantVector
+ ) -> None:
"""Test batch deletion with multiple document IDs."""
ids = ["doc1", "doc2", "doc3"]
vector_instance.delete_by_ids(ids)
- # Verify that delete was called once with MatchAny filter
- vector_instance._client.delete.assert_called_once()
- call_args = vector_instance._client.delete.call_args
-
- # Check collection name
- assert call_args[1]["collection_name"] == "test_collection"
-
- # Verify filter uses MatchAny with all IDs
- filter_selector = call_args[1]["points_selector"]
- filter_obj = filter_selector.filter
- assert len(filter_obj.must) == 1
+ # One delete call on the collection, filtering doc_id with MatchAny over all IDs
+ assert fake_client.delete_requests == [DeleteRequest("test_collection", _doc_id_selector(ids))]
- field_condition = filter_obj.must[0]
- assert field_condition.key == "metadata.doc_id"
- assert isinstance(field_condition.match, rest.MatchAny)
- assert set(field_condition.match.any) == {"doc1", "doc2", "doc3"}
-
- def test_delete_by_ids_with_single_id(self, vector_instance):
+ def test_delete_by_ids_with_single_id(
+ self, fake_client: FakeQdrantClient, vector_instance: TidbOnQdrantVector
+ ) -> None:
"""Test deletion with a single document ID."""
- ids = ["doc1"]
+ vector_instance.delete_by_ids(["doc1"])
- vector_instance.delete_by_ids(ids)
-
- # Verify that delete was called once
- vector_instance._client.delete.assert_called_once()
- call_args = vector_instance._client.delete.call_args
-
- # Verify filter uses MatchAny with single ID
- filter_selector = call_args[1]["points_selector"]
- filter_obj = filter_selector.filter
- field_condition = filter_obj.must[0]
- assert isinstance(field_condition.match, rest.MatchAny)
- assert field_condition.match.any == ["doc1"]
+ assert fake_client.delete_requests == [DeleteRequest("test_collection", _doc_id_selector(["doc1"]))]
- def test_delete_by_ids_with_empty_list(self, vector_instance):
+ def test_delete_by_ids_with_empty_list(
+ self, fake_client: FakeQdrantClient, vector_instance: TidbOnQdrantVector
+ ) -> None:
"""Test deletion with empty ID list returns early without API call."""
vector_instance.delete_by_ids([])
- # Verify that delete was NOT called
- vector_instance._client.delete.assert_not_called()
+ assert fake_client.delete_requests == []
- def test_delete_by_ids_with_404_error(self, vector_instance):
+ def test_delete_by_ids_with_404_error(
+ self, fake_client: FakeQdrantClient, vector_instance: TidbOnQdrantVector
+ ) -> None:
"""Test that 404 errors (collection not found) are handled gracefully."""
- ids = ["doc1", "doc2"]
-
- # Mock a 404 error
- error = UnexpectedResponse(
+ fake_client.delete_error = UnexpectedResponse(
status_code=404,
reason_phrase="Not Found",
content=b"Collection not found",
headers=httpx.Headers(),
)
- vector_instance._client.delete.side_effect = error
# Should not raise an exception
- vector_instance.delete_by_ids(ids)
+ vector_instance.delete_by_ids(["doc1", "doc2"])
- # Verify delete was called
- vector_instance._client.delete.assert_called_once()
+ assert len(fake_client.delete_requests) == 1
- def test_delete_by_ids_with_unexpected_error(self, vector_instance):
+ def test_delete_by_ids_with_unexpected_error(
+ self, fake_client: FakeQdrantClient, vector_instance: TidbOnQdrantVector
+ ) -> None:
"""Test that non-404 errors are re-raised."""
- ids = ["doc1", "doc2"]
-
- # Mock a 500 error
- error = UnexpectedResponse(
+ fake_client.delete_error = UnexpectedResponse(
status_code=500,
reason_phrase="Internal Server Error",
content=b"Server error",
headers=httpx.Headers(),
)
- vector_instance._client.delete.side_effect = error
# Should re-raise the exception
with pytest.raises(UnexpectedResponse) as exc_info:
- vector_instance.delete_by_ids(ids)
+ vector_instance.delete_by_ids(["doc1", "doc2"])
assert exc_info.value.status_code == 500
- def test_delete_by_ids_with_exactly_1000(self, vector_instance):
+ def test_delete_by_ids_with_exactly_1000(
+ self, fake_client: FakeQdrantClient, vector_instance: TidbOnQdrantVector
+ ) -> None:
"""Test deletion with exactly 1000 IDs triggers a single batch."""
ids = [f"doc_{i}" for i in range(1000)]
vector_instance.delete_by_ids(ids)
- vector_instance._client.delete.assert_called_once()
- call_args = vector_instance._client.delete.call_args
-
- filter_selector = call_args[1]["points_selector"]
- filter_obj = filter_selector.filter
- field_condition = filter_obj.must[0]
-
- assert len(field_condition.match.any) == 1000
- assert "doc_0" in field_condition.match.any
- assert "doc_999" in field_condition.match.any
+ assert fake_client.delete_requests == [DeleteRequest("test_collection", _doc_id_selector(ids))]
- def test_delete_by_ids_splits_into_batches(self, vector_instance):
+ def test_delete_by_ids_splits_into_batches(
+ self, fake_client: FakeQdrantClient, vector_instance: TidbOnQdrantVector
+ ) -> None:
"""Test deletion with >1000 IDs triggers multiple batched calls."""
ids = [f"doc_{i}" for i in range(2500)]
vector_instance.delete_by_ids(ids)
- assert vector_instance._client.delete.call_count == 3
-
- batches = []
- for call in vector_instance._client.delete.call_args_list:
- filter_selector = call[1]["points_selector"]
- field_condition = filter_selector.filter.must[0]
- batches.append(field_condition.match.any)
-
- assert len(batches[0]) == 1000
- assert len(batches[1]) == 1000
- assert len(batches[2]) == 500
+ assert [request.points_selector for request in fake_client.delete_requests] == [
+ _doc_id_selector(ids[:1000]),
+ _doc_id_selector(ids[1000:2000]),
+ _doc_id_selector(ids[2000:]),
+ ]
- def test_delete_by_ids_filter_structure(self, vector_instance):
+ def test_delete_by_ids_filter_structure(
+ self, fake_client: FakeQdrantClient, vector_instance: TidbOnQdrantVector
+ ) -> None:
"""Test that the filter structure is correctly constructed."""
ids = ["doc1", "doc2"]
vector_instance.delete_by_ids(ids)
- call_args = vector_instance._client.delete.call_args
- filter_selector = call_args[1]["points_selector"]
- filter_obj = filter_selector.filter
-
- # Verify Filter structure
- assert isinstance(filter_obj, rest.Filter)
- assert filter_obj.must is not None
- assert len(filter_obj.must) == 1
-
- # Verify FieldCondition structure
- field_condition = filter_obj.must[0]
- assert isinstance(field_condition, rest.FieldCondition)
- assert field_condition.key == "metadata.doc_id"
-
- # Verify MatchAny structure
- assert isinstance(field_condition.match, rest.MatchAny)
- assert field_condition.match.any == ids
+ # Pydantic equality also compares the model types: Filter, FieldCondition and MatchAny.
+ (request,) = fake_client.delete_requests
+ assert request.points_selector == rest.FilterSelector(
+ filter=rest.Filter(must=[rest.FieldCondition(key="metadata.doc_id", match=rest.MatchAny(any=ids))])
+ )
class TestInitVectorEndpointSelection:
@@ -323,7 +340,7 @@ class TestInitVectorEndpointSelection:
by testing the endpoint selection logic directly on TidbOnQdrantConfig.
"""
- def test_uses_binding_endpoint_when_present(self):
+ def test_uses_binding_endpoint_when_present(self) -> None:
binding_endpoint = "https://qdrant-custom.tidb.com"
global_url = "https://qdrant-global.tidb.com"
@@ -333,7 +350,7 @@ class TestInitVectorEndpointSelection:
config = TidbOnQdrantConfig(endpoint=qdrant_url)
assert config.endpoint == "https://qdrant-custom.tidb.com"
- def test_falls_back_to_global_when_binding_endpoint_is_none(self):
+ def test_falls_back_to_global_when_binding_endpoint_is_none(self) -> None:
binding_endpoint = None
global_url = "https://qdrant-global.tidb.com"
@@ -343,7 +360,7 @@ class TestInitVectorEndpointSelection:
config = TidbOnQdrantConfig(endpoint=qdrant_url)
assert config.endpoint == "https://qdrant-global.tidb.com"
- def test_falls_back_to_empty_when_both_none(self):
+ def test_falls_back_to_empty_when_both_none(self) -> None:
binding_endpoint = None
global_url = None
@@ -353,7 +370,7 @@ class TestInitVectorEndpointSelection:
config = TidbOnQdrantConfig(endpoint=qdrant_url)
assert config.endpoint == ""
- def test_binding_endpoint_takes_precedence_over_global(self):
+ def test_binding_endpoint_takes_precedence_over_global(self) -> None:
binding_endpoint = "https://qdrant-ap-southeast.tidb.com"
global_url = "https://qdrant-us-east.tidb.com"
@@ -361,7 +378,7 @@ class TestInitVectorEndpointSelection:
assert qdrant_url == "https://qdrant-ap-southeast.tidb.com"
- def test_empty_string_binding_endpoint_falls_back_to_global(self):
+ def test_empty_string_binding_endpoint_falls_back_to_global(self) -> None:
binding_endpoint = ""
global_url = "https://qdrant-global.tidb.com"
@@ -374,34 +391,44 @@ class TestTidbCloudRequestTimeouts:
"""The TiDB Cloud API calls must be bounded: a hanging endpoint must not
block cluster provisioning or password rotation forever."""
- @patch("dify_vdb_tidb_on_qdrant.tidb_on_qdrant_vector.httpx.post")
- def test_create_cluster_passes_bounded_timeout(self, mock_post):
+ def test_create_cluster_passes_bounded_timeout(self, monkeypatch: pytest.MonkeyPatch) -> None:
from dify_vdb_tidb_on_qdrant.tidb_on_qdrant_vector import (
TidbConfig,
TidbOnQdrantVectorFactory,
)
- mock_post.return_value.status_code = 200
- mock_post.return_value.json.return_value = {}
+ timeouts: list[object] = []
+
+ def post(url: str, **kwargs: object) -> httpx.Response:
+ timeouts.append(kwargs.get("timeout"))
+ return httpx.Response(200, json={})
+
+ monkeypatch.setattr("dify_vdb_tidb_on_qdrant.tidb_on_qdrant_vector.httpx.post", post)
factory = TidbOnQdrantVectorFactory()
config = TidbConfig(api_url="https://api.tidbcloud.test", public_key="pub", private_key="priv")
factory.create_tidb_serverless_cluster(config, "display", "us-east-1")
- assert mock_post.call_args.kwargs["timeout"] is not None
+ assert len(timeouts) == 1
+ assert timeouts[0] is not None
- @patch("dify_vdb_tidb_on_qdrant.tidb_on_qdrant_vector.httpx.put")
- def test_change_password_passes_bounded_timeout(self, mock_put):
+ def test_change_password_passes_bounded_timeout(self, monkeypatch: pytest.MonkeyPatch) -> None:
from dify_vdb_tidb_on_qdrant.tidb_on_qdrant_vector import (
TidbConfig,
TidbOnQdrantVectorFactory,
)
- mock_put.return_value.status_code = 200
- mock_put.return_value.json.return_value = {}
+ timeouts: list[object] = []
+
+ def put(url: str, **kwargs: object) -> httpx.Response:
+ timeouts.append(kwargs.get("timeout"))
+ return httpx.Response(200, json={})
+
+ monkeypatch.setattr("dify_vdb_tidb_on_qdrant.tidb_on_qdrant_vector.httpx.put", put)
factory = TidbOnQdrantVectorFactory()
config = TidbConfig(api_url="https://api.tidbcloud.test", public_key="pub", private_key="priv")
factory.change_tidb_serverless_root_password(config, "c-1", "new-password")
- assert mock_put.call_args.kwargs["timeout"] is not None
+ assert len(timeouts) == 1
+ assert timeouts[0] is not None
diff --git a/api/providers/vdb/vdb-upstash/tests/unit_tests/src/dify_vdb_upstash/test_upstash_vector.py b/api/providers/vdb/vdb-upstash/tests/unit_tests/src/dify_vdb_upstash/test_upstash_vector.py
index 2acb04af7ad..e61369ea538 100644
--- a/api/providers/vdb/vdb-upstash/tests/unit_tests/src/dify_vdb_upstash/test_upstash_vector.py
+++ b/api/providers/vdb/vdb-upstash/tests/unit_tests/src/dify_vdb_upstash/test_upstash_vector.py
@@ -1,7 +1,9 @@
import importlib
import json
import sys
-from types import SimpleNamespace
+from dataclasses import dataclass
+from types import ModuleType, SimpleNamespace
+from typing import TYPE_CHECKING
from unittest.mock import MagicMock, patch
import pytest
@@ -11,19 +13,22 @@ from core.rag.datasource.vdb.vector_integration_test_support import FakeModule
from core.rag.models.document import Document
from models.dataset import Dataset
+if TYPE_CHECKING:
+ from dify_vdb_upstash.upstash_vector import UpstashVectorConfig
-def _build_fake_upstash_module():
+
+def _build_fake_upstash_module() -> FakeModule:
upstash_module = FakeModule("upstash_vector")
+ @dataclass(eq=False, repr=False)
class Vector:
- def __init__(self, id, vector, metadata, data):
- self.id = id
- self.vector = vector
- self.metadata = metadata
- self.data = data
+ id: str
+ vector: list[float]
+ metadata: dict[str, object]
+ data: str
class Index:
- def __init__(self, url, token):
+ def __init__(self, url: str, token: str) -> None:
self.url = url
self.token = token
self.info = MagicMock(return_value=SimpleNamespace(dimension=8))
@@ -38,7 +43,7 @@ def _build_fake_upstash_module():
@pytest.fixture
-def upstash_module(monkeypatch: pytest.MonkeyPatch):
+def upstash_module(monkeypatch: pytest.MonkeyPatch) -> ModuleType:
# Remove patched modules if present
for modname in ["upstash_vector", "dify_vdb_upstash.upstash_vector"]:
if modname in sys.modules:
@@ -48,7 +53,7 @@ def upstash_module(monkeypatch: pytest.MonkeyPatch):
return module
-def _config(module):
+def _config(module: ModuleType) -> "UpstashVectorConfig":
return module.UpstashVectorConfig(url="https://upstash.example", token="token-123")
@@ -59,7 +64,7 @@ def _config(module):
("token", "", "Upstash Token is required"),
],
)
-def test_upstash_config_validation(upstash_module, field, value, message):
+def test_upstash_config_validation(upstash_module: ModuleType, field: str, value: str, message: str) -> None:
values = _config(upstash_module).model_dump()
values[field] = value
@@ -67,7 +72,7 @@ def test_upstash_config_validation(upstash_module, field, value, message):
upstash_module.UpstashVectorConfig.model_validate(values)
-def test_init_get_type_and_dimension(upstash_module, monkeypatch: pytest.MonkeyPatch):
+def test_init_get_type_and_dimension(upstash_module: ModuleType, monkeypatch: pytest.MonkeyPatch) -> None:
vector = upstash_module.UpstashVector("collection_1", _config(upstash_module))
assert vector.get_type() == upstash_module.VectorType.UPSTASH
@@ -89,7 +94,7 @@ def test_init_get_type_and_dimension(upstash_module, monkeypatch: pytest.MonkeyP
assert upsert_vectors[0].id == "generated-uuid"
-def test_create_text_exists_and_delete_by_ids(upstash_module):
+def test_create_text_exists_and_delete_by_ids(upstash_module: ModuleType) -> None:
vector = upstash_module.UpstashVector("collection_1", _config(upstash_module))
vector.add_texts = MagicMock()
@@ -99,7 +104,7 @@ def test_create_text_exists_and_delete_by_ids(upstash_module):
vector.get_ids_by_metadata_field = MagicMock(return_value=["id-1"])
assert vector.text_exists("doc-1") is True
- vector.get_ids_by_metadata_field.return_value = []
+ vector.get_ids_by_metadata_field.return_value = list[str]()
assert vector.text_exists("doc-1") is False
vector.get_ids_by_metadata_field = MagicMock(side_effect=[["item-1"], [], ["item-2"]])
@@ -108,7 +113,7 @@ def test_create_text_exists_and_delete_by_ids(upstash_module):
vector._delete_by_ids.assert_called_once_with(ids=["item-1", "item-2"])
-def test_delete_helpers_and_search(upstash_module):
+def test_delete_helpers_and_search(upstash_module: ModuleType) -> None:
vector = upstash_module.UpstashVector("collection_1", _config(upstash_module))
vector._delete_by_ids([])
@@ -129,12 +134,12 @@ def test_delete_helpers_and_search(upstash_module):
vector._delete_by_ids.assert_called_once_with(["x-1"])
vector._delete_by_ids.reset_mock()
- vector.get_ids_by_metadata_field.return_value = []
+ vector.get_ids_by_metadata_field.return_value = list[str]()
vector.delete_by_metadata_field("doc_id", "doc-2")
vector._delete_by_ids.assert_not_called()
-def test_search_by_vector_filter_threshold_and_delete(upstash_module):
+def test_search_by_vector_filter_threshold_and_delete(upstash_module: ModuleType) -> None:
vector = upstash_module.UpstashVector("collection_1", _config(upstash_module))
vector.index.query.return_value = [
SimpleNamespace(metadata={"document_id": "d-1"}, data="text-1", score=0.9),
@@ -164,7 +169,9 @@ def test_search_by_vector_filter_threshold_and_delete(upstash_module):
vector.index.reset.assert_called_once()
-def test_upstash_factory_uses_existing_or_generated_collection(upstash_module, monkeypatch: pytest.MonkeyPatch):
+def test_upstash_factory_uses_existing_or_generated_collection(
+ upstash_module: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
factory = upstash_module.UpstashVectorFactory()
dataset_with_index = Dataset(
id="dataset-1", index_struct=json.dumps({"vector_store": {"class_prefix": "EXISTING_COLLECTION"}})