|
1 | 1 | import json
|
2 | 2 | import os
|
3 | 3 | import sys
|
4 |
| -from typing import Optional |
| 4 | +import uuid |
| 5 | +from typing import Dict, Optional |
5 | 6 | from unittest.mock import AsyncMock, MagicMock, patch
|
6 | 7 |
|
7 | 8 | import pytest
|
|
11 | 12 | 0, os.path.abspath("../../../..")
|
12 | 13 | ) # Adds the parent directory to the system path
|
13 | 14 | from litellm.proxy._types import (
|
| 15 | + LiteLLM_ModelTable, |
14 | 16 | LiteLLM_TeamTable,
|
15 | 17 | LitellmUserRoles,
|
16 | 18 | Member,
|
@@ -230,6 +232,135 @@ async def test_can_user_make_model_call_normal_user_fails(self):
|
230 | 232 | assert "403" in str(exc_info.value)
|
231 | 233 |
|
232 | 234 |
|
| 235 | +class MockModelTable: |
| 236 | + def __init__(self, model_aliases: Dict[str, str], include: Optional[dict] = None): |
| 237 | + for alias, model in model_aliases.items(): |
| 238 | + setattr(self, alias, model) |
| 239 | + self.id = str(uuid.uuid4()) |
| 240 | + self.model_aliases = model_aliases |
| 241 | + |
| 242 | + |
| 243 | +class MockPrismaDB: |
| 244 | + def __init__(self, model_aliases_list): |
| 245 | + self.litellm_modeltable = self |
| 246 | + self.model_aliases_list = model_aliases_list |
| 247 | + self.update_calls = [] |
| 248 | + |
| 249 | + async def find_many(self, include=None): |
| 250 | + print(f"self.model_aliases_list: {self.model_aliases_list}") |
| 251 | + return [LiteLLM_ModelTable(**aliases) for aliases in self.model_aliases_list] |
| 252 | + |
| 253 | + async def update(self, where, data): |
| 254 | + self.update_calls.append({"where": where, "data": data}) |
| 255 | + return None |
| 256 | + |
| 257 | + |
| 258 | +class MockPrismaWrapper: |
| 259 | + def __init__(self, model_aliases_list): |
| 260 | + self.litellm_modeltable = MockPrismaDB(model_aliases_list) |
| 261 | + |
| 262 | + |
| 263 | +class TestDeleteTeamModelAlias: |
| 264 | + @pytest.mark.asyncio |
| 265 | + async def test_delete_team_model_alias_success(self): |
| 266 | + """Test successful deletion of a team model alias""" |
| 267 | + from litellm.proxy.management_endpoints.model_management_endpoints import ( |
| 268 | + delete_team_model_alias, |
| 269 | + ) |
| 270 | + |
| 271 | + # Setup test data |
| 272 | + model_aliases_list = [ |
| 273 | + { |
| 274 | + "id": 1, |
| 275 | + "model_aliases": { |
| 276 | + "alias1": "public_model_1", |
| 277 | + "alias2": "public_model_2", |
| 278 | + }, |
| 279 | + "updated_by": "test_user", |
| 280 | + "created_by": "test_user", |
| 281 | + }, |
| 282 | + { |
| 283 | + "id": 2, |
| 284 | + "model_aliases": { |
| 285 | + "alias3": "public_model_3", |
| 286 | + "alias4": "public_model_1", |
| 287 | + }, |
| 288 | + "updated_by": "test_user", |
| 289 | + "created_by": "test_user", |
| 290 | + }, # public_model_1 appears twice |
| 291 | + ] |
| 292 | + |
| 293 | + # Create mock prisma client |
| 294 | + mock_prisma = MockPrismaClient(team_exists=True) |
| 295 | + mock_prisma.db = MockPrismaWrapper(model_aliases_list) |
| 296 | + |
| 297 | + # Call the function |
| 298 | + await delete_team_model_alias( |
| 299 | + public_model_name="public_model_1", prisma_client=mock_prisma |
| 300 | + ) |
| 301 | + |
| 302 | + # Verify results |
| 303 | + mock_db = mock_prisma.db.litellm_modeltable |
| 304 | + assert ( |
| 305 | + len(mock_db.update_calls) == 2 |
| 306 | + ) # Should have 2 update calls since public_model_1 appears twice |
| 307 | + |
| 308 | + # Verify first update |
| 309 | + first_update = mock_db.update_calls[0] |
| 310 | + assert first_update["where"] == {"id": 1} |
| 311 | + assert json.loads(first_update["data"]["model_aliases"]) == { |
| 312 | + "alias2": "public_model_2" |
| 313 | + } |
| 314 | + |
| 315 | + # Verify second update |
| 316 | + second_update = mock_db.update_calls[1] |
| 317 | + assert second_update["where"] == {"id": 2} |
| 318 | + assert json.loads(second_update["data"]["model_aliases"]) == { |
| 319 | + "alias3": "public_model_3" |
| 320 | + } |
| 321 | + |
| 322 | + @pytest.mark.asyncio |
| 323 | + async def test_delete_team_model_alias_no_matches(self): |
| 324 | + """Test deletion when no matching model alias exists""" |
| 325 | + from litellm.proxy.management_endpoints.model_management_endpoints import ( |
| 326 | + delete_team_model_alias, |
| 327 | + ) |
| 328 | + |
| 329 | + # Setup test data with no matching model |
| 330 | + model_aliases_list = [ |
| 331 | + { |
| 332 | + "id": 1, |
| 333 | + "model_aliases": { |
| 334 | + "alias1": "public_model_1", |
| 335 | + "alias2": "public_model_2", |
| 336 | + }, |
| 337 | + "updated_by": "test_user", |
| 338 | + "created_by": "test_user", |
| 339 | + }, |
| 340 | + { |
| 341 | + "id": 2, |
| 342 | + "model_aliases": { |
| 343 | + "alias3": "public_model_3", |
| 344 | + "alias4": "public_model_4", |
| 345 | + }, |
| 346 | + "updated_by": "test_user", |
| 347 | + "created_by": "test_user", |
| 348 | + }, |
| 349 | + ] |
| 350 | + |
| 351 | + # Create mock prisma client |
| 352 | + mock_prisma = MockPrismaClient(team_exists=True) |
| 353 | + mock_prisma.db = MockPrismaWrapper(model_aliases_list) |
| 354 | + |
| 355 | + # Call the function with non-existent model |
| 356 | + await delete_team_model_alias( |
| 357 | + public_model_name="non_existent_model", prisma_client=mock_prisma |
| 358 | + ) |
| 359 | + |
| 360 | + # Verify no updates were made |
| 361 | + mock_db = mock_prisma.db.litellm_modeltable |
| 362 | + assert len(mock_db.update_calls) == 0 |
| 363 | + |
233 | 364 | class TestClearCache:
|
234 | 365 | """
|
235 | 366 | Tests for the clear_cache function in model_management_endpoints.py
|
|
0 commit comments