1
0
mirror of https://github.com/django/django.git synced 2025-01-03 06:55:47 +00:00

Improved test coverage for django.db.transaction.

This commit is contained in:
Pablo 2022-12-02 23:04:42 -03:00 committed by Mariusz Felisiak
parent 344593893b
commit 48531f53ae
4 changed files with 41 additions and 0 deletions

View File

@ -129,6 +129,19 @@ class TransactionsPerRequestTests(TransactionTestCase):
finally:
connection.settings_dict["ATOMIC_REQUESTS"] = old_atomic_requests
self.assertContains(response, "False")
try:
connection.settings_dict["ATOMIC_REQUESTS"] = True
response = self.client.get("/not_in_transaction_using_none/")
finally:
connection.settings_dict["ATOMIC_REQUESTS"] = old_atomic_requests
self.assertContains(response, "False")
try:
connection.settings_dict["ATOMIC_REQUESTS"] = True
response = self.client.get("/not_in_transaction_using_text/")
finally:
connection.settings_dict["ATOMIC_REQUESTS"] = old_atomic_requests
# The non_atomic_requests decorator is used for an incorrect table.
self.assertContains(response, "True")
@override_settings(ROOT_URLCONF="handlers.urls")

View File

@ -10,6 +10,8 @@ urlpatterns = [
path("streaming/", views.streaming),
path("in_transaction/", views.in_transaction),
path("not_in_transaction/", views.not_in_transaction),
path("not_in_transaction_using_none/", views.not_in_transaction_using_none),
path("not_in_transaction_using_text/", views.not_in_transaction_using_text),
path("bad_request/", views.bad_request),
path("suspicious/", views.suspicious),
path("malformed_post/", views.malformed_post),

View File

@ -33,6 +33,16 @@ def not_in_transaction(request):
return HttpResponse(str(connection.in_atomic_block))
@transaction.non_atomic_requests(using=None)
def not_in_transaction_using_none(request):
return HttpResponse(str(connection.in_atomic_block))
@transaction.non_atomic_requests(using="incorrect")
def not_in_transaction_using_text(request):
return HttpResponse(str(connection.in_atomic_block))
def bad_request(request):
raise BadRequest()

View File

@ -226,6 +226,22 @@ class AtomicTests(TransactionTestCase):
transaction.savepoint_rollback(sid)
self.assertSequenceEqual(Reporter.objects.all(), [reporter])
@skipUnlessDBFeature("can_release_savepoints")
def test_failure_on_exit_transaction(self):
with transaction.atomic():
with self.assertRaises(DatabaseError):
with transaction.atomic():
Reporter.objects.create(last_name="Tintin")
self.assertEqual(len(Reporter.objects.all()), 1)
# Incorrect savepoint id to provoke a database error.
connection.savepoint_ids.append("12")
with self.assertRaises(transaction.TransactionManagementError):
len(Reporter.objects.all())
self.assertIs(connection.needs_rollback, True)
if connection.savepoint_ids:
connection.savepoint_ids.pop()
self.assertSequenceEqual(Reporter.objects.all(), [])
class AtomicInsideTransactionTests(AtomicTests):
"""All basic tests for atomic should also pass within an existing transaction."""