File: test_percentstyle.py

package info (click to toggle)
python-picologging 0.9.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 752 kB
  • sloc: python: 3,921; cpp: 2,430; makefile: 41; sh: 18
file content (93 lines) | stat: -rw-r--r-- 3,197 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
92
93
import logging
import threading

import pytest
from utils import filter_gc

from picologging import INFO, LogRecord, PercentStyle


@pytest.mark.limit_leaks("168B", filter_fn=filter_gc)
def test_percentstyle():
    perc = PercentStyle("%(msg)s %(levelno)d %(name)s")
    record = LogRecord("test", INFO, __file__, 1, "hello", (), None, None, None)
    assert perc.format(record) == "hello 20 test"


@pytest.mark.limit_leaks("225B", filter_fn=filter_gc)
def test_percentstyle_format_bad_argument():
    perc = PercentStyle("%(msg)s %(levelno)d %(name)s")
    with pytest.raises(AttributeError):
        perc.format(None)
    with pytest.raises(AttributeError):
        perc.format("")
    with pytest.raises(AttributeError):
        perc.format({})


@pytest.mark.limit_leaks("223B", filter_fn=filter_gc)
def test_custom_attribute():
    perc = PercentStyle("%(custom)s")
    record = LogRecord("test", INFO, __file__, 1, "hello", (), None, None, None)
    record.custom = "custom"
    assert perc.format(record) == "custom"


@pytest.mark.limit_leaks("192B", filter_fn=filter_gc)
def test_percentstyle_bad_init_args():
    with pytest.raises(TypeError):
        PercentStyle(dog="good boy")


@pytest.mark.limit_leaks("192B", filter_fn=filter_gc)
def test_funcname_format_string():
    perc = PercentStyle("%(funcname)s")
    record = LogRecord("test", INFO, __file__, 1, "hello", (), None, "superfunc", None)
    record.funcName = "superFunc"
    assert perc.format(record) == "superFunc"


@pytest.mark.limit_leaks("192B", filter_fn=filter_gc)
def test_thread_id():
    perc = PercentStyle("%(thread)d")
    record = LogRecord("test", INFO, __file__, 1, "hello", (), None, None, None)
    assert record.thread == threading.get_ident()
    assert perc.format(record) == str(record.thread)


@pytest.mark.limit_leaks("192B", filter_fn=filter_gc)
def test_record_created():
    perc = PercentStyle("%(created)f")
    record = LogRecord("test", INFO, __file__, 1, "hello", (), None, None, None)
    assert perc.format(record) == str(record.created)


def test_custom_field_not_an_attribute():
    perc = PercentStyle("%(custom)s")
    record = LogRecord("test", INFO, __file__, 1, "hello", (), None, None, None)
    with pytest.raises(AttributeError):
        assert perc.format(record)


@pytest.mark.limit_leaks("192B", filter_fn=filter_gc)
def test_percentstyle_repr():
    perc = PercentStyle("%(msg)s %(levelno)d %(name)s")
    assert repr(perc) == "<FormatStyle fmt='%(msg)s %(levelno)d %(name)s' style='%'>"


@pytest.mark.limit_leaks("192B", filter_fn=filter_gc)
def test_record_with_defaults():
    perc = PercentStyle(
        "%(msg)s %(levelno)d %(name)s %(fruit)s", defaults={"fruit": "banana"}
    )
    record = LogRecord("test", INFO, __file__, 1, "hello", (), None, None, None)
    assert perc.format(record) == "hello 20 test banana"


@pytest.mark.limit_leaks("192B", filter_fn=filter_gc)
def test_format_logging_record():
    perc = PercentStyle(
        "%(msg)s %(levelno)d %(name)s %(fruit)s", defaults={"fruit": "banana"}
    )
    record = logging.LogRecord("test", INFO, __file__, 1, "hello", (), None, None, None)
    assert perc.format(record) == "hello 20 test banana"