Browse Source

Test pagination

David Burke 5 years ago
parent
commit
4a402c0844
3 changed files with 18 additions and 2 deletions
  1. 3 2
      glitchtip/pagination.py
  2. 1 0
      glitchtip/settings.py
  3. 14 0
      projects/tests.py

+ 3 - 2
glitchtip/pagination.py

@@ -1,6 +1,5 @@
 from rest_framework.pagination import CursorPagination
 from rest_framework.response import Response
-from rest_framework.utils.urls import remove_query_param, replace_query_param
 
 
 class LinkHeaderPagination(CursorPagination):
@@ -19,7 +18,9 @@ class LinkHeaderPagination(CursorPagination):
             (next_url, "next"),
         ):
             if url is not None:
-                links.append('<{}>; rel="{}"'.format(url, label))
+                links.append('<{}>; rel="{}"; results="true"'.format(url, label))
+            else:
+                links.append('<>; rel="{}"; results="false"'.format(label))
 
         headers = {"Link": ", ".join(links)} if links else {}
 

+ 1 - 0
glitchtip/settings.py

@@ -191,4 +191,5 @@ AUTHENTICATION_BACKENDS = (
 REST_FRAMEWORK = {
     "DEFAULT_PERMISSION_CLASSES": ["rest_framework.permissions.IsAuthenticated",],
     "DEFAULT_PAGINATION_CLASS": "glitchtip.pagination.LinkHeaderPagination",
+    "PAGE_SIZE": 50,
 }

+ 14 - 0
projects/tests.py

@@ -1,3 +1,4 @@
+from django.conf import settings
 from rest_framework.test import APITestCase
 from model_bakery import baker
 from glitchtip import test_utils  # pylint: disable=unused-import
@@ -30,3 +31,16 @@ class ProjectsAPITestCase(APITestCase):
         res = self.client.post(self.url, data)
         self.assertContains(res, name, status_code=201)
         self.assertEqual(ProjectKey.objects.all().count(), 2)
+
+    def test_projects_pagination(self):
+        """
+        Test link header pagination
+        """
+        page_size = settings.REST_FRAMEWORK.get("PAGE_SIZE")
+        projects = baker.make("projects.Project", _quantity=page_size + 1)
+        res = self.client.get(self.url)
+        self.assertNotContains(res, projects[0].name)
+        self.assertContains(res, projects[-1].name)
+        link_header = res.get("Link")
+        self.assertIn('results="true"', link_header)
+