Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
141 changes: 28 additions & 113 deletions tests/test_litellm/caching/test_redis_semantic_cache.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
from collections.abc import Iterator
from contextlib import contextmanager
from importlib import import_module
import sys
from unittest.mock import AsyncMock, MagicMock, patch

import pytest


@contextmanager
def _fake_redisvl_modules(semantic_cache_mock: MagicMock, custom_vectorizer_mock: MagicMock) -> Iterator[None]:
with pytest.MonkeyPatch.context() as mp:
mp.setitem(sys.modules, "redisvl.extensions.llmcache", MagicMock(SemanticCache=semantic_cache_mock))
mp.setitem(sys.modules, "redisvl.utils.vectorize", MagicMock(CustomTextVectorizer=custom_vectorizer_mock))
yield


# Tests for RedisSemanticCache
def test_redis_semantic_cache_initialization(monkeypatch):
# Mock the redisvl import
semantic_cache_mock = MagicMock()
with patch.dict(
"sys.modules",
{
"redisvl.extensions.llmcache": MagicMock(SemanticCache=semantic_cache_mock),
"redisvl.utils.vectorize": MagicMock(CustomTextVectorizer=MagicMock()),
},
):
with _fake_redisvl_modules(semantic_cache_mock, MagicMock()):
from litellm.caching.redis_semantic_cache import RedisSemanticCache

# Set environment variables
Expand Down Expand Up @@ -44,15 +47,7 @@ def test_redis_semantic_cache_get_cache(monkeypatch):
semantic_cache_mock = MagicMock()
custom_vectorizer_mock = MagicMock()

with patch.dict(
"sys.modules",
{
"redisvl.extensions.llmcache": MagicMock(SemanticCache=semantic_cache_mock),
"redisvl.utils.vectorize": MagicMock(
CustomTextVectorizer=custom_vectorizer_mock
),
},
):
with _fake_redisvl_modules(semantic_cache_mock, custom_vectorizer_mock):
from litellm.caching.redis_semantic_cache import RedisSemanticCache

# Set environment variables
Expand Down Expand Up @@ -110,15 +105,7 @@ def test_redis_semantic_cache_rejects_unscoped_cache_hit(monkeypatch):
semantic_cache_mock = MagicMock()
custom_vectorizer_mock = MagicMock()

with patch.dict(
"sys.modules",
{
"redisvl.extensions.llmcache": MagicMock(SemanticCache=semantic_cache_mock),
"redisvl.utils.vectorize": MagicMock(
CustomTextVectorizer=custom_vectorizer_mock
),
},
):
with _fake_redisvl_modules(semantic_cache_mock, custom_vectorizer_mock):
from litellm.caching.redis_semantic_cache import RedisSemanticCache

monkeypatch.setenv("REDIS_HOST", "localhost")
Expand Down Expand Up @@ -162,15 +149,7 @@ def test_redis_semantic_cache_set_cache_stores_cache_key_filter(monkeypatch):
semantic_cache_mock = MagicMock()
custom_vectorizer_mock = MagicMock()

with patch.dict(
"sys.modules",
{
"redisvl.extensions.llmcache": MagicMock(SemanticCache=semantic_cache_mock),
"redisvl.utils.vectorize": MagicMock(
CustomTextVectorizer=custom_vectorizer_mock
),
},
):
with _fake_redisvl_modules(semantic_cache_mock, custom_vectorizer_mock):
from litellm.caching.redis_semantic_cache import RedisSemanticCache

monkeypatch.setenv("REDIS_HOST", "localhost")
Expand Down Expand Up @@ -210,15 +189,7 @@ def test_redis_semantic_cache_uses_isolated_index_for_old_schema(monkeypatch):
)
custom_vectorizer_mock = MagicMock()

with patch.dict(
"sys.modules",
{
"redisvl.extensions.llmcache": MagicMock(SemanticCache=semantic_cache_mock),
"redisvl.utils.vectorize": MagicMock(
CustomTextVectorizer=custom_vectorizer_mock
),
},
):
with _fake_redisvl_modules(semantic_cache_mock, custom_vectorizer_mock):
from litellm.caching.redis_semantic_cache import RedisSemanticCache

