-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathtest_base_service.py
More file actions
70 lines (57 loc) · 2.67 KB
/
test_base_service.py
File metadata and controls
70 lines (57 loc) · 2.67 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
from unittest.mock import patch
import pytest
from easypost.constant import NO_MORE_PAGES_ERROR
from easypost.errors import EndOfPaginationError
@pytest.mark.vcr() # Cassette not needed due to mocking, but used to avoid making real bogus API calls
def test_get_next_page_collects_all_pages(test_client):
page_size = 1 # Doesn't matter what this is, we're mocking the response
all_results = []
# Using scanforms as an example, but this should work for any service since it's a base class method
first_page_response = {
"scan_forms": [
{
"id": "sf_123",
}
],
"has_more": True,
}
# Mock the initial "get all scanforms" call
with patch("easypost.requestor.Requestor.request", return_value=first_page_response):
first_page = test_client.scan_form.all(page_size=page_size)
all_results += first_page["scan_forms"]
previous_page = first_page
second_page_response = {
"scan_forms": [
{
"id": "sf_456",
}
],
"has_more": True,
}
# Mock the first "get next page" call with more to collect after
# (current page "has_more" = True, next page "has_more" = True)
with patch("easypost.requestor.Requestor.request", return_value=second_page_response):
next_page = test_client.scan_form.get_next_page(scan_forms=previous_page, page_size=page_size) # type: ignore
all_results += next_page["scan_forms"]
previous_page = next_page
third_page_response = {
"scan_forms": [
{
"id": "sf_789",
}
],
"has_more": False,
}
# Mock the second "get next page" call with no more to collect
# (current page "has_more" = True, next page "has_more" = False)
with patch("easypost.requestor.Requestor.request", return_value=third_page_response):
next_page = test_client.scan_form.get_next_page(scan_forms=previous_page, page_size=page_size) # type: ignore
all_results += next_page["scan_forms"]
previous_page = next_page
# Verify we have all scan_forms (from the initial "get all scanforms" and two subsequent "get next page" calls)
# Ensures that no guard clauses inside the "get next page" method are preventing us from collecting all scanforms
assert len(all_results) == 3
# Now that the previous page has "has_more" = False, it should throw an error before even making the API call
with pytest.raises(EndOfPaginationError) as error:
_ = test_client.scan_form.get_next_page(scan_forms=previous_page, page_size=page_size) # type: ignore
assert error.value.message == NO_MORE_PAGES_ERROR