def _test_notificator_call(self, logs: Dict, experiment_name=None): experiment_name_text = f" for {experiment_name}" if experiment_name is not None else "" call_list = [] call_list.append(call.send_notification('', subject=f'Start of the training{experiment_name_text}.')) for batch_log in logs: formatted_log_data = " ".join([f"{key}: {value}\n" for key, value in batch_log.items()]) call_list.append( call.send_notification( f"Here the epoch metrics: \n{formatted_log_data}", subject=f"Epoch {batch_log['epoch']} is done{experiment_name_text}.", ) ) call_list.append(call.send_notification('', subject=f'End of the training{experiment_name_text}.')) method_calls = self.notificator_mock.method_calls self.assertEqual(len(method_calls), len(call_list)) # for set_model and set param self.assertEqual(method_calls, call_list)
def test_givenAFailDecorator_whenRunFunRaiseAnyError_thenSendNotificatorErrorMessage( self, ): self._capture_output() expected_error = ValueError @notification_on_fail(self.notif, verbose_level=self.a_verbose_level) def fail_test(): raise expected_error with self.assertRaises(expected_error): fail_test() self.notif.assert_has_calls([call.send_notification(message=ANY)])
def test_givenAFailDecorator_whenRunFunRaiseAnyErrorVerbose3_thenSendNotificatorErrorMessageAccordingVerboseLevel( self, ): expected_error_message = "An error message" expected_type = ValueError expected_error = expected_type(expected_error_message) @notification_on_fail(self.notif, verbose_level=self.a_high_level_verbose) def fail_test(): raise expected_error with self.assertRaises(ValueError): fail_test() self.notif.assert_has_calls([call.send_notification(message=ANY)])
def test_givenAFailDecorator_whenRunFunRaiseAnyErrorVerbose2_thenSendNotificatorErrorMessageAccordingVerboseLevel( self, ): expected_error_message = "An error message" expected_type = ValueError expected_error = expected_type(expected_error_message) @notification_on_fail(self.notif, verbose_level=self.a_mid_verbose_level) def fail_test(): raise expected_error with self.assertRaises(ValueError): fail_test() expected_message = f"An error occurred when running the script. The error message is: {expected_error_message}" self.notif.assert_has_calls( [call.send_notification(message=expected_message)])