monkeypatch.setenv("REDIS_HOST", "localhost")
Expand Down Expand Up @@ -252,15 +223,7 @@ def test_redis_semantic_cache_overwrites_stale_isolated_index(monkeypatch):
)
custom_vectorizer_mock = MagicMock()

with patch.dict(
"sys.modules",
{
"redisvl.extensions.llmcache": MagicMock(SemanticCache=semantic_cache_mock),
"redisvl.utils.vectorize": MagicMock(
CustomTextVectorizer=custom_vectorizer_mock
),
},
):
with _fake_redisvl_modules(semantic_cache_mock, custom_vectorizer_mock):
from litellm.caching.redis_semantic_cache import RedisSemanticCache

monkeypatch.setenv("REDIS_HOST", "localhost")
Expand Down Expand Up @@ -292,15 +255,7 @@ def test_redis_semantic_cache_reraises_unexpected_isolated_index_error(monkeypat
)
custom_vectorizer_mock = MagicMock()

with patch.dict(
"sys.modules",
{
"redisvl.extensions.llmcache": MagicMock(SemanticCache=semantic_cache_mock),
"redisvl.utils.vectorize": MagicMock(
CustomTextVectorizer=custom_vectorizer_mock
),
},
):
with _fake_redisvl_modules(semantic_cache_mock, custom_vectorizer_mock):
from litellm.caching.redis_semantic_cache import RedisSemanticCache

monkeypatch.setenv("REDIS_HOST", "localhost")
Expand Down Expand Up @@ -369,15 +324,15 @@ def __init__(self, field_name):
def __eq__(self, value):
return (self.field_name, value)

with patch.dict("sys.modules", {"redisvl.query.filter": MagicMock(Tag=FakeTag)}):
from litellm.caching.redis_semantic_cache import RedisSemanticCache
monkeypatch.setitem(sys.modules, "redisvl.query.filter", MagicMock(Tag=FakeTag))
from litellm.caching.redis_semantic_cache import RedisSemanticCache

redis_semantic_cache = RedisSemanticCache.__new__(RedisSemanticCache)
redis_semantic_cache = RedisSemanticCache.__new__(RedisSemanticCache)

assert redis_semantic_cache._get_cache_key_filter_expression("test_key") == (
RedisSemanticCache.CACHE_KEY_FIELD_NAME,
"test_key",
)
assert redis_semantic_cache._get_cache_key_filter_expression("test_key") == (
RedisSemanticCache.CACHE_KEY_FIELD_NAME,
"test_key",
)


@pytest.mark.asyncio
Expand All @@ -386,15 +341,7 @@ async def test_redis_semantic_cache_async_get_cache(monkeypatch):
semantic_cache_mock = MagicMock()
custom_vectorizer_mock = MagicMock()

with patch.dict(
"sys.modules",
{
"redisvl.extensions.llmcache": MagicMock(SemanticCache=semantic_cache_mock),
"redisvl.utils.vectorize": MagicMock(
CustomTextVectorizer=custom_vectorizer_mock
),
},
):
with _fake_redisvl_modules(semantic_cache_mock, custom_vectorizer_mock):
from litellm.caching.redis_semantic_cache import RedisSemanticCache

