litellm/tests/unit/llms/anthropic/messages/conftest.py
yujonglee b1b236826a
feat(redis): serve the proxy's common Redis call with Rust stores (#50170)
* feat(redis): route the router, rate limiters and auth through the native store families

* style(redis): satisfy the python lint gates

Format the rate limit store, sort its __all__ and give the routing read batch and two tests concrete list and dict types.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(router): give the registry's default usage store a memory-only handle

RouterUsageStore now takes a handle, so the default passed None and any usage read before RustCore swapped in its store raised AttributeError.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(redis): build stores from native handles and wait for router tasks

Tests still used the pre-refactor store constructors (Store(None), handle= factories, NativeIncrement, tuple scopes). The router outcome tests counted 20 loop ticks, too few now that the allowed-fails increment resolves on the native runtime, so settle() waits for the scheduled tasks instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 06:01:11 +00:00

86 lines
2.8 KiB
Python

from dataclasses import dataclass
from typing import Final
from unittest.mock import MagicMock
import pytest
from litellm.redis.stores.connection import connect
from litellm.redis.stores.spend_counters import SpendCounterStore
from pydantic import BaseModel
import litellm.proxy.proxy_server as proxy_server
from litellm.caching.dual_cache import DualCache
from litellm.proxy._types import (
LiteLLM_EndUserTable,
LiteLLM_ProjectTableCachedObj,
LiteLLM_TeamMembership,
LiteLLM_TeamTableCachedObj,
LiteLLM_UserTable,
)
from litellm.proxy.common_utils.user_api_key_cache import (
UserApiKeyCache,
end_user_cache_key,
project_cache_key,
team_membership_reservation_cache_key,
)
from litellm.proxy.utils import ProxyLogging
from litellm.router import Router
_ProxyObject = (
LiteLLM_TeamTableCachedObj
| LiteLLM_UserTable
| LiteLLM_EndUserTable
| LiteLLM_ProjectTableCachedObj
| LiteLLM_TeamMembership
)
def _cache_key(row: _ProxyObject) -> str:
match row:
case LiteLLM_TeamTableCachedObj():
return f"team_id:{row.team_id}"
case LiteLLM_UserTable():
return row.user_id
case LiteLLM_EndUserTable():
return end_user_cache_key(row.user_id)
case LiteLLM_ProjectTableCachedObj():
return project_cache_key(row.project_id)
case LiteLLM_TeamMembership():
return team_membership_reservation_cache_key(user_id=row.user_id, team_id=row.team_id)
@dataclass(frozen=True, slots=True)
class ProxyAuthState:
"""A running proxy with a master key, so the centralized auth checks run, and its auth cache to seed."""
cache: UserApiKeyCache
monkeypatch: pytest.MonkeyPatch
def seed(self, *rows: _ProxyObject) -> None:
for row in rows:
stored: Final[BaseModel] = row
self.cache.set_cache(key=_cache_key(row), value=stored)
def serve(self, router: Router) -> None:
self.monkeypatch.setattr(proxy_server, "llm_router", router)
@pytest.fixture
def proxy_auth_state(monkeypatch: pytest.MonkeyPatch) -> ProxyAuthState:
cache: Final = UserApiKeyCache()
proxy_globals: Final = {
"master_key": "sk-test-master",
"prisma_client": MagicMock(),
"user_api_key_cache": cache,
"spend_counter_cache": DualCache(),
"spend_counter_store": SpendCounterStore(connect(None).spend_counters()),
"proxy_logging_obj": ProxyLogging(user_api_key_cache=cache),
"general_settings": {},
"user_custom_auth": None,
"litellm_proxy_admin_name": "admin",
"llm_router": None,
"llm_model_list": None,
}
for name, value in proxy_globals.items():
monkeypatch.setattr(proxy_server, name, value)
return ProxyAuthState(cache=cache, monkeypatch=monkeypatch)