|
| 1 | +import copy |
| 2 | +import warnings |
| 3 | + |
1 | 4 | import pytest |
2 | 5 | import requests |
3 | 6 | import responses |
@@ -425,20 +428,115 @@ def test_list_request(gl): |
425 | 428 | match=MATCH_EMPTY_QUERY_PARAMS, |
426 | 429 | ) |
427 | 430 |
|
428 | | - result = gl.http_list("/projects", as_list=True) |
| 431 | + with warnings.catch_warnings(record=True) as caught_warnings: |
| 432 | + result = gl.http_list("/projects", as_list=True) |
| 433 | + assert len(caught_warnings) == 0 |
429 | 434 | assert isinstance(result, list) |
430 | 435 | assert len(result) == 1 |
431 | 436 |
|
432 | 437 | result = gl.http_list("/projects", as_list=False) |
433 | 438 | assert isinstance(result, GitlabList) |
434 | | - assert len(result) == 1 |
| 439 | + assert len(list(result)) == 1 |
435 | 440 |
|
436 | 441 | result = gl.http_list("/projects", all=True) |
437 | 442 | assert isinstance(result, list) |
438 | 443 | assert len(result) == 1 |
439 | 444 | assert responses.assert_call_count(url, 3) is True |
440 | 445 |
|
441 | 446 |
|
| 447 | +large_list_response = { |
| 448 | + "method": responses.GET, |
| 449 | + "url": "http://localhost/api/v4/projects", |
| 450 | + "json": [ |
| 451 | + {"name": "project01"}, |
| 452 | + {"name": "project02"}, |
| 453 | + {"name": "project03"}, |
| 454 | + {"name": "project04"}, |
| 455 | + {"name": "project05"}, |
| 456 | + {"name": "project06"}, |
| 457 | + {"name": "project07"}, |
| 458 | + {"name": "project08"}, |
| 459 | + {"name": "project09"}, |
| 460 | + {"name": "project10"}, |
| 461 | + {"name": "project11"}, |
| 462 | + {"name": "project12"}, |
| 463 | + {"name": "project13"}, |
| 464 | + {"name": "project14"}, |
| 465 | + {"name": "project15"}, |
| 466 | + {"name": "project16"}, |
| 467 | + {"name": "project17"}, |
| 468 | + {"name": "project18"}, |
| 469 | + {"name": "project19"}, |
| 470 | + {"name": "project20"}, |
| 471 | + ], |
| 472 | + "headers": {"X-Total": "30", "x-per-page": "20"}, |
| 473 | + "status": 200, |
| 474 | + "match": MATCH_EMPTY_QUERY_PARAMS, |
| 475 | +} |
| 476 | + |
| 477 | + |
| 478 | +@responses.activate |
| 479 | +def test_list_request_pagination_warning(gl): |
| 480 | + responses.add(**large_list_response) |
| 481 | + |
| 482 | + with warnings.catch_warnings(record=True) as caught_warnings: |
| 483 | + result = gl.http_list("/projects", as_list=True) |
| 484 | + assert len(caught_warnings) == 1 |
| 485 | + warning = caught_warnings[0] |
| 486 | + assert isinstance(warning.message, UserWarning) |
| 487 | + message = str(warning.message) |
| 488 | + assert "Calling a `list()` method" in message |
| 489 | + assert "python-gitlab.readthedocs.io" in message |
| 490 | + assert __file__ == warning.filename |
| 491 | + assert isinstance(result, list) |
| 492 | + assert len(result) == 20 |
| 493 | + assert len(responses.calls) == 1 |
| 494 | + |
| 495 | + |
| 496 | +@responses.activate |
| 497 | +def test_list_request_as_list_false_nowarning(gl): |
| 498 | + responses.add(**large_list_response) |
| 499 | + with warnings.catch_warnings(record=True) as caught_warnings: |
| 500 | + result = gl.http_list("/projects", as_list=False) |
| 501 | + assert len(caught_warnings) == 0 |
| 502 | + assert isinstance(result, GitlabList) |
| 503 | + assert len(list(result)) == 20 |
| 504 | + assert len(responses.calls) == 1 |
| 505 | + |
| 506 | + |
| 507 | +@responses.activate |
| 508 | +def test_list_request_all_true_nowarning(gl): |
| 509 | + responses.add(**large_list_response) |
| 510 | + with warnings.catch_warnings(record=True) as caught_warnings: |
| 511 | + result = gl.http_list("/projects", all=True) |
| 512 | + assert len(caught_warnings) == 0 |
| 513 | + assert isinstance(result, list) |
| 514 | + assert len(result) == 20 |
| 515 | + assert len(responses.calls) == 1 |
| 516 | + |
| 517 | + |
| 518 | +@responses.activate |
| 519 | +def test_list_request_all_false_nowarning(gl): |
| 520 | + responses.add(**large_list_response) |
| 521 | + with warnings.catch_warnings(record=True) as caught_warnings: |
| 522 | + result = gl.http_list("/projects", all=False) |
| 523 | + assert len(caught_warnings) == 0 |
| 524 | + assert isinstance(result, list) |
| 525 | + assert len(result) == 20 |
| 526 | + assert len(responses.calls) == 1 |
| 527 | + |
| 528 | + |
| 529 | +@responses.activate |
| 530 | +def test_list_request_page_nowarning(gl): |
| 531 | + response_dict = copy.deepcopy(large_list_response) |
| 532 | + response_dict["match"] = [responses.matchers.query_param_matcher({"page": "1"})] |
| 533 | + responses.add(**response_dict) |
| 534 | + with warnings.catch_warnings(record=True) as caught_warnings: |
| 535 | + gl.http_list("/projects", page=1) |
| 536 | + assert len(caught_warnings) == 0 |
| 537 | + assert len(responses.calls) == 1 |
| 538 | + |
| 539 | + |
442 | 540 | @responses.activate |
443 | 541 | def test_list_request_404(gl): |
444 | 542 | url = "http://localhost/api/v4/not_there" |
|
0 commit comments