# Set environment variables
Expand Down Expand Up @@ -449,15 +396,7 @@ async def test_redis_semantic_cache_async_get_cache_rejects_unscoped_hit(monkeyp
semantic_cache_mock = MagicMock()
custom_vectorizer_mock = MagicMock()

with patch.dict(
"sys.modules",
{
"redisvl.extensions.llmcache": MagicMock(SemanticCache=semantic_cache_mock),
"redisvl.utils.vectorize": MagicMock(
CustomTextVectorizer=custom_vectorizer_mock
),
},
):
with _fake_redisvl_modules(semantic_cache_mock, custom_vectorizer_mock):
from litellm.caching.redis_semantic_cache import RedisSemanticCache

monkeypatch.setenv("REDIS_HOST", "localhost")
Expand Down Expand Up @@ -499,15 +438,7 @@ async def test_redis_semantic_cache_async_set_cache_stores_cache_key_filter(
semantic_cache_mock = MagicMock()
custom_vectorizer_mock = MagicMock()

with patch.dict(
"sys.modules",
{
"redisvl.extensions.llmcache": MagicMock(SemanticCache=semantic_cache_mock),
"redisvl.utils.vectorize": MagicMock(
CustomTextVectorizer=custom_vectorizer_mock
),
},
):
with _fake_redisvl_modules(semantic_cache_mock, custom_vectorizer_mock):
from litellm.caching.redis_semantic_cache import RedisSemanticCache

monkeypatch.setenv("REDIS_HOST", "localhost")
Expand Down Expand Up @@ -1255,15 +1186,7 @@ def test_redis_init_defers_redisvl_construction(monkeypatch):
semantic_cache_mock = MagicMock()
custom_vectorizer_mock = MagicMock()

with patch.dict(
"sys.modules",
{
"redisvl.extensions.llmcache": MagicMock(SemanticCache=semantic_cache_mock),
"redisvl.utils.vectorize": MagicMock(
CustomTextVectorizer=custom_vectorizer_mock
),
},
):
with _fake_redisvl_modules(semantic_cache_mock, custom_vectorizer_mock):
from litellm.caching.redis_semantic_cache import RedisSemanticCache

monkeypatch.setenv("REDIS_HOST", "localhost")
Expand Down Expand Up @@ -1291,15 +1214,7 @@ def test_redis_failed_llmcache_build_is_not_memoized(monkeypatch):
)
custom_vectorizer_mock = MagicMock()

with patch.dict(
"sys.modules",
{
"redisvl.extensions.llmcache": MagicMock(SemanticCache=semantic_cache_mock),
"redisvl.utils.vectorize": MagicMock(
CustomTextVectorizer=custom_vectorizer_mock
),
},
):
with _fake_redisvl_modules(semantic_cache_mock, custom_vectorizer_mock):
from litellm.caching.redis_semantic_cache import RedisSemanticCache

monkeypatch.setenv("REDIS_HOST", "localhost")
Expand Down
31 changes: 15 additions & 16 deletions tests/test_litellm/integrations/test_langsmith_init.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import os
from unittest.mock import MagicMock, patch
from unittest.mock import AsyncMock, MagicMock, patch

import pytest

Expand Down Expand Up @@ -154,24 +155,22 @@ def test_start_periodic_flush_task_returns_none_without_running_loop(
assert logger._start_periodic_flush_task() is None
mock_get_running_loop.assert_called_once()

@patch("asyncio.get_running_loop")
def test_langsmith_init_starts_periodic_flush_with_running_loop(
self, mock_get_running_loop
):
@pytest.mark.asyncio
async def test_langsmith_init_starts_periodic_flush_with_running_loop(self):
"""Test that init schedules periodic flush when a running loop exists."""
mock_loop = MagicMock()
mock_task = MagicMock()
mock_loop.create_task.return_value = mock_task
mock_get_running_loop.return_value = mock_loop

logger = LangsmithLogger(
langsmith_api_key="test-key", langsmith_project="test-project"
langsmith_api_key="test-key", langsmith_project="test-project", flush_interval=0.01
)

assert logger._flush_task == mock_task
mock_loop.create_task.assert_called_once()
scheduled_coro = mock_loop.create_task.call_args.args[0]
scheduled_coro.close()
batch_sent = asyncio.Event()
logger.async_send_batch = AsyncMock(side_effect=batch_sent.set)
logger.log_queue.append({"id": "run-id"})

flush_task = logger._flush_task
assert isinstance(flush_task, asyncio.Task)
await asyncio.wait_for(batch_sent.wait(), timeout=5)
flush_task.cancel()
with pytest.raises(asyncio.CancelledError):
await flush_task

@pytest.mark.asyncio
async def test_async_log_success_event_lazily_starts_periodic_flush(self):
Expand Down
Loading