Skip to content

Commit

Permalink
Fix pagination of empty response for PaginationType.NONE
Browse files Browse the repository at this point in the history
  • Loading branch information
dtatarkin committed Nov 5, 2024
1 parent a8ee1a0 commit 55706d7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 4 additions & 1 deletion alpaca/common/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,10 @@ def _return_paginated_result(
"""
if handle_pagination == PaginationType.NONE:
# user wants no pagination, so just do a single page
return next(iterator)
try:
return next(iterator)
except StopIteration:
return []
elif handle_pagination == PaginationType.FULL:
# the iterator returns "pages", so we use chain to flatten them all into 1 list
return list(chain.from_iterable(iterator))
Expand Down
21 changes: 20 additions & 1 deletion tests/broker/broker_client/test_rebalancing_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
GetSubscriptionsRequest,
UpdatePortfolioRequest,
)
from alpaca.common.enums import BaseURL
from alpaca.common.enums import BaseURL, PaginationType


def test_create_portfolio(reqmock: Mocker, client: BrokerClient) -> None:
Expand Down Expand Up @@ -403,6 +403,25 @@ def test_get_all_subscriptions(reqmock: Mocker, client: BrokerClient) -> None:
assert isinstance(response[0], Subscription)


def test_get_all_subscriptions_empty_no_pagination(
reqmock: Mocker, client: BrokerClient
) -> None:
"""Test the get_all_subscriptions method."""
reqmock.get(
f"{BaseURL.BROKER_SANDBOX.value}/v1/rebalancing/subscriptions",
text="""{
"subscriptions": [],
"next_page_token": null
}""",
)
response = client.get_all_subscriptions(
filter=GetSubscriptionsRequest(), handle_pagination=PaginationType.NONE
)

assert reqmock.called_once
assert len(response) == 0


def test_get_subscription_by_id(reqmock: Mocker, client: BrokerClient) -> None:
"""Test the get_subscription_by_id method."""
sub_id = UUID("9341be15-8786-4d23-ba1a-fc10ef4f90f4")
Expand Down

0 comments on commit 55706d7

Please sign in to comment.