summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2013-03-04 23:26:31 +0100
committerAymeric Augustin <aymeric.augustin@m4x.org>2013-03-11 14:48:55 +0100
commit7c46c8d5f27fe305507359588ca0635b6d87c59a (patch)
treeca99a91cedfd6d8d03a3fbe334ee9589a51ef002 /tests
parentd7bc4fbc94df6c231d71dffa45cf337ff13512ee (diff)
Added some assertions to enforce the atomicity of atomic.
Diffstat (limited to 'tests')
-rw-r--r--tests/backends/tests.py15
-rw-r--r--tests/fixtures_model_package/tests.py11
-rw-r--r--tests/fixtures_regress/tests.py7
-rw-r--r--tests/middleware/tests.py6
-rw-r--r--tests/transactions/tests.py71
-rw-r--r--tests/transactions_regress/tests.py12
6 files changed, 105 insertions, 17 deletions
diff --git a/tests/backends/tests.py b/tests/backends/tests.py
index 5c8a8955eb..51acbcb07f 100644
--- a/tests/backends/tests.py
+++ b/tests/backends/tests.py
@@ -522,7 +522,8 @@ class FkConstraintsTests(TransactionTestCase):
"""
When constraint checks are disabled, should be able to write bad data without IntegrityErrors.
"""
- with transaction.commit_manually():
+ transaction.set_autocommit(autocommit=False)
+ try:
# 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
@@ -536,12 +537,15 @@ class FkConstraintsTests(TransactionTestCase):
self.fail("IntegrityError should not have occurred.")
finally:
transaction.rollback()
+ finally:
+ transaction.set_autocommit(autocommit=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.
"""
- with transaction.commit_manually():
+ transaction.set_autocommit(autocommit=False)
+ try:
# 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
@@ -554,12 +558,15 @@ class FkConstraintsTests(TransactionTestCase):
self.fail("IntegrityError should not have occurred.")
finally:
transaction.rollback()
+ finally:
+ transaction.set_autocommit(autocommit=True)
def test_check_constraints(self):
"""
Constraint checks should raise an IntegrityError when bad data is in the DB.
"""
- with transaction.commit_manually():
+ try:
+ transaction.set_autocommit(autocommit=False)
# 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
@@ -572,6 +579,8 @@ class FkConstraintsTests(TransactionTestCase):
connection.check_constraints()
finally:
transaction.rollback()
+ finally:
+ transaction.set_autocommit(autocommit=True)
class ThreadTests(TestCase):
diff --git a/tests/fixtures_model_package/tests.py b/tests/fixtures_model_package/tests.py
index d147fe68a7..894a6c7fde 100644
--- a/tests/fixtures_model_package/tests.py
+++ b/tests/fixtures_model_package/tests.py
@@ -25,7 +25,8 @@ class SampleTestCase(TestCase):
class TestNoInitialDataLoading(TransactionTestCase):
def test_syncdb(self):
- with transaction.commit_manually():
+ transaction.set_autocommit(autocommit=False)
+ try:
Book.objects.all().delete()
management.call_command(
@@ -35,6 +36,9 @@ class TestNoInitialDataLoading(TransactionTestCase):
)
self.assertQuerysetEqual(Book.objects.all(), [])
transaction.rollback()
+ finally:
+ transaction.set_autocommit(autocommit=True)
+
def test_flush(self):
# Test presence of fixture (flush called by TransactionTestCase)
@@ -45,7 +49,8 @@ class TestNoInitialDataLoading(TransactionTestCase):
lambda a: a.name
)
- with transaction.commit_manually():
+ transaction.set_autocommit(autocommit=False)
+ try:
management.call_command(
'flush',
verbosity=0,
@@ -55,6 +60,8 @@ class TestNoInitialDataLoading(TransactionTestCase):
)
self.assertQuerysetEqual(Book.objects.all(), [])
transaction.rollback()
+ finally:
+ transaction.set_autocommit(autocommit=True)
class FixtureTestCase(TestCase):
diff --git a/tests/fixtures_regress/tests.py b/tests/fixtures_regress/tests.py
index 61dc4460df..f965dd81ac 100644
--- a/tests/fixtures_regress/tests.py
+++ b/tests/fixtures_regress/tests.py
@@ -684,5 +684,8 @@ class TestTicket11101(TransactionTestCase):
@skipUnlessDBFeature('supports_transactions')
def test_ticket_11101(self):
"""Test that fixtures can be rolled back (ticket #11101)."""
- ticket_11101 = transaction.commit_manually(self.ticket_11101)
- ticket_11101()
+ transaction.set_autocommit(autocommit=False)
+ try:
+ self.ticket_11101()
+ finally:
+ transaction.set_autocommit(autocommit=True)
diff --git a/tests/middleware/tests.py b/tests/middleware/tests.py
index e704fce342..7e26037967 100644
--- a/tests/middleware/tests.py
+++ b/tests/middleware/tests.py
@@ -24,6 +24,8 @@ from django.utils.encoding import force_str
from django.utils.six.moves import xrange
from django.utils.unittest import expectedFailure
+from transactions.tests import IgnorePendingDeprecationWarningsMixin
+
from .models import Band
@@ -670,11 +672,12 @@ class ETagGZipMiddlewareTest(TestCase):
self.assertNotEqual(gzip_etag, nogzip_etag)
-class TransactionMiddlewareTest(TransactionTestCase):
+class TransactionMiddlewareTest(IgnorePendingDeprecationWarningsMixin, TransactionTestCase):
"""
Test the transaction middleware.
"""
def setUp(self):
+ super(TransactionMiddlewareTest, self).setUp()
self.request = HttpRequest()
self.request.META = {
'SERVER_NAME': 'testserver',
@@ -686,6 +689,7 @@ class TransactionMiddlewareTest(TransactionTestCase):
def tearDown(self):
transaction.abort()
+ super(TransactionMiddlewareTest, self).tearDown()
def test_request(self):
TransactionMiddleware().process_request(self.request)
diff --git a/tests/transactions/tests.py b/tests/transactions/tests.py
index 14252dd6dc..d6cfd8ae95 100644
--- a/tests/transactions/tests.py
+++ b/tests/transactions/tests.py
@@ -1,9 +1,10 @@
from __future__ import absolute_import
import sys
+import warnings
from django.db import connection, transaction, IntegrityError
-from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature
+from django.test import TransactionTestCase, skipUnlessDBFeature
from django.utils import six
from django.utils.unittest import skipUnless
@@ -158,7 +159,69 @@ class AtomicInsideTransactionTests(AtomicTests):
self.atomic.__exit__(*sys.exc_info())
-class TransactionTests(TransactionTestCase):
+class AtomicInsideLegacyTransactionManagementTests(AtomicTests):
+
+ def setUp(self):
+ transaction.enter_transaction_management()
+
+ def tearDown(self):
+ # The tests access the database after exercising 'atomic', making the
+ # connection dirty; a rollback is required to make it clean.
+ transaction.rollback()
+ transaction.leave_transaction_management()
+
+
+@skipUnless(connection.features.uses_savepoints,
+ "'atomic' requires transactions and savepoints.")
+class AtomicErrorsTests(TransactionTestCase):
+
+ def test_atomic_requires_autocommit(self):
+ transaction.set_autocommit(autocommit=False)
+ try:
+ with self.assertRaises(transaction.TransactionManagementError):
+ with transaction.atomic():
+ pass
+ finally:
+ transaction.set_autocommit(autocommit=True)
+
+ def test_atomic_prevents_disabling_autocommit(self):
+ autocommit = transaction.get_autocommit()
+ with transaction.atomic():
+ with self.assertRaises(transaction.TransactionManagementError):
+ transaction.set_autocommit(autocommit=not autocommit)
+ # Make sure autocommit wasn't changed.
+ self.assertEqual(connection.autocommit, autocommit)
+
+ def test_atomic_prevents_calling_transaction_methods(self):
+ with transaction.atomic():
+ with self.assertRaises(transaction.TransactionManagementError):
+ transaction.commit()
+ with self.assertRaises(transaction.TransactionManagementError):
+ transaction.rollback()
+
+ def test_atomic_prevents_calling_transaction_management_methods(self):
+ with transaction.atomic():
+ with self.assertRaises(transaction.TransactionManagementError):
+ transaction.enter_transaction_management()
+ with self.assertRaises(transaction.TransactionManagementError):
+ transaction.leave_transaction_management()
+
+
+class IgnorePendingDeprecationWarningsMixin(object):
+
+ def setUp(self):
+ super(IgnorePendingDeprecationWarningsMixin, self).setUp()
+ self.catch_warnings = warnings.catch_warnings()
+ self.catch_warnings.__enter__()
+ warnings.filterwarnings("ignore", category=PendingDeprecationWarning)
+
+ def tearDown(self):
+ self.catch_warnings.__exit__(*sys.exc_info())
+ super(IgnorePendingDeprecationWarningsMixin, self).tearDown()
+
+
+class TransactionTests(IgnorePendingDeprecationWarningsMixin, TransactionTestCase):
+
def create_a_reporter_then_fail(self, first, last):
a = Reporter(first_name=first, last_name=last)
a.save()
@@ -313,7 +376,7 @@ class TransactionTests(TransactionTestCase):
)
-class TransactionRollbackTests(TransactionTestCase):
+class TransactionRollbackTests(IgnorePendingDeprecationWarningsMixin, TransactionTestCase):
def execute_bad_sql(self):
cursor = connection.cursor()
cursor.execute("INSERT INTO transactions_reporter (first_name, last_name) VALUES ('Douglas', 'Adams');")
@@ -330,7 +393,7 @@ class TransactionRollbackTests(TransactionTestCase):
self.assertRaises(IntegrityError, execute_bad_sql)
transaction.rollback()
-class TransactionContextManagerTests(TransactionTestCase):
+class TransactionContextManagerTests(IgnorePendingDeprecationWarningsMixin, TransactionTestCase):
def create_reporter_and_fail(self):
Reporter.objects.create(first_name="Bob", last_name="Holtzman")
raise Exception
diff --git a/tests/transactions_regress/tests.py b/tests/transactions_regress/tests.py
index e86db4d0aa..d5ee62da5e 100644
--- a/tests/transactions_regress/tests.py
+++ b/tests/transactions_regress/tests.py
@@ -6,10 +6,12 @@ from django.test import TransactionTestCase, skipUnlessDBFeature
from django.test.utils import override_settings
from django.utils.unittest import skipIf, skipUnless, expectedFailure
+from transactions.tests import IgnorePendingDeprecationWarningsMixin
+
from .models import Mod, M2mA, M2mB
-class TestTransactionClosing(TransactionTestCase):
+class TestTransactionClosing(IgnorePendingDeprecationWarningsMixin, TransactionTestCase):
"""
Tests to make sure that transactions are properly closed
when they should be, and aren't left pending after operations
@@ -166,7 +168,7 @@ class TestTransactionClosing(TransactionTestCase):
(connection.settings_dict['NAME'] == ':memory:' or
not connection.settings_dict['NAME']),
'Test uses multiple connections, but in-memory sqlite does not support this')
-class TestNewConnection(TransactionTestCase):
+class TestNewConnection(IgnorePendingDeprecationWarningsMixin, TransactionTestCase):
"""
Check that new connections don't have special behaviour.
"""
@@ -211,7 +213,7 @@ class TestNewConnection(TransactionTestCase):
@skipUnless(connection.vendor == 'postgresql',
"This test only valid for PostgreSQL")
-class TestPostgresAutocommitAndIsolation(TransactionTestCase):
+class TestPostgresAutocommitAndIsolation(IgnorePendingDeprecationWarningsMixin, TransactionTestCase):
"""
Tests to make sure psycopg2's autocommit mode and isolation level
is restored after entering and leaving transaction management.
@@ -292,7 +294,7 @@ class TestPostgresAutocommitAndIsolation(TransactionTestCase):
self.assertTrue(connection.autocommit)
-class TestManyToManyAddTransaction(TransactionTestCase):
+class TestManyToManyAddTransaction(IgnorePendingDeprecationWarningsMixin, TransactionTestCase):
def test_manyrelated_add_commit(self):
"Test for https://code.djangoproject.com/ticket/16818"
a = M2mA.objects.create()
@@ -307,7 +309,7 @@ class TestManyToManyAddTransaction(TransactionTestCase):
self.assertEqual(a.others.count(), 1)
-class SavepointTest(TransactionTestCase):
+class SavepointTest(IgnorePendingDeprecationWarningsMixin, TransactionTestCase):
@skipIf(connection.vendor == 'sqlite',
"SQLite doesn't support savepoints in managed mode")