File: test_logging_api.py

package info (click to toggle)
logbook 1.7.0-1.0
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,140 kB
  • sloc: python: 6,558; makefile: 141
file content (91 lines) | stat: -rw-r--r-- 2,713 bytes parent folder | download
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import pickle
import sys

import pytest

import logbook


def test_basic_logging(active_handler, logger):
    logger.warn("This is a warning.  Nice hah?")

    assert active_handler.has_warning("This is a warning.  Nice hah?")
    assert active_handler.formatted_records == [
        "[WARNING] testlogger: This is a warning.  Nice hah?"
    ]


def test_exception_catching(active_handler, logger):
    assert not active_handler.has_error()
    try:
        1 / 0
    except Exception:
        logger.exception()
    try:
        1 / 0
    except Exception:
        logger.exception("Awesome")
    assert active_handler.has_error("Uncaught exception occurred")
    assert active_handler.has_error("Awesome")
    assert active_handler.records[0].exc_info is not None
    assert "1 / 0" in active_handler.records[0].formatted_exception


def test_exception_catching_with_unicode():
    """See https://github.com/getlogbook/logbook/issues/104"""
    try:
        raise Exception("\u202a test \u202c")
    except:
        r = logbook.LogRecord("channel", "DEBUG", "test", exc_info=sys.exc_info())
    r.exception_message


@pytest.mark.parametrize("as_tuple", [True, False])
def test_exc_info(as_tuple, logger, active_handler):
    try:
        1 / 0
    except Exception:
        exc_info = sys.exc_info()
        logger.info("Exception caught", exc_info=exc_info if as_tuple else True)
    assert active_handler.records[0].exc_info is not None
    assert active_handler.records[0].exc_info == exc_info


def test_to_dict(logger, active_handler):
    try:
        1 / 0
    except Exception:
        logger.exception()
        record = active_handler.records[0]

    exported = record.to_dict()
    record.close()
    imported = logbook.LogRecord.from_dict(exported)
    for key, value in record.__dict__.items():
        if key[0] == "_":
            continue
        assert value == getattr(imported, key)


def test_pickle(active_handler, logger):
    try:
        1 / 0
    except Exception:
        logger.exception()
        record = active_handler.records[0]
    record.pull_information()
    record.close()

    for p in range(pickle.HIGHEST_PROTOCOL):
        exported = pickle.dumps(record, p)
        imported = pickle.loads(exported)
        for key, value in record.__dict__.items():
            if key[0] == "_":
                continue
            imported_value = getattr(imported, key)
            if isinstance(value, ZeroDivisionError):
                # in Python 3.2, ZeroDivisionError(x) != ZeroDivisionError(x)
                assert type(value) is type(imported_value)
                assert value.args == imported_value.args
            else:
                assert value == imported_value