From ea95fa13734fcd7abd4fa3f4307d18152f1a4e82 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Wed, 15 Feb 2012 16:35:28 -0800 Subject: [PATCH] album art fetcher tests --- beets/autotag/match.py | 4 +-- test/test_importer.py | 79 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/beets/autotag/match.py b/beets/autotag/match.py index ecf9bebd9..db4e92338 100644 --- a/beets/autotag/match.py +++ b/beets/autotag/match.py @@ -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. """ diff --git a/test/test_importer.py b/test/test_importer.py index 1622e20e3..12a3460bd 100644 --- a/test/test_importer.py +++ b/test/test_importer.py @@ -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__)