def test_success_logging(self): # setup and make sure we have the right mock backend logging_backend = LoggingEmailBackend() batch_id = logging_backend.batch_id eq_(logging_backend.real_backend.__class__, SucceedingMockEmailBackend) eq_(logging_backend.real_backend._is_open, False) eq_(logging_backend.real_backend._sent_messages, False) # Open a connection eq_(logging_backend.open(), True) self.expect_log("Batch %i - Succesfully opened new connection." % (batch_id, )) eq_(logging_backend.real_backend._is_open, True) # Send 3 messages messages = [ EmailMessage(subject="subject%i" % i, to=["*****@*****.**" % i]) for i in range(3) ] eq_(logging_backend.send_messages(messages), 3) log_msg = ( "Batch %i - Attempting to send 3 emails: subject0 - '*****@*****.**'\n" "\tsubject1 - '*****@*****.**'\n" "\tsubject2 - '*****@*****.**'" % (batch_id, )) self.expect_log(log_msg, check=False) self.expect_log("Batch %i - Succesfully sent 3 emails." % (batch_id, )) eq_(logging_backend.real_backend._sent_messages, True) # Close eq_(logging_backend.close(), None) self.expect_log("Batch %i - Closed connection." % (batch_id, )) eq_(logging_backend.real_backend._is_open, False)
def test_flaky_logging(self): # Setup and make sure we have the right mock backend logging_backend = LoggingEmailBackend() batch_id = logging_backend.batch_id eq_(logging_backend.real_backend.__class__, FlakyMockEmailBackend) eq_(logging_backend.real_backend._is_open, False) eq_(logging_backend.real_backend._sent_messages, False) # Open a connection eq_(logging_backend.open(), None) self.expect_log( "Batch %i - Opened a new connection. (Unknown status)" % (batch_id, )) eq_(logging_backend.real_backend._is_open, True) # Send 3 messages messages = [ EmailMessage(subject="subject%i" % i, to=["*****@*****.**" % i]) for i in range(3) ] eq_(logging_backend.send_messages(messages), 1) log_msg = ( "Batch %i - Attempting to send 3 emails: subject0 - '*****@*****.**'\n" "\tsubject1 - '*****@*****.**'\n" "\tsubject2 - '*****@*****.**'" % (batch_id, )) self.expect_log(log_msg, check=False) self.expect_log( "Batch %i - Failed to send all emails. Sent 1 out of 3." % (batch_id, ), level="ERROR") eq_(logging_backend.real_backend._sent_messages, True) # Close eq_(logging_backend.close(), None) self.expect_log("Batch %i - Closed connection." % (batch_id, )) eq_(logging_backend.real_backend._is_open, False)
def test_failing_logging(self): # Setup and make sure we have the right mock backend logging_backend = LoggingEmailBackend() batch_id = logging_backend.batch_id eq_(logging_backend.real_backend.__class__, FailingMockEmailBackend) eq_(logging_backend.real_backend._is_open, False) eq_(logging_backend.real_backend._sent_messages, False) # Open a connection eq_(logging_backend.open(), False) self.expect_log( 'Batch %i - Did not open a new connection. (Either cached or failed)' % (batch_id, )) eq_(logging_backend.real_backend._is_open, True) # Send 3 messages messages = [ EmailMessage(subject='subject%i' % i, to=['*****@*****.**' % i]) for i in range(3) ] eq_(logging_backend.send_messages(messages), 0) log_msg = ( "Batch %i - Attempting to send 3 emails: subject0 - '*****@*****.**'\n" "\tsubject1 - '*****@*****.**'\n" "\tsubject2 - '*****@*****.**'" % (batch_id, )) self.expect_log(log_msg, check=False) self.expect_log( 'Batch %i - Failed to send all emails. Sent 0 out of 3.' % (batch_id, ), level='ERROR') eq_(logging_backend.real_backend._sent_messages, True) # Close eq_(logging_backend.close(), None) self.expect_log('Batch %i - Closed connection.' % (batch_id, )) eq_(logging_backend.real_backend._is_open, False)