From 066658f167c02c20a2403d02306511b86bf4aed3 Mon Sep 17 00:00:00 2001 From: Taizo Simpson Date: Fri, 12 Oct 2018 19:07:48 -0400 Subject: [PATCH] Consider embeded art when serializing albums, add relevant test --- supysonic/db.py | 4 ++++ tests/base/test_db.py | 17 ++++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/supysonic/db.py b/supysonic/db.py index 969f6cf..6c488b6 100644 --- a/supysonic/db.py +++ b/supysonic/db.py @@ -190,6 +190,10 @@ class Album(db.Entity): track_with_cover = self.tracks.select(lambda t: t.folder.cover_art is not None).first() if track_with_cover is not None: info['coverArt'] = str(track_with_cover.folder.id) + else: + track_with_cover = self.tracks.select(lambda t: t.has_art).first() + if track_with_cover is not None: + info['coverArt'] = str(track_with_cover.id) try: starred = StarredAlbum[user.id, self.id] diff --git a/tests/base/test_db.py b/tests/base/test_db.py index c3bc600..e99c7a5 100644 --- a/tests/base/test_db.py +++ b/tests/base/test_db.py @@ -97,9 +97,9 @@ class DbTestCase(unittest.TestCase): return track1, track2 - def create_track_in(self, folder, root): - artist = db.Artist(name = 'Snazzy Artist') - album = db.Album(artist = artist, name = 'Rockin\' Album') + def create_track_in(self, folder, root, artist = None, album = None, has_art = True): + artist = artist or db.Artist(name = 'Snazzy Artist') + album = album or db.Album(artist = artist, name = 'Rockin\' Album') return db.Track( title = 'Nifty Number', album = album, @@ -107,7 +107,7 @@ class DbTestCase(unittest.TestCase): disc = 1, number = 1, duration = 5, - has_art = True, + has_art = has_art, bitrate = 96, path = 'tests/assets/formats/silence.flac', content_type = 'audio/flac', @@ -234,7 +234,8 @@ class DbTestCase(unittest.TestCase): # No tracks, shouldn't be stored under normal circumstances self.assertRaises(ValueError, album.as_subsonic_album, user) - self.create_some_tracks(artist, album) + root_folder, folder_art, folder_noart = self.create_some_folders() + track1 = self.create_track_in(root_folder, folder_noart, artist = artist, album = album) album_dict = album.as_subsonic_album(user) self.assertIsInstance(album_dict, dict) @@ -246,11 +247,13 @@ class DbTestCase(unittest.TestCase): self.assertIn('duration', album_dict) self.assertIn('created', album_dict) self.assertIn('starred', album_dict) + self.assertIn('coverArt', album_dict) self.assertEqual(album_dict['name'], album.name) self.assertEqual(album_dict['artist'], artist.name) self.assertEqual(album_dict['artistId'], str(artist.id)) - self.assertEqual(album_dict['songCount'], 2) - self.assertEqual(album_dict['duration'], 8) + self.assertEqual(album_dict['songCount'], 1) + self.assertEqual(album_dict['duration'], 5) + self.assertEqual(album_dict['coverArt'], str(track1.id)) self.assertRegex(album_dict['created'], date_regex) self.assertRegex(album_dict['starred'], date_regex)