Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Web Client custom iterator #521

Merged
merged 4 commits into from
Oct 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions slack/web/slack_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def __init__(
self.data = data
self.headers = headers
self.status_code = status_code
self._initial_data = data
self._client = client
self._logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -101,6 +102,7 @@ def __iter__(self):
(SlackResponse) self
"""
self._iteration = 0
self.data = self._initial_data
return self

def __next__(self):
Expand Down
41 changes: 41 additions & 0 deletions tests/web/test_web_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,47 @@ def test_requests_can_be_paginated(self, mock_request):
users = users + page["members"]
self.assertTrue(len(users) == 4)

def test_response_can_be_paginated_multiple_times(self, mock_request):
# This test suite verifies the changes in #521 work as expected
page1 = {
"data": {
"ok": True,
"channels": [{"id": "C1"}],
"response_metadata": {"next_cursor": "has_page2"},
},
"status_code": 200,
"headers": {},
}
page2 = {
"data": {
"ok": True,
"channels": [{"id": "C2"}],
"response_metadata": {"next_cursor": "has_page3"},
},
"status_code": 200,
"headers": {},
}
page3 = {
"data": {"ok": True, "channels": [{"id": "C3"}]},
"status_code": 200,
"headers": {},
}
# The initial pagination
mock_request.response.side_effect = [page1, page2, page3]
response = self.client.channels_list(limit=1)
ids = []
for page in response:
ids.append(page["channels"][0]["id"])
self.assertEqual(ids, ["C1", "C2", "C3"])

# The second iteration starting with page 2
# (page1 is already cached in `response`)
mock_request.response.side_effect = [page2, page3]
ids = []
for page in response:
ids.append(page["channels"][0]["id"])
self.assertEqual(ids, ["C1", "C2", "C3"])

def test_request_pagination_stops_when_next_cursor_is_missing(self, mock_request):
mock_request.response.side_effect = [
{
Expand Down