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 import unittest
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 TestHelper, has_program from beets.test.helper import BeetsTestCase, has_program
from beetsplug.replaygain import ( from beetsplug.replaygain import (
FatalGstreamerPluginReplayGainError, FatalGstreamerPluginReplayGainError,
GStreamerBackend, GStreamerBackend,
@ -51,6 +52,34 @@ def reset_replaygain(item):
item.store() 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: class GstBackendMixin:
backend = "gstreamer" backend = "gstreamer"
has_r128_support = True has_r128_support = True
@ -85,22 +114,9 @@ class FfmpegBackendMixin:
pass pass
class ReplayGainCliTestBase(TestHelper): class ReplayGainCliTest:
FNAME: str 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): def _add_album(self, *args, **kwargs):
# Use a file with non-zero volume (most test assets are total silence) # Use a file with non-zero volume (most test assets are total silence)
album = self.add_album_fixture(*args, fname=self.FNAME, **kwargs) album = self.add_album_fixture(*args, fname=self.FNAME, **kwargs)
@ -109,10 +125,6 @@ class ReplayGainCliTestBase(TestHelper):
return album return album
def tearDown(self):
self.teardown_beets()
self.unload_plugins()
def test_cli_saves_track_gain(self): def test_cli_saves_track_gain(self):
self._add_album(2) self._add_album(2)
@ -320,55 +332,33 @@ class ReplayGainCliTestBase(TestHelper):
@unittest.skipIf(not GST_AVAILABLE, "gstreamer cannot be found") @unittest.skipIf(not GST_AVAILABLE, "gstreamer cannot be found")
class ReplayGainGstCliTest( class ReplayGainGstCliTest(
ReplayGainCliTestBase, unittest.TestCase, GstBackendMixin ReplayGainCliTest, ReplayGainTestCase, GstBackendMixin
): ):
FNAME = "full" # file contains only silence FNAME = "full" # file contains only silence
@unittest.skipIf(not GAIN_PROG_AVAILABLE, "no *gain command found") @unittest.skipIf(not GAIN_PROG_AVAILABLE, "no *gain command found")
class ReplayGainCmdCliTest( class ReplayGainCmdCliTest(
ReplayGainCliTestBase, unittest.TestCase, CmdBackendMixin ReplayGainCliTest, ReplayGainTestCase, CmdBackendMixin
): ):
FNAME = "full" # file contains only silence FNAME = "full" # file contains only silence
@unittest.skipIf(not FFMPEG_AVAILABLE, "ffmpeg cannot be found") @unittest.skipIf(not FFMPEG_AVAILABLE, "ffmpeg cannot be found")
class ReplayGainFfmpegCliTest( class ReplayGainFfmpegCliTest(
ReplayGainCliTestBase, unittest.TestCase, FfmpegBackendMixin ReplayGainCliTest, ReplayGainTestCase, FfmpegBackendMixin
): ):
FNAME = "full" # file contains only silence FNAME = "full" # file contains only silence
@unittest.skipIf(not FFMPEG_AVAILABLE, "ffmpeg cannot be found") @unittest.skipIf(not FFMPEG_AVAILABLE, "ffmpeg cannot be found")
class ReplayGainFfmpegNoiseCliTest( class ReplayGainFfmpegNoiseCliTest(
ReplayGainCliTestBase, unittest.TestCase, FfmpegBackendMixin ReplayGainCliTest, ReplayGainTestCase, FfmpegBackendMixin
): ):
FNAME = "whitenoise" FNAME = "whitenoise"
class ImportTest(TestHelper): class ImportTest:
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()
def test_import_converted(self): def test_import_converted(self):
self.importer.run() self.importer.run()
for item in self.lib.items(): for item in self.lib.items():
@ -380,27 +370,27 @@ class ImportTest(TestHelper):
@unittest.skipIf(not GST_AVAILABLE, "gstreamer cannot be found") @unittest.skipIf(not GST_AVAILABLE, "gstreamer cannot be found")
class ReplayGainGstImportTest(ImportTest, unittest.TestCase, GstBackendMixin): class ReplayGainGstImportTest(ImportTest, ReplayGainTestCase, GstBackendMixin):
pass pass
@unittest.skipIf(not GAIN_PROG_AVAILABLE, "no *gain command found") @unittest.skipIf(not GAIN_PROG_AVAILABLE, "no *gain command found")
class ReplayGainCmdImportTest(ImportTest, unittest.TestCase, CmdBackendMixin): class ReplayGainCmdImportTest(ImportTest, ReplayGainTestCase, CmdBackendMixin):
pass pass
@unittest.skipIf(not FFMPEG_AVAILABLE, "ffmpeg cannot be found") @unittest.skipIf(not FFMPEG_AVAILABLE, "ffmpeg cannot be found")
class ReplayGainFfmpegImportTest( class ReplayGainFfmpegImportTest(
ImportTest, unittest.TestCase, FfmpegBackendMixin ImportTest, ReplayGainTestCase, FfmpegBackendMixin
): ):
pass pass
@unittest.skipIf(not FFMPEG_AVAILABLE, "ffmpeg cannot be found") @unittest.skipIf(not FFMPEG_AVAILABLE, "ffmpeg cannot be found")
class ReplayGainFfmpegThreadedImportTest( class ReplayGainFfmpegThreadedImportTest(
ImportTest, unittest.TestCase, FfmpegBackendMixin ThreadedImportMixin, ImportTest, ReplayGainTestCase, FfmpegBackendMixin
): ):
threaded = True pass
def suite(): def suite():
@ -409,3 +399,4 @@ def suite():
if __name__ == "__main__": if __name__ == "__main__":
unittest.main(defaultTest="suite") unittest.main(defaultTest="suite")
unittest.main(defaultTest="suite")