From 7d1825151e2ee9bc6667971d9d9b8f49145bc14c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alban=20F=C3=A9ron?= Date: Sat, 7 Nov 2020 15:44:09 +0100 Subject: [PATCH] Deduplicate on getAlbumList Fixes #199 --- supysonic/api/albums_songs.py | 4 +-- tests/api/test_album_songs.py | 51 ++++++++++++++++++++++------------- tests/testbase.py | 4 +-- 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/supysonic/api/albums_songs.py b/supysonic/api/albums_songs.py index 719998c..774988d 100644 --- a/supysonic/api/albums_songs.py +++ b/supysonic/api/albums_songs.py @@ -85,7 +85,7 @@ def album_list(): dict( album=[ a.as_subsonic_child(request.user) - for a in query.without_distinct().random(size) + for a in query.distinct().random(size) ] ), ) @@ -106,7 +106,7 @@ def album_list(): if s.user.id == request.user.id and count(s.starred.tracks) > 0 ) elif ltype == "alphabeticalByName": - query = query.order_by(Folder.name) + query = query.order_by(Folder.name).distinct() elif ltype == "alphabeticalByArtist": query = query.order_by(lambda f: f.parent.name + f.name) else: diff --git a/tests/api/test_album_songs.py b/tests/api/test_album_songs.py index 3d86c81..257834a 100644 --- a/tests/api/test_album_songs.py +++ b/tests/api/test_album_songs.py @@ -29,13 +29,26 @@ class AlbumSongsTestCase(ApiTestBase): artist = Artist(name="Artist") album = Album(name="Album", artist=artist) - track = Track( - title="Track", + Track( + title="Track 1", album=album, artist=artist, disc=1, number=1, - path="tests/assets/empty", + path="tests/assets/folder/1", + folder=folder, + root_folder=folder, + duration=2, + bitrate=320, + last_modification=0, + ) + Track( + title="Track 2", + album=album, + artist=artist, + disc=1, + number=1, + path="tests/assets/folder/2", folder=folder, root_folder=folder, duration=2, @@ -51,24 +64,24 @@ class AlbumSongsTestCase(ApiTestBase): "getAlbumList", {"type": "newest", "offset": "minus one"}, error=0 ) - types = [ - "random", - "newest", - "highest", - "frequent", - "recent", - "alphabeticalByName", - "alphabeticalByArtist", - "starred", + types_and_count = [ + ("random", 1), + ("newest", 1), + ("highest", 1), + ("frequent", 1), + ("recent", 0), # never played + ("alphabeticalByName", 1), + ( + "alphabeticalByArtist", + 0, # somehow expected due to funky "album" definition on this endpoint + ), + ("starred", 0), # nothing's starred ] - for t in types: - self._make_request( + for t, c in types_and_count: + rv, child = self._make_request( "getAlbumList", {"type": t}, tag="albumList", skip_post=True ) - - rv, child = self._make_request( - "getAlbumList", {"type": "random"}, tag="albumList", skip_post=True - ) + self.assertEqual(len(child), c) with db_session: Folder.get().delete() @@ -106,7 +119,7 @@ class AlbumSongsTestCase(ApiTestBase): ) with db_session: - Track.get().delete() + Track.select().delete() Album.get().delete() rv, child = self._make_request( "getAlbumList2", {"type": "random"}, tag="albumList2" diff --git a/tests/testbase.py b/tests/testbase.py index 9acbc37..bae3b6a 100644 --- a/tests/testbase.py +++ b/tests/testbase.py @@ -82,10 +82,9 @@ class TestBase(unittest.TestCase): __with_api__ = False def setUp(self): - self.__dbfile = tempfile.mkstemp()[1] self.__dir = tempfile.mkdtemp() config = TestConfig(self.__with_webui__, self.__with_api__) - config.BASE["database_uri"] = "sqlite:///" + self.__dbfile + config.BASE["database_uri"] = "sqlite:" config.WEBAPP["cache_dir"] = self.__dir init_database(config.BASE["database_uri"]) @@ -108,4 +107,3 @@ class TestBase(unittest.TestCase): def tearDown(self): release_database() shutil.rmtree(self.__dir) - os.remove(self.__dbfile)