|
| 1 | +import logging |
| 2 | +from ckanext.push_errors.utils import get_error_trace_id, get_error_log_id |
| 3 | + |
| 4 | + |
| 5 | +class CustomError(Exception): |
| 6 | + pass |
| 7 | + |
| 8 | + |
| 9 | +def raise_custom_error(): |
| 10 | + """Raises a CustomError to simulate an error scenario for testing purposes.""" |
| 11 | + raise CustomError("Simulated error") |
| 12 | + |
| 13 | + |
| 14 | +def test_get_error_trace_id_returns_path_and_line(): |
| 15 | + """ |
| 16 | + Test that get_error_trace_id returns a string containing the file path and line number |
| 17 | + where the CustomError was raised, formatted as 'path/to/file.py:<line>'. |
| 18 | + Ensures the returned trace_id contains a colon and does not end with '.py'. |
| 19 | + """ |
| 20 | + try: |
| 21 | + raise_custom_error() |
| 22 | + except CustomError as e: |
| 23 | + trace_id = get_error_trace_id(e) |
| 24 | + assert ":" in trace_id |
| 25 | + assert not trace_id.endswith(".py") # it only ends with ":<line>" |
| 26 | + |
| 27 | + |
| 28 | +def test_get_error_trace_id_unknown_when_no_tb(): |
| 29 | + """ |
| 30 | + Test that get_error_trace_id returns 'unknown' when the exception has no traceback. |
| 31 | +
|
| 32 | + This test creates a CustomError instance without a traceback and verifies that |
| 33 | + the get_error_trace_id function returns the string 'unknown' as expected. |
| 34 | + """ |
| 35 | + e = CustomError("No traceback") |
| 36 | + e.__traceback__ = None |
| 37 | + trace_id = get_error_trace_id(e) |
| 38 | + assert trace_id == "unknown" |
| 39 | + |
| 40 | + |
| 41 | +def test_get_error_log_id_from_exc_info(): |
| 42 | + """ |
| 43 | + Tests the get_error_log_id function to ensure it generates an appropriate log identifier |
| 44 | + from a logging record containing exception information. |
| 45 | +
|
| 46 | + - Creates a test logger. |
| 47 | + - Raises and catches a custom exception. |
| 48 | + - Generates a log record with the captured exception. |
| 49 | + - Obtains the log identifier using get_error_log_id. |
| 50 | + - Verifies that the identifier does not start with "loghash:" and contains ":". |
| 51 | +
|
| 52 | + This function checks that get_error_log_id correctly processes exception information |
| 53 | + in a logging record and generates the expected identifier. |
| 54 | + """ |
| 55 | + logger = logging.getLogger("testlogger") |
| 56 | + |
| 57 | + try: |
| 58 | + raise_custom_error() |
| 59 | + except CustomError as e: |
| 60 | + record = logger.makeRecord( |
| 61 | + name="testlogger", |
| 62 | + level=logging.CRITICAL, |
| 63 | + fn="test_file.py", |
| 64 | + lno=99, |
| 65 | + msg="An error occurred", |
| 66 | + args=(), |
| 67 | + exc_info=(type(e), e, e.__traceback__), |
| 68 | + func="test_func", |
| 69 | + extra=None |
| 70 | + ) |
| 71 | + log_id = get_error_log_id(record) |
| 72 | + assert log_id.startswith("loghash:") is False |
| 73 | + assert ":" in log_id |
| 74 | + |
| 75 | + |
| 76 | +def test_get_error_log_id_from_message_hash(): |
| 77 | + """ |
| 78 | + Test that get_error_log_id generates a unique log ID for a given LogRecord. |
| 79 | +
|
| 80 | + This test creates a LogRecord with a critical error message and verifies that |
| 81 | + the generated log ID starts with the expected prefix ("loghash:") and has a |
| 82 | + length greater than 12 characters, ensuring the uniqueness and format of the |
| 83 | + log identifier. |
| 84 | + """ |
| 85 | + record = logging.LogRecord( |
| 86 | + name="test", level=logging.CRITICAL, pathname="", lineno=0, |
| 87 | + msg="Some random critical error", args=(), exc_info=None |
| 88 | + ) |
| 89 | + log_id = get_error_log_id(record) |
| 90 | + assert log_id.startswith("loghash:") |
| 91 | + assert len(log_id) > 12 |
0 commit comments