def test___call___fail_rollback_failure(self, Session, log_exception): """ Ensure that __call__() correctly handles the failure case when rolling back itself fails. If the wrapped code raises an Exception *and* session.rollback() itself raises an Exception, __call__() should log the failure to roll back, and then close and remove the Session, and should raise the original Exception again. """ tsm = util.TransactionalSessionMaker() exception = ValueError("u can't do that lol") # Now let's make it super bad by having rollback raise an Exception Session.return_value.rollback.side_effect = IOError( "lol now u can't connect to the db") with self.assertRaises(ValueError) as exc_context: with tsm(): raise exception log_exception.assert_called_once_with( 'An Exception was raised while rolling back a transaction.') self.assertTrue(exc_context.exception is exception) self.assertEqual(Session.return_value.commit.call_count, 0) Session.return_value.rollback.assert_called_once_with() Session.return_value.close.assert_called_once_with() Session.remove.assert_called_once_with()
def test___call___success(self, Session, log_exception): """ Ensure that __call__() correctly handles the success case. __call__() should commit the transaction, and close and remove the Session upon a successful operation. """ tsm = util.TransactionalSessionMaker() with tsm(): pass self.assertEqual(log_exception.call_count, 0) self.assertEqual(Session.return_value.rollback.call_count, 0) Session.return_value.commit.assert_called_once_with() Session.return_value.close.assert_called_once_with() Session.remove.assert_called_once_with()
def test___call___fail_rollback_success(self, Session, log_exception): """ Ensure that __call__() correctly handles the failure case when rolling back is successful. If the wrapped code raises an Exception, __call__() should roll back the transaction, and close and remove the Session, and should raise the original Exception again. """ tsm = util.TransactionalSessionMaker() exception = ValueError("u can't do that lol") with self.assertRaises(ValueError) as exc_context: with tsm(): raise exception self.assertEqual(log_exception.call_count, 0) self.assertTrue(exc_context.exception is exception) self.assertEqual(Session.return_value.commit.call_count, 0) Session.return_value.rollback.assert_called_once_with() Session.return_value.close.assert_called_once_with() Session.remove.assert_called_once_with()