Skip to content

Commit fbfc888

Browse files
authored
Adding pytests for logging decorator (#6)
1 parent 30090ba commit fbfc888

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

src/learning/__init__.py

Whitespace-only changes.

src/learning/tests/__init__.py

Whitespace-only changes.

src/learning/tests/mylog_test.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from learning.logging.mylog import LOG_FILE_NAME, Level, logme
2+
from unittest.mock import MagicMock, patch, call
3+
4+
@patch("builtins.open", create=True)
5+
def test_logme_for_none(mock_open):
6+
"""
7+
GIVEN logging for a function
8+
WHEN logme is called with no level set
9+
THEN there is no writing to output log file
10+
"""
11+
12+
# logme returns a wrapper function for the passed function
13+
wrapper = logme(print)()
14+
15+
# this is where the logic happens
16+
wrapper()
17+
18+
# no writing to log happened
19+
mock_open.assert_not_called()
20+
21+
@patch("builtins.open", create=True)
22+
def test_logme_for_info(mock_open):
23+
"""
24+
GIVEN logging for a function
25+
WHEN logme is called with INFO level
26+
THEN there is one write call to output log file
27+
"""
28+
29+
# logme returns a wrapper function for the passed function
30+
# and I am calling it directly
31+
logme(print, level=Level.INFO)()
32+
33+
# one write call to the log output
34+
mock_open.assert_called_with(LOG_FILE_NAME, "a+")
35+
36+
@patch("builtins.open", create=True)
37+
def test_logme_for_debug(mock_open):
38+
"""
39+
GIVEN logging for a function
40+
WHEN logme is called with INFO level
41+
THEN there are two write calls to output log file
42+
"""
43+
44+
# logme returns a wrapper function for the passed function
45+
# and I am calling it directly
46+
logme(print, level=Level.DEBUG)()
47+
48+
# in this case, two write calls to the log output
49+
assert mock_open.call_count == 2

0 commit comments

Comments
 (0)