litellm/tests/local_testing/test_prometheus_service.py
devin-ai-integration[bot] fa2c8984ba
test: move the unit half of 126 mixed legacy files into tests/unit (#45090)
* test: move the unit half of 126 mixed legacy files into tests/unit

* test: restore litellm globals that moved tests set

* test: finalize migration test cleanup

* test: restore original bodies of moved legacy tests

The move into tests/unit had rewritten 612 test bodies, and some of the rewrites dropped assertions. Each moved test now carries its original body from the legacy file, with only the imports, helpers, fake provider credentials and monkeypatched env it needs to run under tests/unit

test_timeout_streaming goes back to tests/local_testing because it needs the fake OpenAI endpoint server. The image payload fixture moves with its only user, and two tests that leaked global state (a registered model cost entry and queued logging tasks) are now isolated

* test: drop module imports shadowed by restored local imports

* test: assert on LiteLLM output in no-assertion moved tests and isolate leaks

Twenty no-assertion candidates get one assertion on the value LiteLLM returns, with the original lines unchanged. Four tests go back to their legacy files because they only check types or imports, write into the working directory, or cannot assert without a body change

Two moved tests leaked globals into later tests in the same worker, so monkeypatch fixtures now restore the retry-after header parser and the end user cost tracking flags

* test: drain queued logging tasks before the Phoenix span test

The moved Phoenix test counted spans from logging tasks that earlier tests had queued, so the drain fixture moves to tests/unit/conftest.py and both it and the Datadog batch test use it. test_factory_function goes back to its legacy file because its returned wrapper calls the real Assistants API and cannot be asserted on without a body change

---------

Co-authored-by: yuneng <yuneng@berri.ai>
2026-10-07 14:07:43 -07:00

68 lines
1.9 KiB
Python

# What is this?
## Unit Tests for prometheus service monitoring
import asyncio
import pytest
from litellm import acompletion, Cache
from litellm._service_logger import ServiceLogging
import litellm
"""
- Check if it receives a call when redis is used
- Check if it fires messages accordingly
"""
@pytest.mark.flaky(retries=3, delay=5)
@pytest.mark.asyncio
async def test_completion_with_caching():
"""
- Run completion with caching
- Assert success callback gets called
"""
litellm.set_verbose = True
litellm.cache = Cache(type="redis")
litellm.service_callback = ["prometheus_system"]
sl = ServiceLogging(mock_testing=True)
sl.prometheusServicesLogger.mock_testing = True
litellm.cache.cache.service_logger_obj = sl
messages = [{"role": "user", "content": "Hey, how's it going?"}]
response1 = await acompletion(
model="gpt-3.5-turbo", messages=messages, caching=True
)
response1 = await acompletion(
model="gpt-3.5-turbo", messages=messages, caching=True
)
assert sl.mock_testing_async_success_hook > 0
assert sl.prometheusServicesLogger.mock_testing_success_calls > 0
assert sl.mock_testing_sync_failure_hook == 0
assert sl.mock_testing_async_failure_hook == 0
@pytest.mark.asyncio
async def test_completion_with_caching_bad_call():
"""
- Run completion with caching (incorrect credentials)
- Assert failure callback gets called
"""
litellm.set_verbose = True
try:
from litellm.caching.caching import RedisCache
litellm.service_callback = ["prometheus_system"]
sl = ServiceLogging(mock_testing=True)
RedisCache(host="hello-world", service_logger_obj=sl)
except Exception as e:
print(f"Receives exception = {str(e)}")
await asyncio.sleep(5)
assert sl.mock_testing_async_failure_hook > 0
assert sl.mock_testing_async_success_hook == 0
assert sl.mock_testing_sync_success_hook == 0