test_replaygain: restructure tests to share setup

This commit is contained in:
Šarūnas Nejus 2024-07-03 16:32:48 +01:00
parent 8373181e02
commit 6ccf33d79e
No known key found for this signature in database
GPG key ID: DD28F6704DBE3435

View file

@ -14,11 +14,12 @@
import unittest
from typing import ClassVar
from mediafile import MediaFile
from beets import config
from beets.test.helper import TestHelper, has_program
from beets.test.helper import BeetsTestCase, has_program
from beetsplug.replaygain import (
FatalGstreamerPluginReplayGainError,
GStreamerBackend,
@ -51,6 +52,34 @@ def reset_replaygain(item):
item.store()
class ReplayGainTestCase(BeetsTestCase):
backend: ClassVar[str]
def setUp(self):
# Implemented by Mixins, see above. This may decide to skip the test.
self.test_backend()
self.setup_beets(disk=True)
self.config["replaygain"]["backend"] = self.backend
try:
self.load_plugins("replaygain")
except Exception:
self.tearDown()
self.importer = self.create_importer()
def tearDown(self):
self.unload_plugins()
super().tearDown()
class ThreadedImportMixin:
def setUp(self):
super().setUp()
self.config["threaded"] = True
class GstBackendMixin:
backend = "gstreamer"
has_r128_support = True
@ -85,22 +114,9 @@ class FfmpegBackendMixin:
pass
class ReplayGainCliTestBase(TestHelper):
class ReplayGainCliTest:
FNAME: str
def setUp(self):
# Implemented by Mixins, see above. This may decide to skip the test.
self.test_backend()
self.setup_beets(disk=True)
self.config["replaygain"]["backend"] = self.backend
try:
self.load_plugins("replaygain")
except Exception:
self.teardown_beets()
self.unload_plugins()
def _add_album(self, *args, **kwargs):
# Use a file with non-zero volume (most test assets are total silence)
album = self.add_album_fixture(*args, fname=self.FNAME, **kwargs)
@ -109,10 +125,6 @@ class ReplayGainCliTestBase(TestHelper):
return album
def tearDown(self):
self.teardown_beets()
self.unload_plugins()
def test_cli_saves_track_gain(self):
self._add_album(2)
@ -320,55 +332,33 @@ class ReplayGainCliTestBase(TestHelper):
@unittest.skipIf(not GST_AVAILABLE, "gstreamer cannot be found")
class ReplayGainGstCliTest(
ReplayGainCliTestBase, unittest.TestCase, GstBackendMixin
ReplayGainCliTest, ReplayGainTestCase, GstBackendMixin
):
FNAME = "full" # file contains only silence
@unittest.skipIf(not GAIN_PROG_AVAILABLE, "no *gain command found")
class ReplayGainCmdCliTest(
ReplayGainCliTestBase, unittest.TestCase, CmdBackendMixin
ReplayGainCliTest, ReplayGainTestCase, CmdBackendMixin
):
FNAME = "full" # file contains only silence
@unittest.skipIf(not FFMPEG_AVAILABLE, "ffmpeg cannot be found")
class ReplayGainFfmpegCliTest(
ReplayGainCliTestBase, unittest.TestCase, FfmpegBackendMixin
ReplayGainCliTest, ReplayGainTestCase, FfmpegBackendMixin
):
FNAME = "full" # file contains only silence
@unittest.skipIf(not FFMPEG_AVAILABLE, "ffmpeg cannot be found")
class ReplayGainFfmpegNoiseCliTest(
ReplayGainCliTestBase, unittest.TestCase, FfmpegBackendMixin
ReplayGainCliTest, ReplayGainTestCase, FfmpegBackendMixin
):
FNAME = "whitenoise"
class ImportTest(TestHelper):
threaded = False
def setUp(self):
# Implemented by Mixins, see above. This may decide to skip the test.
self.test_backend()
self.setup_beets(disk=True)
self.config["threaded"] = self.threaded
self.config["replaygain"]["backend"] = self.backend
try:
self.load_plugins("replaygain")
except Exception:
self.teardown_beets()
self.unload_plugins()
self.importer = self.create_importer()
def tearDown(self):
self.unload_plugins()
self.teardown_beets()
class ImportTest:
def test_import_converted(self):
self.importer.run()
for item in self.lib.items():
@ -380,27 +370,27 @@ class ImportTest(TestHelper):
@unittest.skipIf(not GST_AVAILABLE, "gstreamer cannot be found")
class ReplayGainGstImportTest(ImportTest, unittest.TestCase, GstBackendMixin):
class ReplayGainGstImportTest(ImportTest, ReplayGainTestCase, GstBackendMixin):
pass
@unittest.skipIf(not GAIN_PROG_AVAILABLE, "no *gain command found")
class ReplayGainCmdImportTest(ImportTest, unittest.TestCase, CmdBackendMixin):
class ReplayGainCmdImportTest(ImportTest, ReplayGainTestCase, CmdBackendMixin):
pass
@unittest.skipIf(not FFMPEG_AVAILABLE, "ffmpeg cannot be found")
class ReplayGainFfmpegImportTest(
ImportTest, unittest.TestCase, FfmpegBackendMixin
ImportTest, ReplayGainTestCase, FfmpegBackendMixin
):
pass
@unittest.skipIf(not FFMPEG_AVAILABLE, "ffmpeg cannot be found")
class ReplayGainFfmpegThreadedImportTest(
ImportTest, unittest.TestCase, FfmpegBackendMixin
ThreadedImportMixin, ImportTest, ReplayGainTestCase, FfmpegBackendMixin
):
threaded = True
pass
def suite():
@ -409,3 +399,4 @@ def suite():
if __name__ == "__main__":
unittest.main(defaultTest="suite")
unittest.main(defaultTest="suite")