File: test_test_handler.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 (54 lines) | stat: -rw-r--r-- 1,615 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
import re

import pytest


@pytest.mark.parametrize(
    "level, method",
    [
        ("trace", "has_traces"),
        ("debug", "has_debugs"),
        ("info", "has_infos"),
        ("notice", "has_notices"),
        ("warning", "has_warnings"),
        ("error", "has_errors"),
        ("critical", "has_criticals"),
    ],
)
def test_has_level(active_handler, logger, level, method):
    log = getattr(logger, level)
    log("Hello World")
    assert getattr(active_handler, method)


@pytest.mark.parametrize(
    "level, method",
    [
        ("trace", "has_trace"),
        ("debug", "has_debug"),
        ("info", "has_info"),
        ("notice", "has_notice"),
        ("warning", "has_warning"),
        ("error", "has_error"),
        ("critical", "has_critical"),
    ],
)
def test_regex_matching(active_handler, logger, level, method):
    log = getattr(logger, level)
    log("Hello World")
    has_level_method = getattr(active_handler, method)
    assert has_level_method(re.compile("^Hello"))
    assert not has_level_method(re.compile("world$"))
    assert not has_level_method("^Hello World")


def test_test_handler_cache(active_handler, logger):
    logger.warn("First line")
    assert len(active_handler.formatted_records) == 1
    # store cache, to make sure it is identifiable
    cache = active_handler.formatted_records
    assert len(active_handler.formatted_records) == 1
    assert cache is active_handler.formatted_records
    logger.warn("Second line invalidates cache")
    assert len(active_handler.formatted_records) == 2
    assert cache is not active_handler.formatted_records