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