From 9efa0334fcd570a11aa693eb73ce24e56cfa0946 Mon Sep 17 00:00:00 2001 From: Lvyshnevska Date: Fri, 25 Oct 2024 09:50:37 +0200 Subject: [PATCH 1/2] page size, default order added, id removed from ordering --- BackEnd/administration/filters.py | 1 - BackEnd/administration/pagination.py | 1 + BackEnd/administration/views.py | 4 +--- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/BackEnd/administration/filters.py b/BackEnd/administration/filters.py index 5827449cd..2a522ddc5 100644 --- a/BackEnd/administration/filters.py +++ b/BackEnd/administration/filters.py @@ -32,7 +32,6 @@ def filter_is_deleted(self, queryset, name, value): ordering = filters.OrderingFilter( fields=( - ("id", "id"), ("name", "name"), ("surname", "surname"), ("email", "email"), diff --git a/BackEnd/administration/pagination.py b/BackEnd/administration/pagination.py index 4e104712a..7d3548652 100644 --- a/BackEnd/administration/pagination.py +++ b/BackEnd/administration/pagination.py @@ -4,6 +4,7 @@ class ListPagination(PageNumberPagination): page_size_query_param = "page_size" + page_size = 20 def get_paginated_response(self, data): return Response( diff --git a/BackEnd/administration/views.py b/BackEnd/administration/views.py index 3d100d5f6..213c334ae 100644 --- a/BackEnd/administration/views.py +++ b/BackEnd/administration/views.py @@ -55,9 +55,7 @@ class UsersListView(ListAPIView): serializer_class = AdminUserListSerializer filter_backends = [DjangoFilterBackend] filterset_class = UsersFilter - - def get_queryset(self): - return CustomUser.objects.select_related("profile") + queryset = CustomUser.objects.select_related("profile").order_by("id") class UserDetailView(RetrieveUpdateDestroyAPIView): From 4b613425e3fb75ecfc9057b6289e2c398ae5357d Mon Sep 17 00:00:00 2001 From: Lvyshnevska Date: Fri, 25 Oct 2024 09:51:05 +0200 Subject: [PATCH 2/2] tests updated --- .../tests/test_admin_profiles.py | 2 +- .../administration/tests/test_admin_user.py | 56 ++++--------------- 2 files changed, 11 insertions(+), 47 deletions(-) diff --git a/BackEnd/administration/tests/test_admin_profiles.py b/BackEnd/administration/tests/test_admin_profiles.py index f81bb91d1..c4d4fd36d 100644 --- a/BackEnd/administration/tests/test_admin_profiles.py +++ b/BackEnd/administration/tests/test_admin_profiles.py @@ -75,7 +75,7 @@ def test_get_profiles_structure_json(self): } ] self.assertEqual(status.HTTP_200_OK, response.status_code) - self.assertEqual(data, response.json()) + self.assertEqual(data, response.data["results"]) def test_get_profile_id_authenticated(self): self.client.force_authenticate(self.user) diff --git a/BackEnd/administration/tests/test_admin_user.py b/BackEnd/administration/tests/test_admin_user.py index df94a5485..125db72c4 100644 --- a/BackEnd/administration/tests/test_admin_user.py +++ b/BackEnd/administration/tests/test_admin_user.py @@ -29,45 +29,9 @@ def setUp(self): AdminUserFactory.reset_sequence(1) self.user = self.users[0] - def test_get_users_ordering_desc(self): + def test_get_users_ordering_default(self): self.client.force_authenticate(self.user) - response = self.client.get(path="/api/admin/users/?ordering=-id") - data = [ - { - "id": 3, - "email": "test3@test.com", - "name": "Test person 3", - "surname": "Test person 3 surname", - "status": { - "is_active": True, - "is_staff": True, - "is_superuser": False, - "is_deleted": False, - }, - "company_name": None, - "registration_date": None, - }, - { - "id": 2, - "email": "test2@test.com", - "name": "Test person 2", - "surname": "Test person 2 surname", - "status": { - "is_active": True, - "is_staff": True, - "is_superuser": False, - "is_deleted": False, - }, - "company_name": None, - "registration_date": None, - }, - ] - self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(data, response.json()) - - def test_get_users_ordering_asc(self): - self.client.force_authenticate(self.user) - response = self.client.get(path="/api/admin/users/?ordering=id") + response = self.client.get(path="/api/admin/users/") data = [ { "id": 2, @@ -99,7 +63,7 @@ def test_get_users_ordering_asc(self): }, ] self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(data, response.json()) + self.assertEqual(data, response.data["results"]) def test_get_users_filter_id_surname(self): self.client.force_authenticate(self.user) @@ -121,7 +85,7 @@ def test_get_users_filter_id_surname(self): }, ] self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(data, response.json()) + self.assertEqual(data, response.data["results"]) class TestAdminUsersStatusAPITests(APITestCase): @@ -166,7 +130,7 @@ def test_get_users_filter_status_active_staff(self): }, ] self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(data, response.json()) + self.assertEqual(data, response.data["results"]) def test_get_users_filter_status_superuser_staff_active(self): self.client.force_authenticate(self.user) @@ -175,7 +139,7 @@ def test_get_users_filter_status_superuser_staff_active(self): ) data = [] self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(data, response.json()) + self.assertEqual(data, response.data["results"]) class TestAdminUsersAPITests(APITestCase): @@ -211,11 +175,11 @@ def test_get_users_authenticated(self): } ] self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(data, response.json()) + self.assertEqual(data, response.data["results"]) - def test_get_users_filter(self): + def test_get_users(self): self.client.force_authenticate(self.user) - response = self.client.get(path="/api/admin/users/?ordering=id") + response = self.client.get(path="/api/admin/users/") data = [ { "id": 2, @@ -233,7 +197,7 @@ def test_get_users_filter(self): } ] self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(data, response.json()) + self.assertEqual(data, response.data["results"]) def test_get_user_id_authenticated(self): self.client.force_authenticate(self.user)