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.
This commit is contained in:
Šarūnas Nejus 2024-10-16 06:37:53 +01:00
parent 65d78cb65d
commit 2681c83c5b
No known key found for this signature in database
GPG key ID: DD28F6704DBE3435
2 changed files with 45 additions and 224 deletions

View file

@ -20,9 +20,6 @@ information or mock the environment.
- `has_program` checks the presence of a command on the system. - `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 - The `ImportSessionFixture` allows one to run importer code while
controlling the interactions through code. 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: class AutotagStub:
"""Stub out MusicBrainz album and track matcher and control what the """Stub out MusicBrainz album and track matcher and control what the
autotagger returns. autotagger returns.

View file

@ -12,17 +12,11 @@
# The above copyright notice and this permission notice shall be # The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software. # included in all copies or substantial portions of the Software.
from unittest.mock import patch from unittest.mock import patch
from beets import config from beets.autotag.hooks import AlbumInfo, TrackInfo
from beets.library import Item from beets.library import Item
from beets.test.helper import ( from beets.test.helper import PluginTestCase, capture_log
PluginTestCase,
capture_log,
generate_album_info,
generate_track_info,
)
class MbsyncCliTest(PluginTestCase): class MbsyncCliTest(PluginTestCase):
@ -31,28 +25,28 @@ class MbsyncCliTest(PluginTestCase):
@patch("beets.autotag.mb.album_for_id") @patch("beets.autotag.mb.album_for_id")
@patch("beets.autotag.mb.track_for_id") @patch("beets.autotag.mb.track_for_id")
def test_update_library(self, track_for_id, album_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_item = Item(
album="old title", album="old album",
mb_albumid="81ae60d4-5b75-38df-903a-db2cfa51c2c6", mb_albumid="81ae60d4-5b75-38df-903a-db2cfa51c2c6",
mb_trackid="old track id", mb_trackid="track id",
mb_releasetrackid="release track id",
path="",
) )
album = self.lib.add_album([album_item]) self.lib.add_album([album_item])
item = Item( singleton = Item(
title="old title", title="old title", mb_trackid="b8c2cf90-83f9-3b5f-8ccd-31fb866fcf37"
mb_trackid="b8c2cf90-83f9-3b5f-8ccd-31fb866fcf37", )
path="", 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: with capture_log() as logs:
self.run_command("mbsync") self.run_command("mbsync")
@ -60,130 +54,36 @@ class MbsyncCliTest(PluginTestCase):
assert "Sending event: albuminfo_received" in logs assert "Sending event: albuminfo_received" in logs
assert "Sending event: trackinfo_received" in logs assert "Sending event: trackinfo_received" in logs
item.load() singleton.load()
assert item.title == "singleton info" assert singleton.title == "new title"
album_item.load() 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.mb_trackid == "track id"
assert album_item.get_album().album == "new album"
album.load() def test_custom_format(self):
assert album.album == "album info" for item in [
Item(artist="albumartist", album="no id"),
def test_message_when_skipping(self): Item(
config["format_item"] = "$artist - $album - $title" artist="albumartist",
config["format_album"] = "$albumartist - $album" album="invalid id",
# 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", mb_albumid="a1b2c3d4",
path="", ),
) ]:
self.lib.add_album([album_invalid]) self.lib.add_album([item])
for item in [
Item(artist="artist", title="no id"),
Item(artist="artist", title="invalid id", mb_trackid="a1b2c3d4"),
]:
self.lib.add(item)
# default format
with capture_log("beets.mbsync") as logs: with capture_log("beets.mbsync") as logs:
self.run_command("mbsync") self.run_command("mbsync", "-f", "'%if{$album,$album,$title}'")
e = ( assert set(logs) == {
"mbsync: Skipping album with invalid mb_albumid: " "mbsync: Skipping album with no mb_albumid: 'no id'",
+ "album info - album info" "mbsync: Skipping album with invalid mb_albumid: 'invalid id'",
) "mbsync: Skipping singleton with no mb_trackid: 'no id'",
assert e == logs[0] "mbsync: Skipping singleton with invalid mb_trackid: 'invalid id'",
}
# 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]