Debian Codegraph @

test/plugins/test_chroma.py

raw
1 from unittest.mock import MagicMock, patch
2
3 import pytest
4
5 from beets import metadata_plugins
6 from beets.autotag import AlbumInfo, TrackInfo
7 from beets.library import Item
8 from beets.test.helper import ImportHelper, IOMixin, PluginMixin
9
10 chroma = pytest.importorskip("beetsplug.chroma", exc_type=ImportError)
11
12 TEST_TITLE_1 = "TEST_TITLE_1"
13 TEST_TITLE_2 = "TEST_TITLE_2"
14 FINGERPRINT_1 = "FP_1"
15 FINGERPRINT_1_CLOSE = "FP_1_CLOSE"
16 FINGERPRINT_2 = "FP_2"
17
18
19 @patch("acoustid.compare_fingerprints")
20 class TestChroma(IOMixin, PluginMixin, ImportHelper):
21 plugin = "chroma"
22
23 def setup_lib(self):
24 item1 = Item(path="/file")
25 item1.length = 30
26 item1.title = TEST_TITLE_1
27 item1.acoustid_fingerprint = FINGERPRINT_1
28 item1.add(self.lib)
29
30 item2 = Item(path="/file")
31 item2.length = 30
32 item2.title = TEST_TITLE_2
33 item2.acoustid_fingerprint = FINGERPRINT_2
34 item2.add(self.lib)
35
36 def run_search(self, fp):
37 return self.run_with_output("chromasearch", "-s", fp, "-f", "$title")
38
39 def line_count(self, str_):
40 return len(
41 [line for line in str_.split("\n") if line.strip(" \n") != ""]
42 )
43
44 def compare_fingerprints(self, *args, **kwargs):
45 if args[0][1] == args[1][1]:
46 return 1
47
48 if args[0][1] == FINGERPRINT_1_CLOSE and args[1][1] == FINGERPRINT_1:
49 return 0.9
50
51 return 0.1
52
53 def test_chroma_search_exact(self, compare_fingerprints):
54 self.setup_lib()
55 compare_fingerprints.side_effect = self.compare_fingerprints
56
57 output = self.run_search(FINGERPRINT_2)
58 assert self.line_count(output) == 1
59 assert TEST_TITLE_2 in output
60
61 output = self.run_search(FINGERPRINT_1)
62 assert self.line_count(output) == 1
63 assert TEST_TITLE_1 in output
64
65 def test_chroma_search_close(self, compare_fingerprints):
66 self.setup_lib()
67 compare_fingerprints.side_effect = self.compare_fingerprints
68
69 output = self.run_search(FINGERPRINT_1_CLOSE)
70 assert self.line_count(output) == 2
71 assert TEST_TITLE_1 in output.split("\n")[0]
72
73
74 def _seed_acoustid_match(item_path: bytes = b"/fake/path.mp3") -> Item:
75 """Seed the chroma module-level match cache as if acoustid had run."""
76 chroma._matches[item_path] = (
77 ["rec-id-1"],
78 ["rel-id-1", "rel-id-1", "rel-id-1"],
79 )
80 return Item(path=item_path)
81
82
83 class TestChromaCandidates(PluginMixin):
84 """Regression tests for issue #6212: chroma must respect which metadata
85 source plugins are enabled.
86
87 When the musicbrainz plugin is not loaded, chroma must not produce any
88 MusicBrainz-sourced candidates (via either ``candidates`` or
89 ``item_candidates``). When it IS loaded, chroma resolves acoustid
90 matches through the registered plugin instance.
91
92 ``plugin`` is intentionally not set on the class so that
93 :py:meth:`PluginMixin.load_plugins` honours explicit plugin-name
94 arguments and each test can choose its own combination. The autouse
95 fixture clears the ``@cache``-decorated metadata-source registry and
96 the chroma match state between tests.
97 """
98
99 preload_plugin = False
100
101 @pytest.fixture(autouse=True)
102 def _setup_chroma(self):
103 metadata_plugins.find_metadata_source_plugins.cache_clear()
104 metadata_plugins.get_metadata_source.cache_clear()
105 chroma._matches.clear()
106 yield
107 chroma._matches.clear()
108 self.unload_plugins()
109 metadata_plugins.find_metadata_source_plugins.cache_clear()
110 metadata_plugins.get_metadata_source.cache_clear()
111
112 def test_candidates_returns_empty_without_musicbrainz(self):
113 self.load_plugins("chroma")
114 plugin = chroma.AcoustidPlugin()
115 item = _seed_acoustid_match()
116
117 result = plugin.candidates(
118 [item], artist="A", album="B", va_likely=False
119 )
120
121 assert list(result) == []
122
123 def test_item_candidates_returns_empty_without_musicbrainz(self):
124 self.load_plugins("chroma")
125 plugin = chroma.AcoustidPlugin()
126 item = _seed_acoustid_match()
127
128 result = plugin.item_candidates(item, artist="A", title="B")
129
130 assert list(result) == []
131
132 def test_candidates_returns_mb_albums_with_musicbrainz(self, monkeypatch):
133 self.load_plugins("chroma", "musicbrainz")
134
135 fake_album = AlbumInfo(
136 tracks=[], album_id="rel-id-1", album="Fake Album"
137 )
138 mb_plugin = metadata_plugins.get_metadata_source("musicbrainz")
139 assert mb_plugin is not None
140 monkeypatch.setattr(
141 mb_plugin, "album_for_id", MagicMock(return_value=fake_album)
142 )
143
144 plugin = chroma.AcoustidPlugin()
145 item = _seed_acoustid_match()
146
147 result = list(
148 plugin.candidates([item], artist="A", album="B", va_likely=False)
149 )
150
151 assert result == [fake_album]
152 mb_plugin.album_for_id.assert_called_with("rel-id-1")
153
154 def test_item_candidates_returns_mb_tracks_with_musicbrainz(
155 self, monkeypatch
156 ):
157 self.load_plugins("chroma", "musicbrainz")
158
159 fake_track = TrackInfo(title="Fake Track", track_id="rec-id-1")
160 mb_plugin = metadata_plugins.get_metadata_source("musicbrainz")
161 assert mb_plugin is not None
162 monkeypatch.setattr(
163 mb_plugin, "track_for_id", MagicMock(return_value=fake_track)
164 )
165
166 plugin = chroma.AcoustidPlugin()
167 item = _seed_acoustid_match()
168
169 result = list(plugin.item_candidates(item, artist="A", title="B"))
170
171 assert result == [fake_track]
172 mb_plugin.track_for_id.assert_called_with("rec-id-1")