| 1 |
import pytest |
| 2 |
|
| 3 |
from beets.test.helper import PluginTestHelper |
| 4 |
from beets.ui import UserError |
| 5 |
from beetsplug.rewrite import RewritePlugin |
| 6 |
|
| 7 |
|
| 8 |
class TestRewritePlugin(PluginTestHelper): |
| 9 |
plugin = "rewrite" |
| 10 |
preload_plugin = False |
| 11 |
|
| 12 |
def test_artist_rewrite_applies_to_artist_albumartist_and_album_fields( |
| 13 |
self, |
| 14 |
): |
| 15 |
with self.configure_plugin({"artist .*jimi hendrix.*": "Jimi Hendrix"}): |
| 16 |
item = self.add_item( |
| 17 |
artist="The Jimi Hendrix Experience", |
| 18 |
albumartist="The Jimi Hendrix Experience", |
| 19 |
) |
| 20 |
album = self.lib.add_album([item]) |
| 21 |
|
| 22 |
assert item.artist == "Jimi Hendrix" |
| 23 |
assert item.albumartist == "Jimi Hendrix" |
| 24 |
assert album.evaluate_template("$albumartist") == "Jimi Hendrix" |
| 25 |
|
| 26 |
def test_rewrite_all_matching_rules(self): |
| 27 |
with self.configure_plugin( |
| 28 |
{ |
| 29 |
"artist .*hendrix.*": "hendrix catalog", |
| 30 |
"artist .*catalog.*": "Experience catalog", |
| 31 |
} |
| 32 |
): |
| 33 |
item = self.add_item( |
| 34 |
artist="The Jimi Hendrix Experience", |
| 35 |
albumartist="The Jimi Hendrix Experience", |
| 36 |
) |
| 37 |
|
| 38 |
assert item.artist == "Experience catalog" |
| 39 |
|
| 40 |
def test_rewrite_is_case_insensitive_and_leaves_non_matches_unchanged(self): |
| 41 |
with self.configure_plugin( |
| 42 |
{"artist odd eye circle": "LOONA / ODD EYE CIRCLE"} |
| 43 |
): |
| 44 |
matching_item = self.add_item( |
| 45 |
artist="ODD EYE CIRCLE", albumartist="ODD EYE CIRCLE" |
| 46 |
) |
| 47 |
other_item = self.add_item(artist="ARTMS", albumartist="ARTMS") |
| 48 |
|
| 49 |
assert matching_item.artist == "LOONA / ODD EYE CIRCLE" |
| 50 |
assert other_item.artist == "ARTMS" |
| 51 |
|
| 52 |
def test_rewrite_applied_to_all_list_values(self): |
| 53 |
with self.configure_plugin( |
| 54 |
{"genres rock": "Classic Rock", "genres pop": "Pop"} |
| 55 |
): |
| 56 |
item = self.add_item(genres=["rock", "pop", "techno"]) |
| 57 |
|
| 58 |
assert item.genres == ["Classic Rock", "Pop", "techno"] |
| 59 |
|
| 60 |
def test_invalid_rewrite_spec_raises_user_error(self): |
| 61 |
self.config[self.plugin].set({"artist": "Jimi Hendrix"}) |
| 62 |
|
| 63 |
with pytest.raises(UserError, match="invalid rewrite specification"): |
| 64 |
RewritePlugin() |
| 65 |
|
| 66 |
def test_invalid_field_name_raises_user_error(self): |
| 67 |
self.config[self.plugin].set({"not_a_field rock": "Classic Rock"}) |
| 68 |
|
| 69 |
with pytest.raises( |
| 70 |
UserError, match="invalid field name \\(not_a_field\\) in rewriter" |
| 71 |
): |
| 72 |
RewritePlugin() |