Move create_importer to ImportHelper

This commit is contained in:
Šarūnas Nejus 2024-07-09 00:00:41 +01:00
parent fcff5d72af
commit 1f8466f04a
No known key found for this signature in database
GPG key ID: DD28F6704DBE3435
7 changed files with 73 additions and 68 deletions

View file

@ -253,59 +253,6 @@ class TestHelper(_common.Assertions):
Item._queries = Item._original_queries Item._queries = Item._original_queries
Album._queries = Album._original_queries Album._queries = Album._original_queries
def create_importer(self, item_count=1, album_count=1):
"""Create files to import and return corresponding session.
Copies the specified number of files to a subdirectory of
`self.temp_dir` and creates a `ImportSessionFixture` for this path.
"""
import_dir = os.path.join(self.temp_dir, b"import")
if not os.path.isdir(syspath(import_dir)):
os.mkdir(syspath(import_dir))
album_no = 0
while album_count:
album = util.bytestring_path(f"album {album_no}")
album_dir = os.path.join(import_dir, album)
if os.path.exists(syspath(album_dir)):
album_no += 1
continue
os.mkdir(syspath(album_dir))
album_count -= 1
track_no = 0
album_item_count = item_count
while album_item_count:
title = f"track {track_no}"
src = os.path.join(_common.RSRC, b"full.mp3")
title_file = util.bytestring_path(f"{title}.mp3")
dest = os.path.join(album_dir, title_file)
if os.path.exists(syspath(dest)):
track_no += 1
continue
album_item_count -= 1
shutil.copy(syspath(src), syspath(dest))
mediafile = MediaFile(dest)
mediafile.update(
{
"artist": "artist",
"albumartist": "album artist",
"title": title,
"album": album,
"mb_albumid": None,
"mb_trackid": None,
}
)
mediafile.save()
config["import"]["quiet"] = True
config["import"]["autotag"] = False
config["import"]["resume"] = False
return ImportSessionFixture(
self.lib, loghandler=None, query=None, paths=[import_dir]
)
# Library fixtures methods # Library fixtures methods
def create_item(self, **values): def create_item(self, **values):
@ -642,6 +589,59 @@ class ImportHelper:
self._get_import_session(import_dir or self.import_dir) self._get_import_session(import_dir or self.import_dir)
def create_importer(self, item_count=1, album_count=1):
"""Create files to import and return corresponding session.
Copies the specified number of files to a subdirectory of
`self.temp_dir` and creates a `ImportSessionFixture` for this path.
"""
import_dir = os.path.join(self.temp_dir, b"import")
if not os.path.isdir(syspath(import_dir)):
os.mkdir(syspath(import_dir))
album_no = 0
while album_count:
album = util.bytestring_path(f"album {album_no}")
album_dir = os.path.join(import_dir, album)
if os.path.exists(syspath(album_dir)):
album_no += 1
continue
os.mkdir(syspath(album_dir))
album_count -= 1
track_no = 0
album_item_count = item_count
while album_item_count:
title = f"track {track_no}"
src = os.path.join(_common.RSRC, b"full.mp3")
title_file = util.bytestring_path(f"{title}.mp3")
dest = os.path.join(album_dir, title_file)
if os.path.exists(syspath(dest)):
track_no += 1
continue
album_item_count -= 1
shutil.copy(syspath(src), syspath(dest))
mediafile = MediaFile(dest)
mediafile.update(
{
"artist": "artist",
"albumartist": "album artist",
"title": title,
"album": album,
"mb_albumid": None,
"mb_trackid": None,
}
)
mediafile.save()
config["import"]["quiet"] = True
config["import"]["autotag"] = False
config["import"]["resume"] = False
return ImportSessionFixture(
self.lib, loghandler=None, query=None, paths=[import_dir]
)
def assert_file_in_lib(self, *segments): def assert_file_in_lib(self, *segments):
"""Join the ``segments`` and assert that this path exists in the """Join the ``segments`` and assert that this path exists in the
library directory. library directory.

View file

