#NEWLINE# def test_session_closed_after_exception( self ) : #NEWLINE# #INDENT# msg = mail . EmailMessage ( 'Subject' , 'Message' , 'from@example.com' , [ 'to1@example.com' ] ) #NEWLINE# msg . global_merge_vars = { #NEWLINE# #INDENT# 'PRICE' : Decimal ( '19.99' ) , #NEWLINE# #UNINDENT# } #NEWLINE# with patch ( 'requests.Session.close' , autospec = True ) as mock_close : #NEWLINE# #INDENT# with self . assertRaises ( TypeError ) : #NEWLINE# #INDENT# msg . send ( ) #NEWLINE# #UNINDENT# #UNINDENT# self . assertEqual ( mock_close . call_count , 1 ) #NEWLINE# msg = mail . EmailMessage ( 'Subject' , 'Message' , 'from@example.com' , [ 'to1@example.com' ] ) #NEWLINE# msg . global_merge_vars = { #NEWLINE# #INDENT# 'PRICE' : Decimal ( '19.99' ) , #NEWLINE# #UNINDENT# } #NEWLINE# with patch ( 'requests.Session.close' , autospec = True ) as mock_close : #NEWLINE# #INDENT# sent = msg . send ( fail_silently = True ) #NEWLINE# self . assertEqual ( sent , 0 ) #NEWLINE# #UNINDENT# self . assertEqual ( mock_close . call_count , 1 ) #NEWLINE# with patch ( 'requests.Session.close' , autospec = True ) as mock_close : #NEWLINE# #INDENT# connection = mail . get_connection ( ) #NEWLINE# connection . open ( ) #NEWLINE# msg = mail . EmailMessage ( 'Subject' , 'Message' , 'from@example.com' , [ 'to1@example.com' ] , connection = connection ) #NEWLINE# msg . global_merge_vars = { #NEWLINE# #INDENT# 'PRICE' : Decimal ( '19.99' ) , #NEWLINE# #UNINDENT# } #NEWLINE# with self . assertRaises ( TypeError ) : #NEWLINE# #INDENT# msg . send ( ) #NEWLINE# #UNINDENT# self . assertEqual ( mock_close . call_count , 0 ) #NEWLINE# connection . close ( ) #NEWLINE# self . assertEqual ( mock_close . call_count , 1 )