|
1 | 1 | from django.conf import settings |
2 | 2 | from django.core.exceptions import ValidationError |
3 | | -from django.test import TestCase |
| 3 | +from django.test import TestCase, override_settings |
| 4 | +from openwisp_utils.api.pagination import OpenWispPagination |
| 5 | +from rest_framework.pagination import PageNumberPagination |
| 6 | +from rest_framework.test import APIRequestFactory |
4 | 7 | from test_project.serializers import ShelfSerializer |
5 | 8 |
|
6 | 9 | from ..models import Shelf |
@@ -64,3 +67,135 @@ def test_rest_framework_settings_override(self): |
64 | 67 | "TEST": True, |
65 | 68 | }, |
66 | 69 | ) |
| 70 | + |
| 71 | + |
| 72 | +class TestOpenWispPagination(TestCase): |
| 73 | + """Tests for the OpenWispPagination class.""" |
| 74 | + |
| 75 | + def setUp(self): |
| 76 | + self.factory = APIRequestFactory() |
| 77 | + self.pagination = OpenWispPagination() |
| 78 | + |
| 79 | + def test_inheritance(self): |
| 80 | + """Test that OpenWispPagination inherits from PageNumberPagination.""" |
| 81 | + self.assertIsInstance(self.pagination, PageNumberPagination) |
| 82 | + |
| 83 | + def test_default_page_size(self): |
| 84 | + """Test that default page_size is 10.""" |
| 85 | + pagination = OpenWispPagination() |
| 86 | + self.assertEqual(pagination.page_size, 10) |
| 87 | + |
| 88 | + def test_default_max_page_size(self): |
| 89 | + """Test that default max_page_size is 100.""" |
| 90 | + pagination = OpenWispPagination() |
| 91 | + self.assertEqual(pagination.max_page_size, 100) |
| 92 | + |
| 93 | + def test_default_page_size_query_param(self): |
| 94 | + """Test that default page_size_query_param is 'page_size'.""" |
| 95 | + pagination = OpenWispPagination() |
| 96 | + self.assertEqual(pagination.page_size_query_param, "page_size") |
| 97 | + |
| 98 | + @override_settings(OPENWISP_PAGINATION_PAGE_SIZE=25) |
| 99 | + def test_custom_page_size_from_settings(self): |
| 100 | + """Test that page_size can be overridden via settings.""" |
| 101 | + pagination = OpenWispPagination() |
| 102 | + self.assertEqual(pagination.page_size, 25) |
| 103 | + |
| 104 | + @override_settings(OPENWISP_PAGINATION_MAX_PAGE_SIZE=500) |
| 105 | + def test_custom_max_page_size_from_settings(self): |
| 106 | + """Test that max_page_size can be overridden via settings.""" |
| 107 | + pagination = OpenWispPagination() |
| 108 | + self.assertEqual(pagination.max_page_size, 500) |
| 109 | + |
| 110 | + @override_settings(OPENWISP_PAGINATION_PAGE_SIZE_QUERY_PARAM="limit") |
| 111 | + def test_custom_page_size_query_param_from_settings(self): |
| 112 | + """Test that page_size_query_param can be overridden via settings.""" |
| 113 | + pagination = OpenWispPagination() |
| 114 | + self.assertEqual(pagination.page_size_query_param, "limit") |
| 115 | + |
| 116 | + def test_page_size_setter(self): |
| 117 | + """Test that page_size can be set directly.""" |
| 118 | + pagination = OpenWispPagination() |
| 119 | + pagination.page_size = 50 |
| 120 | + self.assertEqual(pagination.page_size, 50) |
| 121 | + |
| 122 | + def test_max_page_size_setter(self): |
| 123 | + """Test that max_page_size can be set directly.""" |
| 124 | + pagination = OpenWispPagination() |
| 125 | + pagination.max_page_size = 200 |
| 126 | + self.assertEqual(pagination.max_page_size, 200) |
| 127 | + |
| 128 | + def test_page_size_query_param_setter(self): |
| 129 | + """Test that page_size_query_param can be set directly.""" |
| 130 | + pagination = OpenWispPagination() |
| 131 | + pagination.page_size_query_param = "per_page" |
| 132 | + self.assertEqual(pagination.page_size_query_param, "per_page") |
| 133 | + |
| 134 | + |
| 135 | +class TestOpenWispPaginationIntegration(CreateMixin, TestCase): |
| 136 | + """Integration tests for OpenWispPagination with actual queryset.""" |
| 137 | + |
| 138 | + shelf_model = Shelf |
| 139 | + |
| 140 | + def setUp(self): |
| 141 | + self.factory = APIRequestFactory() |
| 142 | + # Create test shelves |
| 143 | + for i in range(15): |
| 144 | + self._create_shelf(name=f"shelf{i}") |
| 145 | + |
| 146 | + def _get_request(self, path="/api/shelves/", data=None): |
| 147 | + """Create a DRF Request object with query_params support.""" |
| 148 | + from rest_framework.request import Request |
| 149 | + |
| 150 | + wsgi_request = self.factory.get(path, data) |
| 151 | + return Request(wsgi_request) |
| 152 | + |
| 153 | + def test_paginate_queryset(self): |
| 154 | + """Test that pagination works correctly with a queryset.""" |
| 155 | + pagination = OpenWispPagination() |
| 156 | + request = self._get_request() |
| 157 | + queryset = Shelf.objects.all().order_by("id") |
| 158 | + paginated = pagination.paginate_queryset(queryset, request) |
| 159 | + # Default page size is 10 |
| 160 | + self.assertEqual(len(paginated), 10) |
| 161 | + |
| 162 | + def test_paginate_queryset_second_page(self): |
| 163 | + """Test that pagination returns correct items for second page.""" |
| 164 | + pagination = OpenWispPagination() |
| 165 | + request = self._get_request(data={"page": 2}) |
| 166 | + queryset = Shelf.objects.all().order_by("id") |
| 167 | + paginated = pagination.paginate_queryset(queryset, request) |
| 168 | + # Remaining 5 items on second page |
| 169 | + self.assertEqual(len(paginated), 5) |
| 170 | + |
| 171 | + def test_paginate_queryset_custom_page_size(self): |
| 172 | + """Test pagination with custom page size via query parameter.""" |
| 173 | + pagination = OpenWispPagination() |
| 174 | + request = self._get_request(data={"page_size": 5}) |
| 175 | + queryset = Shelf.objects.all().order_by("id") |
| 176 | + paginated = pagination.paginate_queryset(queryset, request) |
| 177 | + self.assertEqual(len(paginated), 5) |
| 178 | + |
| 179 | + def test_paginate_queryset_respects_max_page_size(self): |
| 180 | + """Test that page_size is capped at max_page_size.""" |
| 181 | + pagination = OpenWispPagination() |
| 182 | + pagination.max_page_size = 10 |
| 183 | + # Request more than max |
| 184 | + request = self._get_request(data={"page_size": 100}) |
| 185 | + queryset = Shelf.objects.all().order_by("id") |
| 186 | + paginated = pagination.paginate_queryset(queryset, request) |
| 187 | + # Should be capped at max_page_size (10) |
| 188 | + self.assertEqual(len(paginated), 10) |
| 189 | + |
| 190 | + def test_get_paginated_response(self): |
| 191 | + """Test that paginated response has correct structure.""" |
| 192 | + pagination = OpenWispPagination() |
| 193 | + request = self._get_request() |
| 194 | + queryset = Shelf.objects.all().order_by("id") |
| 195 | + pagination.paginate_queryset(queryset, request) |
| 196 | + response = pagination.get_paginated_response([]) |
| 197 | + self.assertIn("count", response.data) |
| 198 | + self.assertIn("next", response.data) |
| 199 | + self.assertIn("previous", response.data) |
| 200 | + self.assertIn("results", response.data) |
| 201 | + self.assertEqual(response.data["count"], 15) |
0 commit comments