Skip to content

Commit

Permalink
Add test case for is_list_view logic
Browse files Browse the repository at this point in the history
Test reveals my logical use of isinstance is not working as intended.
  • Loading branch information
matteius committed Jun 19, 2017
1 parent 421d546 commit 01297b3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rest_framework/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def is_list_view(path, method, view):
return False
if isinstance(view, mixins.ListModelMixin):
return True
if isinstance(view, generics.RetrieveAPIView):
if isinstance(view, mixins.RetrieveModelMixin):
return False
# Otherwise when path ends in a variable it is assumed a singular lookup
path_components = path.strip('/').split('/')
Expand Down
44 changes: 44 additions & 0 deletions tests/test_schema_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from django.test import TestCase

from rest_framework import generics, permissions
from rest_framework import schemas
from rest_framework.response import Response


class ExampleListView(generics.ListAPIView):

def list(self, request, *args, **kwargs):
# Mock actually using models
response_list = [
{'id': '1'},
{'id': '2'},
{'id': '3'},
]
return Response(response_list)


class ExampleRetrieveView(generics.RetrieveAPIView):
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
serializer_class = None

def retrieve(self, request, *args, **kwargs):
# Mock: do something based on request state
data = {'request.user': "A Description of the known request object."}
return Response(data)


class TestIsListViewLogic(TestCase):

def test_when_view_is_retrieve_view(self):
view = ExampleRetrieveView.as_view()
view_url = '^my-profile/?$'

is_list = schemas.is_list_view(view_url, 'get', view)
self.assertFalse(is_list, "Expected a standard RetrieveAPIView to be singular lookup.")

def test_when_view_is_list_view(self):
view = ExampleListView.as_view()
view_url = '^my-list/?$'

is_list = schemas.is_list_view(view_url, 'get', view)
self.assertTrue(is_list, "Expected a standard ListAPIView to return list data.")

0 comments on commit 01297b3

Please sign in to comment.