mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-09 03:18:44 +00:00
fix(test): use patch.object for container API tests to fix mock injection
## Problem
test_retrieve_container_basic tests were failing because mocks weren't
being applied correctly. The tests used string-based patching:
patch('litellm.containers.main.base_llm_http_handler')
But base_llm_http_handler is imported at module level, so the mock wasn't
intercepting the actual handler calls, resulting in real HTTP requests
to OpenAI API.
## Solution
Use patch.object() to directly mock methods on the imported handler
instance. Import base_llm_http_handler in the test file and patch like:
patch.object(base_llm_http_handler, 'container_retrieve_handler', ...)
This ensures the mock is applied to the actual object being used,
regardless of import order or caching.
This commit is contained in:
parent
9ddcdbcaf7
commit
ba736c4b83
1 changed files with 25 additions and 47 deletions
|
|
@ -22,6 +22,7 @@ from litellm.containers.main import (
|
|||
list_containers,
|
||||
retrieve_container,
|
||||
)
|
||||
from litellm.main import base_llm_http_handler
|
||||
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLogging
|
||||
from litellm.llms.openai.containers.transformation import OpenAIContainerConfig
|
||||
from litellm.router import Router
|
||||
|
|
@ -63,9 +64,7 @@ class TestContainerAPI:
|
|||
name="Test Container"
|
||||
)
|
||||
|
||||
with patch('litellm.containers.main.base_llm_http_handler') as mock_handler:
|
||||
mock_handler.container_create_handler.return_value = mock_response
|
||||
|
||||
with patch.object(base_llm_http_handler, 'container_create_handler', return_value=mock_response):
|
||||
response = create_container(
|
||||
name="Test Container",
|
||||
custom_llm_provider="openai"
|
||||
|
|
@ -89,9 +88,7 @@ class TestContainerAPI:
|
|||
name="Expiring Container"
|
||||
)
|
||||
|
||||
with patch('litellm.containers.main.base_llm_http_handler') as mock_handler:
|
||||
mock_handler.container_create_handler.return_value = mock_response
|
||||
|
||||
with patch.object(base_llm_http_handler, 'container_create_handler', return_value=mock_response):
|
||||
response = create_container(
|
||||
name="Expiring Container",
|
||||
expires_after={"anchor": "last_active_at", "minutes": 30},
|
||||
|
|
@ -113,9 +110,7 @@ class TestContainerAPI:
|
|||
name="Container with Files"
|
||||
)
|
||||
|
||||
with patch('litellm.containers.main.base_llm_http_handler') as mock_handler:
|
||||
mock_handler.container_create_handler.return_value = mock_response
|
||||
|
||||
with patch.object(base_llm_http_handler, 'container_create_handler', return_value=mock_response):
|
||||
response = create_container(
|
||||
name="Container with Files",
|
||||
file_ids=["file_123", "file_456"],
|
||||
|
|
@ -137,9 +132,7 @@ class TestContainerAPI:
|
|||
name="Async Test Container"
|
||||
)
|
||||
|
||||
with patch('litellm.containers.main.base_llm_http_handler') as mock_handler:
|
||||
mock_handler.container_create_handler.return_value = mock_response
|
||||
|
||||
with patch.object(base_llm_http_handler, 'container_create_handler', return_value=mock_response):
|
||||
response = await acreate_container(
|
||||
name="Async Test Container",
|
||||
custom_llm_provider="openai"
|
||||
|
|
@ -171,9 +164,7 @@ class TestContainerAPI:
|
|||
has_more=False
|
||||
)
|
||||
|
||||
with patch('litellm.containers.main.base_llm_http_handler') as mock_handler:
|
||||
mock_handler.container_list_handler.return_value = mock_response
|
||||
|
||||
with patch.object(base_llm_http_handler, 'container_list_handler', return_value=mock_response):
|
||||
response = await alist_containers(
|
||||
custom_llm_provider="openai"
|
||||
)
|
||||
|
|
@ -208,9 +199,7 @@ class TestContainerAPI:
|
|||
name=container_name
|
||||
)
|
||||
|
||||
with patch('litellm.containers.main.base_llm_http_handler') as mock_handler:
|
||||
mock_handler.container_retrieve_handler.return_value = mock_response
|
||||
|
||||
with patch.object(base_llm_http_handler, 'container_retrieve_handler', return_value=mock_response) as mock_method:
|
||||
# Act: Call retrieve_container
|
||||
response = retrieve_container(
|
||||
container_id=container_id,
|
||||
|
|
@ -218,8 +207,8 @@ class TestContainerAPI:
|
|||
)
|
||||
|
||||
# Assert: Verify the handler was called correctly
|
||||
mock_handler.container_retrieve_handler.assert_called_once()
|
||||
call_kwargs = mock_handler.container_retrieve_handler.call_args.kwargs
|
||||
mock_method.assert_called_once()
|
||||
call_kwargs = mock_method.call_args.kwargs
|
||||
assert call_kwargs["container_id"] == container_id
|
||||
|
||||
# Assert: Verify response structure and content
|
||||
|
|
@ -245,9 +234,7 @@ class TestContainerAPI:
|
|||
name="Async Retrieved Container"
|
||||
)
|
||||
|
||||
with patch('litellm.containers.main.base_llm_http_handler') as mock_handler:
|
||||
mock_handler.container_retrieve_handler.return_value = mock_response
|
||||
|
||||
with patch.object(base_llm_http_handler, 'container_retrieve_handler', return_value=mock_response):
|
||||
response = await aretrieve_container(
|
||||
container_id=container_id,
|
||||
custom_llm_provider="openai"
|
||||
|
|
@ -265,9 +252,7 @@ class TestContainerAPI:
|
|||
deleted=True
|
||||
)
|
||||
|
||||
with patch('litellm.containers.main.base_llm_http_handler') as mock_handler:
|
||||
mock_handler.container_delete_handler.return_value = mock_response
|
||||
|
||||
with patch.object(base_llm_http_handler, 'container_delete_handler', return_value=mock_response):
|
||||
response = delete_container(
|
||||
container_id=container_id,
|
||||
custom_llm_provider="openai"
|
||||
|
|
@ -288,9 +273,7 @@ class TestContainerAPI:
|
|||
deleted=True
|
||||
)
|
||||
|
||||
with patch('litellm.containers.main.base_llm_http_handler') as mock_handler:
|
||||
mock_handler.container_delete_handler.return_value = mock_response
|
||||
|
||||
with patch.object(base_llm_http_handler, 'container_delete_handler', return_value=mock_response):
|
||||
response = await adelete_container(
|
||||
container_id=container_id,
|
||||
custom_llm_provider="openai"
|
||||
|
|
@ -302,9 +285,7 @@ class TestContainerAPI:
|
|||
|
||||
def test_create_container_error_handling(self):
|
||||
"""Test error handling in container creation."""
|
||||
with patch('litellm.containers.main.base_llm_http_handler') as mock_handler:
|
||||
mock_handler.container_create_handler.side_effect = Exception("API Error")
|
||||
|
||||
with patch.object(base_llm_http_handler, 'container_create_handler', side_effect=Exception("API Error")):
|
||||
with pytest.raises(Exception):
|
||||
create_container(
|
||||
name="Error Test Container",
|
||||
|
|
@ -313,21 +294,20 @@ class TestContainerAPI:
|
|||
|
||||
def test_container_provider_config_retrieval(self):
|
||||
"""Test that provider config is retrieved correctly."""
|
||||
mock_response = ContainerObject(
|
||||
id="cntr_config_test",
|
||||
object="container",
|
||||
created_at=1747857508,
|
||||
status="running",
|
||||
expires_after={"anchor": "last_active_at", "minutes": 20},
|
||||
last_active_at=1747857508,
|
||||
name="Config Test"
|
||||
)
|
||||
|
||||
with patch('litellm.containers.main.ProviderConfigManager') as mock_config_manager:
|
||||
mock_config_manager.get_provider_container_config.return_value = OpenAIContainerConfig()
|
||||
|
||||
with patch('litellm.containers.main.base_llm_http_handler') as mock_handler:
|
||||
mock_response = ContainerObject(
|
||||
id="cntr_config_test",
|
||||
object="container",
|
||||
created_at=1747857508,
|
||||
status="running",
|
||||
expires_after={"anchor": "last_active_at", "minutes": 20},
|
||||
last_active_at=1747857508,
|
||||
name="Config Test"
|
||||
)
|
||||
mock_handler.container_create_handler.return_value = mock_response
|
||||
|
||||
with patch.object(base_llm_http_handler, 'container_create_handler', return_value=mock_response):
|
||||
response = create_container(
|
||||
name="Config Test",
|
||||
custom_llm_provider="openai"
|
||||
|
|
@ -355,9 +335,7 @@ class TestContainerAPI:
|
|||
name="Test Container"
|
||||
)
|
||||
|
||||
with patch('litellm.containers.main.base_llm_http_handler') as mock_handler:
|
||||
mock_handler.container_create_handler.return_value = mock_response
|
||||
|
||||
with patch.object(base_llm_http_handler, 'container_create_handler', return_value=mock_response):
|
||||
result = await router.acreate_container(
|
||||
name="Test Container",
|
||||
custom_llm_provider="openai"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue