Skip to content

Commit 54ab6aa

Browse files
committed
Add tests for ContextReporter
1 parent c743b08 commit 54ab6aa

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

src/docstub/_report.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ class ContextReporter:
1717
1818
Attributes
1919
----------
20+
logger
2021
path :
2122
Path to a file for the current context.
2223
line :
2324
The line in the given file.
24-
column :
25-
The column in the given line.
2625
2726
Examples
2827
--------
@@ -52,7 +51,6 @@ class ContextReporter:
5251
logger: logging.Logger
5352
path: Path | None = None
5453
line: int | None = None
55-
column: int | None = None
5654

5755
def copy_with(self, *, logger=None, path=None, line=None, line_offset=None):
5856
"""Return a new copy with the modified attributes.

tests/test_report.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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

Comments
 (0)