@@ -25,10 +25,15 @@ def setup_mock_sqs(mock_boto_client, return_value={"ResponseMetadata": {"HTTPSta
2525 return mock_sqs
2626
2727 @staticmethod
28- def setup_mock_dynamodb (mock_boto_resource , status_code = 200 ):
28+ def setup_mock_dynamodb (mock_boto_resource , status_code = 200 , rasie_exception = False ):
2929 mock_dynamodb = mock_boto_resource .return_value
3030 mock_table = mock_dynamodb .Table .return_value
31- mock_table .put_item .return_value = {"ResponseMetadata" : {"HTTPStatusCode" : status_code }}
31+
32+ if rasie_exception :
33+ mock_table .put_item .side_effect = Exception ("Simulated DynamoDB failure" )
34+ else :
35+ mock_table .put_item .return_value = {"ResponseMetadata" : {"HTTPStatusCode" : status_code }}
36+
3237 return mock_table
3338
3439 def setUp_mock_resources (self , mock_boto_resource , mock_boto_client ):
@@ -39,7 +44,10 @@ def setUp_mock_resources(self, mock_boto_resource, mock_boto_client):
3944 return mock_table
4045
4146 @staticmethod
42- def get_event (event_name = "INSERT" , operation = "CREATE" , supplier = "EMIS" ):
47+ def get_event (event_name = "INSERT" , operation = "CREATE" , supplier = "EMIS" , is_empty = False ):
48+ if is_empty :
49+ return {}
50+
4351 if operation != "DELETE" :
4452 return {
4553 "Records" : [
@@ -116,8 +124,10 @@ def test_send_message_client_error(self, mock_logger_info, mock_boto_client):
116124 f"Error sending record to DLQ: An error occurred (500) when calling the SendMessage operation: Internal Server Error"
117125 )
118126
127+
128+ @patch ("src.delta.firehose_logger.send_log" )
119129 @patch ("boto3.resource" )
120- def test_handler_success_insert (self , mock_boto_resource ):
130+ def test_handler_success_insert (self , mock_boto_resource , mock_firehose_logger ):
121131 # Arrange
122132 self .setup_mock_dynamodb (mock_boto_resource )
123133 suppilers = ["DPS" , "EMIS" ]
@@ -130,8 +140,9 @@ def test_handler_success_insert(self, mock_boto_resource):
130140 # Assert
131141 self .assertEqual (result ["statusCode" ], 200 )
132142
143+ @patch ("src.delta.firehose_logger.send_log" )
133144 @patch ("boto3.resource" )
134- def test_handler_failure (self , mock_boto_resource ):
145+ def test_handler_failure (self , mock_boto_resource , mock_firehose_logger ):
135146 # Arrange
136147 self .setup_mock_dynamodb (mock_boto_resource , status_code = 500 )
137148 event = self .get_event ()
@@ -142,8 +153,9 @@ def test_handler_failure(self, mock_boto_resource):
142153 # Assert
143154 self .assertEqual (result ["statusCode" ], 500 )
144155
156+ @patch ("src.delta.firehose_logger.send_log" )
145157 @patch ("boto3.resource" )
146- def test_handler_success_update (self , mock_boto_resource ):
158+ def test_handler_success_update (self , mock_boto_resource , mock_firehose_logger ):
147159 # Arrange
148160 self .setup_mock_dynamodb (mock_boto_resource )
149161 event = self .get_event (event_name = "UPDATE" , operation = "UPDATE" )
@@ -154,8 +166,9 @@ def test_handler_success_update(self, mock_boto_resource):
154166 # Assert
155167 self .assertEqual (result ["statusCode" ], 200 )
156168
169+ @patch ("src.delta.firehose_logger.send_log" )
157170 @patch ("boto3.resource" )
158- def test_handler_success_remove (self , mock_boto_resource ):
171+ def test_handler_success_remove (self , mock_boto_resource , mock_firehose_logger ):
159172 # Arrange
160173 self .setup_mock_dynamodb (mock_boto_resource )
161174 event = self .get_event (event_name = "REMOVE" , operation = "DELETE" )
@@ -166,9 +179,10 @@ def test_handler_success_remove(self, mock_boto_resource):
166179 # Assert
167180 self .assertEqual (result ["statusCode" ], 200 )
168181
182+ @patch ("src.delta.firehose_logger.send_log" )
169183 @patch ("boto3.resource" )
170184 @patch ("boto3.client" )
171- def test_handler_exception_intrusion_check (self , mock_boto_resource , mock_boto_client ):
185+ def test_handler_exception_intrusion_check (self , mock_boto_resource , mock_boto_client , mock_firehose_logger ):
172186 # Arrange
173187 self .setup_mock_dynamodb (mock_boto_resource , status_code = 500 )
174188 mock_boto_client .return_value = MagicMock ()
@@ -179,32 +193,39 @@ def test_handler_exception_intrusion_check(self, mock_boto_resource, mock_boto_c
179193 result = handler (event , self .context )
180194 self .assertEqual (result ["statusCode" ], 500 )
181195
196+ @patch ("src.delta.logger.exception" )
197+ @patch ("src.delta.firehose_logger.send_log" )
182198 @patch ("boto3.resource" )
183199 @patch ("boto3.client" )
184- def test_handler_exception_intrusion (self , mock_boto_resource , mock_boto_client ):
200+ def test_handler_exception_intrusion (self , mock_boto_resource , mock_boto_client ,
201+ mock_firehose_logger , mock_logger_exception ):
185202 # Arrange
186- self .setUp_mock_resources (mock_boto_resource , mock_boto_client )
187- event = self .get_event ()
203+ self .setup_mock_dynamodb (mock_boto_resource )
204+ event = self .get_event (is_empty = True )
188205 context = {}
189206
190207 # Act & Assert
191- with self .assertRaises (Exception ):
208+ with self .assertRaises (Exception ) as e :
192209 handler (event , context )
193210
211+ @patch ("src.delta.logger.exception" )
212+ @patch ("src.delta.send_message" )
213+ @patch ("src.delta.firehose_logger.send_log" )
194214 @patch ("boto3.resource" )
195- @ patch ( "delta.handler" )
196- def test_handler_exception_intrusion_check_false ( self , mock_boto_resource , mock_boto_client ):
215+ def test_handler_exception_intrusion_check_false ( self , mock_boto_resource , mock_firehose_send_log ,
216+ mock_sqs_send_message , mock_logger_exception ):
197217 # Arrange
198- self .setUp_mock_resources (mock_boto_resource , mock_boto_client )
218+ self .setup_mock_dynamodb (mock_boto_resource , rasie_exception = True )
199219 event = self .get_event ()
200220 context = {}
201221
202222 # Act & Assert
203- with self .assertRaises (Exception ):
223+ with self .assertRaises (Exception ) as e :
204224 handler (event , context )
225+ self .assertTrue ("Simulated DynamoDB failure" in str (e .exception ))
205226
206- @patch ("delta.firehose_logger.send_log" ) # Mock Firehose logger
207- @patch ("delta.logger.info" ) # Mock logging
227+ @patch ("src. delta.firehose_logger.send_log" ) # Mock Firehose logger
228+ @patch ("src. delta.logger.info" ) # Mock logging
208229 def test_dps_record_skipped (self , mock_logger_info , mock_firehose_send_log ):
209230 event = self .get_event (supplier = "DPSFULL" )
210231 context = {}
@@ -219,10 +240,10 @@ def test_dps_record_skipped(self, mock_logger_info, mock_firehose_send_log):
219240 mock_logger_info .assert_called_with ("Record from DPS skipped for 12345" )
220241
221242 # TODO - amend test once error handling implemented
222- @patch ("delta.firehose_logger.send_log" )
223- @patch ("delta.logger.info" )
243+ @patch ("src. delta.firehose_logger.send_log" )
244+ @patch ("src. delta.logger.info" )
224245 @patch ("Converter.Converter" )
225- @patch ("delta.boto3.resource" )
246+ @patch ("src. delta.boto3.resource" )
226247 def test_partial_success_with_errors (self , mock_dynamodb , mock_converter , mock_logger_info , mock_firehose_send_log ):
227248 mock_converter_instance = MagicMock ()
228249 mock_converter_instance .runConversion .return_value = [{}]
0 commit comments