fix(tests): Fix flaky container and scientific notation tests (#20650)

* fix(tests): Mock async_container_create_handler for async router test

The test was mocking container_create_handler (sync), but
router.acreate_container uses _is_async=True which calls
async_container_create_handler. This caused the test to hit
the real OpenAI API.

Fixed by using AsyncMock on async_container_create_handler.

* fix(tests): Use uuid for unique model name in scientific notation test

The test was using a static "unique" model name which could cause
conflicts when running tests in parallel (-n 16 in CI). Using uuid
ensures truly unique names to prevent test pollution.

---------

Co-authored-by: Shin <shin@openclaw.ai>
This commit is contained in:
shin-bot-litellm 2026-02-07 09:57:08 -08:00 committed by GitHub
parent 5efbed3939
commit df299d3193
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 9 deletions

View File

@ -2,7 +2,7 @@ import asyncio
import json
import os
import sys
from unittest.mock import MagicMock, patch
from unittest.mock import AsyncMock, MagicMock, patch
import httpx
import pytest
@ -335,7 +335,14 @@ class TestContainerAPI:
name="Test Container"
)
with patch.object(base_llm_http_handler, 'container_create_handler', return_value=mock_response):
# Mock async_container_create_handler since router.acreate_container
# uses _is_async=True which calls the async handler
with patch.object(
base_llm_http_handler,
'async_container_create_handler',
new_callable=AsyncMock,
return_value=mock_response
):
result = await router.acreate_container(
name="Test Container",
custom_llm_provider="openai"

View File

@ -2283,18 +2283,14 @@ def test_register_model_with_scientific_notation():
"""
Test that the register_model function can handle scientific notation in the model name.
"""
# Use a unique model name to avoid conflicts with other tests
test_model_name = "test-scientific-notation-model-unique-12345"
import uuid
# Clean up any pre-existing entry and clear caches
if test_model_name in litellm.model_cost:
del litellm.model_cost[test_model_name]
# Use a truly unique model name with uuid to avoid conflicts when tests run in parallel
test_model_name = f"test-scientific-notation-model-{uuid.uuid4().hex[:12]}"
# Clear LRU caches that might have stale data
from litellm.utils import (
_cached_get_model_info_helper,
_invalidate_model_cost_lowercase_map,
get_model_info,
)
_invalidate_model_cost_lowercase_map()