From dd3d18cf9d25f611965e36f8525867aa4db1224f Mon Sep 17 00:00:00 2001 From: lemon24 Date: Tue, 5 Dec 2023 23:59:35 +0200 Subject: [PATCH] Break filtering test_tags into separate feeds/entries tests. #328 --- tests/conftest.py | 10 ++- tests/reader_methods.py | 10 --- tests/test_reader_filter.py | 152 +++++++++++++++++------------------- 3 files changed, 82 insertions(+), 90 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 6398bc35..7bb76d54 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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 diff --git a/tests/reader_methods.py b/tests/reader_methods.py index 8d2bdf46..28378e91 100644 --- a/tests/reader_methods.py +++ b/tests/reader_methods.py @@ -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 diff --git a/tests/test_reader_filter.py b/tests/test_reader_filter.py index 04d29d8c..a63bd741 100644 --- a/tests/test_reader_filter.py +++ b/tests/test_reader_filter.py @@ -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 @@ -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):