diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2013-07-09 21:12:51 +0200 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2013-07-09 21:42:11 +0200 |
| commit | d200405471957fa67e21b2cbf08390cefbcd8482 (patch) | |
| tree | 3998d26f729830aad8dbfea6cda1298df018c54f | |
| parent | ae685e54cb8b5d654180ac66b9fd4d68ad28a66b (diff) | |
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 38bc581bc02d83ecab6d19514ac51b57f0e11866 from master.
| -rw-r--r-- | tests/backends/tests.py | 33 | ||||
| -rw-r--r-- | tests/fixtures_model_package/tests.py | 12 | ||||
| -rw-r--r-- | tests/fixtures_regress/tests.py | 25 |
3 files changed, 21 insertions, 49 deletions
diff --git a/tests/backends/tests.py b/tests/backends/tests.py index 488f8d518b..b196133a68 100644 --- a/tests/backends/tests.py +++ b/tests/backends/tests.py @@ -640,8 +640,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 @@ -653,17 +652,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 @@ -674,31 +669,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) + with connection.constraint_checks_disabled(): + a.save() + with self.assertRaises(IntegrityError): + connection.check_constraints() + transaction.set_rollback(True) class ThreadTests(TestCase): diff --git a/tests/fixtures_model_package/tests.py b/tests/fixtures_model_package/tests.py index 4b00e6dfb9..ad82267da3 100644 --- a/tests/fixtures_model_package/tests.py +++ b/tests/fixtures_model_package/tests.py @@ -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( @@ -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): @@ -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, @@ -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): diff --git a/tests/fixtures_regress/tests.py b/tests/fixtures_regress/tests.py index a9d67ec9a2..a6bad5716d 100644 --- a/tests/fixtures_regress/tests.py +++ b/tests/fixtures_regress/tests.py @@ -660,22 +660,15 @@ class TestTicket11101(TransactionTestCase): 'django.contrib.contenttypes', ] - def ticket_11101(self): - management.call_command( - 'loaddata', - 'thingy.json', - verbosity=0, - ) - self.assertEqual(Thingy.objects.count(), 1) - transaction.rollback() - 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) + with transaction.atomic(): + management.call_command( + 'loaddata', + 'thingy.json', + verbosity=0, + ) + self.assertEqual(Thingy.objects.count(), 1) + transaction.set_rollback(True) + self.assertEqual(Thingy.objects.count(), 0) |
