|
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, override_settings |
| 6 | +from django.test import TestCase, TransactionTestCase, override_settings |
7 | 7 |
|
8 | 8 | from rest_framework import status
|
9 | 9 | from rest_framework.exceptions import APIException
|
@@ -43,20 +43,8 @@ def get(self, request, *args, **kwargs):
|
43 | 43 | raise Http404
|
44 | 44 |
|
45 | 45 |
|
46 |
| -class UrlDecoratedNonAtomicAPIExceptionView(APIView): |
47 |
| - def get(self, request, *args, **kwargs): |
48 |
| - list(BasicModel.objects.all()) |
49 |
| - raise Http404 |
50 |
| - |
51 |
| - |
52 | 46 | urlpatterns = (
|
53 | 47 | 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 |
| - ), |
60 | 48 | )
|
61 | 49 |
|
62 | 50 |
|
@@ -147,25 +135,19 @@ def test_api_exception_rollback_transaction(self):
|
147 | 135 | "'atomic' requires transactions and savepoints."
|
148 | 136 | )
|
149 | 137 | @override_settings(ROOT_URLCONF='tests.test_atomic_requests')
|
150 |
| -class NonAtomicDBTransactionAPIExceptionTests(TestCase): |
| 138 | +class NonAtomicDBTransactionAPIExceptionTests(TransactionTestCase): |
151 | 139 | def setUp(self):
|
152 | 140 | connections.databases['default']['ATOMIC_REQUESTS'] = True
|
153 | 141 |
|
154 |
| - def tearDown(self): |
155 |
| - connections.databases['default']['ATOMIC_REQUESTS'] = False |
| 142 | + @self.addCleanup |
| 143 | + def restore_atomic_requests(): |
| 144 | + connections.databases['default']['ATOMIC_REQUESTS'] = False |
156 | 145 |
|
157 | 146 | def test_api_exception_rollback_transaction_non_atomic_view(self):
|
158 | 147 | response = self.client.get('/non-atomic-exception')
|
159 | 148 |
|
| 149 | + # without check for db.in_atomic_block, would raise 500 due to attempt |
| 150 | + # to rollback without transaction |
160 | 151 | 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 | 152 | # Check we can still perform DB queries
|
171 | 153 | list(BasicModel.objects.all())
|
0 commit comments