Ticket #19442: transaction_behaviour.diff

File transaction_behaviour.diff, 2.8 KB (added by Anssi Kääriäinen, 12 years ago)
  • tests/modeltests/transactions/tests.py

    diff --git a/tests/modeltests/transactions/tests.py b/tests/modeltests/transactions/tests.py
    index f3246ee..c69feb7 100644
    a b from __future__ import absolute_import  
    22
    33from django.db import connection, transaction, IntegrityError
    44from django.test import TransactionTestCase, skipUnlessDBFeature
     5from unittest import skipUnless
    56
    67from .models import Reporter
    78
    class TransactionRollbackTests(TransactionTestCase):  
    177178        """
    178179        execute_bad_sql = transaction.commit_on_success(self.execute_bad_sql)
    179180        self.assertRaises(IntegrityError, execute_bad_sql)
     181
     182    @skipUnless(connection.vendor=='postgres', 'Needs Postgres...')
     183    def test_tx(self):
     184        from psycopg2 import extensions
     185        IN_TX = extensions.TRANSACTION_STATUS_IN_TX
     186        with transaction.commit_on_success():
     187            cursor = connection.cursor()
     188            cursor.execute("INSERT INTO transactions_reporter (first_name, last_name, email) VALUES ('Douglas', 'Adams', 'da@example.com');")
     189        # commit_on_success commits raw SQL
     190        transaction.rollback()
     191        self.assertTrue(Reporter.objects.filter(last_name='Adams').exists())
     192        Reporter.objects.filter(last_name='Adams').delete()
     193
     194        with transaction.autocommit():
     195            cursor = connection.cursor()
     196            cursor.execute("INSERT INTO transactions_reporter (first_name, last_name, email) VALUES ('Douglas', 'Adams', 'da@example.com');")
     197        # autocommit doesn't
     198        transaction.rollback()
     199        self.assertFalse(Reporter.objects.filter(last_name='Adams').exists())
     200        Reporter.objects.filter(last_name='Adams').delete()
     201
     202        with transaction.autocommit():
     203            Reporter.objects.create(first_name='Douglas', last_name='Adams',
     204                                    email='da@example.com')
     205        # But it does commit when writing using Django's ORM
    180206        transaction.rollback()
     207        self.assertTrue(Reporter.objects.filter(last_name='Adams').exists())
     208
     209        with transaction.commit_on_success():
     210            Reporter.objects.filter(last_name='Adams').exists()
     211        # commit_on_success also commits when only reading
     212        self.assertNotEqual(connection.connection.get_transaction_status(), IN_TX)
     213        self.assertFalse(transaction.is_dirty())
     214        with transaction.autocommit():
     215            Reporter.objects.filter(last_name='Adams').exists()
     216        # Using autocommit we are in transaction...
     217        self.assertEqual(connection.connection.get_transaction_status(), IN_TX)
     218        # But the transaction isn't dirty
     219        self.assertFalse(transaction.is_dirty())
     220
    181221
    182222class TransactionContextManagerTests(TransactionTestCase):
    183223    def create_reporter_and_fail(self):
Back to Top