| 1 |
from collections.abc import Iterable |
| 2 |
from concurrent.futures import ThreadPoolExecutor as BaseThreadPoolExecutor |
| 3 |
from threading import Event |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from beets import metadata_plugins |
| 8 |
from beets.test.helper import PluginMixin |
| 9 |
|
| 10 |
|
| 11 |
class ErrorMetadataMockPlugin(metadata_plugins.MetadataSourcePlugin): |
| 12 |
"""A metadata source plugin that raises errors in all its methods.""" |
| 13 |
|
| 14 |
def candidates(self, *args, **kwargs): |
| 15 |
raise ValueError("Mocked error") |
| 16 |
|
| 17 |
def item_candidates(self, *args, **kwargs): |
| 18 |
for i in range(3): |
| 19 |
raise ValueError("Mocked error") |
| 20 |
yield # This is just to make this a generator |
| 21 |
|
| 22 |
def album_for_id(self, *args, **kwargs): |
| 23 |
raise ValueError("Mocked error") |
| 24 |
|
| 25 |
def track_for_id(self, *args, **kwargs): |
| 26 |
raise ValueError("Mocked error") |
| 27 |
|
| 28 |
|
| 29 |
class TestMetadataPluginsException(PluginMixin): |
| 30 |
"""Check that errors during the metadata plugins do not crash beets. |
| 31 |
They should be logged as errors instead. |
| 32 |
""" |
| 33 |
|
| 34 |
@pytest.fixture(autouse=True) |
| 35 |
def setup(self): |
| 36 |
metadata_plugins.find_metadata_source_plugins.cache_clear() |
| 37 |
metadata_plugins.get_metadata_source.cache_clear() |
| 38 |
self.register_plugin(ErrorMetadataMockPlugin) |
| 39 |
yield |
| 40 |
self.unload_plugins() |
| 41 |
|
| 42 |
@pytest.fixture |
| 43 |
def call_method(self, method_name, args): |
| 44 |
def _call(): |
| 45 |
result = getattr(metadata_plugins, method_name)(*args) |
| 46 |
return list(result) if isinstance(result, Iterable) else result |
| 47 |
|
| 48 |
return _call |
| 49 |
|
| 50 |
@pytest.mark.parametrize( |
| 51 |
"method_name,args", |
| 52 |
[ |
| 53 |
("candidates", ()), |
| 54 |
("item_candidates", ()), |
| 55 |
("albums_for_ids", (["some_id"],)), |
| 56 |
("tracks_for_ids", (["some_id"],)), |
| 57 |
("album_for_id", ("some_id", "ErrorMetadataMock")), |
| 58 |
("track_for_id", ("some_id", "ErrorMetadataMock")), |
| 59 |
], |
| 60 |
) |
| 61 |
def test_logging(self, caplog, call_method, method_name): |
| 62 |
self.config["raise_on_error"] = False |
| 63 |
|
| 64 |
call_method() |
| 65 |
|
| 66 |
assert ( |
| 67 |
f"Error in 'ErrorMetadataMock.{method_name}': Mocked error" |
| 68 |
in caplog.text |
| 69 |
) |
| 70 |
|
| 71 |
@pytest.mark.parametrize( |
| 72 |
"method_name,args", |
| 73 |
[ |
| 74 |
("candidates", ()), |
| 75 |
("item_candidates", ()), |
| 76 |
("albums_for_ids", (["some_id"],)), |
| 77 |
("tracks_for_ids", (["some_id"],)), |
| 78 |
("album_for_id", ("some_id", "ErrorMetadataMock")), |
| 79 |
("track_for_id", ("some_id", "ErrorMetadataMock")), |
| 80 |
], |
| 81 |
) |
| 82 |
def test_raising(self, call_method): |
| 83 |
self.config["raise_on_error"] = True |
| 84 |
|
| 85 |
with pytest.raises(ValueError, match="Mocked error"): |
| 86 |
call_method() |
| 87 |
|
| 88 |
|
| 89 |
class TestSearchApiMetadataSourcePlugin(PluginMixin): |
| 90 |
plugin = "none" |
| 91 |
preload_plugin = False |
| 92 |
|
| 93 |
class RaisingSearchApiMetadataMockPlugin( |
| 94 |
metadata_plugins.SearchApiMetadataSourcePlugin[ |
| 95 |
metadata_plugins.IDResponse |
| 96 |
] |
| 97 |
): |
| 98 |
def get_search_query_with_filters(self, _): |
| 99 |
return "", {} |
| 100 |
|
| 101 |
def get_search_response(self, _): |
| 102 |
raise ValueError("Search failure") |
| 103 |
|
| 104 |
def album_for_id(self, _): |
| 105 |
return None |
| 106 |
|
| 107 |
def track_for_id(self, _): |
| 108 |
return None |
| 109 |
|
| 110 |
@pytest.fixture |
| 111 |
def search_plugin(self): |
| 112 |
return self.RaisingSearchApiMetadataMockPlugin() |
| 113 |
|
| 114 |
def test_search_api_returns_empty_when_raise_on_error_disabled( |
| 115 |
self, config, search_plugin, caplog |
| 116 |
): |
| 117 |
config["raise_on_error"] = False |
| 118 |
|
| 119 |
assert search_plugin._search_api("track", "query", {}) == () |
| 120 |
assert "Search failure" in caplog.text |
| 121 |
|
| 122 |
def test_search_api_raises_when_raise_on_error_enabled( |
| 123 |
self, config, search_plugin |
| 124 |
): |
| 125 |
config["raise_on_error"] = True |
| 126 |
|
| 127 |
with pytest.raises(ValueError, match="Search failure"): |
| 128 |
search_plugin._search_api("track", "query", {}) |
| 129 |
|
| 130 |
def test_search_api_skips_request_without_query_and_filters( |
| 131 |
self, config, search_plugin |
| 132 |
): |
| 133 |
"""Empty searches are rejected by APIs, so do not send them.""" |
| 134 |
config["raise_on_error"] = True |
| 135 |
|
| 136 |
assert search_plugin._search_api("track", "", {}) == () |
| 137 |
|
| 138 |
|
| 139 |
def test_albums_for_ids_calls_each_plugin_once(monkeypatch): |
| 140 |
start_workers = Event() |
| 141 |
|
| 142 |
class Plugin: |
| 143 |
def __init__(self, name): |
| 144 |
self.data_source = name |
| 145 |
self.calls: list[list[str]] = [] |
| 146 |
|
| 147 |
def albums_for_ids(self, ids): |
| 148 |
self.calls.append(list(ids)) |
| 149 |
return [self.data_source] |
| 150 |
|
| 151 |
plugins = [Plugin("discogs"), Plugin("musicbrainz"), Plugin("tidal")] |
| 152 |
|
| 153 |
class PluginSequence: |
| 154 |
def __iter__(self): |
| 155 |
yield from plugins |
| 156 |
start_workers.set() |
| 157 |
|
| 158 |
class DelayedStartExecutor: |
| 159 |
def __init__(self): |
| 160 |
self._executor = BaseThreadPoolExecutor(max_workers=1) |
| 161 |
|
| 162 |
def submit(self, fn, *args, **kwargs): |
| 163 |
return self._executor.submit(self._run, fn, args, kwargs) |
| 164 |
|
| 165 |
def _run(self, fn, args, kwargs): |
| 166 |
start_workers.wait() |
| 167 |
return fn(*args, **kwargs) |
| 168 |
|
| 169 |
def __enter__(self): |
| 170 |
return self |
| 171 |
|
| 172 |
def __exit__(self, exc_type, exc, tb): |
| 173 |
self._executor.shutdown(wait=True) |
| 174 |
return False |
| 175 |
|
| 176 |
monkeypatch.setattr( |
| 177 |
metadata_plugins, |
| 178 |
"find_metadata_source_plugins", |
| 179 |
lambda: PluginSequence(), |
| 180 |
) |
| 181 |
monkeypatch.setattr( |
| 182 |
metadata_plugins, "ThreadPoolExecutor", DelayedStartExecutor |
| 183 |
) |
| 184 |
|
| 185 |
assert set(metadata_plugins.albums_for_ids(["42"])) == { |
| 186 |
"discogs", |
| 187 |
"musicbrainz", |
| 188 |
"tidal", |
| 189 |
} |
| 190 |
assert [plugin.calls for plugin in plugins] == [ |
| 191 |
[["42"]], |
| 192 |
[["42"]], |
| 193 |
[["42"]], |
| 194 |
] |