2015-09-22 17:24:22 +03:00
|
|
|
import unittest
|
|
|
|
|
2015-04-29 16:08:52 +03:00
|
|
|
from django.db import connection, connections, transaction
|
2015-06-08 18:15:31 +03:00
|
|
|
from django.http import Http404
|
2016-10-10 15:03:46 +03:00
|
|
|
from django.test import TestCase, TransactionTestCase, override_settings
|
2020-09-08 17:32:27 +03:00
|
|
|
from django.urls import path
|
2015-06-25 23:55:51 +03:00
|
|
|
|
2015-04-29 16:08:52 +03:00
|
|
|
from rest_framework import status
|
2015-06-08 18:15:31 +03:00
|
|
|
from rest_framework.exceptions import APIException
|
2015-04-29 16:08:52 +03:00
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework.test import APIRequestFactory
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
from tests.models import BasicModel
|
|
|
|
|
|
|
|
factory = APIRequestFactory()
|
|
|
|
|
|
|
|
|
|
|
|
class BasicView(APIView):
|
2015-04-29 17:28:22 +03:00
|
|
|
def post(self, request, *args, **kwargs):
|
2015-04-29 16:08:52 +03:00
|
|
|
BasicModel.objects.create()
|
|
|
|
return Response({'method': 'GET'})
|
|
|
|
|
|
|
|
|
|
|
|
class ErrorView(APIView):
|
2015-04-29 17:28:22 +03:00
|
|
|
def post(self, request, *args, **kwargs):
|
2015-04-29 16:08:52 +03:00
|
|
|
BasicModel.objects.create()
|
|
|
|
raise Exception
|
|
|
|
|
|
|
|
|
|
|
|
class APIExceptionView(APIView):
|
2015-04-29 17:28:22 +03:00
|
|
|
def post(self, request, *args, **kwargs):
|
2015-04-29 16:08:52 +03:00
|
|
|
BasicModel.objects.create()
|
|
|
|
raise APIException
|
|
|
|
|
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
class NonAtomicAPIExceptionView(APIView):
|
2018-06-21 21:44:58 +03:00
|
|
|
@transaction.non_atomic_requests
|
2016-10-10 15:03:46 +03:00
|
|
|
def dispatch(self, *args, **kwargs):
|
2019-04-30 18:53:44 +03:00
|
|
|
return super().dispatch(*args, **kwargs)
|
2016-10-10 15:03:46 +03:00
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
BasicModel.objects.all()
|
|
|
|
raise Http404
|
|
|
|
|
2017-05-29 22:15:07 +03:00
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
urlpatterns = (
|
2020-09-08 17:32:27 +03:00
|
|
|
path('', NonAtomicAPIExceptionView.as_view()),
|
2016-10-10 15:03:46 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-08-28 12:03:16 +03:00
|
|
|
@unittest.skipUnless(
|
|
|
|
connection.features.uses_savepoints,
|
|
|
|
"'atomic' requires transactions and savepoints."
|
|
|
|
)
|
2015-04-29 16:08:52 +03:00
|
|
|
class DBTransactionTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.view = BasicView.as_view()
|
|
|
|
connections.databases['default']['ATOMIC_REQUESTS'] = True
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
connections.databases['default']['ATOMIC_REQUESTS'] = False
|
|
|
|
|
2015-09-22 12:13:01 +03:00
|
|
|
def test_no_exception_commit_transaction(self):
|
2015-04-29 17:28:22 +03:00
|
|
|
request = factory.post('/')
|
2015-04-29 16:08:52 +03:00
|
|
|
|
|
|
|
with self.assertNumQueries(1):
|
|
|
|
response = self.view(request)
|
2016-11-30 13:17:30 +03:00
|
|
|
assert not transaction.get_rollback()
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
2015-04-29 17:28:48 +03:00
|
|
|
assert BasicModel.objects.count() == 1
|
2015-04-29 16:08:52 +03:00
|
|
|
|
|
|
|
|
2015-08-28 12:03:16 +03:00
|
|
|
@unittest.skipUnless(
|
|
|
|
connection.features.uses_savepoints,
|
|
|
|
"'atomic' requires transactions and savepoints."
|
|
|
|
)
|
2015-04-29 16:08:52 +03:00
|
|
|
class DBTransactionErrorTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.view = ErrorView.as_view()
|
|
|
|
connections.databases['default']['ATOMIC_REQUESTS'] = True
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
connections.databases['default']['ATOMIC_REQUESTS'] = False
|
|
|
|
|
2015-04-29 17:29:09 +03:00
|
|
|
def test_generic_exception_delegate_transaction_management(self):
|
2015-04-29 16:08:52 +03:00
|
|
|
"""
|
|
|
|
Transaction is eventually managed by outer-most transaction atomic
|
|
|
|
block. DRF do not try to interfere here.
|
2015-04-29 17:29:09 +03:00
|
|
|
|
|
|
|
We let django deal with the transaction when it will catch the Exception.
|
2015-04-29 16:08:52 +03:00
|
|
|
"""
|
2015-04-29 17:28:22 +03:00
|
|
|
request = factory.post('/')
|
2015-04-29 16:08:52 +03:00
|
|
|
with self.assertNumQueries(3):
|
|
|
|
# 1 - begin savepoint
|
|
|
|
# 2 - insert
|
|
|
|
# 3 - release savepoint
|
|
|
|
with transaction.atomic():
|
|
|
|
self.assertRaises(Exception, self.view, request)
|
2016-11-30 13:17:30 +03:00
|
|
|
assert not transaction.get_rollback()
|
2015-04-29 17:28:48 +03:00
|
|
|
assert BasicModel.objects.count() == 1
|
2015-04-29 16:08:52 +03:00
|
|
|
|
|
|
|
|
2015-08-28 12:03:16 +03:00
|
|
|
@unittest.skipUnless(
|
|
|
|
connection.features.uses_savepoints,
|
|
|
|
"'atomic' requires transactions and savepoints."
|
|
|
|
)
|
2015-04-29 16:08:52 +03:00
|
|
|
class DBTransactionAPIExceptionTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.view = APIExceptionView.as_view()
|
|
|
|
connections.databases['default']['ATOMIC_REQUESTS'] = True
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
connections.databases['default']['ATOMIC_REQUESTS'] = False
|
|
|
|
|
|
|
|
def test_api_exception_rollback_transaction(self):
|
|
|
|
"""
|
|
|
|
Transaction is rollbacked by our transaction atomic block.
|
|
|
|
"""
|
2015-04-29 17:28:22 +03:00
|
|
|
request = factory.post('/')
|
2017-11-20 11:35:54 +03:00
|
|
|
num_queries = 4 if connection.features.can_release_savepoints else 3
|
2015-04-29 16:08:52 +03:00
|
|
|
with self.assertNumQueries(num_queries):
|
|
|
|
# 1 - begin savepoint
|
|
|
|
# 2 - insert
|
|
|
|
# 3 - rollback savepoint
|
2017-11-20 11:35:54 +03:00
|
|
|
# 4 - release savepoint
|
2015-04-29 16:08:52 +03:00
|
|
|
with transaction.atomic():
|
|
|
|
response = self.view(request)
|
2016-11-30 13:17:30 +03:00
|
|
|
assert transaction.get_rollback()
|
|
|
|
assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
|
2015-04-29 17:28:48 +03:00
|
|
|
assert BasicModel.objects.count() == 0
|
2015-06-08 07:10:57 +03:00
|
|
|
|
|
|
|
|
2015-08-28 12:03:16 +03:00
|
|
|
@unittest.skipUnless(
|
|
|
|
connection.features.uses_savepoints,
|
|
|
|
"'atomic' requires transactions and savepoints."
|
|
|
|
)
|
2016-10-10 15:03:46 +03:00
|
|
|
@override_settings(ROOT_URLCONF='tests.test_atomic_requests')
|
2015-06-08 18:15:31 +03:00
|
|
|
class NonAtomicDBTransactionAPIExceptionTests(TransactionTestCase):
|
|
|
|
def setUp(self):
|
2015-06-08 07:10:57 +03:00
|
|
|
connections.databases['default']['ATOMIC_REQUESTS'] = True
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
connections.databases['default']['ATOMIC_REQUESTS'] = False
|
|
|
|
|
|
|
|
def test_api_exception_rollback_transaction_non_atomic_view(self):
|
2015-06-08 18:15:31 +03:00
|
|
|
response = self.client.get('/')
|
2015-06-08 07:10:57 +03:00
|
|
|
|
|
|
|
# without checking connection.in_atomic_block view raises 500
|
|
|
|
# due attempt to rollback without transaction
|
2016-11-30 13:17:30 +03:00
|
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|