File: tests.py

package info (click to toggle)
pytest-logdog 0.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116 kB
  • sloc: python: 329; makefile: 3
file content (223 lines) | stat: -rw-r--r-- 5,983 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from concurrent.futures import ThreadPoolExecutor
import logging
from random import random
from time import sleep

import pytest


pytest_plugins = ["pytest_logdog"]


def test_it_works(logdog):
    with logdog(level=logging.INFO) as pile:
        logging.info("Test")
    assert len(pile) == 1
    [rec] = pile
    assert rec.getMessage() == "Test"


def test_log_drain_race(logdog):
    COUNT = 100

    def log():
        for _ in range(COUNT):
            sleep(random() * 0.000_001)
            logging.info("Test")

    drained = []
    with ThreadPoolExecutor() as executor, logdog(level=logging.INFO) as pile:
        future = executor.submit(log)
        while not future.done():
            drained.extend(pile.drain())
    drained.extend(pile.drain())
    assert len(drained) == COUNT


def test_nested(logdog):
    with logdog(level=logging.WARNING) as outer:
        logging.warning("Outer warning 1")

        with logdog(level=logging.INFO) as inner:
            logging.info("Inner info")
            logging.warning("Inner warning")

        logging.warning("Outer warning 2")

    assert outer.messages() == [
        "Outer warning 1",
        "Inner warning",
        "Outer warning 2",
    ]
    assert inner.messages() == [
        "Inner info",
        "Inner warning",
    ]


def test_preset_level(logdog):
    logger = logging.getLogger("mod")
    logger.setLevel(logging.WARNING)

    # Precondition: without it the test in the next block doesn't make sense
    with logdog(level=logging.INFO) as pile:
        logger.info("Silenced")
    assert pile.is_empty()

    with logdog(name="mod", level=logging.INFO) as pile:
        logger.info("Aloud")
    assert pile.messages() == ["Aloud"]


@pytest.mark.parametrize(
    "name, matches",
    [("", False), ("mod", True), ("module", False), ("mod.sub", True)],
)
def test_capture_name(logdog, name, matches):
    with logdog(name="mod") as pile:
        logging.getLogger(name).error("Message")
    assert pile.is_empty() == (not matches)


@pytest.mark.parametrize(
    "name, matches",
    [("", False), ("mod", True), ("module", False), ("mod.sub", True)],
)
def test_filter_drain_name(logdog, name, matches):
    with logdog() as pile:
        logging.getLogger(name).error("Message")

    assert pile.filter(name="mod").is_empty() == (not matches)
    assert not pile.is_empty()

    assert pile.drain(name="mod").is_empty() == (not matches)
    assert pile.is_empty() == matches


@pytest.mark.parametrize(
    "log_level, filter_level, matches",
    [
        (logging.DEBUG, logging.INFO, False),
        (logging.DEBUG, logging.DEBUG, True),
        (logging.DEBUG, logging.NOTSET, True),
        (logging.DEBUG, "DEBUG", True),
        (logging.DEBUG, 5, True),
        (logging.DEBUG, 15, False),
    ],
)
def test_filter_drain_level(logdog, log_level, filter_level, matches):
    with logdog(level=logging.NOTSET) as pile:
        logging.log(log_level, "Message")

    assert pile.filter(level=filter_level).is_empty() == (not matches)
    assert not pile.is_empty()

    assert pile.drain(level=filter_level).is_empty() == (not matches)
    assert pile.is_empty() == matches


@pytest.mark.parametrize(
    "pattern, matches",
    [
        ("^one", True),
        ("two", True),
        ("^two", False),
        ("one.*three", True),
    ],
)
def test_filter_drain_message(logdog, pattern, matches):
    with logdog() as pile:
        logging.error("one two three")

    assert pile.filter(message=pattern).is_empty() == (not matches)
    assert not pile.is_empty()

    assert pile.drain(message=pattern).is_empty() == (not matches)
    assert pile.is_empty() == matches


@pytest.mark.parametrize(
    "exc_info, matches",
    [
        (None, True),
        (False, False),
        (True, True),
        (0, False),
        (1, True),
        (object(), True),
        (ZeroDivisionError, True),
        (Exception, True),
        (RuntimeError, False),
        ((ValueError, ArithmeticError), True),
        ((ValueError, TypeError), False),
    ],
)
def test_filter_drain_exc_info_exception(logdog, exc_info, matches):
    with logdog() as pile:
        try:
            1 / 0
        except:
            logging.exception("Error")

    assert pile.filter(exc_info=exc_info).is_empty() == (not matches)
    assert not pile.is_empty()

    assert pile.drain(exc_info=exc_info).is_empty() == (not matches)
    assert pile.is_empty() == matches


@pytest.mark.parametrize(
    "exc_info, matches",
    [
        (None, True),
        (False, True),
        (True, False),
        (0, True),
        (1, False),
        (object(), False),
        (Exception, False),
        ((ValueError, TypeError), False),
    ],
)
def test_filter_drain_exc_info_no_exception(logdog, exc_info, matches):
    with logdog() as pile:
        logging.error("Error")

    assert pile.filter(exc_info=exc_info).is_empty() == (not matches)
    assert not pile.is_empty()

    assert pile.drain(exc_info=exc_info).is_empty() == (not matches)
    assert pile.is_empty() == matches


@pytest.mark.parametrize(
    "log_stack_info, filter_stack_info, matches",
    [
        (None, None, True),
        (False, None, True),
        (True, None, True),
        (None, False, True),
        (None, True, False),
        (False, False, True),
        (False, 0, True),
        (False, True, False),
        (False, 1, False),
        (False, object(), False),
        (True, False, False),
        (True, 0, False),
        (True, True, True),
        (True, 1, True),
        (True, object(), True),
    ],
)
def test_filter_drain_stack_info(
    logdog, log_stack_info, filter_stack_info, matches
):
    with logdog() as pile:
        logging.error("Error", stack_info=log_stack_info)

    assert pile.filter(stack_info=filter_stack_info).is_empty() == (not matches)
    assert not pile.is_empty()

    assert pile.drain(stack_info=filter_stack_info).is_empty() == (not matches)
    assert pile.is_empty() == matches