Fixed #22327 -- Turned BaseEmailBackend into a context manager

Changed the BaseEmailBackend to allow usage as context manager to open
and close connections.
This commit is contained in:
Daniel Neuhäuser 2014-03-23 17:29:10 +01:00 committed by Tim Graham
parent d902fd625d
commit 4aa80149e7
4 changed files with 52 additions and 1 deletions

View file

@ -638,6 +638,27 @@ class BaseEmailBackendTests(HeadersCheckMixin, object):
except Exception as e:
self.fail("close() unexpectedly raised an exception: %s" % e)
def test_use_as_contextmanager(self):
"""
Test that the connection can be used as a contextmanager.
"""
opened = [False]
closed = [False]
conn = mail.get_connection(username='', password='')
def open():
opened[0] = True
conn.open = open
def close():
closed[0] = True
conn.close = close
with conn as same_conn:
self.assertTrue(opened[0])
self.assertIs(same_conn, conn)
self.assertFalse(closed[0])
self.assertTrue(closed[0])
class LocmemBackendTests(BaseEmailBackendTests, SimpleTestCase):
email_backend = 'django.core.mail.backends.locmem.EmailBackend'