Add test case to check getCoverArt's functionality

Mock two functions `getCoverArt`, `get_image_front` and use
a locally available cover art to check if the created cover
art exists.

Problems:
- How to check image's quality.
- Not sure if only this check is enough (do we need to check the
embedding part?).

Signed-off-by: ABCbum <kimlong221002@gmail.com>
This commit is contained in:
ABCbum
2020-01-04 01:07:25 +07:00
committed by JoeLametta
parent 8181cacca5
commit e2942b07e3
3 changed files with 57 additions and 0 deletions

View File

@@ -497,6 +497,10 @@ Log files will log the path to tracks relative to this directory.
continue
_ripIfNotRipped(i + 1)
# NOTE: Seems like some kind of with … or try: … finally: … clause
# would be more appropriate, since otherwise this would potentially
# leave stray files lying around in case of crashes etc.
# <Freso 2020-01-03, GitHub comment>
if (self.options.fetch_cover_art == "embed" and
self.coverArtPath is not None):
logger.debug('deleting cover art file at: %r', self.coverArtPath)

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

View File

@@ -2,8 +2,10 @@
# vi:si:et:sw=4:sts=4:ts=4
import os
import unittest
from tempfile import NamedTemporaryFile
from whipper.common import program, mbngs, config
from whipper.command.cd import DEFAULT_DISC_TEMPLATE
@@ -38,3 +40,54 @@ class PathTestCase(unittest.TestCase):
path = prog.getPath('/tmp', '%A/%d', 'mbdiscid', md, 0)
self.assertEqual(path,
'/tmp/Jeff Buckley/Grace')
# TODO: Test cover art embedding too.
class CoverArtTestCase(unittest.TestCase):
@staticmethod
def _mock_get_front_image(release_id):
"""
Mock `musicbrainzngs.get_front_image` function.
Reads a local cover art image and returns its binary data.
:param release_id: a release id (self.program.metadata.mbid)
:type release_id: str
:returns: the binary content of the local cover art image
:rtype: bytes
"""
filename = '%s.jpg' % release_id
path = os.path.join(os.path.dirname(__file__), filename)
with open(path, 'rb') as f:
return f.read()
def _mock_getCoverArt(self, path, release_id):
"""
Mock `common.program.getCoverArt` function.
:param path: where to store the fetched image
:type path: str
:param release_id: a release id (self.program.metadata.mbid)
:type release_id: str
:returns: path to the downloaded cover art
:rtype: str
"""
cover_art_path = os.path.join(path, 'cover.jpg')
data = self._mock_get_front_image(release_id)
with NamedTemporaryFile(suffix='.cover.jpg', delete=False) as f:
f.write(data)
os.chmod(f.name, 0o644)
os.replace(f.name, cover_art_path)
return cover_art_path
def testCoverArtPath(self):
"""Test whether a fetched cover art is saved properly."""
# Using: Dummy by Portishead
# https://musicbrainz.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd
path = os.path.dirname(__file__)
release_id = "76df3287-6cda-33eb-8e9a-044b5e15ffdd"
coverArtPath = self._mock_getCoverArt(path, release_id)
self.assertTrue(os.path.isfile(coverArtPath))