Loading tests/backends/tests.py +10 −23 Original line number Diff line number Diff line Loading @@ -641,8 +641,7 @@ class FkConstraintsTests(TransactionTestCase): """ When constraint checks are disabled, should be able to write bad data without IntegrityErrors. """ transaction.set_autocommit(False) try: with transaction.atomic(): # Create an Article. models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r) # Retrive it from the DB Loading @@ -654,17 +653,13 @@ class FkConstraintsTests(TransactionTestCase): connection.enable_constraint_checking() except IntegrityError: self.fail("IntegrityError should not have occurred.") finally: transaction.rollback() finally: transaction.set_autocommit(True) transaction.set_rollback(True) def test_disable_constraint_checks_context_manager(self): """ When constraint checks are disabled (using context manager), should be able to write bad data without IntegrityErrors. """ transaction.set_autocommit(False) try: with transaction.atomic(): # Create an Article. models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r) # Retrive it from the DB Loading @@ -675,31 +670,23 @@ class FkConstraintsTests(TransactionTestCase): a.save() except IntegrityError: self.fail("IntegrityError should not have occurred.") finally: transaction.rollback() finally: transaction.set_autocommit(True) transaction.set_rollback(True) def test_check_constraints(self): """ Constraint checks should raise an IntegrityError when bad data is in the DB. """ try: transaction.set_autocommit(False) with transaction.atomic(): # Create an Article. models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r) # Retrive it from the DB a = models.Article.objects.get(headline="Test article") a.reporter_id = 30 try: with connection.constraint_checks_disabled(): a.save() with self.assertRaises(IntegrityError): connection.check_constraints() finally: transaction.rollback() finally: transaction.set_autocommit(True) transaction.set_rollback(True) class ThreadTests(TestCase): Loading tests/fixtures_model_package/tests.py +2 −10 Original line number Diff line number Diff line Loading @@ -30,8 +30,7 @@ class TestNoInitialDataLoading(TransactionTestCase): available_apps = ['fixtures_model_package'] def test_syncdb(self): transaction.set_autocommit(False) try: with transaction.atomic(): Book.objects.all().delete() management.call_command( Loading @@ -40,9 +39,6 @@ class TestNoInitialDataLoading(TransactionTestCase): load_initial_data=False ) self.assertQuerysetEqual(Book.objects.all(), []) transaction.rollback() finally: transaction.set_autocommit(True) def test_flush(self): Loading @@ -54,8 +50,7 @@ class TestNoInitialDataLoading(TransactionTestCase): lambda a: a.name ) transaction.set_autocommit(False) try: with transaction.atomic(): management.call_command( 'flush', verbosity=0, Loading @@ -63,9 +58,6 @@ class TestNoInitialDataLoading(TransactionTestCase): load_initial_data=False ) self.assertQuerysetEqual(Book.objects.all(), []) transaction.rollback() finally: transaction.set_autocommit(True) class FixtureTestCase(TestCase): Loading tests/fixtures_regress/tests.py +9 −16 Original line number Diff line number Diff line Loading @@ -660,22 +660,15 @@ class TestTicket11101(TransactionTestCase): 'django.contrib.contenttypes', ] def ticket_11101(self): @skipUnlessDBFeature('supports_transactions') def test_ticket_11101(self): """Test that fixtures can be rolled back (ticket #11101).""" with transaction.atomic(): management.call_command( 'loaddata', 'thingy.json', verbosity=0, ) self.assertEqual(Thingy.objects.count(), 1) transaction.rollback() transaction.set_rollback(True) self.assertEqual(Thingy.objects.count(), 0) transaction.commit() @skipUnlessDBFeature('supports_transactions') def test_ticket_11101(self): """Test that fixtures can be rolled back (ticket #11101).""" transaction.set_autocommit(False) try: self.ticket_11101() finally: transaction.set_autocommit(True) Loading
tests/backends/tests.py +10 −23 Original line number Diff line number Diff line Loading @@ -641,8 +641,7 @@ class FkConstraintsTests(TransactionTestCase): """ When constraint checks are disabled, should be able to write bad data without IntegrityErrors. """ transaction.set_autocommit(False) try: with transaction.atomic(): # Create an Article. models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r) # Retrive it from the DB Loading @@ -654,17 +653,13 @@ class FkConstraintsTests(TransactionTestCase): connection.enable_constraint_checking() except IntegrityError: self.fail("IntegrityError should not have occurred.") finally: transaction.rollback() finally: transaction.set_autocommit(True) transaction.set_rollback(True) def test_disable_constraint_checks_context_manager(self): """ When constraint checks are disabled (using context manager), should be able to write bad data without IntegrityErrors. """ transaction.set_autocommit(False) try: with transaction.atomic(): # Create an Article. models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r) # Retrive it from the DB Loading @@ -675,31 +670,23 @@ class FkConstraintsTests(TransactionTestCase): a.save() except IntegrityError: self.fail("IntegrityError should not have occurred.") finally: transaction.rollback() finally: transaction.set_autocommit(True) transaction.set_rollback(True) def test_check_constraints(self): """ Constraint checks should raise an IntegrityError when bad data is in the DB. """ try: transaction.set_autocommit(False) with transaction.atomic(): # Create an Article. models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r) # Retrive it from the DB a = models.Article.objects.get(headline="Test article") a.reporter_id = 30 try: with connection.constraint_checks_disabled(): a.save() with self.assertRaises(IntegrityError): connection.check_constraints() finally: transaction.rollback() finally: transaction.set_autocommit(True) transaction.set_rollback(True) class ThreadTests(TestCase): Loading
tests/fixtures_model_package/tests.py +2 −10 Original line number Diff line number Diff line Loading @@ -30,8 +30,7 @@ class TestNoInitialDataLoading(TransactionTestCase): available_apps = ['fixtures_model_package'] def test_syncdb(self): transaction.set_autocommit(False) try: with transaction.atomic(): Book.objects.all().delete() management.call_command( Loading @@ -40,9 +39,6 @@ class TestNoInitialDataLoading(TransactionTestCase): load_initial_data=False ) self.assertQuerysetEqual(Book.objects.all(), []) transaction.rollback() finally: transaction.set_autocommit(True) def test_flush(self): Loading @@ -54,8 +50,7 @@ class TestNoInitialDataLoading(TransactionTestCase): lambda a: a.name ) transaction.set_autocommit(False) try: with transaction.atomic(): management.call_command( 'flush', verbosity=0, Loading @@ -63,9 +58,6 @@ class TestNoInitialDataLoading(TransactionTestCase): load_initial_data=False ) self.assertQuerysetEqual(Book.objects.all(), []) transaction.rollback() finally: transaction.set_autocommit(True) class FixtureTestCase(TestCase): Loading
tests/fixtures_regress/tests.py +9 −16 Original line number Diff line number Diff line Loading @@ -660,22 +660,15 @@ class TestTicket11101(TransactionTestCase): 'django.contrib.contenttypes', ] def ticket_11101(self): @skipUnlessDBFeature('supports_transactions') def test_ticket_11101(self): """Test that fixtures can be rolled back (ticket #11101).""" with transaction.atomic(): management.call_command( 'loaddata', 'thingy.json', verbosity=0, ) self.assertEqual(Thingy.objects.count(), 1) transaction.rollback() transaction.set_rollback(True) self.assertEqual(Thingy.objects.count(), 0) transaction.commit() @skipUnlessDBFeature('supports_transactions') def test_ticket_11101(self): """Test that fixtures can be rolled back (ticket #11101).""" transaction.set_autocommit(False) try: self.ticket_11101() finally: transaction.set_autocommit(True)