@@ -110,33 +110,33 @@ def test_generate_and_send_logs(self):
110110 additional_log_data = {"additional_key" : "additional_value" }
111111 start_time = 1672531200
112112
113- # CASE: Successful log - is_error_log arg set to False
114- with ( # noqa: E999
115- patch ("logging_decorator.logger" ) as mock_logger , # noqa: E999
116- patch ("logging_decorator.send_log_to_firehose" ) as mock_send_log_to_firehose , # noqa: E999
117- patch ("logging_decorator.time" ) as mock_time , # noqa: E999
118- ): # noqa: E999
119- mock_time .time .return_value = 1672531200.123456 # Mocks the end time to be 0.123456s after the start time
120- generate_and_send_logs (start_time , base_log_data , additional_log_data , is_error_log = False )
121-
122- expected_log_data = {"base_key" : "base_value" , "time_taken" : "0.12346s" , "additional_key" : "additional_value" }
123- log_data = json .loads (mock_logger .info .call_args [0 ][0 ])
124- self .assertEqual (log_data , expected_log_data )
125- mock_send_log_to_firehose .assert_called_once_with (expected_log_data )
126-
127- # CASE: Error log - is_error_log arg set to True
128- with ( # noqa: E999
129- patch ("logging_decorator.logger" ) as mock_logger , # noqa: E999
130- patch ("logging_decorator.send_log_to_firehose" ) as mock_send_log_to_firehose , # noqa: E999
131- patch ("logging_decorator.time" ) as mock_time , # noqa: E999
132- ): # noqa: E999
133- mock_time .time .return_value = 1672531200.123456 # Mocks the end time to be 0.123456s after the start time
134- generate_and_send_logs (start_time , base_log_data , additional_log_data , is_error_log = True )
113+ test_cases = [
114+ ("Using standard log and seconds precision" , False , False ,
115+ {"base_key" : "base_value" , "time_taken" : "0.12346s" , "additional_key" : "additional_value" }),
116+ ("Using error log and seconds precision" , True , False ,
117+ {"base_key" : "base_value" , "time_taken" : "0.12346s" , "additional_key" : "additional_value" }),
118+ ("Using standard log and milliseconds precision" , False , True ,
119+ {"base_key" : "base_value" , "time_taken" : "123.456ms" , "additional_key" : "additional_value" })
120+ ]
135121
136- expected_log_data = {"base_key" : "base_value" , "time_taken" : "0.12346s" , "additional_key" : "additional_value" }
137- log_data = json .loads (mock_logger .error .call_args [0 ][0 ])
138- self .assertEqual (log_data , expected_log_data )
139- mock_send_log_to_firehose .assert_called_once_with (expected_log_data )
122+ for test_desc , use_error_log , use_ms_precision , expected_log_data in test_cases :
123+ with self .subTest (test_desc ):
124+ with ( # noqa: E999
125+ patch ("logging_decorator.logger" ) as mock_logger , # noqa: E999
126+ patch ("logging_decorator.send_log_to_firehose" ) as mock_send_log_to_firehose , # noqa: E999
127+ patch ("logging_decorator.time" ) as mock_time , # noqa: E999
128+ ): # noqa: E999
129+ mock_time .time .return_value = 1672531200.123456 # Mocks end time to be 0.123456s after start
130+ generate_and_send_logs (start_time , base_log_data , additional_log_data , is_error_log = use_error_log ,
131+ use_ms_precision = use_ms_precision )
132+
133+ if use_error_log :
134+ log_data = json .loads (mock_logger .error .call_args [0 ][0 ])
135+ else :
136+ log_data = json .loads (mock_logger .info .call_args [0 ][0 ])
137+
138+ self .assertEqual (log_data , expected_log_data )
139+ mock_send_log_to_firehose .assert_called_once_with (expected_log_data )
140140
141141 def test_logging_successful_validation (self ):
142142 """Tests that the correct logs are sent to cloudwatch and splunk when file validation is successful"""
@@ -156,7 +156,7 @@ def test_logging_successful_validation(self):
156156 expected_log_data = {
157157 "function_name" : "filename_processor_handle_record" ,
158158 "date_time" : fixed_datetime .strftime ("%Y-%m-%d %H:%M:%S" ),
159- "time_taken" : "1.0s " ,
159+ "time_taken" : "1000.0ms " ,
160160 "statusCode" : 200 ,
161161 "message" : "Successfully sent to SQS for further processing" ,
162162 "file_key" : FILE_DETAILS .file_key ,
@@ -188,7 +188,7 @@ def test_logging_failed_validation(self):
188188 expected_log_data = {
189189 "function_name" : "filename_processor_handle_record" ,
190190 "date_time" : fixed_datetime .strftime ("%Y-%m-%d %H:%M:%S" ),
191- "time_taken" : "1.0s " ,
191+ "time_taken" : "1000.0ms " ,
192192 "statusCode" : 403 ,
193193 "message" : "Infrastructure Level Response Value - Processing Error" ,
194194 "file_key" : FILE_DETAILS .file_key ,
0 commit comments