|
| 1 | +from unittest.mock import patch |
| 2 | + |
1 | 3 | import pytest
|
2 | 4 |
|
3 | 5 | import easypost
|
@@ -28,3 +30,73 @@ def test_beta_user_get_next_page(prod_api_key, page_size):
|
28 | 30 | except easypost.Error as e:
|
29 | 31 | if e.message != "There are no more pages to retrieve.":
|
30 | 32 | raise easypost.Error(message="Test failed intentionally.")
|
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.vcr() # Cassette not needed due to mocking, but used to avoid making real bogus API calls |
| 36 | +def test_beta_user_get_next_page_collect_all(prod_api_key): |
| 37 | + page_size = 1 # Doesn't matter what this is, we're mocking the response |
| 38 | + all_children = [] |
| 39 | + |
| 40 | + first_page_response = { |
| 41 | + "children": [ |
| 42 | + { |
| 43 | + "id": "user_123", |
| 44 | + } |
| 45 | + ], |
| 46 | + "has_more": True, |
| 47 | + } |
| 48 | + |
| 49 | + # Mock the initial "get all children" call |
| 50 | + return_value = (first_page_response, prod_api_key) |
| 51 | + with patch("easypost.requestor.Requestor.request", return_value=return_value): |
| 52 | + first_page = easypost.beta.User.all_children(page_size=page_size) |
| 53 | + all_children += first_page["children"] |
| 54 | + previous_page = first_page |
| 55 | + |
| 56 | + second_page_response = { |
| 57 | + "children": [ |
| 58 | + { |
| 59 | + "id": "user_456", |
| 60 | + } |
| 61 | + ], |
| 62 | + "has_more": True, |
| 63 | + } |
| 64 | + |
| 65 | + # Mock the first "get next page" call with more to collect after |
| 66 | + # (current page "has_more" = True, next page "has_more" = True) |
| 67 | + return_value = (second_page_response, prod_api_key) |
| 68 | + with patch("easypost.requestor.Requestor.request", return_value=return_value): |
| 69 | + next_page = easypost.beta.User.get_next_page_of_children( |
| 70 | + children=previous_page, page_size=page_size # type: ignore |
| 71 | + ) |
| 72 | + all_children += next_page["children"] |
| 73 | + previous_page = next_page |
| 74 | + |
| 75 | + third_page_response = { |
| 76 | + "children": [ |
| 77 | + { |
| 78 | + "id": "user_789", |
| 79 | + } |
| 80 | + ], |
| 81 | + "has_more": False, |
| 82 | + } |
| 83 | + |
| 84 | + # Mock the second "get next page" call with no more to collect |
| 85 | + # (current page "has_more" = True, next page "has_more" = False) |
| 86 | + return_value = (third_page_response, prod_api_key) |
| 87 | + with patch("easypost.requestor.Requestor.request", return_value=return_value): |
| 88 | + next_page = easypost.beta.User.get_next_page_of_children( |
| 89 | + children=previous_page, page_size=page_size # type: ignore |
| 90 | + ) |
| 91 | + all_children += next_page["children"] |
| 92 | + previous_page = next_page |
| 93 | + |
| 94 | + # Verify we have all children (from both the "get all children" and "get next page" calls) |
| 95 | + # Ensures that no guard clauses inside the "get next page" method are preventing us from collecting all children |
| 96 | + assert len(all_children) == 3 |
| 97 | + |
| 98 | + # Now that the previous page has "has_more" = False, it should throw an error before even making the API call |
| 99 | + with pytest.raises(easypost.Error) as error: |
| 100 | + _ = easypost.beta.User.get_next_page_of_children(children=previous_page, page_size=page_size) # type: ignore |
| 101 | + |
| 102 | + assert str(error.value) == "There are no more pages to retrieve." |
0 commit comments