tests: Clean up test_log
This commit is contained in:
parent
e447e2de65
commit
794bdf54a3
@ -109,69 +109,44 @@ def logger(log_counter):
|
|||||||
|
|
||||||
class TestLogFilter:
|
class TestLogFilter:
|
||||||
|
|
||||||
"""Tests for LogFilter."""
|
|
||||||
|
|
||||||
def _make_record(self, logger, name, level=logging.DEBUG):
|
def _make_record(self, logger, name, level=logging.DEBUG):
|
||||||
"""Create a bogus logging record with the supplied logger name."""
|
"""Create a bogus logging record with the supplied logger name."""
|
||||||
return logger.makeRecord(name, level=level, fn=None, lno=0, msg="",
|
return logger.makeRecord(name, level=level, fn=None, lno=0, msg="",
|
||||||
args=None, exc_info=None)
|
args=None, exc_info=None)
|
||||||
|
|
||||||
def test_empty(self, logger):
|
@pytest.mark.parametrize('filters, category, logged', [
|
||||||
"""Test if an empty filter lets all messages through."""
|
# Filter letting all messages through
|
||||||
logfilter = log.LogFilter(None)
|
(None, 'eggs.bacon.spam', True),
|
||||||
record = self._make_record(logger, "eggs.bacon.spam")
|
(None, 'eggs', True),
|
||||||
assert logfilter.filter(record)
|
# Matching records
|
||||||
record = self._make_record(logger, "eggs")
|
(['eggs', 'bacon'], 'eggs', True),
|
||||||
assert logfilter.filter(record)
|
(['eggs', 'bacon'], 'bacon', True),
|
||||||
|
(['eggs.bacon'], 'eggs.bacon', True),
|
||||||
|
# Non-matching records
|
||||||
|
(['eggs', 'bacon'], 'spam', False),
|
||||||
|
(['eggs'], 'eggsauce', False),
|
||||||
|
(['eggs.bacon'], 'eggs.baconstrips', False),
|
||||||
|
# Child loggers
|
||||||
|
(['eggs.bacon', 'spam.ham'], 'eggs.bacon.spam', True),
|
||||||
|
(['eggs.bacon', 'spam.ham'], 'spam.ham.salami', True),
|
||||||
|
])
|
||||||
|
def test_logfilter(self, logger, filters, category, logged):
|
||||||
|
logfilter = log.LogFilter(filters)
|
||||||
|
record = self._make_record(logger, category)
|
||||||
|
assert logfilter.filter(record) == logged
|
||||||
|
|
||||||
def test_matching(self, logger):
|
@pytest.mark.parametrize('category', ['eggs', 'bacon'])
|
||||||
"""Test if a filter lets an exactly matching log record through."""
|
def test_debug(self, logger, category):
|
||||||
logfilter = log.LogFilter(["eggs", "bacon"])
|
|
||||||
record = self._make_record(logger, "eggs")
|
|
||||||
assert logfilter.filter(record)
|
|
||||||
record = self._make_record(logger, "bacon")
|
|
||||||
assert logfilter.filter(record)
|
|
||||||
record = self._make_record(logger, "spam")
|
|
||||||
assert not logfilter.filter(record)
|
|
||||||
logfilter = log.LogFilter(["eggs.bacon"])
|
|
||||||
record = self._make_record(logger, "eggs.bacon")
|
|
||||||
assert logfilter.filter(record)
|
|
||||||
|
|
||||||
def test_equal_start(self, logger):
|
|
||||||
"""Test if a filter blocks a logger which looks equal but isn't."""
|
|
||||||
logfilter = log.LogFilter(["eggs"])
|
|
||||||
record = self._make_record(logger, "eggsauce")
|
|
||||||
assert not logfilter.filter(record)
|
|
||||||
logfilter = log.LogFilter("eggs.bacon")
|
|
||||||
record = self._make_record(logger, "eggs.baconstrips")
|
|
||||||
assert not logfilter.filter(record)
|
|
||||||
|
|
||||||
def test_child(self, logger):
|
|
||||||
"""Test if a filter lets through a logger which is a child."""
|
|
||||||
logfilter = log.LogFilter(["eggs.bacon", "spam.ham"])
|
|
||||||
record = self._make_record(logger, "eggs.bacon.spam")
|
|
||||||
assert logfilter.filter(record)
|
|
||||||
record = self._make_record(logger, "spam.ham.salami")
|
|
||||||
assert logfilter.filter(record)
|
|
||||||
|
|
||||||
def test_debug(self, logger):
|
|
||||||
"""Test if messages more important than debug are never filtered."""
|
"""Test if messages more important than debug are never filtered."""
|
||||||
logfilter = log.LogFilter(["eggs"])
|
logfilter = log.LogFilter(['eggs'])
|
||||||
# First check if the filter works as intended with debug messages
|
record = self._make_record(logger, category, level=logging.INFO)
|
||||||
record = self._make_record(logger, "eggs")
|
|
||||||
assert logfilter.filter(record)
|
|
||||||
record = self._make_record(logger, "bacon")
|
|
||||||
assert not logfilter.filter(record)
|
|
||||||
# Then check if info is not filtered
|
|
||||||
record = self._make_record(logger, "eggs", level=logging.INFO)
|
|
||||||
assert logfilter.filter(record)
|
|
||||||
record = self._make_record(logger, "bacon", level=logging.INFO)
|
|
||||||
assert logfilter.filter(record)
|
assert logfilter.filter(record)
|
||||||
|
|
||||||
@pytest.mark.parametrize('category, logged_before, logged_after', [
|
@pytest.mark.parametrize('category, logged_before, logged_after', [
|
||||||
('init', True, False), ('url', False, True), ('js', False, True)])
|
('init', True, False), ('url', False, True), ('js', False, True)])
|
||||||
def test_debug_log_filter_cmd(self, monkeypatch, logger, category,
|
def test_debug_log_filter_cmd(self, monkeypatch, logger, category,
|
||||||
logged_before, logged_after):
|
logged_before, logged_after):
|
||||||
|
"""Test the :debug-log-filter command handler."""
|
||||||
logfilter = log.LogFilter(["init"])
|
logfilter = log.LogFilter(["init"])
|
||||||
monkeypatch.setattr(log, 'console_filter', logfilter)
|
monkeypatch.setattr(log, 'console_filter', logfilter)
|
||||||
|
|
||||||
@ -182,41 +157,24 @@ class TestLogFilter:
|
|||||||
assert logfilter.filter(record) == logged_after
|
assert logfilter.filter(record) == logged_after
|
||||||
|
|
||||||
|
|
||||||
class TestRAMHandler:
|
@pytest.mark.parametrize('data, expected', [
|
||||||
|
# Less data
|
||||||
|
(['one'], ['one']),
|
||||||
|
# Exactly filled
|
||||||
|
(['one', 'two'], ['one', 'two']),
|
||||||
|
# More data
|
||||||
|
(['one', 'two', 'three'], ['two', 'three']),
|
||||||
|
])
|
||||||
|
def test_ram_handler(logger, data, expected):
|
||||||
|
handler = log.RAMHandler(capacity=2)
|
||||||
|
handler.setLevel(logging.NOTSET)
|
||||||
|
logger.addHandler(handler)
|
||||||
|
|
||||||
"""Tests for RAMHandler."""
|
for line in data:
|
||||||
|
logger.debug(line)
|
||||||
|
|
||||||
@pytest.fixture
|
assert [rec.msg for rec in handler._data] == expected
|
||||||
def handler(self, logger):
|
assert handler.dump_log() == '\n'.join(expected)
|
||||||
"""Fixture providing a RAMHandler."""
|
|
||||||
handler = log.RAMHandler(capacity=2)
|
|
||||||
handler.setLevel(logging.NOTSET)
|
|
||||||
logger.addHandler(handler)
|
|
||||||
return handler
|
|
||||||
|
|
||||||
def test_filled(self, handler, logger):
|
|
||||||
"""Test handler with exactly as much records as it can hold."""
|
|
||||||
logger.debug("One")
|
|
||||||
logger.debug("Two")
|
|
||||||
assert len(handler._data) == 2
|
|
||||||
assert handler._data[0].msg == "One"
|
|
||||||
assert handler._data[1].msg == "Two"
|
|
||||||
|
|
||||||
def test_overflow(self, handler, logger):
|
|
||||||
"""Test handler with more records as it can hold."""
|
|
||||||
logger.debug("One")
|
|
||||||
logger.debug("Two")
|
|
||||||
logger.debug("Three")
|
|
||||||
assert len(handler._data) == 2
|
|
||||||
assert handler._data[0].msg == "Two"
|
|
||||||
assert handler._data[1].msg == "Three"
|
|
||||||
|
|
||||||
def test_dump_log(self, handler, logger):
|
|
||||||
"""Test dump_log()."""
|
|
||||||
logger.debug("One")
|
|
||||||
logger.debug("Two")
|
|
||||||
logger.debug("Three")
|
|
||||||
assert handler.dump_log() == "Two\nThree"
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.integration
|
@pytest.mark.integration
|
||||||
@ -250,38 +208,27 @@ class TestHideQtWarning:
|
|||||||
"""Tests for hide_qt_warning/QtWarningFilter."""
|
"""Tests for hide_qt_warning/QtWarningFilter."""
|
||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
def logger(self):
|
def qt_logger(self):
|
||||||
return logging.getLogger('qt-tests')
|
return logging.getLogger('qt-tests')
|
||||||
|
|
||||||
def test_unfiltered(self, logger, caplog):
|
def test_unfiltered(self, qt_logger, caplog):
|
||||||
"""Test a message which is not filtered."""
|
|
||||||
with log.hide_qt_warning("World", 'qt-tests'):
|
with log.hide_qt_warning("World", 'qt-tests'):
|
||||||
with caplog.at_level(logging.WARNING, 'qt-tests'):
|
with caplog.at_level(logging.WARNING, 'qt-tests'):
|
||||||
logger.warning("Hello World")
|
qt_logger.warning("Hello World")
|
||||||
assert len(caplog.records) == 1
|
assert len(caplog.records) == 1
|
||||||
record = caplog.records[0]
|
record = caplog.records[0]
|
||||||
assert record.levelname == 'WARNING'
|
assert record.levelname == 'WARNING'
|
||||||
assert record.message == "Hello World"
|
assert record.message == "Hello World"
|
||||||
|
|
||||||
def test_filtered_exact(self, logger, caplog):
|
@pytest.mark.parametrize('line', [
|
||||||
"""Test a message which is filtered (exact match)."""
|
"Hello", # exact match
|
||||||
|
"Hello World", # match at start of line
|
||||||
|
" Hello World ", # match with spaces
|
||||||
|
])
|
||||||
|
def test_filtered(self, qt_logger, caplog, line):
|
||||||
with log.hide_qt_warning("Hello", 'qt-tests'):
|
with log.hide_qt_warning("Hello", 'qt-tests'):
|
||||||
with caplog.at_level(logging.WARNING, 'qt-tests'):
|
with caplog.at_level(logging.WARNING, 'qt-tests'):
|
||||||
logger.warning("Hello")
|
qt_logger.warning(line)
|
||||||
assert not caplog.records
|
|
||||||
|
|
||||||
def test_filtered_start(self, logger, caplog):
|
|
||||||
"""Test a message which is filtered (match at line start)."""
|
|
||||||
with log.hide_qt_warning("Hello", 'qt-tests'):
|
|
||||||
with caplog.at_level(logging.WARNING, 'qt-tests'):
|
|
||||||
logger.warning("Hello World")
|
|
||||||
assert not caplog.records
|
|
||||||
|
|
||||||
def test_filtered_whitespace(self, logger, caplog):
|
|
||||||
"""Test a message which is filtered (match with whitespace)."""
|
|
||||||
with log.hide_qt_warning("Hello", 'qt-tests'):
|
|
||||||
with caplog.at_level(logging.WARNING, 'qt-tests'):
|
|
||||||
logger.warning(" Hello World ")
|
|
||||||
assert not caplog.records
|
assert not caplog.records
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user