@@ -136,170 +136,105 @@ async def test_page_int_iterator_async_using_auto_pagination(
136
136
assert not int_based_paginator .result .pig_dogs
137
137
138
138
139
+ # Helper function to initialize token paginators
140
+ def initialize_token_paginator (endpoint_mock , request_data , responses , is_async = False ):
141
+ client = AsyncMock () if is_async else Mock ()
142
+ client .configuration .transport .request .side_effect = responses
143
+
144
+ endpoint_mock .request_data = request_data
145
+
146
+ if is_async :
147
+ return AsyncTokenBasedPaginator ._initialize (sinch = client , endpoint = endpoint_mock )
148
+ return TokenBasedPaginator (sinch = client , endpoint = endpoint_mock )
149
+
150
+ EXPECTED_PHONE_NUMBERS = [
151
+ '+12345678901' , '+12345678902' , '+12345678903' , '+12345678904' , '+12345678905'
152
+ ]
153
+
154
+
139
155
def test_page_token_iterator_sync_using_manual_pagination (
140
- token_based_pagination_request_data ,
141
- first_token_based_pagination_response ,
142
- second_token_based_pagination_response ,
143
- third_token_based_pagination_response
156
+ token_based_pagination_request_data ,
157
+ mock_pagination_active_number_responses
144
158
):
145
- endpoint = Mock ()
146
- endpoint .request_data = token_based_pagination_request_data
147
- sinch_client = Mock ()
148
-
149
- sinch_client .configuration .transport .request .side_effect = [
150
- first_token_based_pagination_response ,
151
- second_token_based_pagination_response ,
152
- third_token_based_pagination_response
153
- ]
154
- token_based_paginator = TokenBasedPaginator ._initialize (
155
- sinch = sinch_client ,
156
- endpoint = endpoint
159
+ token_based_paginator = initialize_token_paginator (
160
+ endpoint_mock = Mock (),
161
+ request_data = token_based_pagination_request_data ,
162
+ responses = mock_pagination_active_number_responses
157
163
)
158
- assert token_based_paginator
164
+ assert token_based_paginator is not None
159
165
160
166
page_counter = 1
167
+ active_numbers_list = [num .phone_number for num in token_based_paginator .content ()]
168
+
161
169
while token_based_paginator .has_next_page :
162
170
token_based_paginator = token_based_paginator .next_page ()
163
171
page_counter += 1
164
172
assert isinstance (token_based_paginator , TokenBasedPaginator )
173
+ active_numbers_list .extend (num .phone_number for num in token_based_paginator .content ())
165
174
166
175
assert page_counter == 3
176
+ assert active_numbers_list == EXPECTED_PHONE_NUMBERS
167
177
168
178
169
- def test_page_token_iterator_numbers_sync_using_auto_pagination_expects_iter (token_based_pagination_request_data ,
170
- mock_pagination_active_number_responses ):
171
- """ Test that the pagination iterates correctly through multiple items. """
172
-
173
- sinch_client = Mock ()
174
- sinch_client .configuration .transport .request .side_effect = mock_pagination_active_number_responses
175
- endpoint = Mock ()
176
- endpoint .request_data = token_based_pagination_request_data
177
-
178
- token_based_paginator = TokenBasedPaginator (
179
- sinch = sinch_client ,
180
- endpoint = endpoint
181
- )
182
- assert token_based_paginator
183
-
184
- number_counter = 0
185
- for _ in token_based_paginator .iterator ():
186
- number_counter += 1
187
- assert number_counter == 5
188
-
189
-
190
- def test_page_token_iterator_sync_using_list_expects_correct_metadata (token_based_pagination_request_data ,
191
- mock_pagination_active_number_responses ):
192
- """Test `list()` correctly structures pagination metadata with proper `.content` handling."""
193
-
194
- endpoint = Mock ()
195
- endpoint .request_data = token_based_pagination_request_data
196
- sinch_client = Mock ()
197
-
198
- sinch_client .configuration .transport .request .side_effect = mock_pagination_active_number_responses
199
-
200
- token_based_paginator = TokenBasedPaginator (
201
- sinch = sinch_client ,
202
- endpoint = endpoint
179
+ def test_page_token_iterator_sync_using_auto_pagination (
180
+ token_based_pagination_request_data ,
181
+ mock_pagination_active_number_responses
182
+ ):
183
+ token_based_paginator = initialize_token_paginator (
184
+ endpoint_mock = Mock (),
185
+ request_data = token_based_pagination_request_data ,
186
+ responses = mock_pagination_active_number_responses
203
187
)
204
- assert token_based_paginator
188
+ assert token_based_paginator is not None
205
189
206
- list_response = token_based_paginator .get_content ()
190
+ active_numbers_list = [ num . phone_number for num in token_based_paginator .iterator ()]
207
191
208
- page_counter = 0
209
- reached_last_page = False
210
-
211
- while not reached_last_page :
212
- page_counter += 1
213
- if list_response .has_next_page :
214
- list_response = list_response .next_page ()
215
- else :
216
- reached_last_page = True
217
-
218
- assert page_counter == 3
192
+ assert len (active_numbers_list ) == len (EXPECTED_PHONE_NUMBERS )
193
+ assert active_numbers_list == EXPECTED_PHONE_NUMBERS
219
194
220
195
196
+ @pytest .mark .asyncio
221
197
async def test_page_token_iterator_async_using_manual_pagination (
222
198
token_based_pagination_request_data ,
223
- first_token_based_pagination_response ,
224
- second_token_based_pagination_response ,
225
- third_token_based_pagination_response
199
+ mock_pagination_active_number_responses
226
200
):
227
- endpoint = Mock ()
228
- endpoint .request_data = token_based_pagination_request_data
229
- sinch_client = AsyncMock ()
230
-
231
- sinch_client .configuration .transport .request .side_effect = [
232
- first_token_based_pagination_response ,
233
- second_token_based_pagination_response ,
234
- third_token_based_pagination_response
235
- ]
236
- token_based_paginator = await AsyncTokenBasedPaginator ._initialize (
237
- sinch = sinch_client ,
238
- endpoint = endpoint
201
+ async_token_based_paginator = await initialize_token_paginator (
202
+ endpoint_mock = AsyncMock (),
203
+ request_data = token_based_pagination_request_data ,
204
+ responses = mock_pagination_active_number_responses ,
205
+ is_async = True
239
206
)
240
- assert token_based_paginator
207
+ assert async_token_based_paginator is not None
241
208
209
+ active_numbers_list = [num .phone_number for num in async_token_based_paginator .content ()]
242
210
page_counter = 1
243
- while token_based_paginator .has_next_page :
244
- token_based_paginator = await token_based_paginator .next_page ()
211
+
212
+ while async_token_based_paginator .has_next_page :
213
+ async_token_based_paginator = await async_token_based_paginator .next_page ()
245
214
page_counter += 1
246
- assert isinstance (token_based_paginator , AsyncTokenBasedPaginator )
215
+ assert isinstance (async_token_based_paginator , AsyncTokenBasedPaginator )
216
+ active_numbers_list .extend (num .phone_number for num in async_token_based_paginator .content ())
247
217
248
218
assert page_counter == 3
219
+ assert active_numbers_list == EXPECTED_PHONE_NUMBERS
249
220
250
221
251
- async def test_page_token_iterator_async_using_list_expects_correct_metadata (
252
- token_based_pagination_request_data ,
253
- mock_pagination_active_number_responses
254
- ):
255
- """Test async`list()` correctly structures pagination metadata with proper `.content` handling."""
256
-
257
- endpoint = Mock ()
258
- endpoint .request_data = token_based_pagination_request_data
259
- sinch_client = AsyncMock ()
260
-
261
- sinch_client .configuration .transport .request .side_effect = mock_pagination_active_number_responses
262
-
263
- async_token_based_paginator = await AsyncTokenBasedPaginator ._initialize (
264
- sinch = sinch_client ,
265
- endpoint = endpoint
266
- )
267
- assert async_token_based_paginator
268
-
269
- list_response = await async_token_based_paginator .get_content ()
270
-
271
- page_counter = 0
272
- reached_last_page = False
273
-
274
- while not reached_last_page :
275
- if list_response .has_next_page :
276
- list_response = await list_response .next_page ()
277
- page_counter += 1
278
- else :
279
- reached_last_page = True
280
-
281
- assert page_counter == 2
282
-
283
-
222
+ @pytest .mark .asyncio
284
223
async def test_page_token_iterator_numbers_async_using_auto_pagination_expects_iter (
285
224
token_based_pagination_request_data ,
286
225
mock_pagination_active_number_responses
287
226
):
288
- """Test that the async pagination iterates correctly through multiple items."""
289
-
290
- sinch_client = AsyncMock ()
291
- sinch_client .configuration .transport .request .side_effect = mock_pagination_active_number_responses
292
- endpoint = Mock ()
293
- endpoint .request_data = token_based_pagination_request_data
294
-
295
- async_token_based_paginator = await AsyncTokenBasedPaginator ._initialize (
296
- sinch = sinch_client ,
297
- endpoint = endpoint
227
+ async_token_based_paginator = await initialize_token_paginator (
228
+ endpoint_mock = AsyncMock (),
229
+ request_data = token_based_pagination_request_data ,
230
+ responses = mock_pagination_active_number_responses ,
231
+ is_async = True
298
232
)
299
- assert async_token_based_paginator
233
+ assert async_token_based_paginator is not None
300
234
301
- number_counter = 0
302
- async for _ in async_token_based_paginator .iterator ():
303
- number_counter += 1
235
+ active_numbers_list = [
236
+ num . phone_number async for num in async_token_based_paginator .iterator ()
237
+ ]
304
238
305
- assert number_counter == 5
239
+ assert len (active_numbers_list ) == len (EXPECTED_PHONE_NUMBERS )
240
+ assert active_numbers_list == EXPECTED_PHONE_NUMBERS
0 commit comments