Skip to content

Commit

Permalink
feat: add taxonomy api pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Aug 2, 2023
1 parent 04225e7 commit df959bf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
9 changes: 8 additions & 1 deletion openedx_tagging/core/tagging/rest_api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Tagging API Views
"""
from django.http import Http404
from rest_framework.pagination import PageNumberPagination
from rest_framework.viewsets import ModelViewSet

from ...api import (
Expand Down Expand Up @@ -59,7 +60,6 @@ class TaxonomyView(ModelViewSet):
"allow_free_text": True,
}
**Create Query Returns**
* 201 - Success
* 403 - Permission denied
Expand Down Expand Up @@ -107,6 +107,13 @@ class TaxonomyView(ModelViewSet):
"""

class TaxonomyPagination(PageNumberPagination):
page_size_query_param = "page_size"
page_size = 100
max_page_size = 1000

pagination_class = TaxonomyPagination

serializer_class = TaxonomySerializer
permission_classes = [TaxonomyObjectPermissions]

Expand Down
36 changes: 35 additions & 1 deletion tests/openedx_tagging/core/tagging/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.contrib.auth import get_user_model
from rest_framework import status
from rest_framework.test import APITestCase
from urllib.parse import urlparse, parse_qs

from openedx_tagging.core.tagging.models import Taxonomy
from openedx_tagging.core.tagging.models.system_defined import SystemDefinedTaxonomy
Expand Down Expand Up @@ -85,7 +86,7 @@ def test_list_taxonomy_queryparams(self, enabled, expected_status, expected_coun
# If we were able to list the taxonomies, check that we got the expected number back
# We take into account the Language Taxonomy that is created by the system in a migration
if status.is_success(expected_status):
assert len(response.data) == expected_count
assert len(response.data["results"]) == expected_count

@ddt.data(
(None, status.HTTP_403_FORBIDDEN),
Expand All @@ -103,6 +104,39 @@ def test_list_taxonomy(self, user_attr, expected_status):
response = self.client.get(url)
assert response.status_code == expected_status

def test_list_taxonomy_pagination(self):
url = TAXONOMY_LIST_URL
Taxonomy.objects.create(name="T1", enabled=True).save()
Taxonomy.objects.create(name="T2", enabled=True).save()
Taxonomy.objects.create(name="T3", enabled=False).save()
Taxonomy.objects.create(name="T4", enabled=False).save()
Taxonomy.objects.create(name="T5", enabled=False).save()

self.client.force_authenticate(user=self.staff)

query_params = {"page_size": 2, "page": 2}
response = self.client.get(url, query_params, format="json")

assert response.status_code == status.HTTP_200_OK

self.assertEqual(set(t["name"] for t in response.data["results"]), set(("T2", "T3")))
parsed_url = urlparse(response.data["next"])

next_page = parse_qs(parsed_url.query).get("page", [None])[0]
assert next_page == '3'

def test_list_invalid_page(self):
url = TAXONOMY_LIST_URL

self.client.force_authenticate(user=self.user)

query_params = {"page": 123123}

response = self.client.get(url, query_params, format="json")

assert response.status_code == status.HTTP_404_NOT_FOUND


@ddt.data(
(None, {"enabled": True}, status.HTTP_403_FORBIDDEN),
(None, {"enabled": False}, status.HTTP_403_FORBIDDEN),
Expand Down

0 comments on commit df959bf

Please sign in to comment.