From 88997d5984e6b3e55f5da7156c4cbbaa021c7997 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alban=20F=C3=A9ron?= Date: Sat, 1 Jun 2024 18:02:32 +0200 Subject: [PATCH] Support for empty search3 queries We already did (at least with SQLite), make sure it's the case (ie: test it) and prevent useless WHERE when querying the database --- supysonic/api/search.py | 11 ++++++++--- tests/api/test_search.py | 6 ++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/supysonic/api/search.py b/supysonic/api/search.py index cf2ff5b..dedb544 100644 --- a/supysonic/api/search.py +++ b/supysonic/api/search.py @@ -200,9 +200,14 @@ def search_id3(): song_offset = int(song_offset) if song_offset else 0 root = get_root_folder(mfid) - artists = Artist.select().where(Artist.name.contains(query)) - albums = Album.select().where(Album.name.contains(query)) - songs = Track.select().where(Track.title.contains(query)) + artists = Artist.select() + albums = Album.select() + songs = Track.select() + + if query: + artists = artists.where(Artist.name.contains(query)) + albums = albums.where(Album.name.contains(query)) + songs = songs.where(Track.title.contains(query)) if root is not None: artists = artists.join(Track).where(Track.root_folder == root) diff --git a/tests/api/test_search.py b/tests/api/test_search.py index 6c9167a..7a1aab8 100644 --- a/tests/api/test_search.py +++ b/tests/api/test_search.py @@ -420,6 +420,12 @@ class SearchTestCase(ApiTestBase): ) self.assertEqual(len(self._xpath(child, "./song")), 0) + # empty query + rv, child = self._make_request("search3", {"query": ""}, tag="searchResult3") + self.assertEqual(len(child), 27) # 3 + 3*2 + 3*2*3 + self.assertEqual(len(self._xpath(child, "./artist")), 3) + self.assertEqual(len(self._xpath(child, "./album")), 6) + self.assertEqual(len(self._xpath(child, "./song")), 18) if __name__ == "__main__": unittest.main()