def test_deprecation(self): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") Delivery(base_config) self.assertEqual(len(w), 1, "No, or more than one, warning issued.") self.assertTrue(issubclass(w[-1].category, DeprecationWarning), "Category of warning is not DeprecationWarning.") self.assertTrue('deprecated' in str(w[-1].message), "Warning does not include 'deprecated'.") self.assertTrue('Mailer' in str(w[-1].message), "Warning does not include correct class name.") self.assertTrue('Delivery' in str(w[-1].message), "Warning does not include old class name.")
def test_startup(self): messages = logging.getLogger().handlers[0].buffer interface = Delivery(base_config) interface.start() self.assertEqual(len(messages), 4) self.assertEqual(messages[0].getMessage(), "Mail delivery service starting.") self.assertEqual(messages[-1].getMessage(), "Mail delivery service started.") interface.start() self.assertEqual(len(messages), 5) self.assertEqual(messages[-1].getMessage(), "Attempt made to start an already running delivery service.") interface.stop()
def test_shutdown(self): interface = Delivery(base_config) interface.start() logging.getLogger().handlers[0].truncate() messages = logging.getLogger().handlers[0].buffer interface.stop() self.assertEqual(len(messages), 4) self.assertEqual(messages[0].getMessage(), "Mail delivery service stopping.") self.assertEqual(messages[-1].getMessage(), "Mail delivery service stopped.") interface.stop() self.assertEqual(len(messages), 5) self.assertEqual(messages[-1].getMessage(), "Attempt made to stop an already stopped delivery service.")
def test_send(self): message = Bunch(id='foo') interface = Delivery(base_config) self.assertRaises(MailerNotRunning, lambda: interface.send(message)) interface.start() logging.getLogger().handlers[0].truncate() messages = logging.getLogger().handlers[0].buffer self.assertEqual(interface.send(message), (message, True)) self.assertEqual(messages[0].getMessage(), "Attempting delivery of message foo.") self.assertEqual(messages[-1].getMessage(), "Message foo delivered.") message_fail = Bunch(id='bar', die=True) self.assertRaises(Exception, lambda: interface.send(message_fail)) self.assertEqual(messages[-2].getMessage(), "Attempting delivery of message bar.") self.assertEqual(messages[-1].getMessage(), "Delivery of message bar failed.") interface.stop()
def test_load_entrypoint(self): self.assertEqual(Delivery._load('immediate', 'marrow.mailer.manager'), ImmediateManager)
def test_load_dotcolon(self): self.assertEqual(Delivery._load('marrow.mailer.manager.immediate:ImmediateManager', None), ImmediateManager)
def test_load_literal(self): self.assertEqual(Delivery._load(ImmediateManager, None), ImmediateManager)
import logging from marrow.mailer import Message, Delivery logging.basicConfig(level=logging.INFO) mail = Delivery({ 'manager': 'futures', 'transport': 'smtp', 'transport.host': '', 'transport.tls': 'ssl', 'transport.username': '', 'transport.password': '', 'transport.max_messages_per_connection': 5 }) mail.start() message = Message([('Alice Bevan-McGregor', '*****@*****.**')], [('Alice Two', '*****@*****.**')], "This is a test message.", plain="Testing!") mail.send(message) mail.stop()