33from unittest .mock import patch , MagicMock , ANY
44from src .log_structure import function_info
55
6- "test"
6+
7+ @patch ('src.log_structure.firehose_logger' )
8+ @patch ('src.log_structure.logger' )
79class TestFunctionInfoWrapper (unittest .TestCase ):
810
911 @staticmethod
10- def mock_success_function (event , context ):
12+ def mock_success_function (_event , _context ):
1113 return "Success"
12-
14+
1315 @staticmethod
14- def mock_function_raises (event , context ):
16+ def mock_function_raises (_event , _context ):
1517 raise ValueError ("Test error" )
1618
17- @patch ('src.log_structure.logger' )
18- def test_successful_execution (self , mock_logger ):
19+ def test_successful_execution (self , mock_logger , mock_firehose_logger ):
1920 # Arrange
2021 wrapped_function = function_info (self .mock_success_function )
2122 event = {
2223 'headers' : {
2324 'X-Correlation-ID' : 'test_correlation' ,
2425 'X-Request-ID' : 'test_request'
25- },
26- 'path' : '/test' ,
26+ },
27+ 'path' : '/test' ,
2728 'requestContext' : {'resourcePath' : '/test' }
2829 }
2930
@@ -33,6 +34,7 @@ def test_successful_execution(self, mock_logger):
3334 # Assert
3435 self .assertEqual (result , "Success" )
3536 mock_logger .info .assert_called ()
37+ mock_firehose_logger .send_log .assert_called ()
3638
3739 args , kwargs = mock_logger .info .call_args
3840 logged_info = json .loads (args [0 ])
@@ -43,9 +45,8 @@ def test_successful_execution(self, mock_logger):
4345 self .assertEqual (logged_info ['X-Request-ID' ], 'test_request' )
4446 self .assertEqual (logged_info ['actual_path' ], '/test' )
4547 self .assertEqual (logged_info ['resource_path' ], '/test' )
46-
47- @patch ('src.log_structure.logger' )
48- def test_exception_handling (self , mock_logger ):
48+
49+ def test_exception_handling (self , mock_logger , mock_firehose_logger ):
4950
5051 #Act
5152 decorated_function_raises = function_info (self .mock_function_raises )
@@ -55,20 +56,22 @@ def test_exception_handling(self, mock_logger):
5556 event = {'headers' : {
5657 'X-Correlation-ID' : 'failed_test_correlation' ,
5758 'X-Request-ID' : 'failed_test_request'
58- },
59+ },
5960 'path' : '/failed_test' , 'requestContext' : {'resourcePath' : '/failed_test' }}
6061 context = {}
6162 decorated_function_raises (event , context )
6263
6364 #Assert
6465 mock_logger .exception .assert_called ()
66+ mock_firehose_logger .send_log .assert_called ()
67+
6568 args , kwargs = mock_logger .exception .call_args
6669 logged_info = json .loads (args [0 ])
67-
70+
6871 self .assertIn ('function_name' , logged_info )
6972 self .assertIn ('time_taken' , logged_info )
7073 self .assertEqual (logged_info ['X-Correlation-ID' ], 'failed_test_correlation' )
7174 self .assertEqual (logged_info ['X-Request-ID' ], 'failed_test_request' )
7275 self .assertEqual (logged_info ['actual_path' ], '/failed_test' )
7376 self .assertEqual (logged_info ['resource_path' ], '/failed_test' )
74- self .assertEqual (logged_info ['error' ], str (ValueError ("Test error" )))
77+ self .assertEqual (logged_info ['error' ], str (ValueError ("Test error" )))
0 commit comments