From 17da1ab01d03006b3e421e8722ae6aa5c433bac7 Mon Sep 17 00:00:00 2001 From: Ishaan Jaffer Date: Sat, 21 Feb 2026 13:44:55 -0800 Subject: [PATCH] fix(tests): isolate litellm.cache and CLI env vars in flaky tests - TestSpendLogsPayload: save/restore litellm.cache in setup_method/teardown_method so tests that run after a cache-setting test don't see a non-None cache and get a hash instead of "Cache OFF" in the cache_key field - test_use_prisma_db_push_flag_behavior: apply clean_env pattern (strip DATABASE_URL/DIRECT_URL, then set DATABASE_URL to test value) inside the with block instead of using @patch.dict decorator, matching the pattern from test_skip_server_startup to avoid Click 8.3.x StreamMixer stream lifecycle issues in CI --- .../test_spend_management_endpoints.py | 3 +++ tests/test_litellm/proxy/test_proxy_cli.py | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py b/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py index 97d1d4a2f38..5ca10df9f89 100644 --- a/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py +++ b/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py @@ -1231,9 +1231,12 @@ async def _wait_for_mock_call(mock, timeout=10, interval=0.1): class TestSpendLogsPayload: def setup_method(self): self._original_callbacks = litellm.callbacks[:] + self._original_cache = litellm.cache + litellm.cache = None def teardown_method(self): litellm.callbacks = self._original_callbacks + litellm.cache = self._original_cache @pytest.mark.asyncio async def test_spend_logs_payload_e2e(self): diff --git a/tests/test_litellm/proxy/test_proxy_cli.py b/tests/test_litellm/proxy/test_proxy_cli.py index e8a60e595ca..236080aab7e 100644 --- a/tests/test_litellm/proxy/test_proxy_cli.py +++ b/tests/test_litellm/proxy/test_proxy_cli.py @@ -586,9 +586,6 @@ class TestHealthAppFactory: @patch("litellm.proxy.db.prisma_client.PrismaManager.setup_database") @patch("litellm.proxy.db.check_migration.check_prisma_schema_diff") @patch("litellm.proxy.db.prisma_client.should_update_prisma_schema") - @patch.dict( - os.environ, {"DATABASE_URL": "postgresql://test:test@localhost:5432/test"} - ) def test_use_prisma_db_push_flag_behavior( self, mock_should_update_schema, @@ -616,7 +613,19 @@ class TestHealthAppFactory: save_worker_config=MagicMock(), ) + # Strip DIRECT_URL and set DATABASE_URL to a test value so Click's + # CliRunner doesn't encounter real DB env vars (causes stream lifecycle + # issues with Click 8.3.x in CI environments). + clean_env = { + k: v + for k, v in os.environ.items() + if k not in ("DATABASE_URL", "DIRECT_URL") + } + clean_env["DATABASE_URL"] = "postgresql://test:test@localhost:5432/test" + with patch.dict( + os.environ, clean_env, clear=True + ), patch.dict( "sys.modules", { "proxy_server": mock_proxy_module,