mirror of
https://github.com/BerriAI/litellm.git
synced 2026-08-28 05:25:59 +00:00
`pytest.raises(Exception)` with no `match=` passes on any error that broad. A TypeError from a refactor, a botched fixture, an import that moved: all of them read as the rejection the test claims to police, so the test goes green for the wrong reason and stays green after the behaviour it guards is gone. PT011 closes that gap for the 317 sites B017 could not reach, because B017 only fires on a single-statement body with no `as e` binding. Each pattern here is the message the code actually raised, recorded by running the sites under a plugin that logged the concrete type and text per call site, so the assertions describe observed behaviour rather than a guess. Where a site raises more than one message across its parametrize cases, the pattern is an alternation of what was seen; where the exception carries an empty `str()` and puts the text on `.message`, the site keeps a narrow `noqa` with the reason. PT014 removes four parametrize cases that were listed twice. The duplicate re-runs an assertion that already passed, and it usually marks a case someone meant to vary and forgot to edit.
89 lines
2.9 KiB
Python
89 lines
2.9 KiB
Python
"""Tests for FocusLiteLLMDatabase query construction."""
|
|
|
|
from datetime import datetime, timezone
|
|
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock
|
|
|
|
import pytest
|
|
|
|
from litellm.integrations.focus.database import FocusLiteLLMDatabase
|
|
|
|
|
|
def _setup_db(monkeypatch: pytest.MonkeyPatch, query_return):
|
|
"""Create a database instance with a stubbed prisma client."""
|
|
query_mock = AsyncMock(return_value=query_return)
|
|
mock_client = SimpleNamespace(db=SimpleNamespace(query_raw=query_mock))
|
|
db = FocusLiteLLMDatabase()
|
|
monkeypatch.setattr(db, "_ensure_prisma_client", lambda: mock_client)
|
|
return db, query_mock
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_parameterize_filters_and_limit(monkeypatch: pytest.MonkeyPatch):
|
|
start = datetime(2024, 1, 1, tzinfo=timezone.utc)
|
|
end = datetime(2024, 1, 2, tzinfo=timezone.utc)
|
|
db, query_mock = _setup_db(monkeypatch, [])
|
|
|
|
await db.get_usage_data(limit=25, start_time_utc=start, end_time_utc=end)
|
|
|
|
query_text, *params = query_mock.await_args.args
|
|
assert "dus.updated_at >= $1::timestamptz" in query_text
|
|
assert "dus.updated_at <= $2::timestamptz" in query_text
|
|
assert "LIMIT $3" in query_text
|
|
assert params == [start, end, 25]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_execute_without_filters(monkeypatch: pytest.MonkeyPatch):
|
|
row = {
|
|
"id": 1,
|
|
"user_id": "user",
|
|
"date": datetime(2024, 1, 1, tzinfo=timezone.utc),
|
|
}
|
|
db, query_mock = _setup_db(monkeypatch, [row])
|
|
|
|
result = await db.get_usage_data()
|
|
|
|
query_text, *params = query_mock.await_args.args
|
|
assert "WHERE" not in query_text
|
|
assert "LIMIT $" not in query_text
|
|
assert params == []
|
|
assert result.height == 1
|
|
assert result["id"][0] == 1
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_accept_string_timestamps(monkeypatch: pytest.MonkeyPatch):
|
|
db, query_mock = _setup_db(monkeypatch, [])
|
|
|
|
start = "2024-02-01T00:00:00+00:00"
|
|
end = "2024-02-02T00:00:00+00:00"
|
|
await db.get_usage_data(start_time_utc=start, end_time_utc=end)
|
|
|
|
_, *params = query_mock.await_args.args
|
|
assert params == [start, end]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_reject_invalid_limit(monkeypatch: pytest.MonkeyPatch):
|
|
db, query_mock = _setup_db(monkeypatch, [])
|
|
|
|
with pytest.raises(ValueError, match='limit must be an integer'):
|
|
await db.get_usage_data(limit="invalid")
|
|
|
|
assert query_mock.await_count == 0
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_join_organization_table(monkeypatch: pytest.MonkeyPatch):
|
|
db, query_mock = _setup_db(monkeypatch, [])
|
|
|
|
await db.get_usage_data()
|
|
|
|
query_text, *_ = query_mock.await_args.args
|
|
assert (
|
|
"COALESCE(vt.organization_id, tt.organization_id) as organization_id"
|
|
in query_text
|
|
)
|
|
assert "ot.organization_alias as organization_alias" in query_text
|
|
assert 'LEFT JOIN "LiteLLM_OrganizationTable" ot' in query_text
|