mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-23 00:41:40 +00:00
test(conduct): cover initialize_guardrail to raise patch coverage
Codecov flagged the __init__.initialize_guardrail body as uncovered (30% patch coverage on that file). Added a test that mocks litellm.logging_callback_manager and calls initialize_guardrail with a SimpleNamespace stand-in for LitellmParams — exercises the full function body and confirms the callback is registered.
This commit is contained in:
parent
c281e4d952
commit
e0f9484cb3
1 changed files with 41 additions and 0 deletions
|
|
@ -52,6 +52,47 @@ def test_registries_populated() -> None:
|
|||
assert "conduct" in guardrail_initializer_registry
|
||||
|
||||
|
||||
def test_initialize_guardrail_returns_wired_callback(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
"""``initialize_guardrail`` maps LiteLLM params to Conduct kwargs and
|
||||
registers the callback with ``logging_callback_manager``. This test
|
||||
exercises the full function body so coverage reports don't flag it
|
||||
as dead code."""
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
monkeypatch.setenv("CONDUCT_AGENT_TOKEN", "cond_agt_test_placeholder")
|
||||
|
||||
added_callbacks: list[object] = []
|
||||
fake_manager = SimpleNamespace(
|
||||
add_litellm_callback=lambda cb: added_callbacks.append(cb),
|
||||
)
|
||||
|
||||
import litellm
|
||||
|
||||
monkeypatch.setattr(litellm, "logging_callback_manager", fake_manager)
|
||||
|
||||
from litellm.proxy.guardrails.guardrail_hooks.conduct import (
|
||||
ConductGuardrail,
|
||||
initialize_guardrail,
|
||||
)
|
||||
|
||||
litellm_params = SimpleNamespace(
|
||||
api_base=None,
|
||||
api_key=None,
|
||||
mode="pre_call",
|
||||
default_on=True,
|
||||
)
|
||||
guardrail = MagicMock()
|
||||
guardrail.get.return_value = "conduct-guard"
|
||||
|
||||
callback = initialize_guardrail(litellm_params, guardrail)
|
||||
|
||||
assert isinstance(callback, ConductGuardrail)
|
||||
assert added_callbacks == [callback]
|
||||
|
||||
|
||||
def test_missing_standalone_package_raises_helpful_error(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue