album art fetcher tests

This commit is contained in:
Adrian Sampson 2012-02-15 16:35:28 -08:00
parent d86a3fe9b7
commit ea95fa1373
2 changed files with 81 additions and 2 deletions

View file

@ -347,8 +347,8 @@ def recommendation(results):
return rec
def validate_candidate(items, tuple_dict, info):
"""Given a candidate info dict, attempt to add the candidate to
the output dictionary of result tuples. This involves checking
"""Given a candidate AlbumInfo object, attempt to add the candidate
to the output dictionary of result tuples. This involves checking
the track count, ordering the items, checking for duplicates, and
calculating the distance.
"""

View file

@ -23,6 +23,7 @@ from beets import library
from beets import importer
from beets import mediafile
from beets.autotag import AlbumInfo, TrackInfo
from beets.autotag import art
TEST_TITLES = ('The Opener', 'The Second Track', 'The Last Track')
class NonAutotaggedImportTest(unittest.TestCase):
@ -706,6 +707,84 @@ class DuplicateCheckTest(unittest.TestCase):
self._item_task(False, existing=True))
self.assertFalse(res)
class ArtFetchTest(unittest.TestCase, _common.ExtraAsserts):
def setUp(self):
# Mock the album art fetcher to always return our test file.
self.art_file = os.path.join(_common.RSRC, 'tmpcover.jpg')
_common.touch(self.art_file)
self.old_afa = art.art_for_album
art.art_for_album = lambda a, b: self.art_file
# Test library.
self.libpath = os.path.join(_common.RSRC, 'tmplib.blb')
self.libdir = os.path.join(_common.RSRC, 'tmplib')
os.mkdir(self.libdir)
os.mkdir(os.path.join(self.libdir, 'album'))
itempath = os.path.join(self.libdir, 'album', 'test.mp3')
shutil.copyfile(os.path.join(_common.RSRC, 'full.mp3'), itempath)
self.lib = library.Library(self.libpath)
self.i = _common.item()
self.i.path = itempath
self.album = self.lib.add_album([self.i])
self.lib.save()
# Set up an art-fetching coroutine.
config = _common.iconfig(self.lib)
config.art = True
self.coro = importer.fetch_art(config)
self.coro.next()
# Import task for the coroutine.
self.task = importer.ImportTask(None, None, [self.i])
self.task.is_album = True
self.task.album_id = self.album.id
info = AlbumInfo(
album = 'some album',
album_id = 'albumid',
artist = 'some artist',
artist_id = 'artistid',
tracks = [TrackInfo('one', 'trackid', 'some artist',
'artistid', 1)],
)
self.task.set_choice((info, [self.i]))
def tearDown(self):
art.art_for_album = self.old_afa
if os.path.exists(self.art_file):
os.remove(self.art_file)
if os.path.exists(self.libpath):
os.remove(self.libpath)
if os.path.exists(self.libdir):
shutil.rmtree(self.libdir)
def _fetch_art(self, should_exist):
"""Execute the fetch_art coroutine for the task and return the
album's resulting artpath. ``should_exist`` specifies whether to
assert that art path was set (to the correct value) or or that
the path was not set.
"""
self.coro.send(self.task)
artpath = self.lib.albums()[0].artpath
if should_exist:
self.assertEqual(artpath,
os.path.join(os.path.dirname(self.i.path), 'cover.jpg'))
self.assertExists(artpath)
else:
self.assertEqual(artpath, None)
return artpath
def test_fetch_art(self):
assert not self.lib.albums()[0].artpath
self._fetch_art(True)
def test_art_not_found(self):
art.art_for_album = lambda a, b: None
self._fetch_art(False)
def test_no_art_for_singleton(self):
self.task.is_album = False
self._fetch_art(False)
def suite():
return unittest.TestLoader().loadTestsFromName(__name__)