From 2681c83c5b23f9f4f2bb4941013b07a88dc33e6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0ar=C5=ABnas=20Nejus?= Date: Wed, 16 Oct 2024 06:37:53 +0100 Subject: [PATCH] Remove redundant generate_album_info and generate_track_info functions These functions were used to generate mock data for tests but have been replaced with direct instantiation of AlbumInfo and TrackInfo objects. This change simplifies the test code and removes unnecessary helper functions. --- beets/test/helper.py | 79 --------------- test/plugins/test_mbsync.py | 190 +++++++++--------------------------- 2 files changed, 45 insertions(+), 224 deletions(-) diff --git a/beets/test/helper.py b/beets/test/helper.py index 19f7299ed..322921fb5 100644 --- a/beets/test/helper.py +++ b/beets/test/helper.py @@ -20,9 +20,6 @@ information or mock the environment. - `has_program` checks the presence of a command on the system. -- The `generate_album_info` and `generate_track_info` functions return - fixtures to be used when mocking the autotagger. - - The `ImportSessionFixture` allows one to run importer code while controlling the interactions through code. @@ -803,82 +800,6 @@ class TerminalImportMixin(ImportHelper): ) -def generate_album_info(album_id, track_values): - """Return `AlbumInfo` populated with mock data. - - Sets the album info's `album_id` field is set to the corresponding - argument. For each pair (`id`, `values`) in `track_values` the `TrackInfo` - from `generate_track_info` is added to the album info's `tracks` field. - Most other fields of the album and track info are set to "album - info" and "track info", respectively. - """ - tracks = [generate_track_info(id, values) for id, values in track_values] - album = AlbumInfo( - album_id="album info", - album="album info", - artist="album info", - artist_id="album info", - tracks=tracks, - ) - for field in ALBUM_INFO_FIELDS: - setattr(album, field, "album info") - - return album - - -ALBUM_INFO_FIELDS = [ - "album", - "album_id", - "artist", - "artist_id", - "asin", - "albumtype", - "va", - "label", - "barcode", - "artist_sort", - "releasegroup_id", - "catalognum", - "language", - "country", - "albumstatus", - "media", - "albumdisambig", - "releasegroupdisambig", - "artist_credit", - "data_source", - "data_url", -] - - -def generate_track_info(track_id="track info", values={}): - """Return `TrackInfo` populated with mock data. - - The `track_id` field is set to the corresponding argument. All other - string fields are set to "track info". - """ - track = TrackInfo( - title="track info", - track_id=track_id, - ) - for field in TRACK_INFO_FIELDS: - setattr(track, field, "track info") - for field, value in values.items(): - setattr(track, field, value) - return track - - -TRACK_INFO_FIELDS = [ - "artist", - "artist_id", - "artist_sort", - "disctitle", - "artist_credit", - "data_source", - "data_url", -] - - class AutotagStub: """Stub out MusicBrainz album and track matcher and control what the autotagger returns. diff --git a/test/plugins/test_mbsync.py b/test/plugins/test_mbsync.py index 6cfa6704e..f65df4256 100644 --- a/test/plugins/test_mbsync.py +++ b/test/plugins/test_mbsync.py @@ -12,17 +12,11 @@ # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. - from unittest.mock import patch -from beets import config +from beets.autotag.hooks import AlbumInfo, TrackInfo from beets.library import Item -from beets.test.helper import ( - PluginTestCase, - capture_log, - generate_album_info, - generate_track_info, -) +from beets.test.helper import PluginTestCase, capture_log class MbsyncCliTest(PluginTestCase): @@ -31,28 +25,28 @@ class MbsyncCliTest(PluginTestCase): @patch("beets.autotag.mb.album_for_id") @patch("beets.autotag.mb.track_for_id") def test_update_library(self, track_for_id, album_for_id): - album_for_id.return_value = generate_album_info( - "album id", [("track id", {"release_track_id": "release track id"})] - ) - track_for_id.return_value = generate_track_info( - "singleton track id", {"title": "singleton info"} - ) - album_item = Item( - album="old title", + album="old album", mb_albumid="81ae60d4-5b75-38df-903a-db2cfa51c2c6", - mb_trackid="old track id", - mb_releasetrackid="release track id", - path="", + mb_trackid="track id", ) - album = self.lib.add_album([album_item]) + self.lib.add_album([album_item]) - item = Item( - title="old title", - mb_trackid="b8c2cf90-83f9-3b5f-8ccd-31fb866fcf37", - path="", + singleton = Item( + title="old title", mb_trackid="b8c2cf90-83f9-3b5f-8ccd-31fb866fcf37" + ) + self.lib.add(singleton) + + album_for_id.return_value = AlbumInfo( + album_id="album id", + album="new album", + tracks=[ + TrackInfo(track_id=album_item.mb_trackid, title="new title") + ], + ) + track_for_id.return_value = TrackInfo( + track_id=singleton.mb_trackid, title="new title" ) - self.lib.add(item) with capture_log() as logs: self.run_command("mbsync") @@ -60,130 +54,36 @@ class MbsyncCliTest(PluginTestCase): assert "Sending event: albuminfo_received" in logs assert "Sending event: trackinfo_received" in logs - item.load() - assert item.title == "singleton info" + singleton.load() + assert singleton.title == "new title" album_item.load() - assert album_item.title == "track info" + assert album_item.title == "new title" assert album_item.mb_trackid == "track id" + assert album_item.get_album().album == "new album" - album.load() - assert album.album == "album info" + def test_custom_format(self): + for item in [ + Item(artist="albumartist", album="no id"), + Item( + artist="albumartist", + album="invalid id", + mb_albumid="a1b2c3d4", + ), + ]: + self.lib.add_album([item]) - def test_message_when_skipping(self): - config["format_item"] = "$artist - $album - $title" - config["format_album"] = "$albumartist - $album" + for item in [ + Item(artist="artist", title="no id"), + Item(artist="artist", title="invalid id", mb_trackid="a1b2c3d4"), + ]: + self.lib.add(item) - # Test album with no mb_albumid. - # The default format for an album include $albumartist so - # set that here, too. - album_invalid = Item( - albumartist="album info", album="album info", path="" - ) - self.lib.add_album([album_invalid]) - - # default format with capture_log("beets.mbsync") as logs: - self.run_command("mbsync") - e = ( - "mbsync: Skipping album with no mb_albumid: " - + "album info - album info" - ) - assert e == logs[0] - - # custom format - with capture_log("beets.mbsync") as logs: - self.run_command("mbsync", "-f", "'$album'") - e = "mbsync: Skipping album with no mb_albumid: 'album info'" - assert e == logs[0] - - # restore the config - config["format_item"] = "$artist - $album - $title" - config["format_album"] = "$albumartist - $album" - - # Test singleton with no mb_trackid. - # The default singleton format includes $artist and $album - # so we need to stub them here - item_invalid = Item( - artist="album info", - album="album info", - title="old title", - path="", - ) - self.lib.add(item_invalid) - - # default format - with capture_log("beets.mbsync") as logs: - self.run_command("mbsync") - e = ( - "mbsync: Skipping singleton with no mb_trackid: " - + "album info - album info - old title" - ) - assert e == logs[0] - - # custom format - with capture_log("beets.mbsync") as logs: - self.run_command("mbsync", "-f", "'$title'") - e = "mbsync: Skipping singleton with no mb_trackid: 'old title'" - assert e == logs[0] - - def test_message_when_invalid(self): - config["format_item"] = "$artist - $album - $title" - config["format_album"] = "$albumartist - $album" - - # Test album with invalid mb_albumid. - # The default format for an album include $albumartist so - # set that here, too. - album_invalid = Item( - albumartist="album info", - album="album info", - mb_albumid="a1b2c3d4", - path="", - ) - self.lib.add_album([album_invalid]) - - # default format - with capture_log("beets.mbsync") as logs: - self.run_command("mbsync") - e = ( - "mbsync: Skipping album with invalid mb_albumid: " - + "album info - album info" - ) - assert e == logs[0] - - # custom format - with capture_log("beets.mbsync") as logs: - self.run_command("mbsync", "-f", "'$album'") - e = "mbsync: Skipping album with invalid mb_albumid: 'album info'" - assert e == logs[0] - - # restore the config - config["format_item"] = "$artist - $album - $title" - config["format_album"] = "$albumartist - $album" - - # Test singleton with invalid mb_trackid. - # The default singleton format includes $artist and $album - # so we need to stub them here - item_invalid = Item( - artist="album info", - album="album info", - title="old title", - mb_trackid="a1b2c3d4", - path="", - ) - self.lib.add(item_invalid) - - # default format - with capture_log("beets.mbsync") as logs: - self.run_command("mbsync") - e = ( - "mbsync: Skipping singleton with invalid mb_trackid: " - + "album info - album info - old title" - ) - assert e == logs[0] - - # custom format - with capture_log("beets.mbsync") as logs: - self.run_command("mbsync", "-f", "'$title'") - e = "mbsync: Skipping singleton with invalid mb_trackid: 'old title'" - assert e == logs[0] + self.run_command("mbsync", "-f", "'%if{$album,$album,$title}'") + assert set(logs) == { + "mbsync: Skipping album with no mb_albumid: 'no id'", + "mbsync: Skipping album with invalid mb_albumid: 'invalid id'", + "mbsync: Skipping singleton with no mb_trackid: 'no id'", + "mbsync: Skipping singleton with invalid mb_trackid: 'invalid id'", + }