From d81c669fc639184a61c7f9b514d179addbbddf17 Mon Sep 17 00:00:00 2001 From: kaliko Date: Fri, 4 Jun 2021 16:22:09 +0200 Subject: [PATCH] simadb: More consistent use of albumartist info in fetch_albums_history fetch_albums_history() returns a list of Album objects with an Artist attribute set to albumartist (if provided, else falls back to artist). We want an Album view of history. fetch_albums_history(needle) returns a list of Album objects with Artist attribute set to artist (in this case we want album played for artist == needle). Here we want the artist view on album history. --- sima/lib/simadb.py | 21 +++++++++++++++------ tests/test_simadb.py | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/sima/lib/simadb.py b/sima/lib/simadb.py index a966cf5..d08a6cc 100644 --- a/sima/lib/simadb.py +++ b/sima/lib/simadb.py @@ -476,22 +476,31 @@ class SimaDB: SELECT albums.name AS name, albums.mbid as mbid, artists.name as artist, - artists.mbid as artist_mbib + artists.mbid as artist_mbib, + albumartists.name as albumartist, + albumartists.mbid as albumartist_mbib FROM history JOIN tracks ON history.track = tracks.id LEFT OUTER JOIN albums ON tracks.album = albums.id LEFT OUTER JOIN artists ON tracks.artist = artists.id + LEFT OUTER JOIN albumartists ON tracks.albumartist = albumartists.id WHERE history.last_play > ? AND albums.name NOT NULL AND artists.name NOT NULL ORDER BY history.last_play DESC""", (date.isoformat(' '),)) hist = list() for row in rows: vals = dict(row) - artist = Artist(name=vals.pop('artist'), - mbid=vals.pop('artist_mbib')) - if needle: - if needle != artist: + if needle: # Here use artist instead of albumartist + if needle != Artist(name=vals.get('artist'), + mbid=vals.get('artist_mbib')): continue - album = Album(**vals, artist=artist) + # Use albumartist / MBIDs if possible to build album artist + if not vals.get('albumartist'): + vals['albumartist'] = vals.get('artist') + if not vals.get('albumartist_mbib'): + vals['albumartist_mbib'] = vals.get('artist_mbib') + artist = Artist(name=vals.get('albumartist'), + mbid=vals.pop('albumartist_mbib')) + album = Album(**vals, Artist=artist) if hist and hist[-1] == album: # remove consecutive dupes continue diff --git a/tests/test_simadb.py b/tests/test_simadb.py index c3e400d..42e897d 100644 --- a/tests/test_simadb.py +++ b/tests/test_simadb.py @@ -75,7 +75,7 @@ class Test_00DB(Main): def test_02_history(self): # set records in the past to ease purging then - last = CURRENT - datetime.timedelta(hours=1) + last = CURRENT - datetime.timedelta(seconds=5) trk = Track(**DEVOLT) self.db.add_history(trk, date=last) self.db.add_history(trk, date=last) @@ -168,6 +168,40 @@ class Test_00DB(Main): trk04.artist, trk03.artist], art_history) + def test_albums_history(self): + # set records in the past to ease purging then + last = CURRENT - datetime.timedelta(seconds=5) + track = { + 'album': 'album', 'title': 'title', + 'albumartist': 'albumartist', + 'artist': 'artist', + 'file': 'foo/bar.flac', + 'musicbrainz_albumartistid': 'd8e7e3e2-49ab-4f7c-b148-fc946d521f99', + 'musicbrainz_albumid': 'ea2ef2cf-59e1-443a-817e-9066e3e0be4b', + 'musicbrainz_artistid': 'd8e7e3e2-49ab-4f7c-b148-fc946d521f99',} + self.db.purge_history(duration=0) + self.db.add_history(Track(**track), last) + alb_history = self.db.fetch_albums_history() + alb = alb_history[0] + self.assertEqual(alb.Artist.name, track.get('albumartist')) + # Fetching Album history for "artist" should return "album" + artist = Artist(track.get('artist'), mbid=track.get('musicbrainz_artistid')) + alb_history = self.db.fetch_albums_history(artist) + self.assertTrue(alb_history) + # Falls back to album and MBID when albumartist and + # musicbrainz_albumartistid are not set + self.db.purge_history(duration=0) + track = { + 'album': 'no albumartist set', 'title': 'title', + 'artist': 'no album artist set', + 'file': 'bar/foo.flac', + 'musicbrainz_artistid': 'dddddddd-49ab-4f7c-b148-dddddddddddd',} + self.db.add_history(Track(**track), last) + alb_history = self.db.fetch_albums_history() + album = alb_history[0] + self.assertEqual(album.albumartist, track['artist']) + self.assertEqual(album.Artist.mbid, track['musicbrainz_artistid']) + def test_04_filtering_history(self): # Controls artist history filtering for i in range(0, 5): @@ -244,6 +278,8 @@ class Test_00DB(Main): self.assertIn(('fooart',), artists) conn.close() + def test_06_add_album(self): + pass class Test_01BlockList(Main): -- 2.39.2