litellm/tests/test_litellm/test_router_google_genai.py
yuneng-jiang 6a0d03914c
test: drop the cwd-relative sys.path.insert calls from the test suite (#37802)
* test: drop the cwd-relative sys.path.insert calls from the test suite

TQ003 stands at 1,077 across 1,058 files, and 1,015 of them are the same shape:
sys.path.insert(0, os.path.abspath("../..")) and its deeper siblings. The
argument resolves against the working directory rather than the file, so from
the repo root, where every job runs pytest, it inserts the directory two levels
above the checkout. It has never pointed at litellm. The package is installed
into the environment anyway, which is what actually makes the import work, and
what the rule's message has said all along.

Removing them leaves 1,634 imports of sys and os with no remaining reference,
and those go too, except where another test module imports the name back out of
the file. The rest of TQ003 is 62 call sites that resolve against __file__ or a
variable, which are a different question and are left alone.

Collection is identical either way: 45,871 tests and the same 51 pre-existing
collection errors before and after, and ruff reports no new undefined name.

* test: drop the duplicate imports the sys.path sweep exposed to F811

* test(pre-call-utils): restore the os import the new bedrock tests need
2026-08-22 09:25:58 -07:00

96 lines
3.2 KiB
Python

#!/usr/bin/env python3
"""
Test to verify the new Google GenAI router methods
"""
import asyncio
from unittest.mock import AsyncMock, patch
import pytest
import litellm
from litellm.types.utils import ModelResponse
@pytest.mark.asyncio
async def test_router_agenerate_content_method():
"""Test that the new agenerate_content method in Router works correctly"""
# Create a router instance
router = litellm.Router(
model_list=[
{
"model_name": "test-model",
"litellm_params": {
"model": "gpt-3.5-turbo",
},
}
]
)
# Create a mock response in Google GenAI format
mock_response = {
"candidates": [{"content": {"parts": [{"text": "Hello, world!"}]}}]
}
# Mock the router's underlying agenerate_content method to return a mock response
with patch.object(
router, "agenerate_content", new=AsyncMock(return_value=mock_response)
) as mock_agenerate_content:
# Call the agenerate_content method
response = await router.agenerate_content(
model="test-model",
contents=[{"role": "user", "parts": [{"text": "Hello"}]}],
)
# Verify that router.agenerate_content was called with correct parameters
mock_agenerate_content.assert_called_once()
call_args = mock_agenerate_content.call_args
assert call_args[1]["model"] == "test-model"
assert call_args[1]["contents"] == [
{"role": "user", "parts": [{"text": "Hello"}]}
]
# Verify that the response is the mock response we created
assert response == mock_response
@pytest.mark.asyncio
async def test_router_aadapter_generate_content_method():
"""Test that the new aadapter_generate_content method in Router works correctly"""
# Create a router instance
router = litellm.Router(
model_list=[
{
"model_name": "test-model",
"litellm_params": {
"model": "gpt-3.5-turbo",
},
}
]
)
# Create a mock response in Google GenAI format
mock_response = {
"candidates": [{"content": {"parts": [{"text": "Hello, world!"}]}}]
}
# Mock the router's underlying aadapter_generate_content method to return a mock response
with patch.object(
router, "aadapter_generate_content", new=AsyncMock(return_value=mock_response)
) as mock_aadapter_generate_content:
# Call the aadapter_generate_content method
response = await router.aadapter_generate_content(
model="test-model",
contents=[{"role": "user", "parts": [{"text": "Hello"}]}],
)
# Verify that router.aadapter_generate_content was called with correct parameters
mock_aadapter_generate_content.assert_called_once()
call_args = mock_aadapter_generate_content.call_args
assert call_args[1]["model"] == "test-model"
assert call_args[1]["contents"] == [
{"role": "user", "parts": [{"text": "Hello"}]}
]
# Verify that the response is the mock response we created
assert response == mock_response