mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-06 08:16:43 +00:00
* test: drop the cwd-relative sys.path.insert calls from the test suite
TQ003 stands at 1,077 across 1,058 files, and 1,015 of them are the same shape:
sys.path.insert(0, os.path.abspath("../..")) and its deeper siblings. The
argument resolves against the working directory rather than the file, so from
the repo root, where every job runs pytest, it inserts the directory two levels
above the checkout. It has never pointed at litellm. The package is installed
into the environment anyway, which is what actually makes the import work, and
what the rule's message has said all along.
Removing them leaves 1,634 imports of sys and os with no remaining reference,
and those go too, except where another test module imports the name back out of
the file. The rest of TQ003 is 62 call sites that resolve against __file__ or a
variable, which are a different question and are left alone.
Collection is identical either way: 45,871 tests and the same 51 pre-existing
collection errors before and after, and ruff reports no new undefined name.
* test: drop the duplicate imports the sys.path sweep exposed to F811
* test(pre-call-utils): restore the os import the new bedrock tests need
104 lines
3.9 KiB
Python
104 lines
3.9 KiB
Python
from unittest.mock import MagicMock, AsyncMock, patch
|
|
|
|
import pytest
|
|
|
|
|
|
from litellm.caching.gcs_cache import GCSCache
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_gcs_dependencies():
|
|
"""Mock httpx clients and GCS auth"""
|
|
mock_sync_client = MagicMock()
|
|
mock_async_client = AsyncMock()
|
|
|
|
with (
|
|
patch(
|
|
"litellm.caching.gcs_cache._get_httpx_client", return_value=mock_sync_client
|
|
),
|
|
patch(
|
|
"litellm.caching.gcs_cache.get_async_httpx_client",
|
|
return_value=mock_async_client,
|
|
),
|
|
patch(
|
|
"litellm.caching.gcs_cache.GCSBucketBase.sync_construct_request_headers",
|
|
return_value={},
|
|
),
|
|
):
|
|
yield {
|
|
"sync_client": mock_sync_client,
|
|
"async_client": mock_async_client,
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_gcs_cache_async_set_and_get(mock_gcs_dependencies):
|
|
cache = GCSCache(bucket_name="test-bucket")
|
|
await cache.async_set_cache("key", {"foo": "bar"})
|
|
mock_gcs_dependencies["async_client"].post.assert_called_once()
|
|
|
|
mock_gcs_dependencies["async_client"].get.return_value.status_code = 200
|
|
mock_gcs_dependencies["async_client"].get.return_value.text = '{"foo": "bar"}'
|
|
result = await cache.async_get_cache("key")
|
|
assert result == {"foo": "bar"}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_gcs_cache_async_get_encodes_object_name_in_path(mock_gcs_dependencies):
|
|
"""
|
|
Regression test for https://github.com/BerriAI/litellm/issues/30377
|
|
|
|
When gcs_path is set, the object name contains a '/' (e.g. "my_cache/<hash>").
|
|
The GCS JSON API requires the object name in the GET path to be URL-encoded,
|
|
so the '/' must be sent as '%2F'. Otherwise GCS returns 404 and every read
|
|
silently misses.
|
|
"""
|
|
cache = GCSCache(bucket_name="test-bucket", gcs_path="my_cache/")
|
|
|
|
mock_gcs_dependencies["async_client"].get.return_value.status_code = 200
|
|
mock_gcs_dependencies["async_client"].get.return_value.text = '{"foo": "bar"}'
|
|
|
|
result = await cache.async_get_cache("abc123")
|
|
assert result == {"foo": "bar"}
|
|
|
|
called_url = mock_gcs_dependencies["async_client"].get.call_args.kwargs["url"]
|
|
# The slash from gcs_path must be percent-encoded in the path segment.
|
|
assert "/o/my_cache%2Fabc123?alt=media" in called_url
|
|
assert "/o/my_cache/abc123" not in called_url
|
|
|
|
|
|
def test_gcs_cache_get_encodes_object_name_in_path(mock_gcs_dependencies):
|
|
"""Sync counterpart of the regression test for issue #30377."""
|
|
cache = GCSCache(bucket_name="test-bucket", gcs_path="my_cache/")
|
|
|
|
mock_gcs_dependencies["sync_client"].get.return_value.status_code = 200
|
|
mock_gcs_dependencies["sync_client"].get.return_value.text = '{"foo": "bar"}'
|
|
|
|
result = cache.get_cache("abc123")
|
|
assert result == {"foo": "bar"}
|
|
|
|
called_url = mock_gcs_dependencies["sync_client"].get.call_args.kwargs["url"]
|
|
assert "/o/my_cache%2Fabc123?alt=media" in called_url
|
|
assert "/o/my_cache/abc123" not in called_url
|
|
|
|
|
|
def test_gcs_cache_set_encodes_object_name_in_query(mock_gcs_dependencies):
|
|
"""
|
|
The set path uses the object name as a query parameter. Encoding it keeps
|
|
both sides symmetric so the key written matches the key read back.
|
|
"""
|
|
cache = GCSCache(bucket_name="test-bucket", gcs_path="my_cache/")
|
|
cache.set_cache("abc123", {"foo": "bar"})
|
|
|
|
called_url = mock_gcs_dependencies["sync_client"].post.call_args.kwargs["url"]
|
|
assert "name=my_cache%2Fabc123" in called_url
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_gcs_cache_async_set_encodes_object_name_in_query(mock_gcs_dependencies):
|
|
"""Async counterpart of test_gcs_cache_set_encodes_object_name_in_query."""
|
|
cache = GCSCache(bucket_name="test-bucket", gcs_path="my_cache/")
|
|
await cache.async_set_cache("abc123", {"foo": "bar"})
|
|
|
|
called_url = mock_gcs_dependencies["async_client"].post.call_args.kwargs["url"]
|
|
assert "name=my_cache%2Fabc123" in called_url
|