test(proxy): cover malformed string general_settings at startup

The JSONDecodeError arm of the startup store_model_in_db lookup had no
test, which left codecov/patch just under target. A non-JSON string row
is now asserted to leave store_model_in_db off without tripping the
outer failure path.
This commit is contained in:
pjdurden 2026-10-12 15:27:08 -05:00
parent b8851493ff
commit 74faded4a0
No known key found for this signature in database

View file

@ -1253,6 +1253,53 @@ async def test_initialize_scheduled_jobs_normalizes_string_general_settings(monk
mock_proxy_config.init_mcp_servers_from_db.assert_not_called()
@pytest.mark.asyncio
async def test_initialize_scheduled_jobs_ignores_malformed_string_general_settings(monkeypatch):
"""
A string-valued general_settings row that is not valid JSON must be treated
as empty, leaving store_model_in_db off, instead of failing the lookup.
"""
import types
monkeypatch.delenv("DISABLE_PRISMA_SCHEMA_UPDATE", raising=False)
monkeypatch.delenv("STORE_MODEL_IN_DB", raising=False)
from litellm.proxy.proxy_server import ProxyStartupEvent
from litellm.proxy.utils import ProxyLogging
record = types.SimpleNamespace(param_value="{not json")
find_first = AsyncMock(return_value=record)
mock_prisma_client = MagicMock()
mock_prisma_client.db.litellm_config.find_first = find_first
mock_prisma_client.attempt_db_reconnect = AsyncMock(return_value=True)
mock_proxy_logging = MagicMock(spec=ProxyLogging)
mock_proxy_logging.slack_alerting_instance = MagicMock()
mock_proxy_logging.db_spend_update_writer = MagicMock()
mock_proxy_config = _mock_scheduled_proxy_config()
with (
patch("litellm.proxy.proxy_server.proxy_config", mock_proxy_config),
patch("litellm.proxy.proxy_server.store_model_in_db", False),
patch("litellm.proxy.proxy_server.verbose_proxy_logger") as mock_logger,
):
await ProxyStartupEvent.initialize_scheduled_background_jobs(
general_settings={},
prisma_client=mock_prisma_client,
proxy_budget_rescheduler_min_time=1,
proxy_budget_rescheduler_max_time=2,
proxy_batch_write_at=5,
proxy_logging_obj=mock_proxy_logging,
)
assert find_first.await_count == 1
assert not any(
"Failed to check DB for store_model_in_db" in str(call.args[0]) for call in mock_logger.debug.call_args_list
)
mock_proxy_config.add_deployment.assert_not_called()
mock_proxy_config.init_mcp_servers_from_db.assert_awaited_once()
@pytest.mark.asyncio
async def test_init_mcp_servers_from_db_respects_supported_db_objects(monkeypatch):
"""