Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/#99 paginering list #121

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/nrc/api/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ def test_subscription_list(self):
response = self.client.get(url)

self.assertEqual(response.status_code, status.HTTP_200_OK)

results = response.data
results = response.json()["results"]

self.assertEqual(len(results), 1)

Expand Down Expand Up @@ -97,7 +96,7 @@ def test_read_superuser(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)

response_data = response.json()
self.assertEqual(len(response_data), 1)
self.assertEqual(len(response_data["results"]), 1)


class SubscriptionWriteScopeTests(JWTAuthMixin, APITestCase):
Expand Down Expand Up @@ -181,7 +180,7 @@ def test_domain_list(self):

results = response.data

self.assertEqual(len(results), 1)
self.assertEqual(len(results["results"]), 1)

def test_domain_retrieve(self):
domain = DomainFactory.create()
Expand Down Expand Up @@ -211,7 +210,7 @@ def test_read_superuser(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)

response_data = response.json()
self.assertEqual(len(response_data), 1)
self.assertEqual(len(response_data["results"]), 1)


class DomainWriteScopeTests(JWTAuthMixin, APITestCase):
Expand Down
35 changes: 31 additions & 4 deletions src/nrc/api/tests/test_domains.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from rest_framework import status
from rest_framework.test import APITestCase
from vng_api_common.tests import JWTAuthMixin, get_operation_url
from vng_api_common.tests import JWTAuthMixin, get_operation_url, reverse
from vng_api_common.tests.schema import get_validation_errors

from nrc.datamodel.models import Domain
Expand Down Expand Up @@ -119,11 +119,11 @@ def test_domain_filter_naam(self):

self.assertEqual(response.status_code, status.HTTP_200_OK)

data = response.json()
data = response.json()["results"]

self.assertEqual(len(data), 1)
self.assertEqual(response.data[0]["name"], domain1.name)
self.assertNotEqual(response.data[0]["name"], domain2.name)
self.assertEqual(data[0]["name"], domain1.name)
self.assertNotEqual(data[0]["name"], domain2.name)

def test_filter_attributes(self):
data = {
Expand All @@ -149,3 +149,30 @@ def test_filter_attributes(self):
self.assertEqual(
domain.filter_attributes, ["bronorganisatie", "vertrouwelijkheid"]
)


class DomainPaginationTestsCase(JWTAuthMixin, APITestCase):
heeft_alle_autorisaties = True

def test_pagination_default(self):
domain1, domain2 = DomainFactory.create_batch(2)
url = get_operation_url("domain_list")
response = self.client.get(url)

self.assertEqual(response.status_code, status.HTTP_200_OK)
response_data = response.json()
self.assertEqual(response_data["count"], 2)
self.assertIsNone(response_data["previous"])
self.assertIsNone(response_data["next"])

def test_pagination_page_param(self):
domain1, domain2 = DomainFactory.create_batch(2)
url = get_operation_url("domain_list")

response = self.client.get(url, {"page": 1})

self.assertEqual(response.status_code, status.HTTP_200_OK)
response_data = response.json()
self.assertEqual(response_data["count"], 2)
self.assertIsNone(response_data["previous"])
self.assertIsNone(response_data["next"])
MatthijsBekendam marked this conversation as resolved.
Show resolved Hide resolved
29 changes: 29 additions & 0 deletions src/nrc/api/tests/test_subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,3 +854,32 @@ def test_subscription_custom_filtering_extra_keys(self):
error = get_validation_errors(response, "filters")

self.assertEqual(error["reason"], _("De opgegeven filter is niet valide."))


class SubscriptionPaginationTestsCase(JWTAuthMixin, APITestCase):
heeft_alle_autorisaties = True

def test_pagination_default(self):
sub1, sub2 = SubscriptionFactory.create_batch(2)
url = get_operation_url("subscription_list")
response = self.client.get(url)

self.assertEqual(response.status_code, status.HTTP_200_OK)
response_data = response.json()

self.assertEqual(response_data["count"], 2)
self.assertIsNone(response_data["previous"])
self.assertIsNone(response_data["next"])

def test_pagination_page_param(self):
sub1, sub2 = SubscriptionFactory.create_batch(2)
url = get_operation_url("subscription_list")

response = self.client.get(url, {"page": 1})

self.assertEqual(response.status_code, status.HTTP_200_OK)
response_data = response.json()

self.assertEqual(response_data["count"], 2)
self.assertIsNone(response_data["previous"])
self.assertIsNone(response_data["next"])
MatthijsBekendam marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions src/nrc/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from drf_yasg.utils import swagger_auto_schema
from rest_framework import mixins, status, views, viewsets
from rest_framework.pagination import PageNumberPagination
from rest_framework.parsers import JSONParser
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
Expand Down Expand Up @@ -49,6 +50,7 @@ class SubscriptionViewSet(

queryset = Subscription.objects.all()
serializer_class = SubscriptionSerializer
pagination_class = PageNumberPagination
lookup_field = "uuid"
required_scopes = {
"list": SCOPE_EVENTS_CONSUMEREN | SCOPE_EVENTS_PUBLICEREN,
Expand Down Expand Up @@ -83,6 +85,7 @@ class DomainViewSet(
serializer_class = DomainSerializer
filterset_class = DomainFilter
lookup_field = "uuid"
pagination_class = PageNumberPagination
required_scopes = {
"list": SCOPE_EVENTS_PUBLICEREN | SCOPE_EVENTS_CONSUMEREN,
"retrieve": SCOPE_EVENTS_PUBLICEREN | SCOPE_EVENTS_CONSUMEREN,
Expand Down
1 change: 1 addition & 0 deletions src/nrc/conf/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
API_VERSION = "2.0.0-alpha12"

REST_FRAMEWORK = BASE_REST_FRAMEWORK.copy()
REST_FRAMEWORK["PAGE_SIZE"] = 100
REST_FRAMEWORK.update(
{"DEFAULT_PERMISSION_CLASSES": ("vng_api_common.permissions.AuthScopesRequired",)}
)
Expand Down