Skip to content

Commit

Permalink
Make using starting_after with sort='random' an error.
Browse files Browse the repository at this point in the history
For #196.
  • Loading branch information
lemon24 committed Dec 9, 2020
1 parent d4669fc commit 9f64cf1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
12 changes: 8 additions & 4 deletions src/reader/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ def get_entries(
``'random'``
Random. At at most 256 entries will be returned.
Random order (shuffled). At at most 256 entries will be returned.
.. versionadded:: 1.2
Expand All @@ -755,6 +755,7 @@ def get_entries(
by default, all entries are returned.
starting_after (tuple(str, str) or Entry or None):
Return entries after this entry; a cursor for use in pagination.
Using ``starting_after`` with ``sort='random'`` is not supported.
Yields:
:class:`Entry`: Sorted according to ``sort``.
Expand Down Expand Up @@ -788,7 +789,8 @@ def get_entries(
if not isinstance(limit, numbers.Integral) or limit < 1:
raise ValueError("limit should be a positive integer")

# TODO: raise is starting_after is given for sort=random?
if starting_after and sort == 'random':
raise ValueError("using starting_after with sort='random' not supported")

now = self._now()
return self._storage.get_entries(
Expand Down Expand Up @@ -1105,7 +1107,7 @@ def search_entries(
``'random'``
Random. At at most 256 entries will be returned.
Random order (shuffled). At at most 256 entries will be returned.
.. versionadded:: 1.10
Expand Down Expand Up @@ -1154,6 +1156,7 @@ def search_entries(
by default, all results are returned.
starting_after (tuple(str, str) or EntrySearchResult or None):
Return results after this result; a cursor for use in pagination.
Using ``starting_after`` with ``sort='random'`` is not supported.
Yields:
:class:`EntrySearchResult`: Sorted according to ``sort``.
Expand Down Expand Up @@ -1186,7 +1189,8 @@ def search_entries(
if not isinstance(limit, numbers.Integral) or limit < 1:
raise ValueError("limit should be a positive integer")

# TODO: raise is starting_after is given for sort=random?
if starting_after and sort == 'random':
raise ValueError("using starting_after with sort='random' not supported")

now = self._now()
return self._search.search_entries(
Expand Down
51 changes: 46 additions & 5 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2421,6 +2421,8 @@ def get_feeds_added(reader, **kwargs):
return reader.get_feeds(sort='added', **kwargs)


# TODO: sort could be a separate argument here...

with_call_paginated_method = pytest.mark.parametrize(
'pre_stuff, call_method',
[
Expand All @@ -2433,11 +2435,8 @@ def get_feeds_added(reader, **kwargs):
)


@with_call_paginated_method
@pytest.mark.parametrize('chunk_size', [Storage.chunk_size, 1, 2])
def test_get_entries_pagination_basic(reader, pre_stuff, call_method, chunk_size):
reader._storage.chunk_size = chunk_size

@pytest.fixture
def reader_with_three_feeds(reader):
reader._parser = parser = Parser()

one = parser.feed(1, datetime(2010, 1, 1))
Expand All @@ -2449,6 +2448,16 @@ def test_get_entries_pagination_basic(reader, pre_stuff, call_method, chunk_size

for feed in one, two, three:
reader.add_feed(feed)

return reader


@with_call_paginated_method
@pytest.mark.parametrize('chunk_size', [Storage.chunk_size, 1, 2])
@rename_argument('reader', 'reader_with_three_feeds')
def test_get_entries_pagination_basic(reader, pre_stuff, call_method, chunk_size):
reader._storage.chunk_size = chunk_size

reader.update_feeds()
pre_stuff(reader)

Expand All @@ -2471,6 +2480,38 @@ def get_ids(**kwargs):
assert get_ids(limit=2, starting_after=ids[2]) == ids[3:] == []


@pytest.mark.parametrize(
'pre_stuff, call_method',
[
(lambda _: None, get_entries_random),
(enable_and_update_search, search_entries_random),
],
)
@pytest.mark.parametrize('chunk_size', [Storage.chunk_size, 1, 2])
@rename_argument('reader', 'reader_with_three_feeds')
def test_get_entries_pagination_random(reader, pre_stuff, call_method, chunk_size):
reader._storage.chunk_size = chunk_size

reader.update_feeds()
pre_stuff(reader)

def get_ids(**kwargs):
return [o.object_id for o in call_method(reader, **kwargs)]

# TODO: we could just call_method() if sort was not hardcoded
ids = [o.object_id for o in reader.get_entries()]

assert len(get_ids(limit=1)) == min(1, chunk_size, len(ids))
assert len(get_ids(limit=2)) == min(2, chunk_size, len(ids))
assert len(get_ids(limit=3)) == min(3, chunk_size, len(ids))

with pytest.raises(ValueError):
get_ids(starting_after=ids[0])

with pytest.raises(ValueError):
get_ids(limit=1, starting_after=ids[0])


NOT_FOUND_ERROR_CLS = {
get_feeds_title: FeedNotFoundError,
get_feeds_added: FeedNotFoundError,
Expand Down

0 comments on commit 9f64cf1

Please sign in to comment.