def test_exception(self): transaction.enter_transaction_management() Band.objects.create(name='The Beatles') self.assertTrue(transaction.is_dirty()) TransactionMiddleware().process_exception(self.request, None) self.assertFalse(transaction.is_dirty()) self.assertEqual(Band.objects.count(), 0)
def test_managed_response(self): transaction.enter_transaction_management() transaction.managed(True) Band.objects.create(name='The Beatles') self.assertTrue(transaction.is_dirty()) TransactionMiddleware().process_response(self.request, self.response) self.assertFalse(transaction.is_dirty()) self.assertEqual(Band.objects.count(), 1)
def test_failing_commit(self): # It is possible that connection.commit() fails. Check that # TransactionMiddleware handles such cases correctly. try: def raise_exception(): raise IntegrityError() connections[DEFAULT_DB_ALIAS].commit = raise_exception transaction.enter_transaction_management() Band.objects.create(name='The Beatles') self.assertTrue(transaction.is_dirty()) with self.assertRaises(IntegrityError): TransactionMiddleware().process_response(self.request, None) self.assertFalse(transaction.is_dirty()) self.assertEqual(Band.objects.count(), 0) self.assertFalse(transaction.is_managed()) finally: del connections[DEFAULT_DB_ALIAS].commit
def test_unmanaged_response(self): transaction.managed(False) TransactionMiddleware().process_response(self.request, self.response) self.assertFalse(transaction.is_managed()) self.assertFalse(transaction.is_dirty())
def test_request(self): TransactionMiddleware().process_request(self.request) self.assertTrue(transaction.is_managed())
def test_request(self): TransactionMiddleware().process_request(self.request) self.assertFalse(transaction.get_autocommit())