|
1 | 1 | from __future__ import unicode_literals
|
2 | 2 |
|
| 3 | +from django.conf.urls import patterns, url |
3 | 4 | from django.db import connection, connections, transaction
|
4 |
| -from django.test import TestCase |
| 5 | +from django.test import TestCase, TransactionTestCase |
| 6 | +from django.http import Http404 |
5 | 7 | from django.utils.decorators import method_decorator
|
6 | 8 | from django.utils.unittest import skipUnless
|
7 | 9 | from rest_framework import status
|
8 |
| -from rest_framework.exceptions import APIException, PermissionDenied |
| 10 | +from rest_framework.exceptions import APIException |
9 | 11 | from rest_framework.response import Response
|
10 | 12 | from rest_framework.test import APIRequestFactory
|
11 | 13 | from rest_framework.views import APIView
|
@@ -113,30 +115,33 @@ def test_api_exception_rollback_transaction(self):
|
113 | 115 |
|
114 | 116 | @skipUnless(connection.features.uses_savepoints,
|
115 | 117 | "'atomic' requires transactions and savepoints.")
|
116 |
| -class NonAtomicDBTransactionAPIExceptionTests(TestCase): |
117 |
| - def setUp(self): |
118 |
| - # only Django >= 1.6 provides @transaction.non_atomic_requests |
| 118 | +class NonAtomicDBTransactionAPIExceptionTests(TransactionTestCase): |
| 119 | + @property |
| 120 | + def urls(self): |
119 | 121 | class NonAtomicAPIExceptionView(APIView):
|
120 | 122 | @method_decorator(transaction.non_atomic_requests)
|
121 | 123 | def dispatch(self, *args, **kwargs):
|
122 | 124 | return super(NonAtomicAPIExceptionView, self).dispatch(*args, **kwargs)
|
123 | 125 |
|
124 |
| - def post(self, request, *args, **kwargs): |
125 |
| - BasicModel.objects.create() |
126 |
| - raise PermissionDenied |
| 126 | + def get(self, request, *args, **kwargs): |
| 127 | + BasicModel.objects.all() |
| 128 | + raise Http404 |
| 129 | + |
| 130 | + return patterns( |
| 131 | + '', |
| 132 | + url(r'^$', NonAtomicAPIExceptionView.as_view()) |
| 133 | + ) |
127 | 134 |
|
128 |
| - self.view = NonAtomicAPIExceptionView.as_view() |
| 135 | + def setUp(self): |
129 | 136 | connections.databases['default']['ATOMIC_REQUESTS'] = True
|
130 | 137 |
|
131 | 138 | def tearDown(self):
|
132 | 139 | connections.databases['default']['ATOMIC_REQUESTS'] = False
|
133 | 140 |
|
134 | 141 | def test_api_exception_rollback_transaction_non_atomic_view(self):
|
135 |
| - request = factory.post('/') |
136 |
| - |
137 |
| - response = self.view(request) |
| 142 | + response = self.client.get('/') |
138 | 143 |
|
139 | 144 | # without checking connection.in_atomic_block view raises 500
|
140 | 145 | # due attempt to rollback without transaction
|
141 | 146 | self.assertEqual(response.status_code,
|
142 |
| - status.HTTP_403_FORBIDDEN) |
| 147 | + status.HTTP_404_NOT_FOUND) |
0 commit comments