Skip to content

Commit de171f0

Browse files
authored
Test helpers: Remove unneeded data generation abstractions (#5463)
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`.
2 parents 7e9f7fc + 6f41872 commit de171f0

File tree

7 files changed

+89
-304
lines changed

7 files changed

+89
-304
lines changed

beets/test/_common.py

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,12 @@
5858
log.propagate = True
5959
log.setLevel(logging.DEBUG)
6060

61-
# Dummy item creation.
62-
_item_ident = 0
63-
6461
# OS feature test.
6562
HAVE_SYMLINK = sys.platform != "win32"
6663
HAVE_HARDLINK = sys.platform != "win32"
6764

6865

6966
def item(lib=None):
70-
global _item_ident
71-
_item_ident += 1
7267
i = beets.library.Item(
7368
title="the title",
7469
artist="the artist",
@@ -93,7 +88,6 @@ def item(lib=None):
9388
comments="the comments",
9489
bpm=8,
9590
comp=True,
96-
path=f"somepath{_item_ident}",
9791
length=60.0,
9892
bitrate=128000,
9993
format="FLAC",
@@ -110,30 +104,6 @@ def item(lib=None):
110104
return i
111105

112106

113-
def album(lib=None):
114-
global _item_ident
115-
_item_ident += 1
116-
i = beets.library.Album(
117-
artpath=None,
118-
albumartist="some album artist",
119-
albumartist_sort="some sort album artist",
120-
albumartist_credit="some album artist credit",
121-
album="the album",
122-
genre="the genre",
123-
year=2014,
124-
month=2,
125-
day=5,
126-
tracktotal=0,
127-
disctotal=1,
128-
comp=False,
129-
mb_albumid="someID-1",
130-
mb_albumartistid="someID-1",
131-
)
132-
if lib:
133-
lib.add(i)
134-
return i
135-
136-
137107
# Dummy import session.
138108
def import_session(lib=None, loghandler=None, paths=[], query=[], cli=False):
139109
cls = commands.TerminalImportSession if cli else importer.ImportSession

beets/test/helper.py

Lines changed: 3 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
2121
- `has_program` checks the presence of a command on the system.
2222
23-
- The `generate_album_info` and `generate_track_info` functions return
24-
fixtures to be used when mocking the autotagger.
25-
2623
- The `ImportSessionFixture` allows one to run importer code while
2724
controlling the interactions through code.
2825
@@ -249,16 +246,15 @@ def create_item(self, **values):
249246
250247
The item is attached to the database from `self.lib`.
251248
"""
252-
item_count = self._get_item_count()
253249
values_ = {
254250
"title": "t\u00eftle {0}",
255251
"artist": "the \u00e4rtist",
256252
"album": "the \u00e4lbum",
257-
"track": item_count,
253+
"track": 1,
258254
"format": "MP3",
259255
}
260256
values_.update(values)
261-
values_["title"] = values_["title"].format(item_count)
257+
values_["title"] = values_["title"].format(1)
262258
values_["db"] = self.lib
263259
item = Item(**values_)
264260
if "path" not in values:
@@ -375,12 +371,6 @@ def create_mediafile_fixture(self, ext="mp3", images=[]):
375371

376372
return path
377373

378-
def _get_item_count(self):
379-
if not hasattr(self, "__item_count"):
380-
count = 0
381-
self.__item_count = count + 1
382-
return count
383-
384374
# Running beets commands
385375

386376
def run_command(self, *args, **kwargs):
@@ -723,10 +713,6 @@ def choose_match(self, task):
723713

724714
default_resolution = "REMOVE"
725715

726-
def add_resolution(self, resolution):
727-
assert isinstance(resolution, self.Resolution)
728-
self._resolutions.append(resolution)
729-
730716
def resolve_duplicate(self, task, found_duplicates):
731717
try:
732718
res = self._resolutions.pop(0)
@@ -779,12 +765,10 @@ def _add_choice_input(self):
779765
self.io.addinput("T")
780766
elif choice == importer.action.SKIP:
781767
self.io.addinput("S")
782-
elif isinstance(choice, int):
768+
else:
783769
self.io.addinput("M")
784770
self.io.addinput(str(choice))
785771
self._add_choice_input()
786-
else:
787-
raise Exception("Unknown choice %s" % choice)
788772

789773

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

805789

806-
def generate_album_info(album_id, track_values):
807-
"""Return `AlbumInfo` populated with mock data.
808-
809-
Sets the album info's `album_id` field is set to the corresponding
810-
argument. For each pair (`id`, `values`) in `track_values` the `TrackInfo`
811-
from `generate_track_info` is added to the album info's `tracks` field.
812-
Most other fields of the album and track info are set to "album
813-
info" and "track info", respectively.
814-
"""
815-
tracks = [generate_track_info(id, values) for id, values in track_values]
816-
album = AlbumInfo(
817-
album_id="album info",
818-
album="album info",
819-
artist="album info",
820-
artist_id="album info",
821-
tracks=tracks,
822-
)
823-
for field in ALBUM_INFO_FIELDS:
824-
setattr(album, field, "album info")
825-
826-
return album
827-
828-
829-
ALBUM_INFO_FIELDS = [
830-
"album",
831-
"album_id",
832-
"artist",
833-
"artist_id",
834-
"asin",
835-
"albumtype",
836-
"va",
837-
"label",
838-
"barcode",
839-
"artist_sort",
840-
"releasegroup_id",
841-
"catalognum",
842-
"language",
843-
"country",
844-
"albumstatus",
845-
"media",
846-
"albumdisambig",
847-
"releasegroupdisambig",
848-
"artist_credit",
849-
"data_source",
850-
"data_url",
851-
]
852-
853-
854-
def generate_track_info(track_id="track info", values={}):
855-
"""Return `TrackInfo` populated with mock data.
856-
857-
The `track_id` field is set to the corresponding argument. All other
858-
string fields are set to "track info".
859-
"""
860-
track = TrackInfo(
861-
title="track info",
862-
track_id=track_id,
863-
)
864-
for field in TRACK_INFO_FIELDS:
865-
setattr(track, field, "track info")
866-
for field, value in values.items():
867-
setattr(track, field, value)
868-
return track
869-
870-
871-
TRACK_INFO_FIELDS = [
872-
"artist",
873-
"artist_id",
874-
"artist_sort",
875-
"disctitle",
876-
"artist_credit",
877-
"data_source",
878-
"data_url",
879-
]
880-
881-
882790
class AutotagStub:
883791
"""Stub out MusicBrainz album and track matcher and control what the
884792
autotagger returns.

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ markers =
1414
data_file = .reports/coverage/data
1515
branch = true
1616
relative_files = true
17+
omit = beets/test/*
1718

1819
[coverage:report]
1920
precision = 2

test/plugins/test_aura.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def track_document(self, item, album):
9191
"artist": item.artist,
9292
"size": Path(os.fsdecode(item.path)).stat().st_size,
9393
"title": item.title,
94+
"track": 1,
9495
},
9596
"relationships": {
9697
"albums": {"data": [{"id": str(album.id), "type": "album"}]},

0 commit comments

Comments
 (0)