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