From 2c127a03f5ebe0366157ffefd495013fb4209139 Mon Sep 17 00:00:00 2001 From: Roland Date: Tue, 16 Apr 2024 15:48:09 -0600 Subject: [PATCH] Testing channel suggestions being ordered by video count. --- modules/videos/channel/lib.py | 4 ++-- wrolpi/root_api.py | 4 ++-- wrolpi/schema.py | 1 + wrolpi/test/test_root_api.py | 10 ++++++---- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/modules/videos/channel/lib.py b/modules/videos/channel/lib.py index 4e7e3b6e..26fb2590 100644 --- a/modules/videos/channel/lib.py +++ b/modules/videos/channel/lib.py @@ -212,9 +212,9 @@ async def search_channels_by_name(name: str, limit: int = 5, session: Session = Channel.name.ilike(f'%{name}%'), Channel.name.ilike(f'%{name_no_spaces}%'), )) \ - .join(Video, Video.channel_id == Channel.id) \ + .outerjoin(Video, Video.channel_id == Channel.id) \ .group_by(Channel.id, Channel.name) \ - .order_by(desc('video_count')) \ + .order_by(desc('video_count'), asc(Channel.name)) \ .limit(limit) channels = [i[0] for i in stmt] else: diff --git a/wrolpi/root_api.py b/wrolpi/root_api.py index 77a3ac30..37c315ea 100644 --- a/wrolpi/root_api.py +++ b/wrolpi/root_api.py @@ -538,13 +538,13 @@ async def post_shutdown(_: Request): @api_bp.post('/search_suggestions') @validate(json=schema.SearchSuggestionsRequest) -async def post_search_suggestions(_: Request, body: schema.SearchFileEstimateRequest): +async def post_search_suggestions(_: Request, body: schema.SearchSuggestionsRequest): """Used by the Global search to suggest related Channels/Domains/etc. to the user.""" from modules.videos.channel.lib import search_channels_by_name from modules.archive.lib import search_domains_by_name channels, domains = await asyncio.gather( - search_channels_by_name(body.search_str, order_by_video_count=True), + search_channels_by_name(body.search_str, order_by_video_count=body.order_by_video_count), search_domains_by_name(body.search_str), ) diff --git a/wrolpi/schema.py b/wrolpi/schema.py index 14a4b731..7d19583b 100644 --- a/wrolpi/schema.py +++ b/wrolpi/schema.py @@ -145,6 +145,7 @@ class VINDecoderResponse: @dataclass class SearchSuggestionsRequest: search_str: Optional[str] = None + order_by_video_count: bool = True @dataclass diff --git a/wrolpi/test/test_root_api.py b/wrolpi/test/test_root_api.py index 6f3bb1c4..2805c6cd 100644 --- a/wrolpi/test/test_root_api.py +++ b/wrolpi/test/test_root_api.py @@ -468,13 +468,14 @@ async def test_post_vin_number_decoder(test_async_client): @pytest.mark.asyncio -async def test_search_suggestions(test_session, test_async_client, channel_factory, archive_factory): +async def test_search_suggestions(test_session, test_async_client, channel_factory, archive_factory, video_factory): # WARNING results are cached, this test uses unique queries to avoid conflicts. channel_factory(name='Foo') channel_factory(name='Fool') channel_factory(name='Bar') archive_factory(domain='foo.com') archive_factory(domain='bar.com') + video_factory(channel_id=2) # Channel "Fool" will be first in results because it has the most videos. test_session.commit() async def assert_results(body: dict, expected_channels=None, expected_domains=None): @@ -489,11 +490,12 @@ async def test_search_suggestions(test_session, test_async_client, channel_facto await assert_results( dict(search_str='foo'), - [{'directory': 'Foo', 'id': 1, 'name': 'Foo', 'url': 'https://example.com/Foo'}, - {'directory': 'Fool', + [{'directory': 'Fool', 'id': 2, 'name': 'Fool', - 'url': 'https://example.com/Fool'}], + 'url': 'https://example.com/Fool'}, + {'directory': 'Foo', 'id': 1, 'name': 'Foo', 'url': 'https://example.com/Foo'}, + ], [{'directory': 'archive/foo.com', 'domain': 'foo.com', 'id': 1}], )