artresizer: fix image comparison test

Since the ImageMagick based comparison is now abstracted via ArtResizer,
it becomes a little more involved to mock it for testing.
This commit is contained in:
wisp3rwind 2022-02-14 22:52:00 +01:00
parent 959e24e463
commit 1815d08392
2 changed files with 29 additions and 5 deletions

View file

@ -121,7 +121,13 @@ def resize_image(log, imagepath, maxwidth, quality):
return imagepath
def check_art_similarity(log, item, imagepath, compare_threshold):
def check_art_similarity(
log,
item,
imagepath,
compare_threshold,
artresizer=None,
):
"""A boolean indicating if an image is similar to embedded item art.
If no embedded art exists, always return `True`. If the comparison fails
@ -135,7 +141,10 @@ def check_art_similarity(log, item, imagepath, compare_threshold):
if not art:
return True
return ArtResizer.shared.compare(art, imagepath, compare_threshold)
if artresizer is None:
artresizer = ArtResizer.shared
return artresizer.compare(art, imagepath, compare_threshold)
def extract(log, outpath, item):

View file

@ -24,7 +24,7 @@ from test.helper import TestHelper
from mediafile import MediaFile
from beets import config, logging, ui
from beets.util import syspath, displayable_path
from beets.util import artresizer, syspath, displayable_path
from beets.util.artresizer import ArtResizer
from beets import art
@ -216,16 +216,31 @@ class EmbedartCliTest(_common.TestCase, TestHelper):
self.assertEqual(mediafile.images[0].data, self.image_data)
class DummyArtResizer(ArtResizer):
"""An `ArtResizer` which pretends that ImageMagick is available, and has
a sufficiently recent version to support image comparison.
"""
@staticmethod
def _check_method():
return artresizer.IMAGEMAGICK, (7, 0, 0), True
@patch('beets.util.artresizer.subprocess')
@patch('beets.art.extract')
class ArtSimilarityTest(unittest.TestCase):
def setUp(self):
self.item = _common.item()
self.log = logging.getLogger('beets.embedart')
self.artresizer = DummyArtResizer()
def _similarity(self, threshold):
return art.check_art_similarity(self.log, self.item, b'path',
threshold)
return art.check_art_similarity(
self.log,
self.item,
b'path',
threshold,
artresizer=self.artresizer,
)
def _popen(self, status=0, stdout="", stderr=""):
"""Create a mock `Popen` object."""