litellm/tests/router_unit_tests/test_router_acancel_batch.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

128 lines
3.6 KiB
Python

"""
Test router.acancel_batch() functionality
This ensures the router's batch cancellation method has test coverage.
"""
import pytest
from unittest.mock import patch, AsyncMock, MagicMock
from litellm import Router
import litellm
from litellm.types.utils import CredentialItem
@pytest.fixture
def router():
"""Create a router with a mock deployment"""
return Router(
model_list=[
{
"model_name": "gpt-5.5",
"litellm_params": {
"model": "gpt-5.5",
"api_key": "fake-key",
},
}
]
)
@pytest.mark.asyncio
async def test_router_acancel_batch(router):
"""Test that router.acancel_batch() calls litellm.acancel_batch with correct params"""
mock_response = MagicMock()
mock_response.id = "batch_123"
mock_response.status = "cancelled"
with patch.object(litellm, "acancel_batch", new_callable=AsyncMock) as mock_cancel:
mock_cancel.return_value = mock_response
# This tests that the router method exists and can be called
# The actual API call is mocked
response = await router.acancel_batch(
model="gpt-5.5",
batch_id="batch_123",
)
# Verify the mock was called
assert mock_cancel.called
assert response.id == "batch_123"
assert response.status == "cancelled"
@pytest.mark.asyncio
async def test_router_acancel_batch_resolves_credential_name():
litellm.credential_list = [
CredentialItem(
credential_name="openai-test-credential",
credential_info={"custom_llm_provider": "openai"},
credential_values={"api_key": "resolved-openai-key"},
)
]
router = Router(
model_list=[
{
"model_name": "gpt-5.5",
"litellm_params": {
"model": "openai/gpt-5.5",
"litellm_credential_name": "openai-test-credential",
},
}
]
)
mock_response = MagicMock()
mock_response.id = "batch_123"
mock_response.status = "cancelled"
try:
with patch.object(
litellm, "acancel_batch", new_callable=AsyncMock
) as mock_cancel:
mock_cancel.return_value = mock_response
await router.acancel_batch(
model="gpt-5.5",
batch_id="batch_123",
)
call_kwargs = mock_cancel.call_args.kwargs
assert call_kwargs["api_key"] == "resolved-openai-key"
assert "litellm_credential_name" not in call_kwargs
finally:
litellm.credential_list = []
@pytest.mark.asyncio
async def test_router_acancel_batch_removes_unresolved_credential_name():
router = Router(
model_list=[
{
"model_name": "gpt-5.5",
"litellm_params": {
"model": "openai/gpt-5.5",
"litellm_credential_name": "missing-openai-credential",
},
}
]
)
mock_response = MagicMock()
mock_response.id = "batch_123"
mock_response.status = "cancelled"
with (
patch.object(
router, "get_deployment_credentials_with_provider", return_value=None
),
patch.object(litellm, "acancel_batch", new_callable=AsyncMock) as mock_cancel,
):
mock_cancel.return_value = mock_response
await router.acancel_batch(
model="gpt-5.5",
batch_id="batch_123",
)
call_kwargs = mock_cancel.call_args.kwargs
assert "litellm_credential_name" not in call_kwargs