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): annotate vector store provider test fixtures (#44703)

Eddy ZHANGa220fe3bd3ab
diff --git a/api/providers/vdb/vdb-couchbase/tests/unit_tests/src/dify_vdb_couchbase/test_couchbase_vector.py b/api/providers/vdb/vdb-couchbase/tests/unit_tests/src/dify_vdb_couchbase/test_couchbase_vector.py
index 746077486fe..e309e426d02 100644
--- a/api/providers/vdb/vdb-couchbase/tests/unit_tests/src/dify_vdb_couchbase/test_couchbase_vector.py
+++ b/api/providers/vdb/vdb-couchbase/tests/unit_tests/src/dify_vdb_couchbase/test_couchbase_vector.py
@@ -1,7 +1,10 @@
import importlib
import json
import sys
-from types import SimpleNamespace
+from collections.abc import Iterator
+from dataclasses import dataclass
+from types import ModuleType, SimpleNamespace
+from typing import TYPE_CHECKING, Any, Self
from unittest.mock import MagicMock, call, patch
import pytest
@@ -11,8 +14,11 @@ 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_couchbase.couchbase_vector import CouchbaseConfig
-def _build_fake_couchbase_modules():
+
+def _build_fake_couchbase_modules() -> dict[str, FakeModule]:
couchbase = FakeModule("couchbase")
couchbase_auth = FakeModule("couchbase.auth")
couchbase_cluster = FakeModule("couchbase.cluster")
@@ -22,108 +28,108 @@ def _build_fake_couchbase_modules():
couchbase_vector = FakeModule("couchbase.vector_search")
couchbase_search = FakeModule("couchbase.search")
+ @dataclass(eq=False, repr=False)
class PasswordAuthenticator:
- def __init__(self, user, password):
- self.user = user
- self.password = password
+ user: str
+ password: str
+ @dataclass(eq=False, repr=False)
class ClusterOptions:
- def __init__(self, auth):
- self.auth = auth
+ auth: PasswordAuthenticator
class SearchOptions:
- def __init__(self, **kwargs):
+ def __init__(self, **kwargs: Any) -> None:
self.kwargs = kwargs
+ @dataclass(eq=False, repr=False)
class VectorQuery:
- def __init__(self, field, vector, top_k):
- self.field = field
- self.vector = vector
- self.top_k = top_k
+ field: str
+ vector: list[float]
+ top_k: int
class VectorSearch:
@staticmethod
- def from_vector_query(vector_query):
+ def from_vector_query(vector_query: VectorQuery) -> dict[str, VectorQuery]:
return {"vector_query": vector_query}
+ @dataclass(eq=False, repr=False)
class QueryStringQuery:
- def __init__(self, query):
- self.query = query
+ query: str
class SearchRequest:
@staticmethod
- def create(payload):
+ def create(payload: dict[str, VectorQuery] | QueryStringQuery) -> dict[str, object]:
return {"payload": payload}
+ @dataclass(eq=False, repr=False)
class SearchIndex:
- def __init__(self, name, params, source_name):
- self.name = name
- self.params = params
- self.source_name = source_name
+ name: str
+ params: dict[str, Any]
+ source_name: str
class _QueryResult:
- def __init__(self, rows=None):
+ def __init__(self, rows: list[dict[str, object]] | None = None) -> None:
self._rows = rows or []
- def execute(self):
+ def execute(self) -> Self:
return self
- def __iter__(self):
+ def __iter__(self) -> Iterator[dict[str, object]]:
return iter(self._rows)
class _SearchIter:
- def __init__(self, rows=None):
+ def __init__(self, rows: list[SimpleNamespace] | None = None) -> None:
self._rows = rows or []
- def rows(self):
+ def rows(self) -> list[SimpleNamespace]:
return self._rows
class _Collection:
- def __init__(self):
+ def __init__(self) -> None:
self.upsert = MagicMock(return_value=True)
class _SearchIndexManager:
- def __init__(self):
+ def __init__(self) -> None:
self.upsert_index = MagicMock()
class _Scope:
- def __init__(self):
+ def __init__(self) -> None:
self._collection = _Collection()
self._search_index_manager = _SearchIndexManager()
self.search = MagicMock(return_value=_SearchIter())
- def collection(self, _name):
+ def collection(self, _name: str) -> _Collection:
return self._collection
- def search_indexes(self):
+ def search_indexes(self) -> _SearchIndexManager:
return self._search_index_manager
class _CollectionManager:
- def __init__(self):
+ def __init__(self) -> None:
self.create_collection = MagicMock()
self.drop_collection = MagicMock()
self.get_all_scopes = MagicMock(return_value=[])
class _Bucket:
- def __init__(self):
+ def __init__(self) -> None:
self._scope = _Scope()
self._collections = _CollectionManager()
- def scope(self, _scope_name):
+ def scope(self, _scope_name: str) -> _Scope:
return self._scope
- def collections(self):
+ def collections(self) -> _CollectionManager:
return self._collections
class Cluster:
- def __init__(self, connection_string, options):
+ def __init__(self, connection_string: str, options: ClusterOptions) -> None:
self.connection_string = connection_string
self.options = options
self._bucket = _Bucket()
self.wait_until_ready = MagicMock()
self.query = MagicMock(return_value=_QueryResult())
- def bucket(self, _name):
+ def bucket(self, _name: str) -> _Bucket:
return self._bucket
couchbase_auth.PasswordAuthenticator = PasswordAuthenticator
@@ -152,7 +158,7 @@ def _build_fake_couchbase_modules():
@pytest.fixture
-def couchbase_module(monkeypatch: pytest.MonkeyPatch):
+def couchbase_module(monkeypatch: pytest.MonkeyPatch) -> ModuleType:
for name, module in _build_fake_couchbase_modules().items():
monkeypatch.setitem(sys.modules, name, module)
@@ -161,7 +167,7 @@ def couchbase_module(monkeypatch: pytest.MonkeyPatch):
return importlib.reload(module)
-def _config(module):
+def _config(module: ModuleType) -> "CouchbaseConfig":
return module.CouchbaseConfig(
connection_string="couchbase://localhost",
user="user",
@@ -181,14 +187,14 @@ def _config(module):
("scope_name", "", "COUCHBASE_SCOPE_NAME is required"),
],
)
-def test_couchbase_config_validation(couchbase_module, field, value, message):
+def test_couchbase_config_validation(couchbase_module: ModuleType, field: str, value: str, message: str) -> None:
values = _config(couchbase_module).model_dump()
values[field] = value
with pytest.raises(ValidationError, match=message):
couchbase_module.CouchbaseConfig.model_validate(values)
-def test_init_sets_cluster_handles(couchbase_module):
+def test_init_sets_cluster_handles(couchbase_module: ModuleType) -> None:
vector = couchbase_module.CouchbaseVector("collection_1", _config(couchbase_module))
assert vector._bucket_name == "bucket"
@@ -196,7 +202,7 @@ def test_init_sets_cluster_handles(couchbase_module):
vector._cluster.wait_until_ready.assert_called_once()
-def test_create_and_create_collection_branches(couchbase_module, monkeypatch: pytest.MonkeyPatch):
+def test_create_and_create_collection_branches(couchbase_module: ModuleType, monkeypatch: pytest.MonkeyPatch) -> None:
sleep = MagicMock()
monkeypatch.setattr(couchbase_module, "time", SimpleNamespace(sleep=sleep))
vector = couchbase_module.CouchbaseVector.__new__(couchbase_module.CouchbaseVector)
@@ -248,7 +254,7 @@ def test_create_and_create_collection_branches(couchbase_module, monkeypatch: py
assert sleep.call_args_list == [call(2), call(1)]
-def test_collection_exists_get_type_and_add_texts(couchbase_module):
+def test_collection_exists_get_type_and_add_texts(couchbase_module: ModuleType) -> None:
vector = couchbase_module.CouchbaseVector("collection_1", _config(couchbase_module))
scope_obj = SimpleNamespace(name="scope", collections=[SimpleNamespace(name="collection_1")])
@@ -271,7 +277,7 @@ def test_collection_exists_get_type_and_add_texts(couchbase_module):
assert vector.get_type() == couchbase_module.VectorType.COUCHBASE
-def test_query_delete_helpers(couchbase_module):
+def test_query_delete_helpers(couchbase_module: ModuleType) -> None:
vector = couchbase_module.CouchbaseVector("collection_1", _config(couchbase_module))
vector._cluster.query.return_value = SimpleNamespace(execute=lambda: iter([{"count": 2}]))
@@ -293,7 +299,7 @@ def test_query_delete_helpers(couchbase_module):
vector.delete_by_ids(["id-3"])
-def test_search_methods_and_format_metadata(couchbase_module):
+def test_search_methods_and_format_metadata(couchbase_module: ModuleType) -> None:
vector = couchbase_module.CouchbaseVector("collection_1", _config(couchbase_module))
row_1 = SimpleNamespace(fields={"text": "doc-a", "metadata.document_id": "d-1"}, score=0.9)
@@ -324,7 +330,7 @@ def test_search_methods_and_format_metadata(couchbase_module):
assert vector._format_metadata({"metadata.a": 1, "plain": 2}) == {"a": 1, "plain": 2}
-def test_delete_collection_and_factory(couchbase_module, monkeypatch: pytest.MonkeyPatch):
+def test_delete_collection_and_factory(couchbase_module: ModuleType, monkeypatch: pytest.MonkeyPatch) -> None:
vector = couchbase_module.CouchbaseVector("collection_1", _config(couchbase_module))
scopes = [
SimpleNamespace(collections=[SimpleNamespace(name="other")]),
diff --git a/api/providers/vdb/vdb-hologres/tests/unit_tests/src/dify_vdb_hologres/test_hologres_vector.py b/api/providers/vdb/vdb-hologres/tests/unit_tests/src/dify_vdb_hologres/test_hologres_vector.py
index 9c6ba310234..fff9e0fefbb 100644
--- a/api/providers/vdb/vdb-hologres/tests/unit_tests/src/dify_vdb_hologres/test_hologres_vector.py
+++ b/api/providers/vdb/vdb-hologres/tests/unit_tests/src/dify_vdb_hologres/test_hologres_vector.py
@@ -1,6 +1,8 @@
import importlib
import json
import sys
+from types import ModuleType
+from typing import TYPE_CHECKING, Any
from unittest.mock import MagicMock, patch
import pytest
@@ -10,8 +12,11 @@ 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_hologres.hologres_vector import HologresVectorConfig
-def _build_fake_hologres_modules():
+
+def _build_fake_hologres_modules() -> dict[str, FakeModule]:
holo_module = FakeModule("holo_search_sdk")
holo_types_module = FakeModule("holo_search_sdk.types")
@@ -19,7 +24,7 @@ def _build_fake_hologres_modules():
holo_types_module.DistanceType = str
holo_types_module.TokenizerType = str
- def _connect(**kwargs):
+ def _connect(**kwargs: Any) -> MagicMock:
client = MagicMock()
client.kwargs = kwargs
client.connect = MagicMock()
@@ -38,7 +43,7 @@ def _build_fake_hologres_modules():
@pytest.fixture
-def hologres_module(monkeypatch: pytest.MonkeyPatch):
+def hologres_module(monkeypatch: pytest.MonkeyPatch) -> ModuleType:
for name, module in _build_fake_hologres_modules().items():
monkeypatch.setitem(sys.modules, name, module)
@@ -47,7 +52,7 @@ def hologres_module(monkeypatch: pytest.MonkeyPatch):
return importlib.reload(module)
-def _valid_config(module):
+def _valid_config(module: ModuleType) -> "HologresVectorConfig":
return module.HologresVectorConfig(
host="localhost",
port=80,
@@ -72,7 +77,7 @@ def _valid_config(module):
("access_key_secret", "", "config HOLOGRES_ACCESS_KEY_SECRET is required"),
],
)
-def test_hologres_config_validation(hologres_module, field, value, message):
+def test_hologres_config_validation(hologres_module: ModuleType, field: str, value: str | int, message: str) -> None:
values = _valid_config(hologres_module).model_dump()
values[field] = value
@@ -80,7 +85,7 @@ def test_hologres_config_validation(hologres_module, field, value, message):
hologres_module.HologresVectorConfig.model_validate(values)
-def test_init_client_and_get_type(hologres_module):
+def test_init_client_and_get_type(hologres_module: ModuleType) -> None:
vector = hologres_module.HologresVector("Collection_One", _valid_config(hologres_module))
hologres_module.holo.connect.assert_called_once_with(
@@ -96,7 +101,7 @@ def test_init_client_and_get_type(hologres_module):
assert vector.get_type() == hologres_module.VectorType.HOLOGRES
-def test_create_delegates_collection_creation_and_upsert(hologres_module):
+def test_create_delegates_collection_creation_and_upsert(hologres_module: ModuleType) -> None:
vector = hologres_module.HologresVector("collection_one", _valid_config(hologres_module))
vector._create_collection = MagicMock()
vector.add_texts = MagicMock()
@@ -109,14 +114,14 @@ def test_create_delegates_collection_creation_and_upsert(hologres_module):
vector.add_texts.assert_called_once_with(docs, [[0.1, 0.2]])
-def test_add_texts_returns_empty_for_empty_documents(hologres_module):
+def test_add_texts_returns_empty_for_empty_documents(hologres_module: ModuleType) -> None:
vector = hologres_module.HologresVector("collection_one", _valid_config(hologres_module))
assert vector.add_texts([], []) == []
vector._client.open_table.assert_not_called()
-def test_add_texts_batches_and_serializes_metadata(hologres_module):
+def test_add_texts_batches_and_serializes_metadata(hologres_module: ModuleType) -> None:
vector = hologres_module.HologresVector("collection_one", _valid_config(hologres_module))
table = vector._client.open_table.return_value
documents = [
@@ -144,7 +149,7 @@ def test_add_texts_batches_and_serializes_metadata(hologres_module):
assert second_call["values"][0][2] == "{}"
-def test_text_exists_handles_missing_and_present_tables(hologres_module):
+def test_text_exists_handles_missing_and_present_tables(hologres_module: ModuleType) -> None:
vector = hologres_module.HologresVector("collection_one", _valid_config(hologres_module))
vector._client.check_table_exist.side_effect = [False, True]
vector._client.execute.return_value = [(1,)]
@@ -154,15 +159,15 @@ def test_text_exists_handles_missing_and_present_tables(hologres_module):
vector._client.execute.assert_called_once()
-def test_get_ids_by_metadata_field_returns_ids_or_none(hologres_module):
+def test_get_ids_by_metadata_field_returns_ids_or_none(hologres_module: ModuleType) -> None:
vector = hologres_module.HologresVector("collection_one", _valid_config(hologres_module))
- vector._client.execute.side_effect = [[("id-1",), ("id-2",)], []]
+ vector._client.execute.side_effect = [[("id-1",), ("id-2",)], list[tuple[str]]()]
assert vector.get_ids_by_metadata_field("document_id", "doc-1") == ["id-1", "id-2"]
assert vector.get_ids_by_metadata_field("document_id", "doc-1") is None
-def test_delete_by_ids_branches(hologres_module):
+def test_delete_by_ids_branches(hologres_module: ModuleType) -> None:
vector = hologres_module.HologresVector("collection_one", _valid_config(hologres_module))
vector.delete_by_ids([])
@@ -177,7 +182,7 @@ def test_delete_by_ids_branches(hologres_module):
vector._client.execute.assert_called_once()
-def test_delete_by_metadata_field_branches(hologres_module):
+def test_delete_by_metadata_field_branches(hologres_module: ModuleType) -> None:
vector = hologres_module.HologresVector("collection_one", _valid_config(hologres_module))
vector._client.check_table_exist.return_value = False
@@ -189,14 +194,14 @@ def test_delete_by_metadata_field_branches(hologres_module):
vector._client.execute.assert_called_once()
-def test_search_by_vector_returns_empty_when_table_missing(hologres_module):
+def test_search_by_vector_returns_empty_when_table_missing(hologres_module: ModuleType) -> None:
vector = hologres_module.HologresVector("collection_one", _valid_config(hologres_module))
vector._client.check_table_exist.return_value = False
assert vector.search_by_vector([0.1, 0.2]) == []
-def test_search_by_vector_applies_filter_and_processes_results(hologres_module):
+def test_search_by_vector_applies_filter_and_processes_results(hologres_module: ModuleType) -> None:
vector = hologres_module.HologresVector("collection_one", _valid_config(hologres_module))
vector._client.check_table_exist.return_value = True
table = vector._client.open_table.return_value
@@ -225,14 +230,14 @@ def test_search_by_vector_applies_filter_and_processes_results(hologres_module):
query.where.assert_called_once()
-def test_search_by_full_text_returns_empty_when_table_missing(hologres_module):
+def test_search_by_full_text_returns_empty_when_table_missing(hologres_module: ModuleType) -> None:
vector = hologres_module.HologresVector("collection_one", _valid_config(hologres_module))
vector._client.check_table_exist.return_value = False
assert vector.search_by_full_text("query") == []
-def test_search_by_full_text_applies_filter_and_processes_results(hologres_module):
+def test_search_by_full_text_applies_filter_and_processes_results(hologres_module: ModuleType) -> None:
vector = hologres_module.HologresVector("collection_one", _valid_config(hologres_module))
vector._client.check_table_exist.return_value = True
table = vector._client.open_table.return_value
@@ -255,7 +260,7 @@ def test_search_by_full_text_applies_filter_and_processes_results(hologres_modul
search_query.where.assert_called_once()
-def test_delete_handles_existing_and_missing_tables(hologres_module):
+def test_delete_handles_existing_and_missing_tables(hologres_module: ModuleType) -> None:
vector = hologres_module.HologresVector("collection_one", _valid_config(hologres_module))
vector._client.check_table_exist.side_effect = [False, True]
@@ -266,7 +271,9 @@ def test_delete_handles_existing_and_missing_tables(hologres_module):
vector._client.drop_table.assert_called_once_with(vector.table_name)
-def test_create_collection_returns_early_when_cache_hits(hologres_module, monkeypatch: pytest.MonkeyPatch):
+def test_create_collection_returns_early_when_cache_hits(
+ hologres_module: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
lock = MagicMock()
lock.__enter__.return_value = None
lock.__exit__.return_value = False
@@ -281,7 +288,9 @@ def test_create_collection_returns_early_when_cache_hits(hologres_module, monkey
hologres_module.redis_client.set.assert_not_called()
-def test_create_collection_creates_table_and_indexes(hologres_module, monkeypatch: pytest.MonkeyPatch):
+def test_create_collection_creates_table_and_indexes(
+ hologres_module: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
lock = MagicMock()
lock.__enter__.return_value = None
lock.__exit__.return_value = False
@@ -313,7 +322,9 @@ def test_create_collection_creates_table_and_indexes(hologres_module, monkeypatc
hologres_module.redis_client.set.assert_called_once()
-def test_create_collection_raises_when_table_never_becomes_ready(hologres_module, monkeypatch: pytest.MonkeyPatch):
+def test_create_collection_raises_when_table_never_becomes_ready(
+ hologres_module: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
lock = MagicMock()
lock.__enter__.return_value = None
lock.__exit__.return_value = False
@@ -331,7 +342,9 @@ def test_create_collection_raises_when_table_never_becomes_ready(hologres_module
hologres_module.redis_client.set.assert_not_called()
-def test_hologres_factory_uses_existing_or_generated_collection(hologres_module, monkeypatch: pytest.MonkeyPatch):
+def test_hologres_factory_uses_existing_or_generated_collection(
+ hologres_module: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
factory = hologres_module.HologresVectorFactory()
dataset_with_index = Dataset(
id="dataset-1", index_struct=json.dumps({"vector_store": {"class_prefix": "existing_collection"}})
diff --git a/api/providers/vdb/vdb-opengauss/tests/unit_tests/src/dify_vdb_opengauss/test_opengauss.py b/api/providers/vdb/vdb-opengauss/tests/unit_tests/src/dify_vdb_opengauss/test_opengauss.py
index 3941297545b..330cd4c276b 100644
--- a/api/providers/vdb/vdb-opengauss/tests/unit_tests/src/dify_vdb_opengauss/test_opengauss.py
+++ b/api/providers/vdb/vdb-opengauss/tests/unit_tests/src/dify_vdb_opengauss/test_opengauss.py
@@ -1,8 +1,10 @@
import importlib
import json
import sys
+from collections.abc import Generator
from contextlib import contextmanager
-from types import SimpleNamespace
+from types import ModuleType, SimpleNamespace
+from typing import TYPE_CHECKING
from unittest.mock import MagicMock, patch
import pytest
@@ -12,15 +14,18 @@ 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_opengauss.opengauss import OpenGaussConfig
-def _build_fake_psycopg2_modules():
+
+def _build_fake_psycopg2_modules() -> dict[str, FakeModule]:
psycopg2 = FakeModule("psycopg2")
psycopg2.__path__ = []
psycopg2_extras = FakeModule("psycopg2.extras")
psycopg2_pool = FakeModule("psycopg2.pool")
class SimpleConnectionPool:
- def __init__[**P](self, *args: P.args, **kwargs: P.kwargs):
+ def __init__[**P](self, *args: P.args, **kwargs: P.kwargs) -> None:
self.args = args
self.kwargs = kwargs
self.getconn = MagicMock()
@@ -39,7 +44,7 @@ def _build_fake_psycopg2_modules():
@pytest.fixture
-def opengauss_module(monkeypatch: pytest.MonkeyPatch):
+def opengauss_module(monkeypatch: pytest.MonkeyPatch) -> ModuleType:
for name, module in _build_fake_psycopg2_modules().items():
monkeypatch.setitem(sys.modules, name, module)
@@ -48,7 +53,7 @@ def opengauss_module(monkeypatch: pytest.MonkeyPatch):
return importlib.reload(module)
-def _config(module, *, enable_pq=False):
+def _config(module: ModuleType, *, enable_pq: bool = False) -> "OpenGaussConfig":
return module.OpenGaussConfig(
host="localhost",
port=6600,
@@ -73,7 +78,7 @@ def _config(module, *, enable_pq=False):
("max_connection", 0, "config OPENGAUSS_MAX_CONNECTION is required"),
],
)
-def test_opengauss_config_validation(opengauss_module, field, value, message):
+def test_opengauss_config_validation(opengauss_module: ModuleType, field: str, value: str | int, message: str) -> None:
values = _config(opengauss_module).model_dump()
values[field] = value
@@ -81,7 +86,7 @@ def test_opengauss_config_validation(opengauss_module, field, value, message):
opengauss_module.OpenGaussConfig.model_validate(values)
-def test_opengauss_config_validation_rejects_min_greater_than_max(opengauss_module):
+def test_opengauss_config_validation_rejects_min_greater_than_max(opengauss_module: ModuleType) -> None:
values = _config(opengauss_module).model_dump()
values["min_connection"] = 6
values["max_connection"] = 5
@@ -90,7 +95,7 @@ def test_opengauss_config_validation_rejects_min_greater_than_max(opengauss_modu
opengauss_module.OpenGaussConfig.model_validate(values)
-def test_init_sets_table_name_and_vector_type(opengauss_module, monkeypatch: pytest.MonkeyPatch):
+def test_init_sets_table_name_and_vector_type(opengauss_module: ModuleType, monkeypatch: pytest.MonkeyPatch) -> None:
pool = MagicMock()
monkeypatch.setattr(opengauss_module.psycopg2.pool, "SimpleConnectionPool", MagicMock(return_value=pool))
@@ -101,7 +106,7 @@ def test_init_sets_table_name_and_vector_type(opengauss_module, monkeypatch: pyt
assert vector.pool is pool
-def test_create_index_with_pq_executes_pq_sql(opengauss_module, monkeypatch: pytest.MonkeyPatch):
+def test_create_index_with_pq_executes_pq_sql(opengauss_module: ModuleType, monkeypatch: pytest.MonkeyPatch) -> None:
pool = MagicMock()
monkeypatch.setattr(opengauss_module.psycopg2.pool, "SimpleConnectionPool", MagicMock(return_value=pool))
@@ -116,7 +121,7 @@ def test_create_index_with_pq_executes_pq_sql(opengauss_module, monkeypatch: pyt
cursor = MagicMock()
@contextmanager
- def _cursor_ctx():
+ def _cursor_ctx() -> Generator[MagicMock]:
yield cursor
vector._get_cursor = _cursor_ctx
@@ -128,7 +133,9 @@ def test_create_index_with_pq_executes_pq_sql(opengauss_module, monkeypatch: pyt
opengauss_module.redis_client.set.assert_called_once()
-def test_create_index_skips_index_sql_for_large_dimension(opengauss_module, monkeypatch: pytest.MonkeyPatch):
+def test_create_index_skips_index_sql_for_large_dimension(
+ opengauss_module: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
pool = MagicMock()
monkeypatch.setattr(opengauss_module.psycopg2.pool, "SimpleConnectionPool", MagicMock(return_value=pool))
@@ -143,7 +150,7 @@ def test_create_index_skips_index_sql_for_large_dimension(opengauss_module, monk
cursor = MagicMock()
@contextmanager
- def _cursor_ctx():
+ def _cursor_ctx() -> Generator[MagicMock]:
yield cursor
vector._get_cursor = _cursor_ctx
@@ -153,14 +160,16 @@ def test_create_index_skips_index_sql_for_large_dimension(opengauss_module, monk
opengauss_module.redis_client.set.assert_called_once()
-def test_search_by_vector_validates_top_k(opengauss_module):
+def test_search_by_vector_validates_top_k(opengauss_module: ModuleType) -> None:
vector = opengauss_module.OpenGauss.__new__(opengauss_module.OpenGauss)
with pytest.raises(ValueError, match="top_k must be a positive integer"):
vector.search_by_vector([0.1, 0.2], top_k=0)
-def test_delete_by_ids_short_circuits_with_empty_input(opengauss_module, monkeypatch: pytest.MonkeyPatch):
+def test_delete_by_ids_short_circuits_with_empty_input(
+ opengauss_module: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
pool = MagicMock()
monkeypatch.setattr(opengauss_module.psycopg2.pool, "SimpleConnectionPool", MagicMock(return_value=pool))
vector = opengauss_module.OpenGauss("collection_1", _config(opengauss_module))
@@ -171,7 +180,7 @@ def test_delete_by_ids_short_circuits_with_empty_input(opengauss_module, monkeyp
vector._get_cursor.assert_not_called()
-def test_get_cursor_closes_commits_and_returns_connection(opengauss_module):
+def test_get_cursor_closes_commits_and_returns_connection(opengauss_module: ModuleType) -> None:
vector = opengauss_module.OpenGauss.__new__(opengauss_module.OpenGauss)
pool = MagicMock()
conn = MagicMock()
@@ -188,7 +197,7 @@ def test_get_cursor_closes_commits_and_returns_connection(opengauss_module):
pool.putconn.assert_called_once_with(conn)
-def test_create_calls_collection_insert_and_index(opengauss_module):
+def test_create_calls_collection_insert_and_index(opengauss_module: ModuleType) -> None:
vector = opengauss_module.OpenGauss.__new__(opengauss_module.OpenGauss)
vector._create_collection = MagicMock()
vector.add_texts = MagicMock()
@@ -202,7 +211,7 @@ def test_create_calls_collection_insert_and_index(opengauss_module):
vector._create_index.assert_called_once_with(2)
-def test_create_index_returns_early_on_cache_hit(opengauss_module, monkeypatch: pytest.MonkeyPatch):
+def test_create_index_returns_early_on_cache_hit(opengauss_module: ModuleType, monkeypatch: pytest.MonkeyPatch) -> None:
pool = MagicMock()
monkeypatch.setattr(opengauss_module.psycopg2.pool, "SimpleConnectionPool", MagicMock(return_value=pool))
@@ -222,7 +231,9 @@ def test_create_index_returns_early_on_cache_hit(opengauss_module, monkeypatch:
opengauss_module.redis_client.set.assert_not_called()
-def test_create_index_without_pq_executes_standard_index_sql(opengauss_module, monkeypatch: pytest.MonkeyPatch):
+def test_create_index_without_pq_executes_standard_index_sql(
+ opengauss_module: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
pool = MagicMock()
monkeypatch.setattr(opengauss_module.psycopg2.pool, "SimpleConnectionPool", MagicMock(return_value=pool))
@@ -237,7 +248,7 @@ def test_create_index_without_pq_executes_standard_index_sql(opengauss_module, m
cursor = MagicMock()
@contextmanager
- def _cursor_ctx():
+ def _cursor_ctx() -> Generator[MagicMock]:
yield cursor
vector._get_cursor = _cursor_ctx
@@ -247,7 +258,7 @@ def test_create_index_without_pq_executes_standard_index_sql(opengauss_module, m
assert any("embedding_cosine_embedding_collection_1_idx" in query for query in sql)
-def test_add_texts_uses_execute_values(opengauss_module, monkeypatch: pytest.MonkeyPatch):
+def test_add_texts_uses_execute_values(opengauss_module: ModuleType, monkeypatch: pytest.MonkeyPatch) -> None:
pool = MagicMock()
monkeypatch.setattr(opengauss_module.psycopg2.pool, "SimpleConnectionPool", MagicMock(return_value=pool))
vector = opengauss_module.OpenGauss("collection_1", _config(opengauss_module))
@@ -255,7 +266,7 @@ def test_add_texts_uses_execute_values(opengauss_module, monkeypatch: pytest.Mon
opengauss_module.psycopg2.extras.execute_values.reset_mock()
@contextmanager
- def _cursor_ctx():
+ def _cursor_ctx() -> Generator[MagicMock]:
yield cursor
vector._get_cursor = _cursor_ctx
@@ -271,7 +282,7 @@ def test_add_texts_uses_execute_values(opengauss_module, monkeypatch: pytest.Mon
opengauss_module.psycopg2.extras.execute_values.assert_called_once()
-def test_text_exists_and_get_by_ids(opengauss_module):
+def test_text_exists_and_get_by_ids(opengauss_module: ModuleType) -> None:
vector = opengauss_module.OpenGauss.__new__(opengauss_module.OpenGauss)
vector.table_name = "embedding_collection_1"
cursor = MagicMock()
@@ -279,7 +290,7 @@ def test_text_exists_and_get_by_ids(opengauss_module):
cursor.__iter__.return_value = iter([({"doc_id": "1"}, "text-1"), ({"doc_id": "2"}, "text-2")])
@contextmanager
- def _cursor_ctx():
+ def _cursor_ctx() -> Generator[MagicMock]:
yield cursor
vector._get_cursor = _cursor_ctx
@@ -290,13 +301,13 @@ def test_text_exists_and_get_by_ids(opengauss_module):
assert docs[0].page_content == "text-1"
-def test_delete_and_metadata_field_queries(opengauss_module):
+def test_delete_and_metadata_field_queries(opengauss_module: ModuleType) -> None:
vector = opengauss_module.OpenGauss.__new__(opengauss_module.OpenGauss)
vector.table_name = "embedding_collection_1"
cursor = MagicMock()
@contextmanager
- def _cursor_ctx():
+ def _cursor_ctx() -> Generator[MagicMock]:
yield cursor
vector._get_cursor = _cursor_ctx
@@ -311,7 +322,7 @@ def test_delete_and_metadata_field_queries(opengauss_module):
assert any("DROP TABLE IF EXISTS embedding_collection_1" in query for query in sql)
-def test_search_by_vector_and_full_text(opengauss_module):
+def test_search_by_vector_and_full_text(opengauss_module: ModuleType) -> None:
vector = opengauss_module.OpenGauss.__new__(opengauss_module.OpenGauss)
vector.table_name = "embedding_collection_1"
cursor = MagicMock()
@@ -323,7 +334,7 @@ def test_search_by_vector_and_full_text(opengauss_module):
)
@contextmanager
- def _cursor_ctx():
+ def _cursor_ctx() -> Generator[MagicMock]:
yield cursor
vector._get_cursor = _cursor_ctx
@@ -338,13 +349,13 @@ def test_search_by_vector_and_full_text(opengauss_module):
assert full_docs[0].page_content == "full-text"
-def test_search_by_full_text_validates_top_k(opengauss_module):
+def test_search_by_full_text_validates_top_k(opengauss_module: ModuleType) -> None:
vector = opengauss_module.OpenGauss.__new__(opengauss_module.OpenGauss)
with pytest.raises(ValueError, match="top_k must be a positive integer"):
vector.search_by_full_text("query", top_k=0)
-def test_create_collection_cache_and_create_path(opengauss_module, monkeypatch: pytest.MonkeyPatch):
+def test_create_collection_cache_and_create_path(opengauss_module: ModuleType, monkeypatch: pytest.MonkeyPatch) -> None:
pool = MagicMock()
monkeypatch.setattr(opengauss_module.psycopg2.pool, "SimpleConnectionPool", MagicMock(return_value=pool))
lock = MagicMock()
@@ -357,7 +368,7 @@ def test_create_collection_cache_and_create_path(opengauss_module, monkeypatch:
cursor = MagicMock()
@contextmanager
- def _cursor_ctx():
+ def _cursor_ctx() -> Generator[MagicMock]:
yield cursor
vector._get_cursor = _cursor_ctx
@@ -372,7 +383,9 @@ def test_create_collection_cache_and_create_path(opengauss_module, monkeypatch:
opengauss_module.redis_client.set.assert_called_once()
-def test_opengauss_factory_uses_existing_or_generated_collection(opengauss_module, monkeypatch: pytest.MonkeyPatch):
+def test_opengauss_factory_uses_existing_or_generated_collection(
+ opengauss_module: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
factory = opengauss_module.OpenGaussFactory()
dataset_with_index = Dataset(
id="dataset-1", index_struct=json.dumps({"vector_store": {"class_prefix": "EXISTING_COLLECTION"}})
diff --git a/api/providers/vdb/vdb-tencent/tests/unit_tests/src/dify_vdb_tencent/test_tencent_vector.py b/api/providers/vdb/vdb-tencent/tests/unit_tests/src/dify_vdb_tencent/test_tencent_vector.py
index 259d0582977..51ac7fffe2a 100644
--- a/api/providers/vdb/vdb-tencent/tests/unit_tests/src/dify_vdb_tencent/test_tencent_vector.py
+++ b/api/providers/vdb/vdb-tencent/tests/unit_tests/src/dify_vdb_tencent/test_tencent_vector.py
@@ -1,43 +1,48 @@
import importlib
import json
import sys
-import types
-from types import SimpleNamespace
+from dataclasses import dataclass
+from types import ModuleType, SimpleNamespace
+from typing import TYPE_CHECKING, Any, Self
from unittest.mock import MagicMock, patch
import pytest
+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_tencent.tencent_vector import TencentConfig
-def _build_fake_tencent_modules():
- tcvdb_text = types.ModuleType("tcvdb_text")
- tcvdb_text_encoder = types.ModuleType("tcvdb_text.encoder")
- tcvectordb = types.ModuleType("tcvectordb")
- tcvectordb_model = types.ModuleType("tcvectordb.model")
- tcvectordb_document = types.ModuleType("tcvectordb.model.document")
- tcvectordb_index = types.ModuleType("tcvectordb.model.index")
- tcvectordb_enum = types.ModuleType("tcvectordb.model.enum")
+
+def _build_fake_tencent_modules() -> dict[str, FakeModule]:
+ tcvdb_text = FakeModule("tcvdb_text")
+ tcvdb_text_encoder = FakeModule("tcvdb_text.encoder")
+ tcvectordb = FakeModule("tcvectordb")
+ tcvectordb_model = FakeModule("tcvectordb.model")
+ tcvectordb_document = FakeModule("tcvectordb.model.document")
+ tcvectordb_index = FakeModule("tcvectordb.model.index")
+ tcvectordb_enum = FakeModule("tcvectordb.model.enum")
class _BM25Encoder:
- def encode_texts(self, text):
+ def encode_texts(self, text: str) -> dict[str, str]:
return {"encoded_text": text}
- def encode_queries(self, query):
+ def encode_queries(self, query: str) -> dict[str, str]:
return {"encoded_query": query}
@classmethod
- def default(cls, _lang):
+ def default(cls, _lang: str) -> Self:
return cls()
class VectorDBError(Exception):
- def __init__(self, message):
+ def __init__(self, message: str) -> None:
super().__init__(message)
self.message = message
class RPCVectorDBClient:
- def __init__(self, **kwargs):
+ def __init__(self, **kwargs: Any) -> None:
self.kwargs = kwargs
self.create_database_if_not_exists = MagicMock()
self.exists_collection = MagicMock(return_value=False)
@@ -51,81 +56,80 @@ def _build_fake_tencent_modules():
self.drop_collection = MagicMock()
class _Document:
- def __init__(self, **kwargs):
+ def __init__(self, **kwargs: Any) -> None:
self.__dict__.update(kwargs)
+ @dataclass(eq=False, repr=False)
class _HNSWSearchParams:
- def __init__(self, ef):
- self.ef = ef
+ ef: int
class _AnnSearch:
- def __init__(self, **kwargs):
+ def __init__(self, **kwargs: Any) -> None:
self.kwargs = kwargs
class _KeywordSearch:
- def __init__(self, **kwargs):
+ def __init__(self, **kwargs: Any) -> None:
self.kwargs = kwargs
class _WeightedRerank:
- def __init__(self, **kwargs):
+ def __init__(self, **kwargs: Any) -> None:
self.kwargs = kwargs
class _Filter:
@staticmethod
- def in_(field, values):
+ def in_(field: str, values: list[str]) -> tuple[str, str, list[str]]:
return ("in", field, values)
- def __init__(self, condition):
+ def __init__(self, condition: tuple[str, str, list[str]]) -> None:
self.condition = condition
_Filter.In = staticmethod(_Filter.in_)
class _HNSWParams:
- def __init__(self, **kwargs):
+ def __init__(self, **kwargs: Any) -> None:
self.kwargs = kwargs
class _FilterIndex:
- def __init__(self, *args):
+ def __init__(self, *args: object) -> None:
self.args = args
class _VectorIndex:
- def __init__(self, *args):
+ def __init__(self, *args: object) -> None:
self.args = args
class _SparseIndex:
- def __init__(self, **kwargs):
+ def __init__(self, **kwargs: Any) -> None:
self.kwargs = kwargs
- # ModuleType does not declare these attributes, so set them through the module namespace.
- vars(tcvectordb_enum)["IndexType"] = SimpleNamespace(
+ tcvectordb_enum.IndexType = SimpleNamespace(
__members__={"HNSW": "HNSW", "PRIMARY_KEY": "PRIMARY_KEY", "FILTER": "FILTER", "SPARSE_INVERTED": "SPARSE"},
PRIMARY_KEY="PRIMARY_KEY",
FILTER="FILTER",
SPARSE_INVERTED="SPARSE",
)
- vars(tcvectordb_enum)["MetricType"] = SimpleNamespace(__members__={"IP": "IP"}, IP="IP")
- vars(tcvectordb_enum)["FieldType"] = SimpleNamespace(String="String", Json="Json", SparseVector="SparseVector")
+ tcvectordb_enum.MetricType = SimpleNamespace(__members__={"IP": "IP"}, IP="IP")
+ tcvectordb_enum.FieldType = SimpleNamespace(String="String", Json="Json", SparseVector="SparseVector")
- vars(tcvectordb_document)["Document"] = _Document
- vars(tcvectordb_document)["HNSWSearchParams"] = _HNSWSearchParams
- vars(tcvectordb_document)["AnnSearch"] = _AnnSearch
- vars(tcvectordb_document)["Filter"] = _Filter
- vars(tcvectordb_document)["KeywordSearch"] = _KeywordSearch
- vars(tcvectordb_document)["WeightedRerank"] = _WeightedRerank
+ tcvectordb_document.Document = _Document
+ tcvectordb_document.HNSWSearchParams = _HNSWSearchParams
+ tcvectordb_document.AnnSearch = _AnnSearch
+ tcvectordb_document.Filter = _Filter
+ tcvectordb_document.KeywordSearch = _KeywordSearch
+ tcvectordb_document.WeightedRerank = _WeightedRerank
- vars(tcvectordb_index)["HNSWParams"] = _HNSWParams
- vars(tcvectordb_index)["FilterIndex"] = _FilterIndex
- vars(tcvectordb_index)["VectorIndex"] = _VectorIndex
- vars(tcvectordb_index)["SparseIndex"] = _SparseIndex
+ tcvectordb_index.HNSWParams = _HNSWParams
+ tcvectordb_index.FilterIndex = _FilterIndex
+ tcvectordb_index.VectorIndex = _VectorIndex
+ tcvectordb_index.SparseIndex = _SparseIndex
- vars(tcvdb_text_encoder)["BM25Encoder"] = _BM25Encoder
+ tcvdb_text_encoder.BM25Encoder = _BM25Encoder
- vars(tcvectordb_model)["document"] = tcvectordb_document
- vars(tcvectordb_model)["enum"] = tcvectordb_enum
- vars(tcvectordb_model)["index"] = tcvectordb_index
+ tcvectordb_model.document = tcvectordb_document
+ tcvectordb_model.enum = tcvectordb_enum
+ tcvectordb_model.index = tcvectordb_index
- vars(tcvectordb)["RPCVectorDBClient"] = RPCVectorDBClient
- vars(tcvectordb)["VectorDBException"] = VectorDBError
+ tcvectordb.RPCVectorDBClient = RPCVectorDBClient
+ tcvectordb.VectorDBException = VectorDBError
return {
"tcvdb_text": tcvdb_text,
@@ -139,7 +143,7 @@ def _build_fake_tencent_modules():
@pytest.fixture
-def tencent_module(monkeypatch: pytest.MonkeyPatch):
+def tencent_module(monkeypatch: pytest.MonkeyPatch) -> ModuleType:
for name, module in _build_fake_tencent_modules().items():
monkeypatch.setitem(sys.modules, name, module)
@@ -148,7 +152,7 @@ def tencent_module(monkeypatch: pytest.MonkeyPatch):
return importlib.reload(module)
-def _config(module, **overrides):
+def _config(module: ModuleType, **overrides: Any) -> "TencentConfig":
values = {
"url": "http://vdb.local",
"api_key": "api-key",
@@ -166,7 +170,7 @@ def _config(module, **overrides):
return module.TencentConfig.model_validate(values)
-def test_config_and_init_paths(tencent_module):
+def test_config_and_init_paths(tencent_module: ModuleType) -> None:
config = _config(tencent_module)
assert config.to_tencent_params()["url"] == "http://vdb.local"
@@ -192,12 +196,12 @@ def test_config_and_init_paths(tencent_module):
@pytest.mark.parametrize("database", [None, ""])
-def test_config_requires_a_database(tencent_module, database):
+def test_config_requires_a_database(tencent_module: ModuleType, database: str | None) -> None:
with pytest.raises(ValueError, match="config TENCENT_VECTOR_DB_DATABASE is required"):
_config(tencent_module, database=database)
-def test_create_collection_branches(tencent_module, monkeypatch: pytest.MonkeyPatch):
+def test_create_collection_branches(tencent_module: ModuleType, monkeypatch: pytest.MonkeyPatch) -> None:
vector = tencent_module.TencentVector("collection_1", _config(tencent_module))
lock = MagicMock()
@@ -237,7 +241,7 @@ def test_create_collection_branches(tencent_module, monkeypatch: pytest.MonkeyPa
vector._client.create_collection.side_effect = None
-def test_create_add_delete_and_search_behaviour(tencent_module):
+def test_create_add_delete_and_search_behaviour(tencent_module: ModuleType) -> None:
vector = tencent_module.TencentVector("collection_1", _config(tencent_module, enable_hybrid_search=True))
vector._create_collection = MagicMock()
docs = [
@@ -257,7 +261,7 @@ def test_create_add_delete_and_search_behaviour(tencent_module):
vector._client.query.return_value = [{"id": "a"}]
assert vector.text_exists("a") is True
- vector._client.query.return_value = []
+ vector._client.query.return_value = list[dict[str, object]]()
assert vector.text_exists("a") is False
vector.delete_by_ids([])
@@ -289,7 +293,7 @@ def test_create_add_delete_and_search_behaviour(tencent_module):
vector._client.drop_collection.assert_called_once()
-def test_add_texts_converts_only_true_summary_marker(tencent_module):
+def test_add_texts_converts_only_true_summary_marker(tencent_module: ModuleType) -> None:
vector = tencent_module.TencentVector("collection_1", _config(tencent_module))
summary_metadata = {"doc_id": "summary", "is_summary": True, "published": False}
regular_metadata = {"doc_id": "regular", "published": False}
@@ -306,7 +310,9 @@ def test_add_texts_converts_only_true_summary_marker(tencent_module):
assert summary_metadata["is_summary"] is True
-def test_tencent_factory_existing_and_generated_collection(tencent_module, monkeypatch: pytest.MonkeyPatch):
+def test_tencent_factory_existing_and_generated_collection(
+ tencent_module: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
factory = tencent_module.TencentVectorFactory()
dataset_with_index = Dataset(
id="dataset-1", index_struct=json.dumps({"vector_store": {"class_prefix": "EXISTING_COLLECTION"}})
diff --git a/api/providers/vdb/vdb-vastbase/tests/unit_tests/src/dify_vdb_vastbase/test_vastbase_vector.py b/api/providers/vdb/vdb-vastbase/tests/unit_tests/src/dify_vdb_vastbase/test_vastbase_vector.py
index 1666c804192..13e9f8f2127 100644
--- a/api/providers/vdb/vdb-vastbase/tests/unit_tests/src/dify_vdb_vastbase/test_vastbase_vector.py
+++ b/api/providers/vdb/vdb-vastbase/tests/unit_tests/src/dify_vdb_vastbase/test_vastbase_vector.py
@@ -2,8 +2,10 @@ import hashlib
import importlib
import json
import sys
-from contextlib import contextmanager
-from types import SimpleNamespace
+from collections.abc import Callable, Generator
+from contextlib import AbstractContextManager, contextmanager
+from types import ModuleType, SimpleNamespace
+from typing import TYPE_CHECKING
from unittest.mock import MagicMock, patch
import pytest
@@ -13,8 +15,11 @@ 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_vastbase.vastbase_vector import VastbaseVector, VastbaseVectorConfig
-def _build_fake_psycopg2_modules():
+
+def _build_fake_psycopg2_modules() -> dict[str, FakeModule]:
psycopg2 = FakeModule("psycopg2")
psycopg2.__path__ = []
psycopg2_extras = FakeModule("psycopg2.extras")
@@ -22,7 +27,7 @@ def _build_fake_psycopg2_modules():
psycopg2_errors = FakeModule("psycopg2.errors")
class SimpleConnectionPool:
- def __init__[**P](self, *args: P.args, **kwargs: P.kwargs):
+ def __init__[**P](self, *args: P.args, **kwargs: P.kwargs) -> None:
self.args = args
self.kwargs = kwargs
self.getconn = MagicMock()
@@ -47,7 +52,7 @@ def _build_fake_psycopg2_modules():
@pytest.fixture
-def vastbase_module(monkeypatch: pytest.MonkeyPatch):
+def vastbase_module(monkeypatch: pytest.MonkeyPatch) -> ModuleType:
for name, module in _build_fake_psycopg2_modules().items():
monkeypatch.setitem(sys.modules, name, module)
@@ -56,7 +61,7 @@ def vastbase_module(monkeypatch: pytest.MonkeyPatch):
return importlib.reload(module)
-def _config(module):
+def _config(module: ModuleType) -> "VastbaseVectorConfig":
return module.VastbaseVectorConfig(
host="localhost",
port=5432,
@@ -68,9 +73,9 @@ def _config(module):
)
-def _cursor_ctx(cursor):
+def _cursor_ctx(cursor: MagicMock) -> Callable[[], AbstractContextManager[MagicMock]]:
@contextmanager
- def _ctx():
+ def _ctx() -> Generator[MagicMock]:
yield cursor
return _ctx
@@ -88,7 +93,7 @@ def _cursor_ctx(cursor):
("max_connection", 0, "config VASTBASE_MAX_CONNECTION is required"),
],
)
-def test_vastbase_config_validation(vastbase_module, field, value, message):
+def test_vastbase_config_validation(vastbase_module: ModuleType, field: str, value: str | int, message: str) -> None:
values = _config(vastbase_module).model_dump()
values[field] = value
@@ -96,7 +101,7 @@ def test_vastbase_config_validation(vastbase_module, field, value, message):
vastbase_module.VastbaseVectorConfig.model_validate(values)
-def test_vastbase_config_rejects_invalid_connection_window(vastbase_module):
+def test_vastbase_config_rejects_invalid_connection_window(vastbase_module: ModuleType) -> None:
with pytest.raises(ValidationError, match="VASTBASE_MIN_CONNECTION should less than VASTBASE_MAX_CONNECTION"):
vastbase_module.VastbaseVectorConfig.model_validate(
{
@@ -111,7 +116,7 @@ def test_vastbase_config_rejects_invalid_connection_window(vastbase_module):
)
-def test_init_sets_table_name_index_hash_and_type(vastbase_module, monkeypatch: pytest.MonkeyPatch):
+def test_init_sets_table_name_index_hash_and_type(vastbase_module: ModuleType, monkeypatch: pytest.MonkeyPatch) -> None:
pool = MagicMock()
monkeypatch.setattr(vastbase_module.psycopg2.pool, "SimpleConnectionPool", MagicMock(return_value=pool))
@@ -123,7 +128,7 @@ def test_init_sets_table_name_index_hash_and_type(vastbase_module, monkeypatch:
assert vector.index_hash == hashlib.md5(b"embedding_collection_1").hexdigest()[:8]
-def test_get_cursor_closes_commits_and_returns_connection(vastbase_module):
+def test_get_cursor_closes_commits_and_returns_connection(vastbase_module: ModuleType) -> None:
vector = vastbase_module.VastbaseVector.__new__(vastbase_module.VastbaseVector)
pool = MagicMock()
conn = MagicMock()
@@ -140,7 +145,7 @@ def test_get_cursor_closes_commits_and_returns_connection(vastbase_module):
pool.putconn.assert_called_once_with(conn)
-def test_create_calls_collection_insert_and_index(vastbase_module):
+def test_create_calls_collection_insert_and_index(vastbase_module: ModuleType) -> None:
vector = vastbase_module.VastbaseVector.__new__(vastbase_module.VastbaseVector)
vector._create_collection = MagicMock()
vector.add_texts = MagicMock(return_value=["seg-1"])
@@ -155,7 +160,7 @@ def test_create_calls_collection_insert_and_index(vastbase_module):
assert result == ["seg-1"]
-def test_add_texts_uses_execute_values(vastbase_module, monkeypatch: pytest.MonkeyPatch):
+def test_add_texts_uses_execute_values(vastbase_module: ModuleType, monkeypatch: pytest.MonkeyPatch) -> None:
pool = MagicMock()
monkeypatch.setattr(vastbase_module.psycopg2.pool, "SimpleConnectionPool", MagicMock(return_value=pool))
vector = vastbase_module.VastbaseVector("collection_1", _config(vastbase_module))
@@ -175,7 +180,7 @@ def test_add_texts_uses_execute_values(vastbase_module, monkeypatch: pytest.Monk
vastbase_module.psycopg2.extras.execute_values.assert_called_once()
-def test_text_exists_and_get_by_ids(vastbase_module):
+def test_text_exists_and_get_by_ids(vastbase_module: ModuleType) -> None:
vector = vastbase_module.VastbaseVector.__new__(vastbase_module.VastbaseVector)
vector.table_name = "embedding_collection_1"
cursor = MagicMock()
@@ -189,7 +194,7 @@ def test_text_exists_and_get_by_ids(vastbase_module):
assert docs[0].page_content == "text-1"
-def test_delete_and_metadata_field_queries(vastbase_module):
+def test_delete_and_metadata_field_queries(vastbase_module: ModuleType) -> None:
vector = vastbase_module.VastbaseVector.__new__(vastbase_module.VastbaseVector)
vector.table_name = "embedding_collection_1"
cursor = MagicMock()
@@ -205,7 +210,7 @@ def test_delete_and_metadata_field_queries(vastbase_module):
assert any("DROP TABLE IF EXISTS embedding_collection_1" in query for query in sql)
-def test_delete_by_ids_short_circuits_with_empty_input(vastbase_module):
+def test_delete_by_ids_short_circuits_with_empty_input(vastbase_module: ModuleType) -> None:
vector = vastbase_module.VastbaseVector.__new__(vastbase_module.VastbaseVector)
vector._get_cursor = MagicMock()
@@ -214,7 +219,7 @@ def test_delete_by_ids_short_circuits_with_empty_input(vastbase_module):
vector._get_cursor.assert_not_called()
-def test_delete_by_ids_swallows_undefined_table(vastbase_module):
+def test_delete_by_ids_swallows_undefined_table(vastbase_module: ModuleType) -> None:
vector = vastbase_module.VastbaseVector.__new__(vastbase_module.VastbaseVector)
vector.table_name = "embedding_collection_1"
cursor = MagicMock()
@@ -225,13 +230,13 @@ def test_delete_by_ids_swallows_undefined_table(vastbase_module):
vector.delete_by_ids(["seg-1"])
-def test_search_by_vector_validates_top_k(vastbase_module):
+def test_search_by_vector_validates_top_k(vastbase_module: ModuleType) -> None:
vector = vastbase_module.VastbaseVector.__new__(vastbase_module.VastbaseVector)
with pytest.raises(ValueError, match="top_k must be a positive integer"):
vector.search_by_vector([0.1, 0.2], top_k=0)
-def test_search_by_vector_applies_threshold_and_metadata_filter(vastbase_module):
+def test_search_by_vector_applies_threshold_and_metadata_filter(vastbase_module: ModuleType) -> None:
vector = vastbase_module.VastbaseVector.__new__(vastbase_module.VastbaseVector)
vector.table_name = "embedding_collection_1"
cursor = MagicMock()
@@ -253,7 +258,7 @@ def test_search_by_vector_applies_threshold_and_metadata_filter(vastbase_module)
assert params[1] == ("doc-a", "doc-b")
-def test_search_by_vector_without_filter_omits_where_clause(vastbase_module):
+def test_search_by_vector_without_filter_omits_where_clause(vastbase_module: ModuleType) -> None:
vector = vastbase_module.VastbaseVector.__new__(vastbase_module.VastbaseVector)
vector.table_name = "embedding_collection_1"
cursor = MagicMock()
@@ -268,13 +273,13 @@ def test_search_by_vector_without_filter_omits_where_clause(vastbase_module):
assert params == [json.dumps([0.1, 0.2])]
-def test_search_by_full_text_validates_top_k(vastbase_module):
+def test_search_by_full_text_validates_top_k(vastbase_module: ModuleType) -> None:
vector = vastbase_module.VastbaseVector.__new__(vastbase_module.VastbaseVector)
with pytest.raises(ValueError, match="top_k must be a positive integer"):
vector.search_by_full_text("query", top_k=0)
-def test_search_by_full_text_uses_bm25_and_metadata_filter(vastbase_module):
+def test_search_by_full_text_uses_bm25_and_metadata_filter(vastbase_module: ModuleType) -> None:
vector = vastbase_module.VastbaseVector.__new__(vastbase_module.VastbaseVector)
vector.table_name = "embedding_collection_1"
cursor = MagicMock()
@@ -293,7 +298,7 @@ def test_search_by_full_text_uses_bm25_and_metadata_filter(vastbase_module):
assert params == ["hello world", ("doc-a",)]
-def test_create_collection_cache_and_create_path(vastbase_module, monkeypatch: pytest.MonkeyPatch):
+def test_create_collection_cache_and_create_path(vastbase_module: ModuleType, monkeypatch: pytest.MonkeyPatch) -> None:
pool = MagicMock()
monkeypatch.setattr(vastbase_module.psycopg2.pool, "SimpleConnectionPool", MagicMock(return_value=pool))
lock = MagicMock()
@@ -318,7 +323,9 @@ def test_create_collection_cache_and_create_path(vastbase_module, monkeypatch: p
vastbase_module.redis_client.set.assert_called_once()
-def _index_vector(vastbase_module, monkeypatch, *, cache_hit=False):
+def _index_vector(
+ vastbase_module: ModuleType, monkeypatch: pytest.MonkeyPatch, *, cache_hit: bool = False
+) -> tuple["VastbaseVector", MagicMock]:
pool = MagicMock()
monkeypatch.setattr(vastbase_module.psycopg2.pool, "SimpleConnectionPool", MagicMock(return_value=pool))
lock = MagicMock()
@@ -333,7 +340,9 @@ def _index_vector(vastbase_module, monkeypatch, *, cache_hit=False):
return vector, cursor
-def test_create_index_builds_graph_and_fulltext_indexes(vastbase_module, monkeypatch: pytest.MonkeyPatch):
+def test_create_index_builds_graph_and_fulltext_indexes(
+ vastbase_module: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
vector, cursor = _index_vector(vastbase_module, monkeypatch)
vector._create_index(1536)
@@ -345,7 +354,9 @@ def test_create_index_builds_graph_and_fulltext_indexes(vastbase_module, monkeyp
vastbase_module.redis_client.set.assert_called_once()
-def test_create_index_skips_graph_index_for_large_dimension(vastbase_module, monkeypatch: pytest.MonkeyPatch):
+def test_create_index_skips_graph_index_for_large_dimension(
+ vastbase_module: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
vector, cursor = _index_vector(vastbase_module, monkeypatch)
vector._create_index(vastbase_module.MAX_GRAPH_INDEX_DIMENSION + 1)
@@ -356,7 +367,7 @@ def test_create_index_skips_graph_index_for_large_dimension(vastbase_module, mon
assert any("USING fulltext (text)" in query for query in sql)
-def test_create_index_returns_early_on_cache_hit(vastbase_module, monkeypatch: pytest.MonkeyPatch):
+def test_create_index_returns_early_on_cache_hit(vastbase_module: ModuleType, monkeypatch: pytest.MonkeyPatch) -> None:
vector, cursor = _index_vector(vastbase_module, monkeypatch, cache_hit=True)
vector._create_index(1536)
@@ -365,7 +376,9 @@ def test_create_index_returns_early_on_cache_hit(vastbase_module, monkeypatch: p
vastbase_module.redis_client.set.assert_not_called()
-def test_vastbase_factory_uses_existing_or_generated_collection(vastbase_module, monkeypatch: pytest.MonkeyPatch):
+def test_vastbase_factory_uses_existing_or_generated_collection(
+ vastbase_module: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
factory = vastbase_module.VastbaseVectorFactory()
dataset_with_index = Dataset(
id="dataset-1", index_struct=json.dumps({"vector_store": {"class_prefix": "EXISTING_COLLECTION"}})