Skip to content

Commit

Permalink
Break filtering test_tags into separate feeds/entries tests. #328
Browse files Browse the repository at this point in the history
  • Loading branch information
lemon24 committed Dec 5, 2023
1 parent c541546 commit dd3d18c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 90 deletions.
10 changes: 9 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,15 @@ def chunk_size(request):
return request.param


@pytest.fixture(params=reader_methods.get_entries_methods)
@pytest.fixture(
params=[
reader_methods.get_entries_recent,
slow(reader_methods.get_entries_random),
reader_methods.search_entries_relevant,
slow(reader_methods.search_entries_recent),
slow(reader_methods.search_entries_random),
]
)
def get_entries(request):
yield request.param

Expand Down
10 changes: 0 additions & 10 deletions tests/reader_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,6 @@ def search_entry_counts(reader, **kwargs):
obj.counts = search_entry_counts


get_entries_methods = [
# defaults not included
get_entries_recent,
get_entries_random,
search_entries_relevant,
search_entries_recent,
search_entries_random,
]


class _update_feed_methods:
# TODO: we can remove the update_feeds() variant if we add a test to confirm update_feeds is a thin wrapper over update_feeds_iter

Expand Down
152 changes: 73 additions & 79 deletions tests/test_reader_filter.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,50 @@
import pytest
from fakeparser import Parser
from reader_methods import get_entries
from reader_methods import get_entries_random
from reader_methods import get_entries_recent
from reader_methods import get_feeds
from reader_methods import search_entries
from reader_methods import search_entries_random
from reader_methods import search_entries_recent
from reader_methods import search_entries_relevant
from utils import rename_argument
from utils import utc_datetime as datetime


with_call_feed_tags_method = pytest.mark.parametrize(
# tags_arg_name is exposed so we can later test get_entries(tags=...)
'call_method, tags_arg_name',
[
(get_entries_recent, 'feed_tags'),
pytest.param(get_entries_random, 'feed_tags', marks=pytest.mark.slow),
(search_entries_relevant, 'feed_tags'),
pytest.param(search_entries_recent, 'feed_tags', marks=pytest.mark.slow),
pytest.param(search_entries_random, 'feed_tags', marks=pytest.mark.slow),
# TODO: maybe test all the get_feeds sort orders
(get_feeds, 'tags'),
],
)


ALL_IDS = {(1, 1), (1, 2), (2, 1), (3, 1)}


