@@ -11,10 +11,18 @@ class TestLogDecorator(unittest.TestCase):
1111 def setUp (self ):
1212 self .test_stream = "test-stream"
1313 self .test_prefix = "test"
14+ self .logger_info_patcher = patch ("common.log_decorator.logger.info" )
15+ self .mock_logger_info = self .logger_info_patcher .start ()
16+ self .logger_exception_patcher = patch ("common.log_decorator.logger.exception" )
17+ self .mock_logger_exception = self .logger_exception_patcher .start ()
18+ self .logger_error_patcher = patch ("common.log_decorator.logger.error" )
19+ self .mock_logger_error = self .logger_error_patcher .start ()
20+
21+ def tearDown (self ):
22+ patch .stopall ()
1423
1524 @patch ("common.log_decorator.firehose_client" )
16- @patch ("common.log_decorator.logger" )
17- def test_send_log_to_firehose_success (self , mock_logger , mock_firehose_client ):
25+ def test_send_log_to_firehose_success (self , mock_firehose_client ):
1826 """Test send_log_to_firehose with successful firehose response"""
1927 # Arrange
2028 test_log_data = {"function_name" : "test_func" , "result" : "success" }
@@ -30,12 +38,9 @@ def test_send_log_to_firehose_success(self, mock_logger, mock_firehose_client):
3038 DeliveryStreamName = self .test_stream ,
3139 Record = expected_record
3240 )
33- mock_logger .info .assert_called_once_with ("Log sent to Firehose: %s" , mock_response )
34- mock_logger .exception .assert_not_called ()
3541
3642 @patch ("common.log_decorator.firehose_client" )
37- @patch ("common.log_decorator.logger" )
38- def test_send_log_to_firehose_exception (self , mock_logger , mock_firehose_client ):
43+ def test_send_log_to_firehose_exception (self , mock_firehose_client ):
3944 """Test send_log_to_firehose with firehose exception"""
4045 # Arrange
4146 test_log_data = {"function_name" : "test_func" , "result" : "error" }
@@ -46,16 +51,14 @@ def test_send_log_to_firehose_exception(self, mock_logger, mock_firehose_client)
4651
4752 # Assert
4853 mock_firehose_client .put_record .assert_called_once ()
49- mock_logger . exception .assert_called_once_with (
54+ self . mock_logger_exception .assert_called_once_with (
5055 "Error sending log to Firehose: %s" ,
5156 mock_firehose_client .put_record .side_effect
5257 )
53- mock_logger .info .assert_not_called ()
5458
5559 @patch ("common.log_decorator.send_log_to_firehose" )
56- @patch ("common.log_decorator.logger" )
5760 @patch ("time.time" )
58- def test_generate_and_send_logs_success (self , mock_time , mock_logger , mock_send_log ):
61+ def test_generate_and_send_logs_success (self , mock_time , mock_send_log ):
5962 """Test generate_and_send_logs with successful log generation"""
6063 # Arrange
6164 mock_time .return_value = 1000.5
@@ -74,14 +77,12 @@ def test_generate_and_send_logs_success(self, mock_time, mock_logger, mock_send_
7477 "statusCode" : 200 ,
7578 "result" : "success"
7679 }
77- mock_logger .info .assert_called_once_with (json .dumps (expected_log_data ))
78- mock_logger .error .assert_not_called ()
80+ self .mock_logger_error .assert_not_called ()
7981 mock_send_log .assert_called_once_with (self .test_stream , expected_log_data )
8082
8183 @patch ("common.log_decorator.send_log_to_firehose" )
82- @patch ("common.log_decorator.logger" )
8384 @patch ("time.time" )
84- def test_generate_and_send_logs_error (self , mock_time , mock_logger , mock_send_log ):
85+ def test_generate_and_send_logs_error (self , mock_time , mock_send_log ):
8586 """Test generate_and_send_logs with error log generation"""
8687 # Arrange
8788 mock_time .return_value = 1000.75
@@ -100,15 +101,13 @@ def test_generate_and_send_logs_error(self, mock_time, mock_logger, mock_send_lo
100101 "statusCode" : 500 ,
101102 "error" : "Test error"
102103 }
103- mock_logger .error .assert_called_once_with (json .dumps (expected_log_data ))
104- mock_logger .info .assert_not_called ()
104+ self .mock_logger_error .assert_called_once_with (json .dumps (expected_log_data ))
105105 mock_send_log .assert_called_once_with (self .test_stream , expected_log_data )
106106
107107 @patch ("common.log_decorator.generate_and_send_logs" )
108- @patch ("common.log_decorator.logger" )
109108 @patch ("common.log_decorator.time" )
110109 @patch ("common.log_decorator.datetime" )
111- def test_logging_decorator_success (self , mock_datetime , mock_time , mock_logger , mock_generate_send ):
110+ def test_logging_decorator_success (self , mock_datetime , mock_time , mock_generate_send ):
112111 """Test logging_decorator with successful function execution"""
113112 # Arrange
114113 mock_datetime .now .return_value = datetime (2023 , 1 , 1 , 12 , 0 , 0 )
@@ -124,7 +123,6 @@ def test_function(x, y):
124123
125124 # Assert
126125 self .assertEqual (result , {"statusCode" : 200 , "result" : 5 })
127- mock_logger .info .assert_called_once_with ("Starting function: %s" , "test_function" )
128126
129127 # Verify generate_and_send_logs was called with correct parameters
130128 mock_generate_send .assert_called_once ()
@@ -137,10 +135,9 @@ def test_function(x, y):
137135 self .assertNotIn ("is_error_log" , call_kwargs ) # Should not be error log
138136
139137 @patch ("common.log_decorator.generate_and_send_logs" )
140- @patch ("common.log_decorator.logger" )
141138 @patch ("common.log_decorator.time" )
142139 @patch ("common.log_decorator.datetime" )
143- def test_logging_decorator_exception (self , mock_datetime , mock_time , mock_logger , mock_generate_send ):
140+ def test_logging_decorator_exception (self , mock_datetime , mock_time , mock_generate_send ):
144141 """Test logging_decorator with function raising exception"""
145142 # Arrange
146143 mock_datetime .now .return_value = datetime (2023 , 1 , 1 , 12 , 0 , 0 )
@@ -155,8 +152,6 @@ def test_function_with_error():
155152 with self .assertRaises (ValueError ):
156153 test_function_with_error ()
157154
158- mock_logger .info .assert_called_once_with ("Starting function: %s" , "test_function_with_error" )
159-
160155 # Verify generate_and_send_logs was called with error parameters
161156 mock_generate_send .assert_called_once ()
162157 call_args = mock_generate_send .call_args [0 ]
@@ -169,8 +164,7 @@ def test_function_with_error():
169164 self .assertTrue (call_kwargs .get ("is_error_log" , False )) # Should be error log
170165
171166 @patch ("common.log_decorator.generate_and_send_logs" )
172- @patch ("common.log_decorator.logger" )
173- def test_logging_decorator_preserves_function_metadata (self , mock_logger , mock_generate_send ):
167+ def test_logging_decorator_preserves_function_metadata (self , mock_generate_send ):
174168 """Test that the decorator preserves the original function's metadata"""
175169 # Arrange
176170 mock_generate_send .return_value = None
0 commit comments