diff --git a/test/plugins/test_rewrite.py b/test/plugins/test_rewrite.py new file mode 100644 index 000000000..91cc02fcb --- /dev/null +++ b/test/plugins/test_rewrite.py @@ -0,0 +1,67 @@ +import pytest + +from beets.test.helper import PluginTestCase +from beets.ui import UserError +from beetsplug.rewrite import RewritePlugin + + +class RewritePluginTest(PluginTestCase): + plugin = "rewrite" + preload_plugin = False + + def test_artist_rewrite_applies_to_artist_albumartist_and_album_fields( + self, + ): + with self.configure_plugin({"artist .*jimi hendrix.*": "Jimi Hendrix"}): + item = self.add_item( + artist="The Jimi Hendrix Experience", + albumartist="The Jimi Hendrix Experience", + ) + album = self.lib.add_album([item]) + + assert item.artist == "Jimi Hendrix" + assert item.albumartist == "Jimi Hendrix" + assert album.evaluate_template("$albumartist") == "Jimi Hendrix" + + def test_rewrite_uses_first_matching_rule(self): + with self.configure_plugin( + { + "artist .*hendrix.*": "Hendrix catalog", + "artist the jimi hendrix experience": "Experience catalog", + } + ): + item = self.add_item( + artist="The Jimi Hendrix Experience", + albumartist="The Jimi Hendrix Experience", + ) + + assert item.artist == "Hendrix catalog" + + def test_rewrite_is_case_insensitive_and_leaves_non_matches_unchanged( + self, + ): + with self.configure_plugin( + {"artist odd eye circle": "LOONA / ODD EYE CIRCLE"} + ): + matching_item = self.add_item( + artist="ODD EYE CIRCLE", + albumartist="ODD EYE CIRCLE", + ) + other_item = self.add_item(artist="ARTMS", albumartist="ARTMS") + + assert matching_item.artist == "LOONA / ODD EYE CIRCLE" + assert other_item.artist == "ARTMS" + + def test_invalid_rewrite_spec_raises_user_error(self): + self.config[self.plugin].set({"artist": "Jimi Hendrix"}) + + with pytest.raises(UserError, match="invalid rewrite specification"): + RewritePlugin() + + def test_invalid_field_name_raises_user_error(self): + self.config[self.plugin].set({"not_a_field rock": "Classic Rock"}) + + with pytest.raises( + UserError, match="invalid field name \\(not_a_field\\) in rewriter" + ): + RewritePlugin()