diff --git a/test/plugins/test_lastgenre.py b/test/plugins/test_lastgenre.py index 5f905e8e53..90e9c28a9c 100644 --- a/test/plugins/test_lastgenre.py +++ b/test/plugins/test_lastgenre.py @@ -16,6 +16,8 @@ from unittest.mock import Mock +import pytest + from beets import config from beets.test import _common from beets.test.helper import BeetsTestCase @@ -163,77 +165,7 @@ def get_top_tags(self): res = plugin._tags_for(MockPylastObj(), min_weight=50) assert res == ["pop"] - def test_get_genre(self): - """All possible (genre, label) pairs""" - mock_genres = {"track": "1", "album": "2", "artist": "3"} - - def mock_fetch_track_genre(self, obj=None): - return mock_genres["track"] - - def mock_fetch_album_genre(self, obj): - return mock_genres["album"] - - def mock_fetch_artist_genre(self, obj): - return mock_genres["artist"] - - lastgenre.LastGenrePlugin.fetch_track_genre = mock_fetch_track_genre - lastgenre.LastGenrePlugin.fetch_album_genre = mock_fetch_album_genre - lastgenre.LastGenrePlugin.fetch_artist_genre = mock_fetch_artist_genre - - self._setup_config(whitelist=False) - item = _common.item() - item.genre = mock_genres["track"] - - # The default setting - config["lastgenre"] = {"force": False, "keep_allowed": True} - res = self.plugin._get_genre(item) - assert res == (item.genre, "keep allowed") - - # Not forcing and keeping any existing - config["lastgenre"] = {"force": False, "keep_allowed": False} - res = self.plugin._get_genre(item) - assert res == (item.genre, "keep any") - - # Track - config["lastgenre"] = {"force": True, "keep_allowed": False, "source": "track"} - res = self.plugin._get_genre(item) - assert res == (mock_genres["track"], "track") - - config["lastgenre"] = {"force": True, "keep_allowed": True, "source": "track"} - res = self.plugin._get_genre(item) - assert res == (mock_genres["track"], "keep + track") - print("res after track check:", res) - - # Album - config["lastgenre"] = {"source": "album", "keep_allowed": False} - res = self.plugin._get_genre(item) - print("res is:", res) - print("mock_genres is:", mock_genres["album"]) - assert res == (mock_genres["album"], "album") - - config["lastgenre"] = {"source": "album", "keep_allowed": True} - res = self.plugin._get_genre(item) - assert res == (mock_genres["album"], "keep + album") - - # Artist - config["lastgenre"] = {"source": "artist", "keep_allowed": False} - res = self.plugin._get_genre(item) - assert res == (mock_genres["artist"], "artist") - - config["lastgenre"] = {"source": "artist", "keep_allowed": True} - res = self.plugin._get_genre(item) - assert res == (mock_genres["artist"], "keep + artist") - - # Original - mock_genres["artist"] = None - res = self.plugin._get_genre(item) - assert res == (item.genre, "original") - - # Fallback - config["lastgenre"] = {"fallback": "rap", "keep_allowed": False} - item.genre = None - res = self.plugin._get_genre(item) - assert res == (config["lastgenre"]["fallback"].get(), "fallback") + import pytest def test_sort_by_depth(self): self._setup_config(canonical=True) @@ -245,3 +177,205 @@ def test_sort_by_depth(self): tags = ("electronic", "ambient", "chillout") res = self.plugin._sort_by_depth(tags) assert res == ["ambient", "electronic"] + +@pytest.mark.parametrize( + "config_values, item_genre, mock_genres, expected_result", + [ + # 0 - The default setting + ( + { + "force": False, + "keep_allowed": True, + "source": "album", + "whitelist": True, + }, + "ORIGINAL TRACK GENRE", + { + "track": "LAST TRACK GENRE", + "album": "LAST ALBUM GENRE", + "artist": "LAST ARTIST GENRE", + }, + # Only empty existing tags will get a last.fm genre; This one had a + # non-whitelisted existing genre which corretly gets cleared. This + # might not be a desired outcome but is expected behaviour! + ("", "keep allowed"), + ), + # 1 - Not forcing and keeping any existing + ( + { + "force": False, + "keep_allowed": False, + "source": "album", + "whitelist": True, + }, + "SOMETHING EXISTING", + { + "track": "LAST TRACK GENRE", + "album": "LAST ALBUM GENRE", + "artist": "LAST ARTIST GENRE", + }, + ("SOMETHING EXISTING", "keep any"), + ), + # 2 - Track scenarios + ( + { + "force": True, + "keep_allowed": False, + "source": "track", + "whitelist": True, + }, + "ALLOWED GENRE ONE, ALLOWED GENRE TWO", + { + "track": "LAST TRACK GENRE", + "album": "LAST ALBUM GENRE", + "artist": "LAST ARTIST GENRE", + }, + ("LAST TRACK GENRE", "track"), + ), + # 3 - Force and keep whitelisted + ( + { + "force": True, + "keep_allowed": True, + "source": "track", + "whitelist": True, + }, + "ALLOWED GENRE ONE", + { + "track": "ALLOWED GENRE TWO", + "album": "LAST ALBUM GENRE", + "artist": "LAST ARTIST GENRE", + }, + ("ALLOWED GENRE TWO", "keep + track"), + ), + # 4 - Album scenarios + ( + { + "force": True, + "keep_allowed": False, + "source": "album", + "whitelist": True, + }, + "LAST TRACK GENRE", + { + "track": "LAST TRACK GENRE", + "album": "LAST ALBUM GENRE", + "artist": "LAST ARTIST GENRE", + }, + ("LAST ALBUM GENRE", "album"), + ), + ( + { + "force": True, + "keep_allowed": True, + "source": "album", + "whitelist": True, + }, + "LAST TRACK GENRE", + { + "track": "LAST TRACK GENRE", + "album": "LAST ALBUM GENRE", + "artist": "LAST ARTIST GENRE", + }, + ("LAST ALBUM GENRE", "keep + album"), + ), + # Artist scenarios + ( + { + "force": True, + "keep_allowed": False, + "source": "artist", + "whitelist": True, + }, + "ORIGINAL TRACK GENRE", + { + "track": "LAST TRACK GENRE", + "album": "LAST ALBUM GENRE", + "artist": "LAST ARTIST GENRE", + }, + ("LAST ARTIST GENRE", "artist"), + ), + ( + { + "force": True, + "keep_allowed": True, + "source": "artist", + "whitelist": True, + }, + "ORIGINAL TRACK GENRE, ALLOWED GENRE ONE", + { + "track": "LAST TRACK GENRE", + "album": "LAST ALBUM GENRE", + "artist": "LAST ARTIST GENRE", + }, + ("LAST ARTIST GENRE, ALLOWED GENRE ONE", "keep + artist"), + ), + # Original genre + ( + { + "force": False, + "keep_allowed": False, + "source": "album", + "whitelist": True, + }, + "ORIGINAL TRACK GENRE", + { + "track": "LAST TRACK GENRE", + "album": "LAST ALBUM GENRE", + "artist": None, + }, + ("ORIGINAL TRACK GENRE", "original"), + ), + # Fallback genre + ( + { + "fallback": "FALLBACK GENRE", + "force": False, + "keep_allowed": False, + "whitelist": True, + }, + "ORIGINAL TRACK GENRE", + { + "track": "", + "album": "", + "artist": "", + }, + ("FALLBACK GENRE", "fallback"), + ), + ], +) +def test_get_genre(config_values, item_genre, mock_genres, expected_result): + """Test _get_genre with various configurations.""" + + def mock_fetch_track_genre(self, obj=None): + return mock_genres["track"] + + def mock_fetch_album_genre(self, obj): + return mock_genres["album"] + + def mock_fetch_artist_genre(self, obj): + return mock_genres["artist"] + + # Mock methods + lastgenre.LastGenrePlugin.fetch_track_genre = mock_fetch_track_genre + lastgenre.LastGenrePlugin.fetch_album_genre = mock_fetch_album_genre + lastgenre.LastGenrePlugin.fetch_artist_genre = mock_fetch_artist_genre + + # Initialize plugin instance and item + plugin = lastgenre.LastGenrePlugin() + item = _common.item() + item.genre = item_genre + + # Set configuration + config["lastgenre"] = config_values + + # Mock the whitelist instance variable with a list + plugin.whitelist = set([ + "ALLOWED GENRE ONE", + "ALLOWED GENRE TWO", + ]) if config_values.get("whitelist") else set([]) + + # Run the test and check the result + res = plugin._get_genre(item) + print(res) + assert res == expected_result