Skip to content

Commit

Permalink
Deduplicate on getAlbumList
Browse files Browse the repository at this point in the history
Fixes #199
  • Loading branch information
spl0k committed Nov 7, 2020
1 parent 194bf5e commit 7d18251
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
4 changes: 2 additions & 2 deletions supysonic/api/albums_songs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]
),
)
Expand All @@ -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:
Expand Down
51 changes: 32 additions & 19 deletions tests/api/test_album_songs.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,26 @@ def setUp(self):
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,
Expand All @@ -51,24 +64,24 @@ def test_get_album_list(self):
"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()
Expand Down Expand Up @@ -106,7 +119,7 @@ def test_get_album_list2(self):
)

with db_session:
Track.get().delete()
Track.select().delete()
Album.get().delete()
rv, child = self._make_request(
"getAlbumList2", {"type": "random"}, tag="albumList2"
Expand Down
4 changes: 1 addition & 3 deletions tests/testbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand All @@ -108,4 +107,3 @@ def request_context(self, *args, **kwargs):
def tearDown(self):
release_database()
shutil.rmtree(self.__dir)
os.remove(self.__dbfile)

0 comments on commit 7d18251

Please sign in to comment.