@@ -12,17 +12,17 @@ class TestCustomPagination:
1212 def test_paginate_first_page (self ):
1313 """Test pagination on the first page."""
1414 paginator = CustomPagination ()
15-
15+
1616 mock_queryset = MagicMock ()
1717 mock_queryset .count .return_value = 25
1818 mock_queryset .__getitem__ = MagicMock (return_value = ["item1" , "item2" , "item3" ])
19-
19+
2020 mock_pagination = MagicMock ()
2121 mock_pagination .page = 1
2222 mock_pagination .page_size = 10
23-
23+
2424 result = paginator .paginate_queryset (mock_queryset , mock_pagination )
25-
25+
2626 assert result ["current_page" ] == 1
2727 assert result ["total_count" ] == 25
2828 assert result ["total_pages" ] == 3
@@ -34,17 +34,17 @@ def test_paginate_first_page(self):
3434 def test_paginate_middle_page (self ):
3535 """Test pagination on a middle page."""
3636 paginator = CustomPagination ()
37-
37+
3838 mock_queryset = MagicMock ()
3939 mock_queryset .count .return_value = 35
4040 mock_queryset .__getitem__ = MagicMock (return_value = ["item11" , "item12" ])
41-
41+
4242 mock_pagination = MagicMock ()
4343 mock_pagination .page = 2
4444 mock_pagination .page_size = 10
45-
45+
4646 result = paginator .paginate_queryset (mock_queryset , mock_pagination )
47-
47+
4848 assert result ["current_page" ] == 2
4949 assert result ["total_count" ] == 35
5050 assert result ["total_pages" ] == 4
@@ -56,17 +56,19 @@ def test_paginate_middle_page(self):
5656 def test_paginate_last_page (self ):
5757 """Test pagination on the last page."""
5858 paginator = CustomPagination ()
59-
59+
6060 mock_queryset = MagicMock ()
6161 mock_queryset .count .return_value = 25
62- mock_queryset .__getitem__ = MagicMock (return_value = ["item21" , "item22" , "item23" , "item24" , "item25" ])
63-
62+ mock_queryset .__getitem__ = MagicMock (
63+ return_value = ["item21" , "item22" , "item23" , "item24" , "item25" ]
64+ )
65+
6466 mock_pagination = MagicMock ()
6567 mock_pagination .page = 3
6668 mock_pagination .page_size = 10
67-
69+
6870 result = paginator .paginate_queryset (mock_queryset , mock_pagination )
69-
71+
7072 assert result ["current_page" ] == 3
7173 assert result ["total_count" ] == 25
7274 assert result ["total_pages" ] == 3
@@ -78,17 +80,19 @@ def test_paginate_last_page(self):
7880 def test_paginate_single_page (self ):
7981 """Test pagination when all items fit on a single page."""
8082 paginator = CustomPagination ()
81-
83+
8284 mock_queryset = MagicMock ()
8385 mock_queryset .count .return_value = 5
84- mock_queryset .__getitem__ = MagicMock (return_value = ["item1" , "item2" , "item3" , "item4" , "item5" ])
85-
86+ mock_queryset .__getitem__ = MagicMock (
87+ return_value = ["item1" , "item2" , "item3" , "item4" , "item5" ]
88+ )
89+
8690 mock_pagination = MagicMock ()
8791 mock_pagination .page = 1
8892 mock_pagination .page_size = 10
89-
93+
9094 result = paginator .paginate_queryset (mock_queryset , mock_pagination )
91-
95+
9296 assert result ["current_page" ] == 1
9397 assert result ["total_count" ] == 5
9498 assert result ["total_pages" ] == 1
@@ -100,17 +104,17 @@ def test_paginate_single_page(self):
100104 def test_paginate_empty_queryset_page_one (self ):
101105 """Test pagination with empty queryset on page 1 (should succeed)."""
102106 paginator = CustomPagination ()
103-
107+
104108 mock_queryset = MagicMock ()
105109 mock_queryset .count .return_value = 0
106110 mock_queryset .__getitem__ = MagicMock (return_value = [])
107-
111+
108112 mock_pagination = MagicMock ()
109113 mock_pagination .page = 1
110114 mock_pagination .page_size = 10
111-
115+
112116 result = paginator .paginate_queryset (mock_queryset , mock_pagination )
113-
117+
114118 assert result ["current_page" ] == 1
115119 assert result ["total_count" ] == 0
116120 assert result ["total_pages" ] == 1 # max(1, 0) = 1
@@ -122,49 +126,49 @@ def test_paginate_empty_queryset_page_one(self):
122126 def test_paginate_empty_queryset_page_two (self ):
123127 """Test pagination with empty queryset on page 2 (should raise Http404)."""
124128 paginator = CustomPagination ()
125-
129+
126130 mock_queryset = MagicMock ()
127131 mock_queryset .count .return_value = 0
128-
132+
129133 mock_pagination = MagicMock ()
130134 mock_pagination .page = 2
131135 mock_pagination .page_size = 10
132-
136+
133137 with pytest .raises (Http404 ) as exc_info :
134138 paginator .paginate_queryset (mock_queryset , mock_pagination )
135-
139+
136140 assert "Page 2 not found. Valid pages are 1 to 1." in str (exc_info .value )
137141
138142 def test_paginate_page_out_of_range (self ):
139143 """Test pagination when requested page exceeds total pages."""
140144 paginator = CustomPagination ()
141-
145+
142146 mock_queryset = MagicMock ()
143147 mock_queryset .count .return_value = 25
144-
148+
145149 mock_pagination = MagicMock ()
146150 mock_pagination .page = 5
147151 mock_pagination .page_size = 10
148-
152+
149153 with pytest .raises (Http404 ) as exc_info :
150154 paginator .paginate_queryset (mock_queryset , mock_pagination )
151-
155+
152156 assert "Page 5 not found. Valid pages are 1 to 3." in str (exc_info .value )
153157
154158 def test_paginate_partial_last_page (self ):
155159 """Test pagination when last page has fewer items than page_size."""
156160 paginator = CustomPagination ()
157-
161+
158162 mock_queryset = MagicMock ()
159163 mock_queryset .count .return_value = 23
160164 mock_queryset .__getitem__ = MagicMock (return_value = ["item21" , "item22" , "item23" ])
161-
165+
162166 mock_pagination = MagicMock ()
163167 mock_pagination .page = 3
164168 mock_pagination .page_size = 10
165-
169+
166170 result = paginator .paginate_queryset (mock_queryset , mock_pagination )
167-
171+
168172 assert result ["current_page" ] == 3
169173 assert result ["total_count" ] == 23
170174 assert result ["total_pages" ] == 3
@@ -176,50 +180,72 @@ def test_paginate_partial_last_page(self):
176180 def test_paginate_exact_multiple_pages (self ):
177181 """Test pagination when total items is exact multiple of page_size."""
178182 paginator = CustomPagination ()
179-
183+
180184 mock_queryset = MagicMock ()
181185 mock_queryset .count .return_value = 30
182- mock_queryset .__getitem__ = MagicMock (return_value = ["item21" , "item22" , "item23" , "item24" , "item25" ,
183- "item26" , "item27" , "item28" , "item29" , "item30" ])
184-
186+ mock_queryset .__getitem__ = MagicMock (
187+ return_value = [
188+ "item21" ,
189+ "item22" ,
190+ "item23" ,
191+ "item24" ,
192+ "item25" ,
193+ "item26" ,
194+ "item27" ,
195+ "item28" ,
196+ "item29" ,
197+ "item30" ,
198+ ]
199+ )
200+
185201 mock_pagination = MagicMock ()
186202 mock_pagination .page = 3
187203 mock_pagination .page_size = 10
188-
204+
189205 result = paginator .paginate_queryset (mock_queryset , mock_pagination )
190-
206+
191207 assert result ["current_page" ] == 3
192208 assert result ["total_count" ] == 30
193209 assert result ["total_pages" ] == 3
194210 assert result ["has_next" ] is False
195211 assert result ["has_previous" ] is True
196- assert result ["items" ] == ["item21" , "item22" , "item23" , "item24" , "item25" ,
197- "item26" , "item27" , "item28" , "item29" , "item30" ]
212+ assert result ["items" ] == [
213+ "item21" ,
214+ "item22" ,
215+ "item23" ,
216+ "item24" ,
217+ "item25" ,
218+ "item26" ,
219+ "item27" ,
220+ "item28" ,
221+ "item29" ,
222+ "item30" ,
223+ ]
198224 mock_queryset .__getitem__ .assert_called_once_with (slice (20 , 30 ))
199225
200226 def test_paginate_offset_calculation (self ):
201227 """Test that offset calculation is correct for various pages."""
202228 paginator = CustomPagination ()
203-
229+
204230 mock_queryset = MagicMock ()
205231 mock_queryset .count .return_value = 100
206232 mock_queryset .__getitem__ = MagicMock (return_value = [])
207-
233+
208234 mock_pagination = MagicMock ()
209235 mock_pagination .page_size = 20
210-
236+
211237 # Page 1: offset = (1-1) * 20 = 0
212238 mock_pagination .page = 1
213239 paginator .paginate_queryset (mock_queryset , mock_pagination )
214240 mock_queryset .__getitem__ .assert_called_with (slice (0 , 20 ))
215241 mock_queryset .__getitem__ .reset_mock ()
216-
242+
217243 # Page 3: offset = (3-1) * 20 = 40
218244 mock_pagination .page = 3
219245 paginator .paginate_queryset (mock_queryset , mock_pagination )
220246 mock_queryset .__getitem__ .assert_called_with (slice (40 , 60 ))
221247 mock_queryset .__getitem__ .reset_mock ()
222-
248+
223249 # Page 5: offset = (5-1) * 20 = 80
224250 mock_pagination .page = 5
225251 paginator .paginate_queryset (mock_queryset , mock_pagination )
@@ -228,39 +254,39 @@ def test_paginate_offset_calculation(self):
228254 def test_paginate_total_pages_calculation (self ):
229255 """Test total_pages calculation for various edge cases."""
230256 paginator = CustomPagination ()
231-
257+
232258 mock_queryset = MagicMock ()
233259 mock_queryset .__getitem__ = MagicMock (return_value = [])
234-
260+
235261 mock_pagination = MagicMock ()
236262 mock_pagination .page = 1
237263 mock_pagination .page_size = 10
238-
264+
239265 # 0 items: total_pages = max(1, (0 + 10 - 1) // 10) = max(1, 0) = 1
240266 mock_queryset .count .return_value = 0
241267 result = paginator .paginate_queryset (mock_queryset , mock_pagination )
242268 assert result ["total_pages" ] == 1
243-
269+
244270 # 1 item: total_pages = max(1, (1 + 10 - 1) // 10) = max(1, 1) = 1
245271 mock_queryset .count .return_value = 1
246272 result = paginator .paginate_queryset (mock_queryset , mock_pagination )
247273 assert result ["total_pages" ] == 1
248-
274+
249275 # 10 items: total_pages = max(1, (10 + 10 - 1) // 10) = max(1, 1) = 1
250276 mock_queryset .count .return_value = 10
251277 result = paginator .paginate_queryset (mock_queryset , mock_pagination )
252278 assert result ["total_pages" ] == 1
253-
279+
254280 # 11 items: total_pages = max(1, (11 + 10 - 1) // 10) = max(1, 2) = 2
255281 mock_queryset .count .return_value = 11
256282 result = paginator .paginate_queryset (mock_queryset , mock_pagination )
257283 assert result ["total_pages" ] == 2
258-
284+
259285 # 99 items: total_pages = max(1, (99 + 10 - 1) // 10) = max(1, 10) = 10
260286 mock_queryset .count .return_value = 99
261287 result = paginator .paginate_queryset (mock_queryset , mock_pagination )
262288 assert result ["total_pages" ] == 10
263-
289+
264290 # 100 items: total_pages = max(1, (100 + 10 - 1) // 10) = max(1, 10) = 10
265291 mock_queryset .count .return_value = 100
266292 result = paginator .paginate_queryset (mock_queryset , mock_pagination )
0 commit comments