Skip to content

Commit

Permalink
Test helpers: Remove unneeded data generation abstractions (#5463)
Browse files Browse the repository at this point in the history
This PR refactors the test codebase by removing redundant functions and
simplifying item and album creation. Key changes include:

- Removed redundant `_item_ident` index tracker from `_common.py`.
- Removed `album` function from `_common.py` replacing it with direct
`library.Album` invocations.
- Removed `generate_album_info` and `generate_track_info` functions,
replacing them directly with `TrackInfo` and `AlbumInfo`.
- Updated `setup.cfg` to exclude test helper files from coverage
reports.
- Adjusted the tests regarding the changes, and simplified
`test_mbsync.py`.
  • Loading branch information
snejus authored Nov 13, 2024
2 parents 7e9f7fc + 6f41872 commit de171f0
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 304 deletions.
30 changes: 0 additions & 30 deletions beets/test/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,12 @@
log.propagate = True
log.setLevel(logging.DEBUG)

# Dummy item creation.
_item_ident = 0

# OS feature test.
HAVE_SYMLINK = sys.platform != "win32"
HAVE_HARDLINK = sys.platform != "win32"


def item(lib=None):
global _item_ident
_item_ident += 1
i = beets.library.Item(
title="the title",
artist="the artist",
Expand All @@ -93,7 +88,6 @@ def item(lib=None):
comments="the comments",
bpm=8,
comp=True,
path=f"somepath{_item_ident}",
length=60.0,
bitrate=128000,
format="FLAC",
Expand All @@ -110,30 +104,6 @@ def item(lib=None):
return i


def album(lib=None):
global _item_ident
_item_ident += 1
i = beets.library.Album(
artpath=None,
albumartist="some album artist",
albumartist_sort="some sort album artist",
albumartist_credit="some album artist credit",
album="the album",
genre="the genre",
year=2014,
month=2,
day=5,
tracktotal=0,
disctotal=1,
comp=False,
mb_albumid="someID-1",
mb_albumartistid="someID-1",
)
if lib:
lib.add(i)
return i


# Dummy import session.
def import_session(lib=None, loghandler=None, paths=[], query=[], cli=False):
cls = commands.TerminalImportSession if cli else importer.ImportSession
Expand Down
98 changes: 3 additions & 95 deletions beets/test/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
- `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.
Expand Down Expand Up @@ -249,16 +246,15 @@ def create_item(self, **values):
The item is attached to the database from `self.lib`.
"""
item_count = self._get_item_count()
values_ = {
"title": "t\u00eftle {0}",
"artist": "the \u00e4rtist",
"album": "the \u00e4lbum",
"track": item_count,
"track": 1,
"format": "MP3",
}
values_.update(values)
values_["title"] = values_["title"].format(item_count)
values_["title"] = values_["title"].format(1)
values_["db"] = self.lib
item = Item(**values_)
if "path" not in values:
Expand Down Expand Up @@ -375,12 +371,6 @@ def create_mediafile_fixture(self, ext="mp3", images=[]):

return path

def _get_item_count(self):
if not hasattr(self, "__item_count"):
count = 0
self.__item_count = count + 1
return count

# Running beets commands

def run_command(self, *args, **kwargs):
Expand Down Expand Up @@ -723,10 +713,6 @@ def choose_match(self, task):

default_resolution = "REMOVE"

def add_resolution(self, resolution):
assert isinstance(resolution, self.Resolution)
self._resolutions.append(resolution)

def resolve_duplicate(self, task, found_duplicates):
try:
res = self._resolutions.pop(0)
Expand Down Expand Up @@ -779,12 +765,10 @@ def _add_choice_input(self):
self.io.addinput("T")
elif choice == importer.action.SKIP:
self.io.addinput("S")
elif isinstance(choice, int):
else:
self.io.addinput("M")
self.io.addinput(str(choice))
self._add_choice_input()
else:
raise Exception("Unknown choice %s" % choice)


class TerminalImportMixin(ImportHelper):
Expand All @@ -803,82 +787,6 @@ def _get_import_session(self, import_dir: bytes) -> importer.ImportSession:
)


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.
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ markers =
data_file = .reports/coverage/data
branch = true
relative_files = true
omit = beets/test/*

[coverage:report]
precision = 2
Expand Down
1 change: 1 addition & 0 deletions test/plugins/test_aura.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def track_document(self, item, album):
"artist": item.artist,
"size": Path(os.fsdecode(item.path)).stat().st_size,
"title": item.title,
"track": 1,
},
"relationships": {
"albums": {"data": [{"id": str(album.id), "type": "album"}]},
Expand Down
Loading

0 comments on commit de171f0

Please sign in to comment.