|
| 1 | +import logging |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from docstub._report import ContextReporter |
| 7 | + |
| 8 | + |
| 9 | +class Test_ContextReporter: |
| 10 | + @pytest.mark.parametrize("level", ["debug", "info", "warn", "error"]) |
| 11 | + def test_basic(self, level, caplog): |
| 12 | + caplog.set_level(logging.DEBUG) |
| 13 | + logger = logging.getLogger(__name__) |
| 14 | + rep = ContextReporter(logger=logger) |
| 15 | + |
| 16 | + func = getattr(rep, level) |
| 17 | + |
| 18 | + func("Message") |
| 19 | + assert len(caplog.records) == 1 |
| 20 | + |
| 21 | + record = caplog.records[0] |
| 22 | + assert record.message == "Message" |
| 23 | + assert record.details is None |
| 24 | + assert not hasattr(record, "src_location") |
| 25 | + assert record.levelname.startswith(level.upper()) |
| 26 | + |
| 27 | + def test_details(self, caplog): |
| 28 | + logger = logging.getLogger(__name__) |
| 29 | + rep = ContextReporter(logger=logger) |
| 30 | + |
| 31 | + rep.info("Message", details="More\nmultiline details.") |
| 32 | + assert len(caplog.records) == 1 |
| 33 | + |
| 34 | + record = caplog.records[0] |
| 35 | + assert record.message == "Message" |
| 36 | + assert record.details == "More\nmultiline details." |
| 37 | + assert not hasattr(record, "src_location") |
| 38 | + |
| 39 | + def test_src_location(self, caplog): |
| 40 | + logger = logging.getLogger(__name__) |
| 41 | + rep = ContextReporter(logger=logger, path=Path("foo.py"), line=3) |
| 42 | + |
| 43 | + rep.info("Message") |
| 44 | + assert len(caplog.records) == 1 |
| 45 | + |
| 46 | + record = caplog.records[0] |
| 47 | + assert record.message == "Message" |
| 48 | + assert record.details is None |
| 49 | + assert record.src_location == "foo.py:3" |
| 50 | + |
| 51 | + def test_copy_with(self, caplog): |
| 52 | + logger = logging.getLogger(__name__) |
| 53 | + |
| 54 | + rep = ContextReporter(logger=logger, path=Path("foo.py"), line=3) |
| 55 | + rep_new_path = rep.copy_with(path=Path("bar.py")) |
| 56 | + rep_new_line = rep.copy_with(line=7) |
| 57 | + rep_line_offset = rep.copy_with(line_offset=8) |
| 58 | + |
| 59 | + rep_new_path.info("Message") |
| 60 | + rep_new_line.info("Message") |
| 61 | + rep_line_offset.info("Message") |
| 62 | + assert len(caplog.records) == 3 |
| 63 | + |
| 64 | + assert caplog.records[0].src_location == "bar.py:3" |
| 65 | + assert caplog.records[1].src_location == "foo.py:7" |
| 66 | + assert caplog.records[2].src_location == "foo.py:11" |
| 67 | + |
| 68 | + |
| 69 | +# TODO test ReportHandler |
0 commit comments