fixes for album artist inference

- Inference must be enabled explicitly with the "infer_aa" flag. It does not
  happen transparently.
- Infer both artist and artist ID.
- Fixed a bug where only the database row was using the inferred data, not the
  returned data structure.
- Added tests.
This commit is contained in:
Adrian Sampson 2011-02-18 12:17:22 -08:00
parent ce5f9f9936
commit 0d869340ec
2 changed files with 34 additions and 6 deletions

View file

@ -1080,18 +1080,25 @@ class Library(BaseLibrary):
if record:
return Album(self, dict(record))
def add_album(self, items):
def add_album(self, items, infer_aa=False):
"""Create a new album in the database with metadata derived
from its items. The items are added to the database if they
don't yet have an ID. Returns an Album object.
don't yet have an ID. Returns an Album object. If the
infer_aa flag is set, then the album artist field will be
guessed from artist fields when not present.
"""
# Set the metadata from the first item.
#fixme: check for consensus?
item_values = dict(
(key, getattr(items[0], key)) for key in ALBUM_KEYS_ITEM)
if not item_values['albumartist']:
item_values['albumartist'] = getattr(items[0], 'artist')
if infer_aa:
namemap = {
'albumartist': 'artist',
'mb_albumartistid': 'mb_artistid',
}
for field, itemfield in namemap.iteritems():
if not item_values[field]:
item_values[field] = getattr(items[0], itemfield)
sql = 'INSERT INTO albums (%s) VALUES (%s)' % \
(', '.join(ALBUM_KEYS_ITEM),
@ -1104,7 +1111,7 @@ class Library(BaseLibrary):
record = {}
for key in ALBUM_KEYS:
if key in ALBUM_KEYS_ITEM:
record[key] = getattr(items[0], key)
record[key] = item_values[key]
else:
# Non-item fields default to None.
record[key] = None

View file

@ -445,6 +445,27 @@ class AlbumInfoTest(unittest.TestCase):
self.assertEqual(ai.album, self.i.album)
self.assertEqual(ai.year, self.i.year)
def test_infer_aa_gets_artist_and_id(self):
i = item()
i.albumartist = ''
i.mb_albumartistid = ''
ai = self.lib.add_album((i,), infer_aa=True)
self.assertEqual(ai.albumartist, i.artist)
self.assertEqual(ai.mb_albumartistid, i.mb_artistid)
def test_no_infer_aa_sets_blank_artist_and_id(self):
i = item()
i.albumartist = ''
i.mb_albumartistid = ''
ai = self.lib.add_album((i,), infer_aa=False)
self.assertEqual(ai.albumartist, '')
self.assertEqual(ai.mb_albumartistid, '')
def test_infer_aa_lets_album_values_override(self):
ai = self.lib.add_album((self.i,), infer_aa=True)
self.assertEqual(ai.albumartist, self.i.albumartist)
self.assertEqual(ai.mb_albumartistid, self.i.mb_albumartistid)
def test_albuminfo_stores_art(self):
ai = self.lib.get_album(self.i)
ai.artpath = '/my/great/art'