From ec1fc4d2b896486580b2d3f0306c8de86cb2936f Mon Sep 17 00:00:00 2001 From: matthijsbekendam Date: Mon, 25 Jul 2022 10:55:45 +0200 Subject: [PATCH 1/3] [#99] added pagination --- src/nrc/api/tests/test_auth.py | 9 +++---- src/nrc/api/tests/test_domains.py | 34 ++++++++++++++++++++++--- src/nrc/api/tests/test_subscriptions.py | 29 +++++++++++++++++++++ src/nrc/api/viewsets.py | 3 +++ src/nrc/conf/api.py | 1 + 5 files changed, 67 insertions(+), 9 deletions(-) diff --git a/src/nrc/api/tests/test_auth.py b/src/nrc/api/tests/test_auth.py index 9037ac3e..94ded0dc 100644 --- a/src/nrc/api/tests/test_auth.py +++ b/src/nrc/api/tests/test_auth.py @@ -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) @@ -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): @@ -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() @@ -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): diff --git a/src/nrc/api/tests/test_domains.py b/src/nrc/api/tests/test_domains.py index c1e93a23..26b51871 100644 --- a/src/nrc/api/tests/test_domains.py +++ b/src/nrc/api/tests/test_domains.py @@ -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 @@ -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 = { @@ -149,3 +149,29 @@ 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"]) diff --git a/src/nrc/api/tests/test_subscriptions.py b/src/nrc/api/tests/test_subscriptions.py index b3183046..2e95c9ba 100644 --- a/src/nrc/api/tests/test_subscriptions.py +++ b/src/nrc/api/tests/test_subscriptions.py @@ -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"]) diff --git a/src/nrc/api/viewsets.py b/src/nrc/api/viewsets.py index d00b9ab2..0f8b83f9 100644 --- a/src/nrc/api/viewsets.py +++ b/src/nrc/api/viewsets.py @@ -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 @@ -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, @@ -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, diff --git a/src/nrc/conf/api.py b/src/nrc/conf/api.py index 4119abb8..997cb078 100644 --- a/src/nrc/conf/api.py +++ b/src/nrc/conf/api.py @@ -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",)} ) From 1592df2960260333c22f662ef630d96a123bfe87 Mon Sep 17 00:00:00 2001 From: matthijsbekendam Date: Mon, 25 Jul 2022 11:00:20 +0200 Subject: [PATCH 2/3] [#99] reformat --- src/nrc/api/tests/test_auth.py | 8 ++++---- src/nrc/api/tests/test_domains.py | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/nrc/api/tests/test_auth.py b/src/nrc/api/tests/test_auth.py index 94ded0dc..4585cd2e 100644 --- a/src/nrc/api/tests/test_auth.py +++ b/src/nrc/api/tests/test_auth.py @@ -61,7 +61,7 @@ def test_subscription_list(self): response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) - results = response.json()['results'] + results = response.json()["results"] self.assertEqual(len(results), 1) @@ -96,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['results']), 1) + self.assertEqual(len(response_data["results"]), 1) class SubscriptionWriteScopeTests(JWTAuthMixin, APITestCase): @@ -180,7 +180,7 @@ def test_domain_list(self): results = response.data - self.assertEqual(len(results['results']), 1) + self.assertEqual(len(results["results"]), 1) def test_domain_retrieve(self): domain = DomainFactory.create() @@ -210,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['results']), 1) + self.assertEqual(len(response_data["results"]), 1) class DomainWriteScopeTests(JWTAuthMixin, APITestCase): diff --git a/src/nrc/api/tests/test_domains.py b/src/nrc/api/tests/test_domains.py index 26b51871..f45f82d1 100644 --- a/src/nrc/api/tests/test_domains.py +++ b/src/nrc/api/tests/test_domains.py @@ -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,reverse +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 @@ -119,7 +119,7 @@ def test_domain_filter_naam(self): self.assertEqual(response.status_code, status.HTTP_200_OK) - data = response.json()['results'] + data = response.json()["results"] self.assertEqual(len(data), 1) self.assertEqual(data[0]["name"], domain1.name) @@ -150,6 +150,7 @@ def test_filter_attributes(self): domain.filter_attributes, ["bronorganisatie", "vertrouwelijkheid"] ) + class DomainPaginationTestsCase(JWTAuthMixin, APITestCase): heeft_alle_autorisaties = True From 95b2bed36fa736bb523eabcdffce8f53209bff7e Mon Sep 17 00:00:00 2001 From: matthijsbekendam Date: Thu, 28 Jul 2022 09:14:09 +0200 Subject: [PATCH 3/3] [#99] removed irrelevant tests --- src/nrc/api/tests/test_domains.py | 27 ----------------------- src/nrc/api/tests/test_subscriptions.py | 29 ------------------------- 2 files changed, 56 deletions(-) diff --git a/src/nrc/api/tests/test_domains.py b/src/nrc/api/tests/test_domains.py index f45f82d1..6018f9a7 100644 --- a/src/nrc/api/tests/test_domains.py +++ b/src/nrc/api/tests/test_domains.py @@ -149,30 +149,3 @@ 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"]) diff --git a/src/nrc/api/tests/test_subscriptions.py b/src/nrc/api/tests/test_subscriptions.py index 2e95c9ba..b3183046 100644 --- a/src/nrc/api/tests/test_subscriptions.py +++ b/src/nrc/api/tests/test_subscriptions.py @@ -854,32 +854,3 @@ 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"])