Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

standard-tests: add override check #24407

Merged
merged 6 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
standard-tests: add override check
  • Loading branch information
efriis committed Jul 18, 2024
commit c8fb9494e2ed329da344e351150cb37144c7e563
48 changes: 48 additions & 0 deletions libs/standard-tests/langchain_standard_tests/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from abc import ABC
from typing import Type


class BaseStandardTests(ABC):
def test_no_overrides_DO_NOT_OVERRIDE(self) -> None:
"""
Test that no standard tests are overridden.
"""
# find path to standard test implementations
comparison_class = None

def explore_bases(cls: Type) -> None:
nonlocal comparison_class
for base in cls.__bases__:
if base.__module__.startswith("langchain_standard_tests."):
if comparison_class is None:
comparison_class = base
else:
raise ValueError(
"Multiple standard test base classes found: "
f"{comparison_class}, {base}"
)
else:
explore_bases(base)

explore_bases(self.__class__)
assert comparison_class is not None, "No standard test base class found."

print(f"Comparing {self.__class__} to {comparison_class}") # noqa: T201

running_tests = set(
[method for method in dir(self) if method.startswith("test_")]
)
base_tests = set(
[method for method in dir(comparison_class) if method.startswith("test_")]
)
non_standard_tests = running_tests - base_tests
assert not non_standard_tests, f"Non-standard tests found: {non_standard_tests}"
deleted_tests = base_tests - running_tests
assert not deleted_tests, f"Standard tests deleted: {deleted_tests}"

overriden_tests = [
method
for method in running_tests
if getattr(self.__class__, method) is not getattr(comparison_class, method)
]
assert not overriden_tests, f"Standard tests overridden: {overriden_tests}"
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from abc import ABC, abstractmethod
from abc import abstractmethod
from typing import AsyncGenerator, Generator, Generic, Tuple, TypeVar

import pytest
from langchain_core.stores import BaseStore

from langchain_standard_tests.base import BaseStandardTests

V = TypeVar("V")


class BaseStoreSyncTests(ABC, Generic[V]):
class BaseStoreSyncTests(BaseStandardTests, Generic[V]):
"""Test suite for checking the key-value API of a BaseStore.

This test suite verifies the basic key-value API of a BaseStore.
Expand Down Expand Up @@ -138,7 +140,7 @@ def test_yield_keys(
assert sorted(kv_store.yield_keys(prefix="foo")) == ["foo"]


class BaseStoreAsyncTests(ABC):
class BaseStoreAsyncTests(BaseStandardTests):
"""Test suite for checking the key-value API of a BaseStore.

This test suite verifies the basic key-value API of a BaseStore.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from abc import ABC, abstractmethod
from abc import abstractmethod

import pytest
from langchain_core.caches import BaseCache
from langchain_core.outputs import Generation

from langchain_standard_tests.base import BaseStandardTests

class SyncCacheTestSuite(ABC):

class SyncCacheTestSuite(BaseStandardTests):
"""Test suite for checking the BaseCache API of a caching layer for LLMs.

This test suite verifies the basic caching API of a caching layer for LLMs.
Expand Down Expand Up @@ -95,7 +97,7 @@ def test_update_cache_with_multiple_generations(self, cache: BaseCache) -> None:
assert cache.lookup(prompt, llm_string) == generations


class AsyncCacheTestSuite(ABC):
class AsyncCacheTestSuite(BaseStandardTests):
"""Test suite for checking the BaseCache API of a caching layer for LLMs.

This test suite verifies the basic caching API of a caching layer for LLMs.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
"""Test suite to test vectostores."""

import inspect
from abc import ABC, abstractmethod
from abc import abstractmethod

import pytest
from langchain_core.documents import Document
from langchain_core.embeddings.fake import DeterministicFakeEmbedding, Embeddings
from langchain_core.vectorstores import VectorStore

from langchain_standard_tests.base import BaseStandardTests

# Arbitrarily chosen. Using a small embedding size
# so tests are faster and easier to debug.
EMBEDDING_SIZE = 6


class ReadWriteTestSuite(ABC):
class ReadWriteTestSuite(BaseStandardTests):
"""Test suite for checking the read-write API of a vectorstore.

This test suite verifies the basic read-write API of a vectorstore.
Expand Down Expand Up @@ -200,7 +203,7 @@ def test_upsert_documents_has_no_ids(self, vectorstore: VectorStore) -> None:
assert "ids" not in signature.parameters


class AsyncReadWriteTestSuite(ABC):
class AsyncReadWriteTestSuite(BaseStandardTests):
"""Test suite for checking the **async** read-write API of a vectorstore.

This test suite verifies the basic read-write API of a vectorstore.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from abc import ABC, abstractmethod
from abc import abstractmethod
from typing import Any, List, Literal, Optional, Type

import pytest
Expand All @@ -7,6 +7,8 @@
from langchain_core.runnables import RunnableBinding
from langchain_core.tools import tool

from langchain_standard_tests.base import BaseStandardTests


class Person(BaseModel):
"""Record attributes of a person."""
Expand All @@ -26,7 +28,7 @@ def my_adder(a: int, b: int) -> int:
return a + b


class ChatModelTests(ABC):
class ChatModelTests(BaseStandardTests):
@property
@abstractmethod
def chat_model_class(self) -> Type[BaseChatModel]:
Expand Down
Loading