This repository was archived by the owner on Aug 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtest_pagination_helper.py
More file actions
227 lines (179 loc) · 7.74 KB
/
Copy pathtest_pagination_helper.py
File metadata and controls
227 lines (179 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"""Tests for PaginationHelper utility class."""
from __future__ import annotations
import pytest
from unittest.mock import Mock, call
from gradient._utils import PaginationHelper
class TestPaginationHelper:
"""Test cases for PaginationHelper class."""
def test_init(self) -> None:
"""Test PaginationHelper initialization."""
helper = PaginationHelper(page_size=50, max_pages=10)
assert helper.page_size == 50
assert helper.max_pages == 10
# Test defaults
helper_default = PaginationHelper()
assert helper_default.page_size == 20
assert helper_default.max_pages is None
def test_paginate_basic(self) -> None:
"""Test basic pagination functionality."""
helper = PaginationHelper(page_size=2, max_pages=3)
# Mock fetch function that returns different data for each page
call_count = 0
def mock_fetch(params):
nonlocal call_count
call_count += 1
page = params.get('page', 1)
per_page = params.get('per_page', 2)
if call_count == 1:
return {'data': ['item1', 'item2']}
elif call_count == 2:
return {'data': ['item3', 'item4']}
elif call_count == 3:
return {'data': ['item5']} # Last page with fewer items
else:
return {'data': []} # No more data
result = helper.paginate(mock_fetch)
assert result == ['item1', 'item2', 'item3', 'item4', 'item5']
assert call_count == 3
def test_paginate_with_kwargs(self) -> None:
"""Test pagination with additional kwargs."""
helper = PaginationHelper(page_size=1)
def mock_fetch(params):
# Should include both pagination params and custom params
assert 'page' in params
assert 'per_page' in params
assert params['custom_param'] == 'value'
return {'data': [f'item{params["page"]}']}
# Mock to return only one page
call_count = 0
def mock_fetch_single(params):
nonlocal call_count
call_count += 1
if call_count == 1:
return {'data': ['item1']}
return {'data': []}
result = helper.paginate(mock_fetch_single, custom_param='value')
assert result == ['item1']
def test_paginate_max_pages_limit(self) -> None:
"""Test that max_pages limits the number of pages fetched."""
helper = PaginationHelper(page_size=1, max_pages=2)
call_count = 0
def mock_fetch(params):
nonlocal call_count
call_count += 1
return {'data': [f'item{call_count}']}
result = helper.paginate(mock_fetch)
assert result == ['item1', 'item2']
assert call_count == 2
def test_paginate_different_response_formats(self) -> None:
"""Test pagination with different response formats."""
helper = PaginationHelper(page_size=1)
# Test different response formats
responses = [
{'data': ['item1']}, # Standard format
{'items': ['item2']}, # Alternative format
{'results': ['item3']}, # Another format
['item4'], # Direct list
{'objects': ['item5']}, # Another common format
]
call_count = 0
def mock_fetch(params):
nonlocal call_count
call_count += 1
if call_count <= len(responses):
return responses[call_count - 1]
return []
result = helper.paginate(mock_fetch)
assert result == ['item1', 'item2', 'item3', 'item4', 'item5']
def test_paginate_empty_response(self) -> None:
"""Test pagination with empty response."""
helper = PaginationHelper()
def mock_fetch(params):
return {'data': []}
result = helper.paginate(mock_fetch)
assert result == []
def test_paginate_error_handling(self) -> None:
"""Test pagination error handling."""
helper = PaginationHelper(page_size=1)
call_count = 0
def mock_fetch(params):
nonlocal call_count
call_count += 1
if call_count == 1:
return {'data': ['item1']}
elif call_count == 2:
raise ValueError("Page not found")
return {'data': []}
# Should stop when error indicates end of pagination
result = helper.paginate(mock_fetch)
assert result == ['item1']
assert call_count == 2
def test_paginate_response_object_with_attributes(self) -> None:
"""Test pagination with response objects that have attributes."""
helper = PaginationHelper(page_size=1)
# Mock response objects
class MockResponse:
def __init__(self, data):
self.data = data
call_count = 0
def mock_fetch(params):
nonlocal call_count
call_count += 1
if call_count == 1:
return MockResponse(['item1'])
return MockResponse([])
result = helper.paginate(mock_fetch)
assert result == ['item1']
def test_paginate_partial_page_stops_pagination(self) -> None:
"""Test that getting fewer items than page_size stops pagination."""
helper = PaginationHelper(page_size=3)
call_count = 0
def mock_fetch(params):
nonlocal call_count
call_count += 1
if call_count == 1:
return {'data': ['item1', 'item2']} # Fewer than page_size
return {'data': ['item3', 'item4', 'item5']} # This shouldn't be called
result = helper.paginate(mock_fetch)
assert result == ['item1', 'item2']
assert call_count == 1
@pytest.mark.asyncio
async def test_paginate_async(self) -> None:
"""Test async pagination functionality."""
helper = PaginationHelper(page_size=2, max_pages=2)
call_count = 0
async def mock_fetch(params):
nonlocal call_count
call_count += 1
if call_count == 1:
return {'data': ['item1', 'item2']}
elif call_count == 2:
return {'data': ['item3']} # Last page
return {'data': []}
result = await helper.paginate_async(mock_fetch)
assert result == ['item1', 'item2', 'item3']
assert call_count == 2
def test_extract_items_edge_cases(self) -> None:
"""Test _extract_items method with edge cases."""
helper = PaginationHelper()
# Test None response
assert helper._extract_items(None) == []
# Test dict without expected keys
assert helper._extract_items({'other_key': 'value'}) == []
# Test dict with non-list values
assert helper._extract_items({'data': 'not_a_list'}) == []
# Test nested structures
assert helper._extract_items({'data': {'nested': ['item']}}) == []
def test_is_pagination_end_error(self) -> None:
"""Test _is_pagination_end_error method."""
helper = PaginationHelper()
# Should return True for pagination end errors
assert helper._is_pagination_end_error(ValueError("Page not found"))
assert helper._is_pagination_end_error(ValueError("Invalid page"))
assert helper._is_pagination_end_error(ValueError("No more pages"))
# Should return False for other errors
assert not helper._is_pagination_end_error(ValueError("Network error"))
assert not helper._is_pagination_end_error(ValueError("Timeout"))
# Should handle different error types
assert helper._is_pagination_end_error(Exception("page not found"))
assert not helper._is_pagination_end_error(Exception("other error"))