|
3 | 3 | from django.conf.urls import url
|
4 | 4 | from django.db import connection, connections, transaction
|
5 | 5 | from django.http import Http404
|
6 |
| -from django.test import TestCase, TransactionTestCase, override_settings |
| 6 | +from django.test import TestCase, override_settings |
7 | 7 |
|
8 | 8 | from rest_framework import status
|
9 | 9 | from rest_framework.exceptions import APIException
|
@@ -39,12 +39,24 @@ def dispatch(self, *args, **kwargs):
|
39 | 39 | return super().dispatch(*args, **kwargs)
|
40 | 40 |
|
41 | 41 | def get(self, request, *args, **kwargs):
|
42 |
| - BasicModel.objects.all() |
| 42 | + list(BasicModel.objects.all()) |
| 43 | + raise Http404 |
| 44 | + |
| 45 | + |
| 46 | +class UrlDecoratedNonAtomicAPIExceptionView(APIView): |
| 47 | + def get(self, request, *args, **kwargs): |
| 48 | + list(BasicModel.objects.all()) |
43 | 49 | raise Http404
|
44 | 50 |
|
45 | 51 |
|
46 | 52 | urlpatterns = (
|
47 |
| - url(r'^$', NonAtomicAPIExceptionView.as_view()), |
| 53 | + url(r'^non-atomic-exception$', NonAtomicAPIExceptionView.as_view()), |
| 54 | + url( |
| 55 | + r'^url-decorated-non-atomic-exception$', |
| 56 | + transaction.non_atomic_requests( |
| 57 | + UrlDecoratedNonAtomicAPIExceptionView.as_view() |
| 58 | + ), |
| 59 | + ), |
48 | 60 | )
|
49 | 61 |
|
50 | 62 |
|
@@ -94,8 +106,8 @@ def test_generic_exception_delegate_transaction_management(self):
|
94 | 106 | # 1 - begin savepoint
|
95 | 107 | # 2 - insert
|
96 | 108 | # 3 - release savepoint
|
97 |
| - with transaction.atomic(): |
98 |
| - self.assertRaises(Exception, self.view, request) |
| 109 | + with transaction.atomic(), self.assertRaises(Exception): |
| 110 | + self.view(request) |
99 | 111 | assert not transaction.get_rollback()
|
100 | 112 | assert BasicModel.objects.count() == 1
|
101 | 113 |
|
@@ -135,16 +147,25 @@ def test_api_exception_rollback_transaction(self):
|
135 | 147 | "'atomic' requires transactions and savepoints."
|
136 | 148 | )
|
137 | 149 | @override_settings(ROOT_URLCONF='tests.test_atomic_requests')
|
138 |
| -class NonAtomicDBTransactionAPIExceptionTests(TransactionTestCase): |
| 150 | +class NonAtomicDBTransactionAPIExceptionTests(TestCase): |
139 | 151 | def setUp(self):
|
140 | 152 | connections.databases['default']['ATOMIC_REQUESTS'] = True
|
141 | 153 |
|
142 | 154 | def tearDown(self):
|
143 | 155 | connections.databases['default']['ATOMIC_REQUESTS'] = False
|
144 | 156 |
|
145 | 157 | def test_api_exception_rollback_transaction_non_atomic_view(self):
|
146 |
| - response = self.client.get('/') |
| 158 | + response = self.client.get('/non-atomic-exception') |
147 | 159 |
|
148 |
| - # without checking connection.in_atomic_block view raises 500 |
149 |
| - # due attempt to rollback without transaction |
150 | 160 | assert response.status_code == status.HTTP_404_NOT_FOUND
|
| 161 | + assert not transaction.get_rollback() |
| 162 | + # Check we can still perform DB queries |
| 163 | + list(BasicModel.objects.all()) |
| 164 | + |
| 165 | + def test_api_exception_rollback_transaction_url_decorated_non_atomic_view(self): |
| 166 | + response = self.client.get('/url-decorated-non-atomic-exception') |
| 167 | + |
| 168 | + assert response.status_code == status.HTTP_404_NOT_FOUND |
| 169 | + assert not transaction.get_rollback() |
| 170 | + # Check we can still perform DB queries |
| 171 | + list(BasicModel.objects.all()) |
0 commit comments