@with_call_feed_tags_method
@pytest.mark.parametrize(
'args, expected',
[
((), ALL_IDS),
((None,), ALL_IDS),
(([],), ALL_IDS),
(([[]],), ALL_IDS),
((True,), ALL_IDS - {(3, 1)}),
(([True],), ALL_IDS - {(3, 1)}),
((False,), {(3, 1)}),
(([False],), {(3, 1)}),
(([True, False],), set()),
(([[True, False]],), ALL_IDS),
((['tag'],), ALL_IDS - {(3, 1)}),
(([['tag']],), ALL_IDS - {(3, 1)}),
((['tag', 'tag'],), ALL_IDS - {(3, 1)}),
(([['tag'], ['tag']],), ALL_IDS - {(3, 1)}),
(([['tag', 'tag']],), ALL_IDS - {(3, 1)}),
((['-tag'],), {(3, 1)}),
((['unknown'],), set()),
((['-unknown'],), ALL_IDS),
((['first'],), {(1, 1), (1, 2)}),
((['second'],), {(2, 1)}),
((['first', 'second'],), set()),
(([['first'], ['second']],), set()),
(([['first', 'second']],), {(1, 1), (1, 2), (2, 1)}),
((['first', 'tag'],), {(1, 1), (1, 2)}),
((['second', 'tag'],), {(2, 1)}),
(([['first', 'second'], 'tag'],), {(1, 1), (1, 2), (2, 1)}),
(([['first'], ['tag']],), {(1, 1), (1, 2)}),
(([['first', 'tag']],), {(1, 1), (1, 2), (2, 1)}),
((['-first', 'tag'],), {(2, 1)}),
(([['first', '-tag']],), ALL_IDS - {(2, 1)}),
(([[False, 'first']],), {(1, 1), (1, 2), (3, 1)}),
(([True, '-first'],), {(2, 1)}),
],
)
def test_tags(reader, call_method, tags_arg_name, args, expected):
TAGS_AND_EXPECTED_IDS = [
((), ALL_IDS),
((None,), ALL_IDS),
(([],), ALL_IDS),
(([[]],), ALL_IDS),
((True,), ALL_IDS - {(3, 1)}),
(([True],), ALL_IDS - {(3, 1)}),
((False,), {(3, 1)}),
(([False],), {(3, 1)}),
(([True, False],), set()),
(([[True, False]],), ALL_IDS),
((['tag'],), ALL_IDS - {(3, 1)}),
(([['tag']],), ALL_IDS - {(3, 1)}),
((['tag', 'tag'],), ALL_IDS - {(3, 1)}),
(([['tag'], ['tag']],), ALL_IDS - {(3, 1)}),
(([['tag', 'tag']],), ALL_IDS - {(3, 1)}),
((['-tag'],), {(3, 1)}),
((['unknown'],), set()),
((['-unknown'],), ALL_IDS),
((['first'],), {(1, 1), (1, 2)}),
((['second'],), {(2, 1)}),
((['first', 'second'],), set()),
(([['first'], ['second']],), set()),
(([['first', 'second']],), {(1, 1), (1, 2), (2, 1)}),
((['first', 'tag'],), {(1, 1), (1, 2)}),
((['second', 'tag'],), {(2, 1)}),
(([['first', 'second'], 'tag'],), {(1, 1), (1, 2), (2, 1)}),
(([['first'], ['tag']],), {(1, 1), (1, 2)}),
(([['first', 'tag']],), {(1, 1), (1, 2), (2, 1)}),
((['-first', 'tag'],), {(2, 1)}),
(([['first', '-tag']],), ALL_IDS - {(2, 1)}),
(([[False, 'first']],), {(1, 1), (1, 2), (3, 1)}),
(([True, '-first'],), {(2, 1)}),
]


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

one = parser.feed(1, datetime(2010, 1, 1)) # tag, first
Expand All @@ -84,28 +61,45 @@ def test_tags(reader, call_method, tags_arg_name, args, expected):
reader.add_feed(feed)

reader.update_feeds()
call_method.after_update(reader)
return reader


@pytest.fixture
@rename_argument('reader', 'reader_for_tags')
def reader_feed_tags(reader):
reader.set_tag('1', 'tag')
reader.set_tag('1', 'first')
reader.set_tag('2', 'tag')
reader.set_tag('2', 'second')
return reader


@pytest.mark.parametrize('args, expected', TAGS_AND_EXPECTED_IDS)
@rename_argument('reader', 'reader_feed_tags')
def test_entries_by_feed_tags(reader, get_entries, args, expected):
get_entries.after_update(reader)

assert len(args) <= 1
kwargs = {'feed_tags': a for a in args}

actual = {tuple(map(eval, o.resource_id)) for o in get_entries(reader, **kwargs)}
assert actual == expected, args


# TODO: maybe test all the get_feeds sort orders (maybe fixture?)

reader.set_tag(one, 'tag')
reader.set_tag(one, 'first')
reader.set_tag(two, 'tag')
reader.set_tag(two, 'second')

if '_entries' in call_method.__name__:
resource_id_length = 2
elif '_feeds' in call_method.__name__:
resource_id_length = 1
else:
assert False, call_method
@pytest.mark.parametrize('get_feeds', [get_feeds])
@pytest.mark.parametrize('args, expected', TAGS_AND_EXPECTED_IDS)
@rename_argument('reader', 'reader_feed_tags')
def test_feeds_by_tags(reader, get_feeds, args, expected):
get_feeds.after_update(reader)

assert len(args) <= 1
kwargs = {tags_arg_name: a for a in args}
kwargs = {'tags': a for a in args}

actual_set = {
tuple(map(eval, o.resource_id)) for o in call_method(reader, **kwargs)
}
expected_set = {t[:resource_id_length] for t in expected}
assert actual_set == expected_set, kwargs
actual = {eval(o.resource_id[0]) for o in get_feeds(reader, **kwargs)}
assert actual == {t[0] for t in expected}, args


def test_entry_tags_basic(reader):
Expand Down

0 comments on commit dd3d18c

Please sign in to comment.