-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OPIK-41] Show the link to UI in pytest integration (#165)
* Add summary printing to pytest integration * Implement summary printing for pytest integration, implement additional protection to make sure opik failures won't break pytest execution * Justify right column to left side * Add unit tests for convert_exception_to_log_message * Fix lint errors * Fix the way UI url is built
- Loading branch information
1 parent
8c0b1a8
commit 68f096c
Showing
7 changed files
with
193 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from typing import List | ||
|
||
import opik | ||
import pytest | ||
|
||
import rich.console | ||
import rich.panel | ||
import rich.table | ||
import rich.text | ||
|
||
import opik.url_helpers | ||
|
||
|
||
def print(reports: List[pytest.TestReport]) -> None: | ||
opik_ui_url = opik.url_helpers.get_ui_url() | ||
|
||
console = rich.console.Console() | ||
|
||
total_tests = len(reports) | ||
passed_tests = len([report for report in reports if report.passed]) | ||
failed_tests = len([report for report in reports if report.failed]) | ||
|
||
table = rich.table.Table(show_header=False, show_lines=False, padding=(0, 1)) | ||
table.add_column(justify="left") | ||
table.add_column(justify="left") | ||
|
||
table.add_row("Passed:", f"{passed_tests}", style="green") | ||
table.add_row("Failed:", f"{failed_tests}", style="red") | ||
table.add_row("Total:", f"{total_tests}") | ||
table.add_row() | ||
table.add_row("See the results:", f"[underline blue]{opik_ui_url}[/]") | ||
|
||
panel = rich.panel.Panel( | ||
table, | ||
title="[bold]Opik: LLM Test Results[/bold]", | ||
title_align="left", | ||
expand=False, | ||
) | ||
|
||
console.print("\n") | ||
console.print(panel) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import opik.config | ||
|
||
|
||
def get_ui_url() -> str: | ||
config = opik.config.OpikConfig() | ||
opik_url_override = config.url_override | ||
|
||
return opik_url_override.rstrip("/api") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import mock | ||
import logging | ||
from opik import _logging | ||
|
||
|
||
def test_convert_exception_to_log_message__happy_scenario_with_original_returned_value(): | ||
function = mock.Mock(return_value="return value") | ||
logger = mock.Mock(spec=["log"]) | ||
|
||
convert_exception_to_log_message_decorator = ( | ||
_logging.convert_exception_to_log_message( | ||
"Error message", | ||
logger=logger, | ||
exception_info=True, | ||
logging_level="some-level", | ||
) | ||
) | ||
decorated_function = convert_exception_to_log_message_decorator(function) | ||
|
||
assert decorated_function() == "return value" | ||
|
||
function.assert_called_once() | ||
logger.log.assert_not_called() | ||
|
||
|
||
def test_convert_exception_to_log_message__exception_raised__exception_converted_into_log_message__another_value_returned__log_is_called_with_passed_kwarg(): | ||
function = mock.Mock(side_effect=Exception()) | ||
logger = mock.Mock(spec=["log"]) | ||
|
||
convert_exception_to_log_message_decorator = ( | ||
_logging.convert_exception_to_log_message( | ||
"Error message", | ||
logger=logger, | ||
some_log_kwarg="some-log-kwarg", | ||
return_on_exception="return_value", | ||
logging_level="some-level", | ||
) | ||
) | ||
decorated_function = convert_exception_to_log_message_decorator(function) | ||
|
||
assert decorated_function() == "return_value" | ||
|
||
function.assert_called_once() | ||
logger.log.assert_called_once_with( | ||
"some-level", "Error message", some_log_kwarg="some-log-kwarg" | ||
) | ||
|
||
|
||
def test_convert_exception_to_log_message__logging_level_not_set__error_level_used(): | ||
function = mock.Mock(side_effect=Exception()) | ||
logger = mock.Mock(spec=["log"]) | ||
|
||
convert_exception_to_log_message_decorator = ( | ||
_logging.convert_exception_to_log_message( | ||
"Error message", | ||
logger=logger, | ||
some_log_kwarg="some-log-kwarg", | ||
return_on_exception="return_value", | ||
) | ||
) | ||
decorated_function = convert_exception_to_log_message_decorator(function) | ||
|
||
assert decorated_function() == "return_value" | ||
|
||
function.assert_called_once() | ||
logger.log.assert_called_once_with( | ||
logging.ERROR, | ||
"Error message", | ||
some_log_kwarg="some-log-kwarg", | ||
) |