@ -23,7 +23,12 @@ from mediafile import MediaFile
from beets import util from beets import util
from beets.test import _common from beets.test import _common
from beets.test.helper import PluginTestCase, capture_log, control_stdin from beets.test.helper import (
ImportHelper,
PluginTestCase,
capture_log,
control_stdin,
)
from beets.util import bytestring_path, displayable_path from beets.util import bytestring_path, displayable_path
@ -90,7 +95,7 @@ class ConvertTestCase(ConvertMixin, PluginTestCase):
@_common.slow_test() @_common.slow_test()
class ImportConvertTest(ConvertTestCase): class ImportConvertTest(ImportHelper, ConvertTestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.importer = self.create_importer() self.importer = self.create_importer()

View file

@ -17,11 +17,11 @@ from unittest.mock import patch
from beets import util from beets import util
from beets.library import Item from beets.library import Item
from beets.test.helper import PluginTestCase from beets.test.helper import ImportTestCase, PluginMixin
@patch("beets.util.command_output") @patch("beets.util.command_output")
class KeyFinderTest(PluginTestCase): class KeyFinderTest(PluginMixin, ImportTestCase):
plugin = "keyfinder" plugin = "keyfinder"
def test_add_key(self, command_output): def test_add_key(self, command_output):

View file

@ -6,7 +6,7 @@ import platform
from unittest.mock import Mock, patch from unittest.mock import Mock, patch
from beets.test._common import touch from beets.test._common import touch
from beets.test.helper import PluginTestCase from beets.test.helper import ImportTestCase, PluginMixin
from beets.util import displayable_path from beets.util import displayable_path
from beetsplug.permissions import ( from beetsplug.permissions import (
check_permissions, check_permissions,
@ -15,7 +15,7 @@ from beetsplug.permissions import (
) )
class PermissionsPluginTest(PluginTestCase): class PermissionsPluginTest(PluginMixin, ImportTestCase):
plugin = "permissions" plugin = "permissions"
def setUp(self): def setUp(self):

View file

@ -19,7 +19,7 @@ from typing import ClassVar
from mediafile import MediaFile from mediafile import MediaFile
from beets import config from beets import config
from beets.test.helper import BeetsTestCase, has_program from beets.test.helper import ImportTestCase, has_program
from beetsplug.replaygain import ( from beetsplug.replaygain import (
FatalGstreamerPluginReplayGainError, FatalGstreamerPluginReplayGainError,
GStreamerBackend, GStreamerBackend,
@ -52,7 +52,7 @@ def reset_replaygain(item):
item.store() item.store()
class ReplayGainTestCase(BeetsTestCase): class ReplayGainTestCase(ImportTestCase):
db_on_disk = True db_on_disk = True
backend: ClassVar[str] backend: ClassVar[str]

View file

@ -1159,7 +1159,7 @@ def match_album_mock(*args, **kwargs):
@patch("beets.autotag.mb.match_album", Mock(side_effect=match_album_mock)) @patch("beets.autotag.mb.match_album", Mock(side_effect=match_album_mock))
class ImportDuplicateAlbumTest(BeetsTestCase): class ImportDuplicateAlbumTest(ImportTestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
@ -1278,7 +1278,7 @@ def match_track_mock(*args, **kwargs):
@patch("beets.autotag.mb.match_track", Mock(side_effect=match_track_mock)) @patch("beets.autotag.mb.match_track", Mock(side_effect=match_track_mock))
class ImportDuplicateSingletonTest(BeetsTestCase): class ImportDuplicateSingletonTest(ImportTestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
@ -1364,7 +1364,7 @@ class TagLogTest(BeetsTestCase):
self.assertIn("status caf\xe9", sio.getvalue()) self.assertIn("status caf\xe9", sio.getvalue())
class ResumeImportTest(BeetsTestCase): class ResumeImportTest(ImportTestCase):
@patch("beets.plugins.send") @patch("beets.plugins.send")
def test_resume_album(self, plugins_send): def test_resume_album(self, plugins_send):
self.importer = self.create_importer(album_count=2) self.importer = self.create_importer(album_count=2)
@ -1409,7 +1409,7 @@ class ResumeImportTest(BeetsTestCase):
self.assertIsNotNone(self.lib.items("title:track 1").get()) self.assertIsNotNone(self.lib.items("title:track 1").get())
class IncrementalImportTest(BeetsTestCase): class IncrementalImportTest(ImportTestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.config["import"]["incremental"] = True self.config["import"]["incremental"] = True

View file

@ -9,7 +9,7 @@ import beets.logging as blog
import beetsplug import beetsplug
from beets import plugins, ui from beets import plugins, ui
from beets.test import _common, helper from beets.test import _common, helper
from beets.test.helper import BeetsTestCase, PluginTestCase from beets.test.helper import BeetsTestCase, ImportTestCase, PluginMixin
class LoggingTest(BeetsTestCase): class LoggingTest(BeetsTestCase):
@ -46,7 +46,7 @@ class LoggingTest(BeetsTestCase):
self.assertTrue(stream.getvalue(), "foo oof baz") self.assertTrue(stream.getvalue(), "foo oof baz")
class LoggingLevelTest(PluginTestCase): class LoggingLevelTest(PluginMixin, ImportTestCase):
plugin = "dummy" plugin = "dummy"
class DummyModule: class DummyModule:
@ -161,7 +161,7 @@ class LoggingLevelTest(PluginTestCase):
@_common.slow_test() @_common.slow_test()
class ConcurrentEventsTest(BeetsTestCase): class ConcurrentEventsTest(ImportTestCase):
"""Similar to LoggingLevelTest but lower-level and focused on multiple """Similar to LoggingLevelTest but lower-level and focused on multiple
events interaction. Since this is a bit heavy we don't do it in events interaction. Since this is a bit heavy we don't do it in
LoggingLevelTest. LoggingLevelTest.