From 48531f53ae9c5781094c91b6aaec204920359018 Mon Sep 17 00:00:00 2001 From: Pablo Date: Fri, 2 Dec 2022 23:04:42 -0300 Subject: [PATCH] Improved test coverage for django.db.transaction. --- tests/handlers/tests.py | 13 +++++++++++++ tests/handlers/urls.py | 2 ++ tests/handlers/views.py | 10 ++++++++++ tests/transactions/tests.py | 16 ++++++++++++++++ 4 files changed, 41 insertions(+) diff --git a/tests/handlers/tests.py b/tests/handlers/tests.py index 1e2da4672b..980887edfd 100644 --- a/tests/handlers/tests.py +++ b/tests/handlers/tests.py @@ -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") diff --git a/tests/handlers/urls.py b/tests/handlers/urls.py index 59cc919ba5..73d99c7edf 100644 --- a/tests/handlers/urls.py +++ b/tests/handlers/urls.py @@ -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), diff --git a/tests/handlers/views.py b/tests/handlers/views.py index b7d0716afc..351eb65264 100644 --- a/tests/handlers/views.py +++ b/tests/handlers/views.py @@ -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() diff --git a/tests/transactions/tests.py b/tests/transactions/tests.py index bdf912653c..1f2634224b 100644 --- a/tests/transactions/tests.py +++ b/tests/transactions/tests.py @@ -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."""