Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Avoided transaction.set_autocommit in tests.

It doesn't work as one might expect on a certain database backend where
autocommits_when_autocommit_is_off = True. That backend happens to be
popular for running tests.

Backport of 38bc581 from master.
  • Loading branch information...
commit d200405471957fa67e21b2cbf08390cefbcd8482 1 parent ae685e5
Aymeric Augustin authored
33  tests/backends/tests.py
@@ -640,8 +640,7 @@ def test_disable_constraint_checks_manually(self):
640 640
         """
641 641
         When constraint checks are disabled, should be able to write bad data without IntegrityErrors.
642 642
         """
643  
-        transaction.set_autocommit(False)
644  
-        try:
  643
+        with transaction.atomic():
645 644
             # Create an Article.
646 645
             models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r)
647 646
             # Retrive it from the DB
@@ -653,17 +652,13 @@ def test_disable_constraint_checks_manually(self):
653 652
                 connection.enable_constraint_checking()
654 653
             except IntegrityError:
655 654
                 self.fail("IntegrityError should not have occurred.")
656  
-            finally:
657  
-                transaction.rollback()
658  
-        finally:
659  
-            transaction.set_autocommit(True)
  655
+            transaction.set_rollback(True)
660 656
 
661 657
     def test_disable_constraint_checks_context_manager(self):
662 658
         """
663 659
         When constraint checks are disabled (using context manager), should be able to write bad data without IntegrityErrors.
664 660
         """
665  
-        transaction.set_autocommit(False)
666  
-        try:
  661
+        with transaction.atomic():
667 662
             # Create an Article.
668 663
             models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r)
669 664
             # Retrive it from the DB
@@ -674,31 +669,23 @@ def test_disable_constraint_checks_context_manager(self):
674 669
                     a.save()
675 670
             except IntegrityError:
676 671
                 self.fail("IntegrityError should not have occurred.")
677  
-            finally:
678  
-                transaction.rollback()
679  
-        finally:
680  
-            transaction.set_autocommit(True)
  672
+            transaction.set_rollback(True)
681 673
 
682 674
     def test_check_constraints(self):
683 675
         """
684 676
         Constraint checks should raise an IntegrityError when bad data is in the DB.
685 677
         """
686  
-        try:
687  
-            transaction.set_autocommit(False)
  678
+        with transaction.atomic():
688 679
             # Create an Article.
689 680
             models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r)
690 681
             # Retrive it from the DB
691 682
             a = models.Article.objects.get(headline="Test article")
692 683
             a.reporter_id = 30
693  
-            try:
694  
-                with connection.constraint_checks_disabled():
695  
-                    a.save()
696  
-                    with self.assertRaises(IntegrityError):
697  
-                        connection.check_constraints()
698  
-            finally:
699  
-                transaction.rollback()
700  
-        finally:
701  
-            transaction.set_autocommit(True)
  684
+            with connection.constraint_checks_disabled():
  685
+                a.save()
  686
+                with self.assertRaises(IntegrityError):
  687
+                    connection.check_constraints()
  688
+            transaction.set_rollback(True)
702 689
 
703 690
 
704 691
 class ThreadTests(TestCase):
12  tests/fixtures_model_package/tests.py
@@ -30,8 +30,7 @@ class TestNoInitialDataLoading(TransactionTestCase):
30 30
     available_apps = ['fixtures_model_package']
31 31
 
32 32
     def test_syncdb(self):
33  
-        transaction.set_autocommit(False)
34  
-        try:
  33
+        with transaction.atomic():
35 34
             Book.objects.all().delete()
36 35
 
37 36
             management.call_command(
@@ -40,9 +39,6 @@ def test_syncdb(self):
40 39
                 load_initial_data=False
41 40
             )
42 41
             self.assertQuerysetEqual(Book.objects.all(), [])
43  
-            transaction.rollback()
44  
-        finally:
45  
-            transaction.set_autocommit(True)
46 42
 
47 43
 
48 44
     def test_flush(self):
@@ -54,8 +50,7 @@ def test_flush(self):
54 50
             lambda a: a.name
55 51
         )
56 52
 
57  
-        transaction.set_autocommit(False)
58  
-        try:
  53
+        with transaction.atomic():
59 54
             management.call_command(
60 55
                 'flush',
61 56
                 verbosity=0,
@@ -63,9 +58,6 @@ def test_flush(self):
63 58
                 load_initial_data=False
64 59
             )
65 60
             self.assertQuerysetEqual(Book.objects.all(), [])
66  
-            transaction.rollback()
67  
-        finally:
68  
-            transaction.set_autocommit(True)
69 61
 
70 62
 
71 63
 class FixtureTestCase(TestCase):
25  tests/fixtures_regress/tests.py
@@ -660,22 +660,15 @@ class TestTicket11101(TransactionTestCase):
660 660
         'django.contrib.contenttypes',
661 661
     ]
662 662
 
663  
-    def ticket_11101(self):
664  
-        management.call_command(
665  
-            'loaddata',
666  
-            'thingy.json',
667  
-            verbosity=0,
668  
-        )
669  
-        self.assertEqual(Thingy.objects.count(), 1)
670  
-        transaction.rollback()
671  
-        self.assertEqual(Thingy.objects.count(), 0)
672  
-        transaction.commit()
673  
-
674 663
     @skipUnlessDBFeature('supports_transactions')
675 664
     def test_ticket_11101(self):
676 665
         """Test that fixtures can be rolled back (ticket #11101)."""
677  
-        transaction.set_autocommit(False)
678  
-        try:
679  
-            self.ticket_11101()
680  
-        finally:
681  
-            transaction.set_autocommit(True)
  666
+        with transaction.atomic():
  667
+            management.call_command(
  668
+                'loaddata',
  669
+                'thingy.json',
  670
+                verbosity=0,
  671
+            )
  672
+            self.assertEqual(Thingy.objects.count(), 1)
  673
+            transaction.set_rollback(True)
  674
+        self.assertEqual(Thingy.objects.count(), 0)

0 notes on commit d200405

Please sign in to comment.
Something went wrong with that request. Please try again.