mirror of
https://github.com/django/django.git
synced 2025-08-03 10:34:04 +00:00
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:
parent
d902fd625d
commit
4aa80149e7
4 changed files with 52 additions and 1 deletions
|
@ -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'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue