forked from langfuse/langfuse-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_error_logging.py
More file actions
67 lines (49 loc) · 1.79 KB
/
test_error_logging.py
File metadata and controls
67 lines (49 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import logging
import pytest
from langfuse._utils.error_logging import (
auto_decorate_methods_with,
catch_and_log_errors,
)
# Test for the catch_and_log_errors decorator applied to a standalone function
@catch_and_log_errors
def function_that_raises():
raise ValueError("This is a test error.")
def test_catch_and_log_errors_logs_error_silently(caplog):
function_that_raises()
assert len(caplog.records) == 1
assert caplog.records[0].levelno == logging.ERROR
assert (
"An error occurred in function_that_raises: This is a test error."
in caplog.text
)
caplog.clear()
# Test for the auto_decorate_methods_with decorator applied to a class
@auto_decorate_methods_with(catch_and_log_errors, exclude=["excluded_instance_method"])
class TestClass:
def instance_method(self):
raise ValueError("Error in instance method.")
def excluded_instance_method(self):
raise ValueError("Error in instance method.")
@classmethod
def class_method(cls):
raise ValueError("Error in class method.")
@staticmethod
def static_method():
raise ValueError("Error in static method.")
def test_auto_decorate_class_methods(caplog):
test_obj = TestClass()
# Test the instance method
test_obj.instance_method()
assert "Error in instance method." in caplog.text
caplog.clear()
# Test the class method
TestClass.class_method()
assert "Error in class method." in caplog.text
caplog.clear()
# Test the static method
TestClass.static_method()
assert "Error in static method." in caplog.text
caplog.clear()
# Test the excluded instance method that should raise an error
with pytest.raises(ValueError, match="Error in instance method."):
test_obj.excluded_instance_method()