From 794bdf54a3cadafd9de6b1c5b33b95af473e8ba7 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sat, 20 Aug 2016 21:57:15 +0200 Subject: [PATCH] tests: Clean up test_log --- tests/unit/utils/test_log.py | 155 ++++++++++++----------------------- 1 file changed, 51 insertions(+), 104 deletions(-) diff --git a/tests/unit/utils/test_log.py b/tests/unit/utils/test_log.py index c386b9694..0b5a2c59c 100644 --- a/tests/unit/utils/test_log.py +++ b/tests/unit/utils/test_log.py @@ -109,69 +109,44 @@ def logger(log_counter): class TestLogFilter: - """Tests for LogFilter.""" - def _make_record(self, logger, name, level=logging.DEBUG): """Create a bogus logging record with the supplied logger name.""" return logger.makeRecord(name, level=level, fn=None, lno=0, msg="", args=None, exc_info=None) - def test_empty(self, logger): - """Test if an empty filter lets all messages through.""" - logfilter = log.LogFilter(None) - record = self._make_record(logger, "eggs.bacon.spam") - assert logfilter.filter(record) - record = self._make_record(logger, "eggs") - assert logfilter.filter(record) + @pytest.mark.parametrize('filters, category, logged', [ + # Filter letting all messages through + (None, 'eggs.bacon.spam', True), + (None, 'eggs', True), + # Matching records + (['eggs', 'bacon'], 'eggs', True), + (['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): - """Test if a filter lets an exactly matching log record through.""" - 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): + @pytest.mark.parametrize('category', ['eggs', 'bacon']) + def test_debug(self, logger, category): """Test if messages more important than debug are never filtered.""" - logfilter = log.LogFilter(["eggs"]) - # First check if the filter works as intended with debug messages - 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) + logfilter = log.LogFilter(['eggs']) + record = self._make_record(logger, category, level=logging.INFO) assert logfilter.filter(record) @pytest.mark.parametrize('category, logged_before, logged_after', [ ('init', True, False), ('url', False, True), ('js', False, True)]) def test_debug_log_filter_cmd(self, monkeypatch, logger, category, logged_before, logged_after): + """Test the :debug-log-filter command handler.""" logfilter = log.LogFilter(["init"]) monkeypatch.setattr(log, 'console_filter', logfilter) @@ -182,41 +157,24 @@ class TestLogFilter: 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 - def handler(self, logger): - """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" + assert [rec.msg for rec in handler._data] == expected + assert handler.dump_log() == '\n'.join(expected) @pytest.mark.integration @@ -250,38 +208,27 @@ class TestHideQtWarning: """Tests for hide_qt_warning/QtWarningFilter.""" @pytest.fixture() - def logger(self): + def qt_logger(self): return logging.getLogger('qt-tests') - def test_unfiltered(self, logger, caplog): - """Test a message which is not filtered.""" + def test_unfiltered(self, qt_logger, caplog): with log.hide_qt_warning("World", 'qt-tests'): with caplog.at_level(logging.WARNING, 'qt-tests'): - logger.warning("Hello World") + qt_logger.warning("Hello World") assert len(caplog.records) == 1 record = caplog.records[0] assert record.levelname == 'WARNING' assert record.message == "Hello World" - def test_filtered_exact(self, logger, caplog): - """Test a message which is filtered (exact match).""" + @pytest.mark.parametrize('line', [ + "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 caplog.at_level(logging.WARNING, 'qt-tests'): - logger.warning("Hello") - 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 ") + qt_logger.warning(line) assert not caplog.records