diff --git a/beets/test/helper.py b/beets/test/helper.py index 85ea6bcf7..ca9b8f406 100644 --- a/beets/test/helper.py +++ b/beets/test/helper.py @@ -35,6 +35,7 @@ import subprocess import sys import unittest from contextlib import contextmanager +from dataclasses import dataclass from enum import Enum from functools import cached_property from io import StringIO @@ -774,6 +775,7 @@ class TerminalImportMixin(ImportHelper): ) +@dataclass class AutotagStub: """Stub out MusicBrainz album and track matcher and control what the autotagger returns. @@ -784,11 +786,9 @@ class AutotagStub: GOOD = "GOOD" BAD = "BAD" MISSING = "MISSING" - """Generate an album match for all but one track - """ + matching: str length = 2 - matching = IDENT def install(self): self.mb_match_album = autotag.mb.match_album @@ -877,6 +877,15 @@ class AutotagStub: ) +class AutotagImportTestCase(ImportTestCase): + matching = AutotagStub.IDENT + + def setUp(self): + super().setUp() + self.matcher = AutotagStub(self.matching).install() + self.addCleanup(self.matcher.restore) + + class FetchImageHelper: """Helper mixin for mocking requests when fetching images with remote art sources. diff --git a/test/plugins/test_edit.py b/test/plugins/test_edit.py index 2d557d623..278e04b9e 100644 --- a/test/plugins/test_edit.py +++ b/test/plugins/test_edit.py @@ -19,9 +19,9 @@ from beets.dbcore.query import TrueQuery from beets.library import Item from beets.test import _common from beets.test.helper import ( + AutotagImportTestCase, AutotagStub, BeetsTestCase, - ImportTestCase, PluginMixin, TerminalImportMixin, control_stdin, @@ -316,10 +316,12 @@ class EditCommandTest(EditMixin, BeetsTestCase): @_common.slow_test() class EditDuringImporterTestCase( - EditMixin, TerminalImportMixin, ImportTestCase + EditMixin, TerminalImportMixin, AutotagImportTestCase ): """TODO""" + matching = AutotagStub.GOOD + IGNORED = ["added", "album_id", "id", "mtime", "path"] def setUp(self): @@ -327,12 +329,6 @@ class EditDuringImporterTestCase( # Create some mediafiles, and store them for comparison. self.prepare_album_for_import(1) self.items_orig = [Item.from_path(f.path) for f in self.import_media] - self.matcher = AutotagStub().install() - self.matcher.matching = AutotagStub.GOOD - - def tearDown(self): - super().tearDown() - self.matcher.restore() @_common.slow_test() diff --git a/test/plugins/test_importadded.py b/test/plugins/test_importadded.py index 608afb399..c3c7065d6 100644 --- a/test/plugins/test_importadded.py +++ b/test/plugins/test_importadded.py @@ -20,7 +20,7 @@ import os import pytest from beets import importer -from beets.test.helper import AutotagStub, ImportTestCase, PluginMixin +from beets.test.helper import AutotagImportTestCase, PluginMixin from beets.util import displayable_path, syspath from beetsplug.importadded import ImportAddedPlugin @@ -41,7 +41,7 @@ def modify_mtimes(paths, offset=-60000): os.utime(syspath(path), (mstat.st_atime, mstat.st_mtime + offset * i)) -class ImportAddedTest(PluginMixin, ImportTestCase): +class ImportAddedTest(PluginMixin, AutotagImportTestCase): # The minimum mtime of the files to be imported plugin = "importadded" min_mtime = None @@ -56,15 +56,9 @@ class ImportAddedTest(PluginMixin, ImportTestCase): self.min_mtime = min( os.path.getmtime(mfile.path) for mfile in self.import_media ) - self.matcher = AutotagStub().install() - self.matcher.matching = AutotagStub.IDENT self.importer = self.setup_importer() self.importer.add_choice(importer.action.APPLY) - def tearDown(self): - super().tearDown() - self.matcher.restore() - def find_media_file(self, item): """Find the pre-import MediaFile for an Item""" for m in self.import_media: diff --git a/test/plugins/test_mbsubmit.py b/test/plugins/test_mbsubmit.py index 04b1b736e..712c90866 100644 --- a/test/plugins/test_mbsubmit.py +++ b/test/plugins/test_mbsubmit.py @@ -14,8 +14,7 @@ from beets.test.helper import ( - AutotagStub, - ImportTestCase, + AutotagImportTestCase, PluginMixin, TerminalImportMixin, capture_stdout, @@ -23,23 +22,18 @@ from beets.test.helper import ( ) -class MBSubmitPluginTest(PluginMixin, TerminalImportMixin, ImportTestCase): +class MBSubmitPluginTest( + PluginMixin, TerminalImportMixin, AutotagImportTestCase +): plugin = "mbsubmit" def setUp(self): super().setUp() self.prepare_album_for_import(2) self.setup_importer() - self.matcher = AutotagStub().install() - - def tearDown(self): - super().tearDown() - self.matcher.restore() def test_print_tracks_output(self): """Test the output of the "print tracks" choice.""" - self.matcher.matching = AutotagStub.BAD - with capture_stdout() as output: with control_stdin("\n".join(["p", "s"])): # Print tracks; Skip @@ -55,8 +49,6 @@ class MBSubmitPluginTest(PluginMixin, TerminalImportMixin, ImportTestCase): def test_print_tracks_output_as_tracks(self): """Test the output of the "print tracks" choice, as singletons.""" - self.matcher.matching = AutotagStub.BAD - with capture_stdout() as output: with control_stdin("\n".join(["t", "s", "p", "s"])): # as Tracks; Skip; Print tracks; Skip diff --git a/test/test_importer.py b/test/test_importer.py index a28b646cf..804266116 100644 --- a/test/test_importer.py +++ b/test/test_importer.py @@ -39,6 +39,7 @@ from beets.test import _common from beets.test.helper import ( NEEDS_REFLINK, AsIsImporterMixin, + AutotagImportTestCase, AutotagStub, BeetsTestCase, ImportTestCase, @@ -306,7 +307,7 @@ class ImportPasswordRarTest(ImportZipTest): return os.path.join(_common.RSRC, b"password.rar") -class ImportSingletonTest(ImportTestCase): +class ImportSingletonTest(AutotagImportTestCase): """Test ``APPLY`` and ``ASIS`` choices for an import session with singletons config set to True. """ @@ -315,11 +316,6 @@ class ImportSingletonTest(ImportTestCase): super().setUp() self.prepare_album_for_import(1) self.importer = self.setup_singleton_importer() - self.matcher = AutotagStub().install() - - def tearDown(self): - super().tearDown() - self.matcher.restore() def test_apply_asis_adds_track(self): assert self.lib.items().get() is None @@ -432,19 +428,13 @@ class ImportSingletonTest(ImportTestCase): assert item.disc == disc -class ImportTest(ImportTestCase): +class ImportTest(AutotagImportTestCase): """Test APPLY, ASIS and SKIP choices.""" def setUp(self): super().setUp() self.prepare_album_for_import(1) self.setup_importer() - self.matcher = AutotagStub().install() - self.matcher.matching = AutotagStub.IDENT - - def tearDown(self): - super().tearDown() - self.matcher.restore() def test_apply_asis_adds_album(self): assert self.lib.albums().get() is None @@ -639,18 +629,13 @@ class ImportTest(ImportTestCase): assert item.disc == disc -class ImportTracksTest(ImportTestCase): +class ImportTracksTest(AutotagImportTestCase): """Test TRACKS and APPLY choice.""" def setUp(self): super().setUp() self.prepare_album_for_import(1) self.setup_importer() - self.matcher = AutotagStub().install() - - def tearDown(self): - super().tearDown() - self.matcher.restore() def test_apply_tracks_adds_singleton_track(self): assert self.lib.items().get() is None @@ -673,18 +658,13 @@ class ImportTracksTest(ImportTestCase): self.assert_file_in_lib(b"singletons", b"Applied Track 1.mp3") -class ImportCompilationTest(ImportTestCase): +class ImportCompilationTest(AutotagImportTestCase): """Test ASIS import of a folder containing tracks with different artists.""" def setUp(self): super().setUp() self.prepare_album_for_import(3) self.setup_importer() - self.matcher = AutotagStub().install() - - def tearDown(self): - super().tearDown() - self.matcher.restore() def test_asis_homogenous_sets_albumartist(self): self.importer.add_choice(importer.action.ASIS) @@ -783,21 +763,16 @@ class ImportCompilationTest(ImportTestCase): assert asserted_multi_artists_1 -class ImportExistingTest(ImportTestCase): +class ImportExistingTest(AutotagImportTestCase): """Test importing files that are already in the library directory.""" def setUp(self): super().setUp() self.prepare_album_for_import(1) - self.matcher = AutotagStub().install() self.reimporter = self.setup_importer(import_dir=self.libdir) self.importer = self.setup_importer() - def tearDown(self): - super().tearDown() - self.matcher.restore() - def test_does_not_duplicate_item(self): self.importer.run() assert len(self.lib.items()) == 1 @@ -904,12 +879,12 @@ class ImportExistingTest(ImportTestCase): self.assertNotExists(self.import_media[0].path) -class GroupAlbumsImportTest(ImportTestCase): +class GroupAlbumsImportTest(AutotagImportTestCase): + matching = AutotagStub.NONE + def setUp(self): super().setUp() self.prepare_album_for_import(3) - self.matcher = AutotagStub().install() - self.matcher.matching = AutotagStub.NONE self.setup_importer() # Split tracks into two albums and use both as-is @@ -917,10 +892,6 @@ class GroupAlbumsImportTest(ImportTestCase): self.importer.add_choice(importer.action.ASIS) self.importer.add_choice(importer.action.ASIS) - def tearDown(self): - super().tearDown() - self.matcher.restore() - def test_add_album_for_different_artist_and_different_album(self): self.import_media[0].artist = "Artist B" self.import_media[0].album = "Album B" @@ -976,17 +947,13 @@ class GlobalGroupAlbumsImportTest(GroupAlbumsImportTest): config["import"]["group_albums"] = True -class ChooseCandidateTest(ImportTestCase): +class ChooseCandidateTest(AutotagImportTestCase): + matching = AutotagStub.BAD + def setUp(self): super().setUp() self.prepare_album_for_import(1) self.setup_importer() - self.matcher = AutotagStub().install() - self.matcher.matching = AutotagStub.BAD - - def tearDown(self): - super().tearDown() - self.matcher.restore() def test_choose_first_candidate(self): self.importer.add_choice(1) @@ -1566,7 +1533,7 @@ class MultiDiscAlbumsInDirTest(BeetsTestCase): assert len(items) == 3 -class ReimportTest(ImportTestCase): +class ReimportTest(AutotagImportTestCase): """Test "re-imports", in which the autotagging machinery is used for music that's already in the library. @@ -1575,6 +1542,8 @@ class ReimportTest(ImportTestCase): attributes and the added date. """ + matching = AutotagStub.GOOD + def setUp(self): super().setUp() @@ -1589,14 +1558,6 @@ class ReimportTest(ImportTestCase): item.added = 4747.0 item.store() - # Set up an import pipeline with a "good" match. - self.matcher = AutotagStub().install() - self.matcher.matching = AutotagStub.GOOD - - def tearDown(self): - super().tearDown() - self.matcher.restore() - def _setup_session(self, singletons=False): self.setup_importer(import_dir=self.libdir, singletons=singletons) self.importer.add_choice(importer.action.APPLY) @@ -1677,22 +1638,17 @@ class ReimportTest(ImportTestCase): assert self._album().data_source == "match_source" -class ImportPretendTest(ImportTestCase): +class ImportPretendTest(AutotagImportTestCase): """Test the pretend commandline option""" def setUp(self): super().setUp() - self.matcher = AutotagStub().install() self.io.install() self.album_track_path = self.prepare_album_for_import(1)[0] self.single_path = self.prepare_track_for_import(2, self.import_path) self.album_path = self.album_track_path.parent - def tearDown(self): - super().tearDown() - self.matcher.restore() - def __run(self, importer): with capture_log() as logs: importer.run() diff --git a/test/test_plugins.py b/test/test_plugins.py index d273de698..4564f6690 100644 --- a/test/test_plugins.py +++ b/test/test_plugins.py @@ -347,7 +347,8 @@ class PromptChoicesTest(TerminalImportMixin, PluginImportTestCase): def setUp(self): super().setUp() self.setup_importer() - self.matcher = AutotagStub().install() + self.matcher = AutotagStub(AutotagStub.IDENT).install() + self.addCleanup(self.matcher.restore) # keep track of ui.input_option() calls self.input_options_patcher = patch( "beets.ui.input_options", side_effect=ui.input_options @@ -357,7 +358,6 @@ class PromptChoicesTest(TerminalImportMixin, PluginImportTestCase): def tearDown(self): super().tearDown() self.input_options_patcher.stop() - self.matcher.restore() def test_plugin_choices_in_ui_input_options_album(self): """Test the presence of plugin choices on the prompt (album)."""