Skip to content

Commit 9f6d506

Browse files
committed
Replace _assertImageIsValidArt
1 parent 72caf0d commit 9f6d506

File tree

1 file changed

+31
-47
lines changed

1 file changed

+31
-47
lines changed

test/plugins/test_art.py

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -863,60 +863,41 @@ def test_fetch_art_if_imported_file_deleted(self):
863863
assert self.album.art_filepath.exists()
864864

865865

866+
IMAGE_PATH = os.path.join(_common.RSRC, b"abbey-similar.jpg")
867+
IMAGE_SIZE = os.stat(util.syspath(IMAGE_PATH)).st_size
868+
869+
870+
def fs_source_get(_self, album, settings, paths):
871+
if paths:
872+
yield fetchart.Candidate(logger, source_name=_self.ID, path=IMAGE_PATH)
873+
874+
875+
@patch("beetsplug.fetchart.FileSystem.get", fs_source_get)
866876
class ArtForAlbumTest(UseThePlugin):
867877
"""Tests that fetchart.art_for_album respects the scale & filesize
868878
configurations (e.g., minwidth, enforce_ratio, max_filesize)
869879
"""
870880

871881
IMG_225x225 = os.path.join(_common.RSRC, b"abbey.jpg")
872882
IMG_348x348 = os.path.join(_common.RSRC, b"abbey-different.jpg")
873-
IMG_500x490 = os.path.join(_common.RSRC, b"abbey-similar.jpg")
874883

875884
IMG_225x225_SIZE = os.stat(util.syspath(IMG_225x225)).st_size
876-
IMG_348x348_SIZE = os.stat(util.syspath(IMG_348x348)).st_size
877885

878886
RESIZE_OP = "resize"
879887
DEINTERLACE_OP = "deinterlace"
880888
REFORMAT_OP = "reformat"
881889

882-
def setUp(self):
883-
super().setUp()
890+
album = _common.Bag()
884891

885-
self.old_fs_source_get = fetchart.FileSystem.get
886-
887-
def fs_source_get(_self, album, settings, paths):
888-
if paths:
889-
yield fetchart.Candidate(
890-
logger, source_name=_self.ID, path=self.image_file
891-
)
892-
893-
fetchart.FileSystem.get = fs_source_get
894-
895-
self.album = _common.Bag()
896-
897-
def tearDown(self):
898-
fetchart.FileSystem.get = self.old_fs_source_get
899-
super().tearDown()
900-
901-
def assertImageIsValidArt(self, image_file, should_exist):
902-
assert Path(os.fsdecode(image_file)).exists()
903-
self.image_file = image_file
904-
905-
candidate = self.plugin.art_for_album(self.album, [""], True)
906-
907-
if should_exist:
908-
assert candidate is not None
909-
assert candidate.path == self.image_file
910-
assert Path(os.fsdecode(candidate.path)).exists()
911-
else:
912-
assert candidate is None
892+
def get_album_art(self):
893+
return self.plugin.art_for_album(self.album, [""], True)
913894

914895
def _assert_image_operated(self, image_file, operation, should_operate):
915896
self.image_file = image_file
916897
with patch.object(
917898
ArtResizer.shared, operation, return_value=self.image_file
918899
) as mock_operation:
919-
self.plugin.art_for_album(self.album, [""], True)
900+
self.get_album_art()
920901
assert mock_operation.called == should_operate
921902

922903
def _require_backend(self):
@@ -929,48 +910,51 @@ def _require_backend(self):
929910
def test_respect_minwidth(self):
930911
self._require_backend()
931912
self.plugin.minwidth = 300
932-
self.assertImageIsValidArt(self.IMG_225x225, False)
933-
self.assertImageIsValidArt(self.IMG_348x348, True)
913+
assert self.get_album_art()
914+
915+
def test_respect_minwidth_no(self):
916+
self._require_backend()
917+
self.plugin.minwidth = 600
918+
assert not self.get_album_art()
934919

935920
def test_respect_enforce_ratio_yes(self):
936921
self._require_backend()
937922
self.plugin.enforce_ratio = True
938-
self.assertImageIsValidArt(self.IMG_500x490, False)
939-
self.assertImageIsValidArt(self.IMG_225x225, True)
923+
assert not self.get_album_art()
940924

941925
def test_respect_enforce_ratio_no(self):
942926
self.plugin.enforce_ratio = False
943-
self.assertImageIsValidArt(self.IMG_500x490, True)
927+
assert self.get_album_art()
944928

945929
def test_respect_enforce_ratio_px_above(self):
946930
self._require_backend()
947931
self.plugin.enforce_ratio = True
948932
self.plugin.margin_px = 5
949-
self.assertImageIsValidArt(self.IMG_500x490, False)
933+
assert not self.get_album_art()
950934

951935
def test_respect_enforce_ratio_px_below(self):
952936
self._require_backend()
953937
self.plugin.enforce_ratio = True
954938
self.plugin.margin_px = 15
955-
self.assertImageIsValidArt(self.IMG_500x490, True)
939+
assert self.get_album_art()
956940

957941
def test_respect_enforce_ratio_percent_above(self):
958942
self._require_backend()
959943
self.plugin.enforce_ratio = True
960944
self.plugin.margin_percent = (500 - 490) / 500 * 0.5
961-
self.assertImageIsValidArt(self.IMG_500x490, False)
945+
assert not self.get_album_art()
962946

963947
def test_respect_enforce_ratio_percent_below(self):
964948
self._require_backend()
965949
self.plugin.enforce_ratio = True
966950
self.plugin.margin_percent = (500 - 490) / 500 * 1.5
967-
self.assertImageIsValidArt(self.IMG_500x490, True)
951+
assert self.get_album_art()
968952

969953
def test_resize_if_necessary(self):
970954
self._require_backend()
971955
self.plugin.maxwidth = 300
972-
self._assert_image_operated(self.IMG_225x225, self.RESIZE_OP, False)
973-
self._assert_image_operated(self.IMG_348x348, self.RESIZE_OP, True)
956+
assert self.get_album_art()
957+
self._assert_image_operated(IMAGE_PATH, self.RESIZE_OP, True)
974958

975959
def test_fileresize(self):
976960
self._require_backend()
@@ -979,9 +963,9 @@ def test_fileresize(self):
979963

980964
def test_fileresize_if_necessary(self):
981965
self._require_backend()
982-
self.plugin.max_filesize = self.IMG_225x225_SIZE
983-
self._assert_image_operated(self.IMG_225x225, self.RESIZE_OP, False)
984-
self.assertImageIsValidArt(self.IMG_225x225, True)
966+
self.plugin.max_filesize = IMAGE_SIZE
967+
self._assert_image_operated(IMAGE_PATH, self.RESIZE_OP, False)
968+
assert self.get_album_art()
985969

986970
def test_fileresize_no_scale(self):
987971
self._require_backend()

0 commit comments

Comments
 (0)