From 01297b3abfda2ca9c642b1b9638b5530375aa529 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Mon, 19 Jun 2017 08:55:33 -0400 Subject: [PATCH] Add test case for is_list_view logic Test reveals my logical use of isinstance is not working as intended. --- rest_framework/schemas.py | 2 +- tests/test_schema_helpers.py | 44 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tests/test_schema_helpers.py diff --git a/rest_framework/schemas.py b/rest_framework/schemas.py index 02c98a10aa..723f78b9ea 100644 --- a/rest_framework/schemas.py +++ b/rest_framework/schemas.py @@ -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('/') diff --git a/tests/test_schema_helpers.py b/tests/test_schema_helpers.py new file mode 100644 index 0000000000..df1c7deccc --- /dev/null +++ b/tests/test_schema_helpers.py @@ -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.")