Simplify tests

This commit is contained in:
Šarūnas Nejus 2026-01-30 22:36:54 +00:00
parent cb6ad89ce6
commit 2196bd89de

View file

@ -1,4 +1,4 @@
from typing import Iterable
from collections.abc import Iterable
import pytest
@ -9,8 +9,6 @@ from beets.test.helper import PluginMixin
class ErrorMetadataMockPlugin(metadata_plugins.MetadataSourcePlugin):
"""A metadata source plugin that raises errors in all its methods."""
data_source = "ErrorMetadataMockPlugin"
def candidates(self, *args, **kwargs):
raise ValueError("Mocked error")
@ -25,12 +23,6 @@ class ErrorMetadataMockPlugin(metadata_plugins.MetadataSourcePlugin):
def track_for_id(self, *args, **kwargs):
raise ValueError("Mocked error")
def track_distance(self, *args, **kwargs):
raise ValueError("Mocked error")
def album_distance(self, *args, **kwargs):
raise ValueError("Mocked error")
class TestMetadataPluginsException(PluginMixin):
"""Check that errors during the metadata plugins do not crash beets.
@ -44,6 +36,14 @@ class TestMetadataPluginsException(PluginMixin):
yield
self.unload_plugins()
@pytest.fixture
def call_method(self, method_name, args):
def _call():
result = getattr(metadata_plugins, method_name)(*args)
return list(result) if isinstance(result, Iterable) else result
return _call
@pytest.mark.parametrize(
"method_name,error_method_name,args",
[
@ -57,30 +57,15 @@ class TestMetadataPluginsException(PluginMixin):
("track_for_id", "tracks_for_ids", ("some_id",)),
],
)
def test_logging(
self,
caplog,
method_name,
error_method_name,
args,
):
def test_logging(self, caplog, call_method, error_method_name):
self.config["raise_on_error"] = False
with caplog.at_level("ERROR"):
# Call the method to trigger the error
ret = getattr(metadata_plugins, method_name)(*args)
if isinstance(ret, Iterable):
list(ret)
# Check that an error was logged
assert len(caplog.records) >= 1
logs = [record.getMessage() for record in caplog.records]
for msg in logs:
assert (
msg
== f"Error in 'ErrorMetadataMockPlugin.{error_method_name}': Mocked error" # noqa: E501
)
call_method()
caplog.clear()
assert (
f"Error in 'ErrorMetadataMock.{error_method_name}': Mocked error"
in caplog.text
)
@pytest.mark.parametrize(
"method_name,args",
@ -91,13 +76,8 @@ class TestMetadataPluginsException(PluginMixin):
("track_for_id", ("some_id",)),
],
)
def test_raising(
self,
method_name,
args,
):
def test_raising(self, call_method):
self.config["raise_on_error"] = True
with pytest.raises(ValueError, match="Mocked error"):
getattr(metadata_plugins, method_name)(*args) if not isinstance(
args, Iterable
) else list(getattr(metadata_plugins, method_name)(*args))
call_method()