|
1 | 1 | # Copyright (c) Microsoft Corporation. |
2 | 2 | # Licensed under the MIT license. |
3 | 3 |
|
| 4 | +from unittest.mock import AsyncMock |
| 5 | + |
4 | 6 | import pytest |
5 | 7 |
|
6 | 8 | from pyrit.identifiers import ComponentIdentifier |
@@ -57,3 +59,44 @@ async def render_async( |
57 | 59 |
|
58 | 60 | printer = CompletePrinter() |
59 | 61 | assert isinstance(printer, ScorerPrinterBase) |
| 62 | + |
| 63 | + |
| 64 | +def _make_complete_printer() -> ScorerPrinterBase: |
| 65 | + class CompletePrinter(ScorerPrinterBase): |
| 66 | + def __init__(self) -> None: |
| 67 | + self.write_async = AsyncMock() |
| 68 | + |
| 69 | + def _get_objective_metrics(self, *, scorer_identifier: ComponentIdentifier): |
| 70 | + return None |
| 71 | + |
| 72 | + def _get_harm_metrics(self, *, scorer_identifier: ComponentIdentifier, harm_category: str): |
| 73 | + return None |
| 74 | + |
| 75 | + async def render_async( |
| 76 | + self, *, scorer_identifier: ComponentIdentifier, harm_category: str | None = None |
| 77 | + ) -> str: |
| 78 | + return "" |
| 79 | + |
| 80 | + return CompletePrinter() |
| 81 | + |
| 82 | + |
| 83 | +async def test_print_objective_scorer_emits_deprecation_warning_and_delegates(): |
| 84 | + """``print_objective_scorer`` is a deprecated shim that should warn and call ``write_async``.""" |
| 85 | + printer = _make_complete_printer() |
| 86 | + scorer_identifier = ComponentIdentifier(class_name="TestScorer", class_module="tests") |
| 87 | + |
| 88 | + with pytest.warns(DeprecationWarning, match="print_objective_scorer"): |
| 89 | + await printer.print_objective_scorer(scorer_identifier=scorer_identifier) |
| 90 | + |
| 91 | + printer.write_async.assert_awaited_once_with(scorer_identifier=scorer_identifier) |
| 92 | + |
| 93 | + |
| 94 | +async def test_print_harm_scorer_emits_deprecation_warning_and_delegates(): |
| 95 | + """``print_harm_scorer`` is a deprecated shim that should warn and call ``write_async``.""" |
| 96 | + printer = _make_complete_printer() |
| 97 | + scorer_identifier = ComponentIdentifier(class_name="TestScorer", class_module="tests") |
| 98 | + |
| 99 | + with pytest.warns(DeprecationWarning, match="print_harm_scorer"): |
| 100 | + await printer.print_harm_scorer(scorer_identifier=scorer_identifier, harm_category="violence") |
| 101 | + |
| 102 | + printer.write_async.assert_awaited_once_with(scorer_identifier=scorer_identifier, harm_category="violence") |
0 commit comments