Skip to content

The agent engineering platform.

feat(standard-tests): add a property to skip relevant tests if the vector store doesn't support `get_by_ids()` (#32633)

Christophe Bornetee268db1c5b6
diff --git a/libs/standard-tests/langchain_tests/integration_tests/vectorstores.py b/libs/standard-tests/langchain_tests/integration_tests/vectorstores.py
index 0f27ca0d6f..ab6286fe9b 100644
--- a/libs/standard-tests/langchain_tests/integration_tests/vectorstores.py
+++ b/libs/standard-tests/langchain_tests/integration_tests/vectorstores.py
@@ -118,6 +118,11 @@ class VectorStoreIntegrationTests(BaseStandardTests):
"""Configurable property to enable or disable async tests."""
return True
+ @property
+ def has_get_by_ids(self) -> bool:
+ """Whether the vector store supports get_by_ids."""
+ return True
+
@staticmethod
def get_embeddings() -> Embeddings:
"""Get embeddings.
@@ -336,18 +341,22 @@ class VectorStoreIntegrationTests(BaseStandardTests):
.. note::
``get_by_ids`` was added to the ``VectorStore`` interface in
``langchain-core`` version 0.2.11. If difficult to implement, this
- test can be skipped using a pytest ``xfail`` on the test class:
+ test can be skipped by setting the ``has_get_by_ids`` property to
+ ``False``.
.. code-block:: python
- @pytest.mark.xfail(reason=("get_by_ids not implemented."))
- def test_get_by_ids(self, vectorstore: VectorStore) -> None:
- super().test_get_by_ids(vectorstore)
+ @property
+ def has_get_by_ids(self) -> bool:
+ return False
"""
if not self.has_sync:
pytest.skip("Sync tests not supported.")
+ if not self.has_get_by_ids:
+ pytest.skip("get_by_ids not implemented.")
+
documents = [
Document(page_content="foo", metadata={"id": 1}),
Document(page_content="bar", metadata={"id": 2}),
@@ -372,18 +381,22 @@ class VectorStoreIntegrationTests(BaseStandardTests):
.. note::
``get_by_ids`` was added to the ``VectorStore`` interface in
``langchain-core`` version 0.2.11. If difficult to implement, this
- test can be skipped using a pytest ``xfail`` on the test class:
+ test can be skipped by setting the ``has_get_by_ids`` property to
+ ``False``.
.. code-block:: python
- @pytest.mark.xfail(reason=("get_by_ids not implemented."))
- def test_get_by_ids_missing(self, vectorstore: VectorStore) -> None:
- super().test_get_by_ids_missing(vectorstore)
+ @property
+ def has_get_by_ids(self) -> bool:
+ return False
"""
if not self.has_sync:
pytest.skip("Sync tests not supported.")
+ if not self.has_get_by_ids:
+ pytest.skip("get_by_ids not implemented.")
+
# This should not raise an exception
documents = vectorstore.get_by_ids(["1", "2", "3"])
assert documents == []
@@ -402,20 +415,22 @@ class VectorStoreIntegrationTests(BaseStandardTests):
.. note::
``get_by_ids`` was added to the ``VectorStore`` interface in
``langchain-core`` version 0.2.11. If difficult to implement, this
- test can be skipped using a pytest ``xfail`` on the test class:
+ test can be skipped by setting the ``has_get_by_ids`` property to
+ ``False``.
.. code-block:: python
- @pytest.mark.xfail(reason=("get_by_ids not implemented."))
- def test_add_documents_documents(
- self, vectorstore: VectorStore
- ) -> None:
- super().test_add_documents_documents(vectorstore)
+ @property
+ def has_get_by_ids(self) -> bool:
+ return False
"""
if not self.has_sync:
pytest.skip("Sync tests not supported.")
+ if not self.has_get_by_ids:
+ pytest.skip("get_by_ids not implemented.")
+
documents = [
Document(page_content="foo", metadata={"id": 1}),
Document(page_content="bar", metadata={"id": 2}),
@@ -446,20 +461,22 @@ class VectorStoreIntegrationTests(BaseStandardTests):
.. note::
``get_by_ids`` was added to the ``VectorStore`` interface in
``langchain-core`` version 0.2.11. If difficult to implement, this
- test can be skipped using a pytest ``xfail`` on the test class:
+ test can be skipped by setting the ``has_get_by_ids`` property to
+ ``False``.
.. code-block:: python
- @pytest.mark.xfail(reason=("get_by_ids not implemented."))
- def test_add_documents_with_existing_ids(
- self, vectorstore: VectorStore
- ) -> None:
- super().test_add_documents_with_existing_ids(vectorstore)
+ @property
+ def has_get_by_ids(self) -> bool:
+ return False
"""
if not self.has_sync:
pytest.skip("Sync tests not supported.")
+ if not self.has_get_by_ids:
+ pytest.skip("get_by_ids not implemented.")
+
documents = [
Document(id="foo", page_content="foo", metadata={"id": 1}),
Document(page_content="bar", metadata={"id": 2}),
@@ -683,18 +700,22 @@ class VectorStoreIntegrationTests(BaseStandardTests):
.. note::
``get_by_ids`` was added to the ``VectorStore`` interface in
``langchain-core`` version 0.2.11. If difficult to implement, this
- test can be skipped using a pytest ``xfail`` on the test class:
+ test can be skipped by setting the ``has_get_by_ids`` property to
+ ``False``.
.. code-block:: python
- @pytest.mark.xfail(reason=("get_by_ids not implemented."))
- async def test_get_by_ids(self, vectorstore: VectorStore) -> None:
- await super().test_get_by_ids(vectorstore)
+ @property
+ def has_get_by_ids(self) -> bool:
+ return False
"""
if not self.has_async:
pytest.skip("Async tests not supported.")
+ if not self.has_get_by_ids:
+ pytest.skip("get_by_ids not implemented.")
+
documents = [
Document(page_content="foo", metadata={"id": 1}),
Document(page_content="bar", metadata={"id": 2}),
@@ -719,20 +740,22 @@ class VectorStoreIntegrationTests(BaseStandardTests):
.. note::
``get_by_ids`` was added to the ``VectorStore`` interface in
``langchain-core`` version 0.2.11. If difficult to implement, this
- test can be skipped using a pytest ``xfail`` on the test class:
+ test can be skipped by setting the ``has_get_by_ids`` property to
+ ``False``.
.. code-block:: python
- @pytest.mark.xfail(reason=("get_by_ids not implemented."))
- async def test_get_by_ids_missing(
- self, vectorstore: VectorStore
- ) -> None:
- await super().test_get_by_ids_missing(vectorstore)
+ @property
+ def has_get_by_ids(self) -> bool:
+ return False
"""
if not self.has_async:
pytest.skip("Async tests not supported.")
+ if not self.has_get_by_ids:
+ pytest.skip("get_by_ids not implemented.")
+
# This should not raise an exception
assert await vectorstore.aget_by_ids(["1", "2", "3"]) == []
@@ -752,20 +775,22 @@ class VectorStoreIntegrationTests(BaseStandardTests):
.. note::
``get_by_ids`` was added to the ``VectorStore`` interface in
``langchain-core`` version 0.2.11. If difficult to implement, this
- test can be skipped using a pytest ``xfail`` on the test class:
+ test can be skipped by setting the ``has_get_by_ids`` property to
+ ``False``.
.. code-block:: python
- @pytest.mark.xfail(reason=("get_by_ids not implemented."))
- async def test_add_documents_documents(
- self, vectorstore: VectorStore
- ) -> None:
- await super().test_add_documents_documents(vectorstore)
+ @property
+ def has_get_by_ids(self) -> bool:
+ return False
"""
if not self.has_async:
pytest.skip("Async tests not supported.")
+ if not self.has_get_by_ids:
+ pytest.skip("get_by_ids not implemented.")
+
documents = [
Document(page_content="foo", metadata={"id": 1}),
Document(page_content="bar", metadata={"id": 2}),
@@ -798,20 +823,22 @@ class VectorStoreIntegrationTests(BaseStandardTests):
.. note::
``get_by_ids`` was added to the ``VectorStore`` interface in
``langchain-core`` version 0.2.11. If difficult to implement, this
- test can be skipped using a pytest ``xfail`` on the test class:
+ test can be skipped by setting the ``has_get_by_ids`` property to
+ ``False``.
.. code-block:: python
- @pytest.mark.xfail(reason=("get_by_ids not implemented."))
- async def test_add_documents_with_existing_ids(
- self, vectorstore: VectorStore
- ) -> None:
- await super().test_add_documents_with_existing_ids(vectorstore)
+ @property
+ def has_get_by_ids(self) -> bool:
+ return False
"""
if not self.has_async:
pytest.skip("Async tests not supported.")
+ if not self.has_get_by_ids:
+ pytest.skip("get_by_ids not implemented.")
+
documents = [
Document(id="foo", page_content="foo", metadata={"id": 1}),
Document(page_content="bar", metadata={"id": 2}),
diff --git a/libs/standard-tests/tests/unit_tests/test_in_memory_vectorstore.py b/libs/standard-tests/tests/unit_tests/test_in_memory_vectorstore.py
index 53b9dd1a9c..2f02694805 100644
--- a/libs/standard-tests/tests/unit_tests/test_in_memory_vectorstore.py
+++ b/libs/standard-tests/tests/unit_tests/test_in_memory_vectorstore.py
@@ -1,3 +1,5 @@
+from __future__ import annotations
+
import pytest
from langchain_core.vectorstores import (
InMemoryVectorStore,
@@ -12,3 +14,27 @@ class TestInMemoryVectorStore(VectorStoreIntegrationTests):
def vectorstore(self) -> VectorStore:
embeddings = self.get_embeddings()
return InMemoryVectorStore(embedding=embeddings)
+
+
+class WithoutGetByIdsVectorStore(InMemoryVectorStore):
+ """InMemoryVectorStore that does not implement get_by_ids."""
+
+ get_by_ids = VectorStore.get_by_ids
+
+
+class TestWithoutGetByIdVectorStore(VectorStoreIntegrationTests):
+ @pytest.fixture
+ def vectorstore(self) -> VectorStore:
+ embeddings = self.get_embeddings()
+ return WithoutGetByIdsVectorStore(embedding=embeddings)
+
+ @property
+ def has_get_by_ids(self) -> bool:
+ return False
+
+ def test_get_by_ids_fails(self, vectorstore: VectorStore) -> None:
+ with pytest.raises(
+ NotImplementedError,
+ match="WithoutGetByIdsVectorStore does not yet support get_by_ids.",
+ ):
+ vectorstore.get_by_ids(["id1", "id2"])