mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-10 03:28:53 +00:00
litellm_fix_mapped_tests_core: clear client cache and fix isinstance checks
## Problem
Tests using mocked HTTP clients were hitting real APIs because:
1. HTTP client cache was returning previously cached real clients
2. isinstance checks failed due to module identity issues from sys.path
### Tests affected:
- test_send_email_missing_api_key
- test_send_email_multiple_recipients (resend & sendgrid)
- test_search_uses_registry_credentials
- test_vector_store_create_with_simple_provider_name
- test_vector_store_create_with_provider_api_type
- test_vector_store_create_with_ragflow_provider
- test_image_edit_merges_headers_and_extra_headers
- test_retrieve_container_basic (container API tests)
## Solution
1. Add clear_client_cache fixture (autouse=True) to clear
litellm.in_memory_llm_clients_cache before each test
2. Fix isinstance checks to use type name comparison
(avoids module identity issues from sys.path.insert)
## Why not disable_aiohttp_transport
The default transport is aiohttp, so tests should work with it.
Clearing the cache ensures mocks are used instead of cached real clients.
## Regression
PR #19829 (commit f95572e3ed) added @respx.mock but cached clients
from earlier tests were being reused, bypassing the mocks.
This commit is contained in:
parent
c7522e356f
commit
2d28c5a056
6 changed files with 84 additions and 9 deletions
|
|
@ -32,6 +32,21 @@ from litellm.types.containers.main import (
|
|||
)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def clear_client_cache():
|
||||
"""
|
||||
Clear the HTTP client cache before each test to ensure mocks are used.
|
||||
This prevents cached real clients from being reused across tests.
|
||||
"""
|
||||
cache = getattr(litellm, "in_memory_llm_clients_cache", None)
|
||||
if cache is not None:
|
||||
cache.flush_cache()
|
||||
yield
|
||||
# Clear again after test to avoid polluting other tests
|
||||
if cache is not None:
|
||||
cache.flush_cache()
|
||||
|
||||
|
||||
class TestContainerAPI:
|
||||
"""Test suite for container API functionality."""
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ from httpx import Response
|
|||
|
||||
sys.path.insert(0, os.path.abspath("../../.."))
|
||||
|
||||
import litellm
|
||||
from litellm_enterprise.enterprise_callbacks.send_emails.resend_email import (
|
||||
ResendEmailLogger,
|
||||
)
|
||||
|
|
@ -16,6 +17,21 @@ from litellm_enterprise.enterprise_callbacks.send_emails.resend_email import (
|
|||
# Test file for Resend email integration
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def clear_client_cache():
|
||||
"""
|
||||
Clear the HTTP client cache before each test to ensure mocks are used.
|
||||
This prevents cached real clients from being reused across tests.
|
||||
"""
|
||||
cache = getattr(litellm, "in_memory_llm_clients_cache", None)
|
||||
if cache is not None:
|
||||
cache.flush_cache()
|
||||
yield
|
||||
# Clear again after test to avoid polluting other tests
|
||||
if cache is not None:
|
||||
cache.flush_cache()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_env_vars():
|
||||
with mock.patch.dict(os.environ, {"RESEND_API_KEY": "test_api_key"}):
|
||||
|
|
|
|||
|
|
@ -9,11 +9,26 @@ from httpx import Response
|
|||
|
||||
sys.path.insert(0, os.path.abspath("../../.."))
|
||||
|
||||
import litellm
|
||||
from litellm_enterprise.enterprise_callbacks.send_emails.sendgrid_email import (
|
||||
SendGridEmailLogger,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def clear_client_cache():
|
||||
"""
|
||||
Clear the HTTP client cache before each test to ensure mocks are used.
|
||||
This prevents cached real clients from being reused across tests.
|
||||
"""
|
||||
cache = getattr(litellm, "in_memory_llm_clients_cache", None)
|
||||
if cache is not None:
|
||||
cache.flush_cache()
|
||||
yield
|
||||
if cache is not None:
|
||||
cache.flush_cache()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_env_vars():
|
||||
# Store original values
|
||||
|
|
|
|||
|
|
@ -18,6 +18,20 @@ import litellm
|
|||
from litellm import main as litellm_main
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def clear_client_cache():
|
||||
"""
|
||||
Clear the HTTP client cache before each test to ensure mocks are used.
|
||||
This prevents cached real clients from being reused across tests.
|
||||
"""
|
||||
cache = getattr(litellm, "in_memory_llm_clients_cache", None)
|
||||
if cache is not None:
|
||||
cache.flush_cache()
|
||||
yield
|
||||
if cache is not None:
|
||||
cache.flush_cache()
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def add_api_keys_to_env(monkeypatch):
|
||||
monkeypatch.setenv("ANTHROPIC_API_KEY", "sk-ant-api03-1234567890")
|
||||
|
|
|
|||
|
|
@ -51,9 +51,10 @@ def test_vector_store_create_with_simple_provider_name():
|
|||
)
|
||||
|
||||
assert vector_store_provider_config is not None, "Should return a config for OpenAI"
|
||||
assert isinstance(
|
||||
vector_store_provider_config, OpenAIVectorStoreConfig
|
||||
), "Should return OpenAIVectorStoreConfig for OpenAI provider"
|
||||
# Use type name check instead of isinstance to avoid module identity issues
|
||||
# caused by sys.path manipulation in test setup
|
||||
assert type(vector_store_provider_config).__name__ == "OpenAIVectorStoreConfig", \
|
||||
f"Should return OpenAIVectorStoreConfig for OpenAI provider, got {type(vector_store_provider_config).__name__}"
|
||||
|
||||
print("✅ Test passed: Simple provider name 'openai' handled correctly")
|
||||
|
||||
|
|
@ -97,9 +98,9 @@ def test_vector_store_create_with_provider_api_type():
|
|||
)
|
||||
|
||||
assert vector_store_provider_config is not None, "Should return a config for Vertex AI"
|
||||
assert isinstance(
|
||||
vector_store_provider_config, VertexVectorStoreConfig
|
||||
), "Should return VertexVectorStoreConfig for vertex_ai provider with rag_api"
|
||||
# Use type name check instead of isinstance to avoid module identity issues
|
||||
assert type(vector_store_provider_config).__name__ == "VertexVectorStoreConfig", \
|
||||
f"Should return VertexVectorStoreConfig for vertex_ai provider with rag_api, got {type(vector_store_provider_config).__name__}"
|
||||
|
||||
print("✅ Test passed: Provider with api_type 'vertex_ai/rag_api' handled correctly")
|
||||
|
||||
|
|
@ -134,9 +135,9 @@ def test_vector_store_create_with_ragflow_provider():
|
|||
)
|
||||
|
||||
assert vector_store_provider_config is not None, "Should return a config for RAGFlow"
|
||||
assert isinstance(
|
||||
vector_store_provider_config, RAGFlowVectorStoreConfig
|
||||
), "Should return RAGFlowVectorStoreConfig for RAGFlow provider"
|
||||
# Use type name check instead of isinstance to avoid module identity issues
|
||||
assert type(vector_store_provider_config).__name__ == "RAGFlowVectorStoreConfig", \
|
||||
f"Should return RAGFlowVectorStoreConfig for RAGFlow provider, got {type(vector_store_provider_config).__name__}"
|
||||
|
||||
print("✅ Test passed: RAGFlow provider handled correctly")
|
||||
|
||||
|
|
|
|||
|
|
@ -21,6 +21,20 @@ from litellm.vector_stores.main import search
|
|||
from litellm.vector_stores.vector_store_registry import VectorStoreRegistry
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def clear_client_cache():
|
||||
"""
|
||||
Clear the HTTP client cache before each test to ensure mocks are used.
|
||||
This prevents cached real clients from being reused across tests.
|
||||
"""
|
||||
cache = getattr(litellm, "in_memory_llm_clients_cache", None)
|
||||
if cache is not None:
|
||||
cache.flush_cache()
|
||||
yield
|
||||
if cache is not None:
|
||||
cache.flush_cache()
|
||||
|
||||
|
||||
def test_get_credentials_for_vector_store():
|
||||
"""Test that get_credentials_for_vector_store returns correct credentials"""
|
||||
# Create test vector stores
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue