litellm/tests/e2e_harness/test_lifecycle.py
joshua-berri d6f468e0bf
test(cost): verify basic provider pricing from returned usage (#55363)
* test(cost): verify basic provider pricing from returned usage

* test(cost): distinguish catalog prices from runtime prices in harness

* test(cost): reuse existing translation calls for default pricing

* test(cost): escape literal model name in billing assertion check

* test(cost): check observed prices without forcing a bundled map

* test(cost): isolate failing resource setup assertion

* test(cost): cover unavailable price observation boundaries

* test(cost): fail bills naming the wrong Azure model family

* test(cost): retry failed shared calls during fixture setup

---------

Co-authored-by: Joshua Valluru <326636767+joshua-berri@users.noreply.github.com>
2026-12-01 16:46:46 -08:00

50 lines
1.7 KiB
Python

from __future__ import annotations
from dataclasses import dataclass, field
from queue import SimpleQueue
from typing import Final
import pytest
from lifecycle import owned_resources
from models import KeyGenerateBody
@dataclass(frozen=True, slots=True)
class RecordingClient:
calls: SimpleQueue[str] = field(default_factory=SimpleQueue)
def generate_key(self, body: KeyGenerateBody) -> str:
self.calls.put("generate_key")
return "sk-owned"
def delete_key(self, key: str) -> None:
self.calls.put(f"delete_key {key}")
def delete_customers(self, user_ids: list[str]) -> None:
self.calls.put(f"delete_customers {user_ids}")
def _drained(calls: SimpleQueue[str]) -> tuple[str, ...]:
return tuple(calls.get_nowait() for _ in range(calls.qsize()))
def _create_key_then_fail_registration(client: RecordingClient) -> None:
with owned_resources(client) as resources:
_ = resources.key()
raise RuntimeError("registration failed")
class TestOwnedResources:
def test_cleanups_run_last_first_once_the_block_exits(self) -> None:
client: Final = RecordingClient()
with owned_resources(client) as resources:
_ = resources.key()
resources.defer(lambda: client.calls.put("delete_model"))
assert _drained(client.calls) == ("generate_key",)
assert _drained(client.calls) == ("delete_model", "delete_key sk-owned")
def test_cleanups_still_run_when_setup_inside_the_block_fails(self) -> None:
client: Final = RecordingClient()
with pytest.raises(RuntimeError, match="registration failed"):
_create_key_then_fail_registration(client)
assert _drained(client.calls) == ("generate_key", "delete_key sk-owned")