mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 09:36:49 +03:00
Minor test improvements (#7297)
* Pass data via response instead of renderer_context Instead of relying on internal implementation details to get a reference to the response or view, attach these directly to the returned response. * Remove unused method
This commit is contained in:
parent
5828d8f7ca
commit
900773ad06
|
@ -22,10 +22,6 @@ class DecoratorTestCase(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.factory = APIRequestFactory()
|
self.factory = APIRequestFactory()
|
||||||
|
|
||||||
def _finalize_response(self, request, response, *args, **kwargs):
|
|
||||||
response.request = request
|
|
||||||
return APIView.finalize_response(self, request, response, *args, **kwargs)
|
|
||||||
|
|
||||||
def test_api_view_incorrect(self):
|
def test_api_view_incorrect(self):
|
||||||
"""
|
"""
|
||||||
If @api_view is not applied correct, we should raise an assertion.
|
If @api_view is not applied correct, we should raise an assertion.
|
||||||
|
|
|
@ -741,6 +741,11 @@ class AdminRendererTests(TestCase):
|
||||||
class DummyGenericViewsetLike(APIView):
|
class DummyGenericViewsetLike(APIView):
|
||||||
lookup_field = 'test'
|
lookup_field = 'test'
|
||||||
|
|
||||||
|
def get(self, request):
|
||||||
|
response = Response()
|
||||||
|
response.view = self
|
||||||
|
return response
|
||||||
|
|
||||||
def reverse_action(view, *args, **kwargs):
|
def reverse_action(view, *args, **kwargs):
|
||||||
self.assertEqual(kwargs['kwargs']['test'], 1)
|
self.assertEqual(kwargs['kwargs']['test'], 1)
|
||||||
return '/example/'
|
return '/example/'
|
||||||
|
@ -749,7 +754,7 @@ class AdminRendererTests(TestCase):
|
||||||
view = DummyGenericViewsetLike.as_view()
|
view = DummyGenericViewsetLike.as_view()
|
||||||
request = factory.get('/')
|
request = factory.get('/')
|
||||||
response = view(request)
|
response = view(request)
|
||||||
view = response.renderer_context['view']
|
view = response.view
|
||||||
|
|
||||||
self.assertEqual(self.renderer.get_result_url({'test': 1}, view), '/example/')
|
self.assertEqual(self.renderer.get_result_url({'test': 1}, view), '/example/')
|
||||||
self.assertIsNone(self.renderer.get_result_url({}, view))
|
self.assertIsNone(self.renderer.get_result_url({}, view))
|
||||||
|
@ -760,11 +765,16 @@ class AdminRendererTests(TestCase):
|
||||||
class DummyView(APIView):
|
class DummyView(APIView):
|
||||||
lookup_field = 'test'
|
lookup_field = 'test'
|
||||||
|
|
||||||
|
def get(self, request):
|
||||||
|
response = Response()
|
||||||
|
response.view = self
|
||||||
|
return response
|
||||||
|
|
||||||
# get the view instance instead of the view function
|
# get the view instance instead of the view function
|
||||||
view = DummyView.as_view()
|
view = DummyView.as_view()
|
||||||
request = factory.get('/')
|
request = factory.get('/')
|
||||||
response = view(request)
|
response = view(request)
|
||||||
view = response.renderer_context['view']
|
view = response.view
|
||||||
|
|
||||||
self.assertIsNone(self.renderer.get_result_url({'test': 1}, view))
|
self.assertIsNone(self.renderer.get_result_url({'test': 1}, view))
|
||||||
self.assertIsNone(self.renderer.get_result_url({}, view))
|
self.assertIsNone(self.renderer.get_result_url({}, view))
|
||||||
|
|
|
@ -137,7 +137,9 @@ class MockView(APIView):
|
||||||
|
|
||||||
class EchoView(APIView):
|
class EchoView(APIView):
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
return Response(status=status.HTTP_200_OK, data=request.data)
|
response = Response(status=status.HTTP_200_OK, data=request.data)
|
||||||
|
response._request = request # test client sets `request` input
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
class FileUploadView(APIView):
|
class FileUploadView(APIView):
|
||||||
|
@ -306,7 +308,7 @@ class TestHttpRequest(TestCase):
|
||||||
`RawPostDataException` being raised.
|
`RawPostDataException` being raised.
|
||||||
"""
|
"""
|
||||||
response = APIClient().post('/echo/', data={'a': 'b'}, format='json')
|
response = APIClient().post('/echo/', data={'a': 'b'}, format='json')
|
||||||
request = response.renderer_context['request']
|
request = response._request
|
||||||
|
|
||||||
# ensure that request stream was consumed by json parser
|
# ensure that request stream was consumed by json parser
|
||||||
assert request.content_type.startswith('application/json')
|
assert request.content_type.startswith('application/json')
|
||||||
|
@ -325,7 +327,7 @@ class TestHttpRequest(TestCase):
|
||||||
the duplicate stream parse exception.
|
the duplicate stream parse exception.
|
||||||
"""
|
"""
|
||||||
response = APIClient().post('/echo/', data={'a': 'b'})
|
response = APIClient().post('/echo/', data={'a': 'b'})
|
||||||
request = response.renderer_context['request']
|
request = response._request
|
||||||
|
|
||||||
# ensure that request stream was consumed by form parser
|
# ensure that request stream was consumed by form parser
|
||||||
assert request.content_type.startswith('multipart/form-data')
|
assert request.content_type.startswith('multipart/form-data')
|
||||||
|
@ -333,7 +335,7 @@ class TestHttpRequest(TestCase):
|
||||||
|
|
||||||
# pass same HttpRequest to view, form data set on underlying request
|
# pass same HttpRequest to view, form data set on underlying request
|
||||||
response = EchoView.as_view()(request._request)
|
response = EchoView.as_view()(request._request)
|
||||||
request = response.renderer_context['request']
|
request = response._request
|
||||||
|
|
||||||
# ensure that request stream was consumed by form parser
|
# ensure that request stream was consumed by form parser
|
||||||
assert request.content_type.startswith('multipart/form-data')
|
assert request.content_type.startswith('multipart/form-data')
|
||||||
|
|
|
@ -42,7 +42,9 @@ class ActionViewSet(GenericViewSet):
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def retrieve(self, request, *args, **kwargs):
|
def retrieve(self, request, *args, **kwargs):
|
||||||
return Response()
|
response = Response()
|
||||||
|
response.view = self
|
||||||
|
return response
|
||||||
|
|
||||||
@action(detail=False)
|
@action(detail=False)
|
||||||
def list_action(self, request, *args, **kwargs):
|
def list_action(self, request, *args, **kwargs):
|
||||||
|
@ -70,7 +72,9 @@ class ActionViewSet(GenericViewSet):
|
||||||
class ActionNamesViewSet(GenericViewSet):
|
class ActionNamesViewSet(GenericViewSet):
|
||||||
|
|
||||||
def retrieve(self, request, *args, **kwargs):
|
def retrieve(self, request, *args, **kwargs):
|
||||||
return Response()
|
response = Response()
|
||||||
|
response.view = self
|
||||||
|
return response
|
||||||
|
|
||||||
@action(detail=True)
|
@action(detail=True)
|
||||||
def unnamed_action(self, request, *args, **kwargs):
|
def unnamed_action(self, request, *args, **kwargs):
|
||||||
|
@ -209,7 +213,7 @@ class GetExtraActionUrlMapTests(TestCase):
|
||||||
|
|
||||||
def test_list_view(self):
|
def test_list_view(self):
|
||||||
response = self.client.get('/api/actions/')
|
response = self.client.get('/api/actions/')
|
||||||
view = response.renderer_context['view']
|
view = response.view
|
||||||
|
|
||||||
expected = OrderedDict([
|
expected = OrderedDict([
|
||||||
('Custom list action', 'http://testserver/api/actions/custom_list_action/'),
|
('Custom list action', 'http://testserver/api/actions/custom_list_action/'),
|
||||||
|
@ -220,7 +224,7 @@ class GetExtraActionUrlMapTests(TestCase):
|
||||||
|
|
||||||
def test_detail_view(self):
|
def test_detail_view(self):
|
||||||
response = self.client.get('/api/actions/1/')
|
response = self.client.get('/api/actions/1/')
|
||||||
view = response.renderer_context['view']
|
view = response.view
|
||||||
|
|
||||||
expected = OrderedDict([
|
expected = OrderedDict([
|
||||||
('Custom detail action', 'http://testserver/api/actions/1/custom_detail_action/'),
|
('Custom detail action', 'http://testserver/api/actions/1/custom_detail_action/'),
|
||||||
|
@ -236,7 +240,7 @@ class GetExtraActionUrlMapTests(TestCase):
|
||||||
def test_action_names(self):
|
def test_action_names(self):
|
||||||
# Action 'name' and 'suffix' kwargs should be respected
|
# Action 'name' and 'suffix' kwargs should be respected
|
||||||
response = self.client.get('/api/names/1/')
|
response = self.client.get('/api/names/1/')
|
||||||
view = response.renderer_context['view']
|
view = response.view
|
||||||
|
|
||||||
expected = OrderedDict([
|
expected = OrderedDict([
|
||||||
('Custom Name', 'http://testserver/api/names/1/named_action/'),
|
('Custom Name', 'http://testserver/api/names/1/named_action/'),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user