|
| 1 | +from __future__ import unicode_literals |
| 2 | + |
| 3 | +from django.db import connection, connections, transaction |
| 4 | +from django.test import TestCase |
| 5 | +from django.utils.unittest import skipUnless |
| 6 | +from rest_framework import status |
| 7 | +from rest_framework.exceptions import APIException |
| 8 | +from rest_framework.response import Response |
| 9 | +from rest_framework.test import APIRequestFactory |
| 10 | +from rest_framework.views import APIView |
| 11 | +from tests.models import BasicModel |
| 12 | + |
| 13 | + |
| 14 | +factory = APIRequestFactory() |
| 15 | + |
| 16 | + |
| 17 | +class BasicView(APIView): |
| 18 | + def post(self, request, *args, **kwargs): |
| 19 | + BasicModel.objects.create() |
| 20 | + return Response({'method': 'GET'}) |
| 21 | + |
| 22 | + |
| 23 | +class ErrorView(APIView): |
| 24 | + def post(self, request, *args, **kwargs): |
| 25 | + BasicModel.objects.create() |
| 26 | + raise Exception |
| 27 | + |
| 28 | + |
| 29 | +class APIExceptionView(APIView): |
| 30 | + def post(self, request, *args, **kwargs): |
| 31 | + BasicModel.objects.create() |
| 32 | + raise APIException |
| 33 | + |
| 34 | + |
| 35 | +@skipUnless(connection.features.uses_savepoints, |
| 36 | + "'atomic' requires transactions and savepoints.") |
| 37 | +class DBTransactionTests(TestCase): |
| 38 | + def setUp(self): |
| 39 | + self.view = BasicView.as_view() |
| 40 | + connections.databases['default']['ATOMIC_REQUESTS'] = True |
| 41 | + |
| 42 | + def tearDown(self): |
| 43 | + connections.databases['default']['ATOMIC_REQUESTS'] = False |
| 44 | + |
| 45 | + def test_no_exception_conmmit_transaction(self): |
| 46 | + request = factory.post('/') |
| 47 | + |
| 48 | + with self.assertNumQueries(1): |
| 49 | + response = self.view(request) |
| 50 | + self.assertFalse(transaction.get_rollback()) |
| 51 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 52 | + assert BasicModel.objects.count() == 1 |
| 53 | + |
| 54 | + |
| 55 | +@skipUnless(connection.features.uses_savepoints, |
| 56 | + "'atomic' requires transactions and savepoints.") |
| 57 | +class DBTransactionErrorTests(TestCase): |
| 58 | + def setUp(self): |
| 59 | + self.view = ErrorView.as_view() |
| 60 | + connections.databases['default']['ATOMIC_REQUESTS'] = True |
| 61 | + |
| 62 | + def tearDown(self): |
| 63 | + connections.databases['default']['ATOMIC_REQUESTS'] = False |
| 64 | + |
| 65 | + def test_generic_exception_delegate_transaction_management(self): |
| 66 | + """ |
| 67 | + Transaction is eventually managed by outer-most transaction atomic |
| 68 | + block. DRF do not try to interfere here. |
| 69 | +
|
| 70 | + We let django deal with the transaction when it will catch the Exception. |
| 71 | + """ |
| 72 | + request = factory.post('/') |
| 73 | + with self.assertNumQueries(3): |
| 74 | + # 1 - begin savepoint |
| 75 | + # 2 - insert |
| 76 | + # 3 - release savepoint |
| 77 | + with transaction.atomic(): |
| 78 | + self.assertRaises(Exception, self.view, request) |
| 79 | + self.assertFalse(transaction.get_rollback()) |
| 80 | + assert BasicModel.objects.count() == 1 |
| 81 | + |
| 82 | + |
| 83 | +@skipUnless(connection.features.uses_savepoints, |
| 84 | + "'atomic' requires transactions and savepoints.") |
| 85 | +class DBTransactionAPIExceptionTests(TestCase): |
| 86 | + def setUp(self): |
| 87 | + self.view = APIExceptionView.as_view() |
| 88 | + connections.databases['default']['ATOMIC_REQUESTS'] = True |
| 89 | + |
| 90 | + def tearDown(self): |
| 91 | + connections.databases['default']['ATOMIC_REQUESTS'] = False |
| 92 | + |
| 93 | + def test_api_exception_rollback_transaction(self): |
| 94 | + """ |
| 95 | + Transaction is rollbacked by our transaction atomic block. |
| 96 | + """ |
| 97 | + request = factory.post('/') |
| 98 | + num_queries = (4 if getattr(connection.features, |
| 99 | + 'can_release_savepoints', False) else 3) |
| 100 | + with self.assertNumQueries(num_queries): |
| 101 | + # 1 - begin savepoint |
| 102 | + # 2 - insert |
| 103 | + # 3 - rollback savepoint |
| 104 | + # 4 - release savepoint (django>=1.8 only) |
| 105 | + with transaction.atomic(): |
| 106 | + response = self.view(request) |
| 107 | + self.assertTrue(transaction.get_rollback()) |
| 108 | + self.assertEqual(response.status_code, |
| 109 | + status.HTTP_500_INTERNAL_SERVER_ERROR) |
| 110 | + assert BasicModel.objects.count() == 0 |
0 commit comments