Testing channel suggestions being ordered by video count.

This commit is contained in:
Roland 2024-04-16 15:48:09 -06:00
parent 4564a99771
commit 2c127a03f5
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
4 changed files with 11 additions and 8 deletions

View file

@ -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:

View file

@ -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),
)

View file

@ -145,6 +145,7 @@ class VINDecoderResponse:
@dataclass
class SearchSuggestionsRequest:
search_str: Optional[str] = None
order_by_video_count: bool = True
@dataclass

View file

@ -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}],
)