|
| 1 | +import warnings |
| 2 | + |
1 | 3 | import pytest
|
2 | 4 | import requests
|
3 | 5 | import responses
|
@@ -324,25 +326,88 @@ def test_list_request(gl):
|
324 | 326 | method=responses.GET,
|
325 | 327 | url=url,
|
326 | 328 | json=[{"name": "project1"}],
|
327 |
| - headers={"X-Total": "1"}, |
| 329 | + headers={"X-Total": "1", "x-per-page": "20"}, |
328 | 330 | status=200,
|
329 | 331 | match=MATCH_EMPTY_QUERY_PARAMS,
|
330 | 332 | )
|
331 | 333 |
|
332 |
| - result = gl.http_list("/projects", as_list=True) |
| 334 | + with warnings.catch_warnings(record=True) as caught_warnings: |
| 335 | + result = gl.http_list("/projects", as_list=True) |
| 336 | + assert len(caught_warnings) == 0 |
333 | 337 | assert isinstance(result, list)
|
334 | 338 | assert len(result) == 1
|
335 | 339 |
|
336 | 340 | result = gl.http_list("/projects", as_list=False)
|
337 | 341 | assert isinstance(result, GitlabList)
|
338 |
| - assert len(result) == 1 |
| 342 | + assert len(list(result)) == 1 |
339 | 343 |
|
340 | 344 | result = gl.http_list("/projects", all=True)
|
341 | 345 | assert isinstance(result, list)
|
342 | 346 | assert len(result) == 1
|
343 | 347 | assert responses.assert_call_count(url, 3) is True
|
344 | 348 |
|
345 | 349 |
|
| 350 | +@responses.activate |
| 351 | +def test_list_request_pagination_warning(gl): |
| 352 | + url = "http://localhost/api/v4/projects" |
| 353 | + responses.add( |
| 354 | + method=responses.GET, |
| 355 | + url=url, |
| 356 | + json=[ |
| 357 | + {"name": "project01"}, |
| 358 | + {"name": "project02"}, |
| 359 | + {"name": "project03"}, |
| 360 | + {"name": "project04"}, |
| 361 | + {"name": "project05"}, |
| 362 | + {"name": "project06"}, |
| 363 | + {"name": "project07"}, |
| 364 | + {"name": "project08"}, |
| 365 | + {"name": "project09"}, |
| 366 | + {"name": "project10"}, |
| 367 | + {"name": "project11"}, |
| 368 | + {"name": "project12"}, |
| 369 | + {"name": "project13"}, |
| 370 | + {"name": "project14"}, |
| 371 | + {"name": "project15"}, |
| 372 | + {"name": "project16"}, |
| 373 | + {"name": "project17"}, |
| 374 | + {"name": "project18"}, |
| 375 | + {"name": "project19"}, |
| 376 | + {"name": "project20"}, |
| 377 | + ], |
| 378 | + headers={"X-Total": "30", "x-per-page": "20"}, |
| 379 | + status=200, |
| 380 | + match=MATCH_EMPTY_QUERY_PARAMS, |
| 381 | + ) |
| 382 | + |
| 383 | + with warnings.catch_warnings(record=True) as caught_warnings: |
| 384 | + result = gl.http_list("/projects", as_list=True) |
| 385 | + assert len(caught_warnings) == 1 |
| 386 | + warning = caught_warnings[0] |
| 387 | + assert isinstance(warning.message, UserWarning) |
| 388 | + message = str(caught_warnings[0].message) |
| 389 | + assert "Calling" in message |
| 390 | + assert "return a maximum of" in message |
| 391 | + assert "readthedocs" in message |
| 392 | + |
| 393 | + assert isinstance(result, list) |
| 394 | + assert len(result) == 20 |
| 395 | + |
| 396 | + with warnings.catch_warnings(record=True) as caught_warnings: |
| 397 | + result = gl.http_list("/projects", as_list=False) |
| 398 | + assert len(caught_warnings) == 0 |
| 399 | + assert isinstance(result, GitlabList) |
| 400 | + assert len(list(result)) == 20 |
| 401 | + |
| 402 | + with warnings.catch_warnings(record=True) as caught_warnings: |
| 403 | + result = gl.http_list("/projects", all=True) |
| 404 | + assert len(caught_warnings) == 0 |
| 405 | + |
| 406 | + assert isinstance(result, list) |
| 407 | + assert len(result) == 20 |
| 408 | + assert responses.assert_call_count(url, 3) is True |
| 409 | + |
| 410 | + |
346 | 411 | @responses.activate
|
347 | 412 | def test_list_request_404(gl
3CC0
):
|
348 | 413 | url = "http://localhost/api/v4/not_there"
|
|
0 commit comments