Skip to content

Commit ce3083f

Browse files
author
Theekshna Kotian
committed
Merged PR 5345: Add tests for driver's custom logger
Add tests for driver's custom logger Related work items: #33972
1 parent b227c64 commit ce3083f

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

mssql_python/logging_config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
from logging.handlers import RotatingFileHandler
33
import os
4+
import sys
45

56
ENABLE_LOGGING = False
67

@@ -37,11 +38,11 @@ def setup_logging(mode='file', log_level=logging.DEBUG):
3738

3839
if mode == 'stdout':
3940
# If the mode is stdout, then we want to log to the console as well
40-
stdout_handler = logging.StreamHandler()
41+
stdout_handler = logging.StreamHandler(sys.stdout)
4142
stdout_handler.setLevel(log_level)
4243
stdout_handler.setFormatter(formatter)
4344
logger.addHandler(stdout_handler)
44-
else:
45+
elif mode != 'file':
4546
raise ValueError(f'Invalid logging mode: {mode}')
4647

4748
def get_logger():

tests/test_006_logging.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import logging
2+
import os
3+
import pytest
4+
from mssql_python.logging_config import setup_logging, get_logger, ENABLE_LOGGING
5+
6+
def get_log_file_path():
7+
repo_root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
8+
pid = os.getpid()
9+
log_file = os.path.join(repo_root_dir, "mssql_python", f"mssql_python_trace_{pid}.log")
10+
return log_file
11+
12+
@pytest.fixture
13+
def cleanup_logger():
14+
"""Cleanup logger & log files before and after each test"""
15+
def cleanup():
16+
logger = get_logger()
17+
if logger is not None:
18+
logger.handlers.clear()
19+
log_file_path = get_log_file_path()
20+
if os.path.exists(log_file_path):
21+
os.remove(log_file_path)
22+
ENABLE_LOGGING = False
23+
# Perform cleanup before the test
24+
cleanup()
25+
yield
26+
# Perform cleanup after the test
27+
cleanup()
28+
29+
def test_no_logging(cleanup_logger):
30+
"""Test that logging is off by default"""
31+
try:
32+
logger = get_logger()
33+
assert logger is None
34+
assert ENABLE_LOGGING == False
35+
except Exception as e:
36+
pytest.fail(f"Logging not off by default. Error: {e}")
37+
38+
def test_setup_logging(cleanup_logger):
39+
"""Test if logging is set up correctly"""
40+
try:
41+
setup_logging() # This must enable logging
42+
logger = get_logger()
43+
assert logger is not None
44+
assert logger == logging.getLogger('mssql_python.logging_config')
45+
assert logger.level == logging.DEBUG # DEBUG level
46+
except Exception as e:
47+
pytest.fail(f"Logging setup failed: {e}")
48+
49+
def test_logging_in_file_mode(cleanup_logger):
50+
"""Test if logging works correctly in file mode"""
51+
try:
52+
setup_logging()
53+
logger = get_logger()
54+
assert logger is not None
55+
# Log a test message
56+
test_message = "Testing file logging mode"
57+
logger.info(test_message)
58+
# Check if the log file is created and contains the test message
59+
log_file_path = get_log_file_path()
60+
assert os.path.exists(log_file_path), "Log file not created"
61+
# open the log file and check its content
62+
with open(log_file_path, 'r') as f:
63+
log_content = f.read()
64+
assert test_message in log_content, "Log message not found in log file"
65+
except Exception as e:
66+
pytest.fail(f"Logging in file mode failed: {e}")
67+
68+
def test_logging_in_stdout_mode(cleanup_logger, capsys):
69+
"""Test if logging works correctly in stdout mode"""
70+
try:
71+
setup_logging('stdout')
72+
logger = get_logger()
73+
assert logger is not None
74+
# Log a test message
75+
test_message = "Testing file + stdout logging mode"
76+
logger.info(test_message)
77+
# Check if the log file is created and contains the test message
78+
log_file_path = get_log_file_path()
79+
assert os.path.exists(log_file_path), "Log file not created in file+stdout mode"
80+
with open(log_file_path, 'r') as f:
81+
log_content = f.read()
82+
assert test_message in log_content, "Log message not found in log file"
83+
# Check if the message is printed to stdout
84+
captured_stdout = capsys.readouterr().out
85+
assert test_message in captured_stdout, "Log message not found in stdout"
86+
except Exception as e:
87+
pytest.fail(f"Logging in stdout mode failed: {e}")

0 commit comments

Comments
 (0)