| 1 |
"""Test the advancedrewrite plugin for various configurations.""" |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from beets.test.helper import PluginTestHelper |
| 6 |
from beets.ui import UserError |
| 7 |
|
| 8 |
|
| 9 |
class TestAdvancedRewritePlugin(PluginTestHelper): |
| 10 |
plugin = "advancedrewrite" |
| 11 |
preload_plugin = False |
| 12 |
|
| 13 |
def test_simple_rewrite_example(self): |
| 14 |
with self.configure_plugin( |
| 15 |
[{"artist ODD EYE CIRCLE": "이달의 소녀 오드아이써클"}] |
| 16 |
): |
| 17 |
item = self.add_item( |
| 18 |
artist="ODD EYE CIRCLE", albumartist="ODD EYE CIRCLE" |
| 19 |
) |
| 20 |
|
| 21 |
assert item.artist == "이달의 소녀 오드아이써클" |
| 22 |
|
| 23 |
def test_list_field(self): |
| 24 |
with self.configure_plugin([{"genres rock": "techno"}]): |
| 25 |
item = self.add_item(genres=["rock", "pop"]) |
| 26 |
|
| 27 |
assert item.genres == ["techno", "pop"] |
| 28 |
|
| 29 |
def test_advanced_rewrite_example(self): |
| 30 |
with self.configure_plugin( |
| 31 |
[ |
| 32 |
{ |
| 33 |
"match": "mb_artistid:dec0f331-cb08-4c8e-9c9f-aeb1f0f6d88c year:..2022", # noqa: E501 |
| 34 |
"replacements": { |
| 35 |
"artist": "이달의 소녀 오드아이써클", |
| 36 |
"artist_sort": "LOONA / ODD EYE CIRCLE", |
| 37 |
}, |
| 38 |
} |
| 39 |
] |
| 40 |
): |
| 41 |
item_a = self.add_item( |
| 42 |
artist="ODD EYE CIRCLE", |
| 43 |
artist_sort="ODD EYE CIRCLE", |
| 44 |
mb_artistid="dec0f331-cb08-4c8e-9c9f-aeb1f0f6d88c", |
| 45 |
year=2017, |
| 46 |
) |
| 47 |
item_b = self.add_item( |
| 48 |
artist="ODD EYE CIRCLE", |
| 49 |
artist_sort="ODD EYE CIRCLE", |
| 50 |
mb_artistid="dec0f331-cb08-4c8e-9c9f-aeb1f0f6d88c", |
| 51 |
year=2023, |
| 52 |
) |
| 53 |
|
| 54 |
# Assert that all replacements were applied to item_a |
| 55 |
assert "이달의 소녀 오드아이써클" == item_a.artist |
| 56 |
assert "LOONA / ODD EYE CIRCLE" == item_a.artist_sort |
| 57 |
assert "LOONA / ODD EYE CIRCLE" == item_a.albumartist_sort |
| 58 |
|
| 59 |
# Assert that no replacements were applied to item_b |
| 60 |
assert "ODD EYE CIRCLE" == item_b.artist |
| 61 |
|
| 62 |
def test_advanced_rewrite_example_with_multi_valued_field(self): |
| 63 |
with self.configure_plugin( |
| 64 |
[ |
| 65 |
{ |
| 66 |
"match": "artist:배유빈 feat. 김미현", |
| 67 |
"replacements": {"artists": ["유빈", "미미"]}, |
| 68 |
} |
| 69 |
] |
| 70 |
): |
| 71 |
item = self.add_item( |
| 72 |
artist="배유빈 feat. 김미현", artists=["배유빈", "김미현"] |
| 73 |
) |
| 74 |
|
| 75 |
assert item.artists == ["유빈", "미미"] |
| 76 |
|
| 77 |
def test_fail_when_replacements_empty(self): |
| 78 |
with ( |
| 79 |
pytest.raises( |
| 80 |
UserError, |
| 81 |
match="Advanced rewrites must have at least one replacement", |
| 82 |
), |
| 83 |
self.configure_plugin([{"match": "artist:A", "replacements": {}}]), |
| 84 |
): |
| 85 |
pass |
| 86 |
|
| 87 |
def test_fail_when_rewriting_single_valued_field_with_list(self): |
| 88 |
with ( |
| 89 |
pytest.raises( |
| 90 |
UserError, |
| 91 |
match="Field artist is not a multi-valued field but a list was given: C, D", # noqa: E501 |
| 92 |
), |
| 93 |
self.configure_plugin( |
| 94 |
[ |
| 95 |
{ |
| 96 |
"match": "artist:'A & B'", |
| 97 |
"replacements": {"artist": ["C", "D"]}, |
| 98 |
} |
| 99 |
] |
| 100 |
), |
| 101 |
): |
| 102 |
pass |
| 103 |
|
| 104 |
def test_combined_rewrite_example(self): |
| 105 |
with self.configure_plugin( |
| 106 |
[ |
| 107 |
{"artist A": "B"}, |
| 108 |
{"match": "album:'C'", "replacements": {"artist": "D"}}, |
| 109 |
] |
| 110 |
): |
| 111 |
item = self.add_item(artist="A", albumartist="A") |
| 112 |
assert item.artist == "B" |
| 113 |
|
| 114 |
item = self.add_item(artist="C", albumartist="C", album="C") |
| 115 |
assert item.artist == "D" |