| 1 |
"""Tests for the 'titlecase' plugin""" |
| 2 |
|
| 3 |
from unittest.mock import patch |
| 4 |
|
| 5 |
from beets.autotag import AlbumInfo, TrackInfo |
| 6 |
from beets.importer import ImportSession, ImportTask |
| 7 |
from beets.library import Item |
| 8 |
from beets.test.helper import PluginTestCase |
| 9 |
from beetsplug.titlecase import TitlecasePlugin |
| 10 |
|
| 11 |
|
| 12 |
class TestTitlecasePlugin(PluginTestCase): |
| 13 |
plugin = "titlecase" |
| 14 |
preload_plugin = False |
| 15 |
|
| 16 |
def test_auto(self): |
| 17 |
"""Ensure automatic processing gets assigned""" |
| 18 |
with self.configure_plugin({"auto": True, "after_choice": True}): |
| 19 |
assert callable(TitlecasePlugin().import_stages[0]) |
| 20 |
with self.configure_plugin({"auto": False, "after_choice": False}): |
| 21 |
assert len(TitlecasePlugin().import_stages) == 0 |
| 22 |
with self.configure_plugin({"auto": False, "after_choice": True}): |
| 23 |
assert len(TitlecasePlugin().import_stages) == 0 |
| 24 |
|
| 25 |
def test_basic_titlecase(self): |
| 26 |
"""Check that default behavior is as expected.""" |
| 27 |
testcases = [ |
| 28 |
("a", "A"), |
| 29 |
("PENDULUM", "Pendulum"), |
| 30 |
("Aaron-carl", "Aaron-Carl"), |
| 31 |
("LTJ bukem", "LTJ Bukem"), |
| 32 |
("(original mix)", "(Original Mix)"), |
| 33 |
("ALL CAPS TITLE", "All Caps Title"), |
| 34 |
] |
| 35 |
for testcase in testcases: |
| 36 |
given, expected = testcase |
| 37 |
assert TitlecasePlugin().titlecase(given) == expected |
| 38 |
|
| 39 |
def test_small_first_last(self): |
| 40 |
"""Check the behavior for supporting small first last""" |
| 41 |
testcases = [ |
| 42 |
(True, "In a Silent Way", "In a Silent Way"), |
| 43 |
(False, "In a Silent Way", "in a Silent Way"), |
| 44 |
] |
| 45 |
for testcase in testcases: |
| 46 |
sfl, given, expected = testcase |
| 47 |
cfg = {"small_first_last": sfl} |
| 48 |
with self.configure_plugin(cfg): |
| 49 |
assert TitlecasePlugin().titlecase(given) == expected |
| 50 |
|
| 51 |
def test_preserve(self): |
| 52 |
"""Test using given strings to preserve case""" |
| 53 |
preserve_list = [ |
| 54 |
"easyFun", |
| 55 |
"A.D.O.R", |
| 56 |
"D'Angelo", |
| 57 |
"ABBA", |
| 58 |
"LaTeX", |
| 59 |
"O.R.B", |
| 60 |
"PinkPantheress", |
| 61 |
"THE PSYCHIC ED RUSH", |
| 62 |
"LTJ Bukem", |
| 63 |
] |
| 64 |
for word in preserve_list: |
| 65 |
with self.configure_plugin({"preserve": preserve_list}): |
| 66 |
assert TitlecasePlugin().titlecase(word.upper()) == word |
| 67 |
assert TitlecasePlugin().titlecase(word.lower()) == word |
| 68 |
|
| 69 |
def test_separators(self): |
| 70 |
testcases = [ |
| 71 |
([], "it / a / in / of / to / the", "It / a / in / of / to / The"), |
| 72 |
(["/"], "it / the test", "It / The Test"), |
| 73 |
( |
| 74 |
["/"], |
| 75 |
"it / a / in / of / to / the", |
| 76 |
"It / A / In / Of / To / The", |
| 77 |
), |
| 78 |
(["/"], "//it/a/in/of/to/the", "//It/A/In/Of/To/The"), |
| 79 |
( |
| 80 |
["/", ";", "|"], |
| 81 |
"it ; a / in | of / to | the", |
| 82 |
"It ; A / In | Of / To | The", |
| 83 |
), |
| 84 |
] |
| 85 |
for testcase in testcases: |
| 86 |
separators, given, expected = testcase |
| 87 |
with self.configure_plugin({"separators": separators}): |
| 88 |
assert TitlecasePlugin().titlecase(given) == expected |
| 89 |
|
| 90 |
def test_all_caps(self): |
| 91 |
testcases = [ |
| 92 |
(True, "Unaffected", "Unaffected"), |
| 93 |
(True, "RBMK1000", "RBMK1000"), |
| 94 |
(False, "RBMK1000", "Rbmk1000"), |
| 95 |
(True, "P A R I S!", "P A R I S!"), |
| 96 |
(True, "pillow dub...", "Pillow Dub..."), |
| 97 |
(False, "P A R I S!", "P a R I S!"), |
| 98 |
] |
| 99 |
for testcase in testcases: |
| 100 |
all_caps, given, expected = testcase |
| 101 |
with self.configure_plugin({"all_caps": all_caps}): |
| 102 |
assert TitlecasePlugin().titlecase(given) == expected |
| 103 |
|
| 104 |
def test_all_lowercase(self): |
| 105 |
testcases = [ |
| 106 |
(True, "Unaffected", "Unaffected"), |
| 107 |
(True, "RBMK1000", "Rbmk1000"), |
| 108 |
(True, "pillow dub...", "pillow dub..."), |
| 109 |
(False, "pillow dub...", "Pillow Dub..."), |
| 110 |
] |
| 111 |
for testcase in testcases: |
| 112 |
all_lowercase, given, expected = testcase |
| 113 |
with self.configure_plugin({"all_lowercase": all_lowercase}): |
| 114 |
assert TitlecasePlugin().titlecase(given) == expected |
| 115 |
|
| 116 |
def test_received_info_handler(self): |
| 117 |
testcases = [ |
| 118 |
( |
| 119 |
TrackInfo( |
| 120 |
album="test album", |
| 121 |
artist_credit="test artist credit", |
| 122 |
artists=["artist one", "artist two"], |
| 123 |
), |
| 124 |
TrackInfo( |
| 125 |
album="Test Album", |
| 126 |
artist_credit="Test Artist Credit", |
| 127 |
artists=["Artist One", "Artist Two"], |
| 128 |
), |
| 129 |
), |
| 130 |
( |
| 131 |
AlbumInfo( |
| 132 |
tracks=[ |
| 133 |
TrackInfo( |
| 134 |
album="test album", |
| 135 |
artist_credit="test artist credit", |
| 136 |
artists=["artist one", "artist two"], |
| 137 |
) |
| 138 |
], |
| 139 |
album="test album", |
| 140 |
artist_credit="test artist credit", |
| 141 |
artists=["artist one", "artist two"], |
| 142 |
), |
| 143 |
AlbumInfo( |
| 144 |
tracks=[ |
| 145 |
TrackInfo( |
| 146 |
album="Test Album", |
| 147 |
artist_credit="Test Artist Credit", |
| 148 |
artists=["Artist One", "Artist Two"], |
| 149 |
) |
| 150 |
], |
| 151 |
album="Test Album", |
| 152 |
artist_credit="Test Artist Credit", |
| 153 |
artists=["Artist One", "Artist Two"], |
| 154 |
), |
| 155 |
), |
| 156 |
] |
| 157 |
cfg = {"fields": ["album", "artist_credit", "artists"]} |
| 158 |
for testcase in testcases: |
| 159 |
given, expected = testcase |
| 160 |
with self.configure_plugin(cfg): |
| 161 |
TitlecasePlugin().received_info_handler(given) |
| 162 |
assert given == expected |
| 163 |
|
| 164 |
def test_titlecase_fields(self): |
| 165 |
testcases = [ |
| 166 |
# Test with preserve, replace, and mb_albumid |
| 167 |
# Test with the_artist |
| 168 |
( |
| 169 |
{ |
| 170 |
"preserve": ["D'Angelo"], |
| 171 |
"replace": [("’", "'")], |
| 172 |
"fields": ["artist", "albumartist", "mb_albumid"], |
| 173 |
}, |
| 174 |
Item( |
| 175 |
artist="d’angelo and the vanguard", |
| 176 |
mb_albumid="ab140e13-7b36-402a-a528-b69e3dee38a8", |
| 177 |
albumartist="d’angelo", |
| 178 |
format="CD", |
| 179 |
album="the black messiah", |
| 180 |
title="Till It's Done (Tutu)", |
| 181 |
), |
| 182 |
Item( |
| 183 |
artist="D'Angelo and The Vanguard", |
| 184 |
mb_albumid="Ab140e13-7b36-402a-A528-B69e3dee38a8", |
| 185 |
albumartist="D'Angelo", |
| 186 |
format="CD", |
| 187 |
album="the black messiah", |
| 188 |
title="Till It's Done (Tutu)", |
| 189 |
), |
| 190 |
), |
| 191 |
# Test with force_lowercase, preserve, and an incorrect field |
| 192 |
( |
| 193 |
{ |
| 194 |
"force_lowercase": True, |
| 195 |
"fields": [ |
| 196 |
"artist", |
| 197 |
"albumartist", |
| 198 |
"format", |
| 199 |
"title", |
| 200 |
"year", |
| 201 |
"label", |
| 202 |
"format", |
| 203 |
"INCORRECT_FIELD", |
| 204 |
], |
| 205 |
"preserve": ["CD"], |
| 206 |
}, |
| 207 |
Item( |
| 208 |
artist="OPHIDIAN", |
| 209 |
albumartist="OphiDIAN", |
| 210 |
format="cd", |
| 211 |
year=2003, |
| 212 |
album="BLACKBOX", |
| 213 |
title="KhAmElEoN", |
| 214 |
label="enzyme records", |
| 215 |
), |
| 216 |
Item( |
| 217 |
artist="Ophidian", |
| 218 |
albumartist="Ophidian", |
| 219 |
format="CD", |
| 220 |
year=2003, |
| 221 |
album="Blackbox", |
| 222 |
title="Khameleon", |
| 223 |
label="Enzyme Records", |
| 224 |
), |
| 225 |
), |
| 226 |
# Test with no changes |
| 227 |
( |
| 228 |
{ |
| 229 |
"fields": [ |
| 230 |
"artist", |
| 231 |
"artists", |
| 232 |
"albumartist", |
| 233 |
"format", |
| 234 |
"title", |
| 235 |
"year", |
| 236 |
"label", |
| 237 |
"format", |
| 238 |
"INCORRECT_FIELD", |
| 239 |
], |
| 240 |
"preserve": ["CD"], |
| 241 |
}, |
| 242 |
Item( |
| 243 |
artist="Ophidian", |
| 244 |
artists=["Ophidian"], |
| 245 |
albumartist="Ophidian", |
| 246 |
format="CD", |
| 247 |
year=2003, |
| 248 |
album="Blackbox", |
| 249 |
title="Khameleon", |
| 250 |
label="Enzyme Records", |
| 251 |
), |
| 252 |
Item( |
| 253 |
artist="Ophidian", |
| 254 |
artists=["Ophidian"], |
| 255 |
albumartist="Ophidian", |
| 256 |
format="CD", |
| 257 |
year=2003, |
| 258 |
album="Blackbox", |
| 259 |
title="Khameleon", |
| 260 |
label="Enzyme Records", |
| 261 |
), |
| 262 |
), |
| 263 |
# Test with the_artist disabled |
| 264 |
( |
| 265 |
{"the_artist": False, "fields": ["artist", "artists_sort"]}, |
| 266 |
Item(artists_sort=["b-52s, the"], artist="a day in the park"), |
| 267 |
Item(artists_sort=["B-52s, The"], artist="A Day in the Park"), |
| 268 |
), |
| 269 |
# Test to make sure preserve and the_artist |
| 270 |
# dont target the middle of sentences |
| 271 |
# show that The artist applies to any field |
| 272 |
# with artist mentioned |
| 273 |
( |
| 274 |
{ |
| 275 |
"preserve": ["PANTHER"], |
| 276 |
"fields": ["artist", "artists", "artists_ids"], |
| 277 |
}, |
| 278 |
Item( |
| 279 |
artist="pinkpantheress", |
| 280 |
artists=["pinkpantheress", "artist_two"], |
| 281 |
artists_ids=["the the", "the the"], |
| 282 |
), |
| 283 |
Item( |
| 284 |
artist="Pinkpantheress", |
| 285 |
artists=["Pinkpantheress", "Artist_two"], |
| 286 |
artists_ids=["The The", "The The"], |
| 287 |
), |
| 288 |
), |
| 289 |
] |
| 290 |
for testcase in testcases: |
| 291 |
cfg, given, expected = testcase |
| 292 |
with self.configure_plugin(cfg): |
| 293 |
TitlecasePlugin().titlecase_fields(given) |
| 294 |
assert given.artist == expected.artist |
| 295 |
assert given.artists == expected.artists |
| 296 |
assert given.artists_sort == expected.artists_sort |
| 297 |
assert given.albumartist == expected.albumartist |
| 298 |
assert given.artists_ids == expected.artists_ids |
| 299 |
assert given.format == expected.format |
| 300 |
assert given.year == expected.year |
| 301 |
assert given.title == expected.title |
| 302 |
assert given.label == expected.label |
| 303 |
|
| 304 |
def test_cli_write(self): |
| 305 |
given = Item( |
| 306 |
album="retrodelica 2: back 2 the future", |
| 307 |
artist="blue planet corporation", |
| 308 |
title="generator", |
| 309 |
) |
| 310 |
expected = Item( |
| 311 |
album="Retrodelica 2: Back 2 the Future", |
| 312 |
artist="Blue Planet Corporation", |
| 313 |
title="Generator", |
| 314 |
) |
| 315 |
cfg = {"fields": ["album", "artist", "title"]} |
| 316 |
with self.configure_plugin(cfg): |
| 317 |
given.add(self.lib) |
| 318 |
self.run_command("titlecase") |
| 319 |
assert self.lib.items().get().artist == expected.artist |
| 320 |
assert self.lib.items().get().album == expected.album |
| 321 |
assert self.lib.items().get().title == expected.title |
| 322 |
self.lib.items().get().remove() |
| 323 |
|
| 324 |
def test_cli_no_write(self): |
| 325 |
given = Item( |
| 326 |
album="retrodelica 2: back 2 the future", |
| 327 |
artist="blue planet corporation", |
| 328 |
title="generator", |
| 329 |
) |
| 330 |
expected = Item( |
| 331 |
album="retrodelica 2: back 2 the future", |
| 332 |
artist="blue planet corporation", |
| 333 |
title="generator", |
| 334 |
) |
| 335 |
cfg = {"fields": ["album", "artist", "title"]} |
| 336 |
with self.configure_plugin(cfg): |
| 337 |
given.add(self.lib) |
| 338 |
self.run_command("-p", "titlecase") |
| 339 |
assert self.lib.items().get().artist == expected.artist |
| 340 |
assert self.lib.items().get().album == expected.album |
| 341 |
assert self.lib.items().get().title == expected.title |
| 342 |
self.lib.items().get().remove() |
| 343 |
|
| 344 |
def test_imported(self): |
| 345 |
given = Item( |
| 346 |
album="retrodelica 2: back 2 the future", |
| 347 |
artist="blue planet corporation", |
| 348 |
title="generator", |
| 349 |
) |
| 350 |
expected = Item( |
| 351 |
album="Retrodelica 2: Back 2 the Future", |
| 352 |
artist="Blue Planet Corporation", |
| 353 |
title="Generator", |
| 354 |
) |
| 355 |
p = patch("beets.importer.ImportTask.imported_items", lambda x: [given]) |
| 356 |
p.start() |
| 357 |
with self.configure_plugin({"fields": ["album", "artist", "title"]}): |
| 358 |
import_session = ImportSession( |
| 359 |
self.lib, loghandler=None, paths=None, query=None |
| 360 |
) |
| 361 |
import_task = ImportTask(toppath=None, paths=None, items=[given]) |
| 362 |
TitlecasePlugin().imported(import_session, import_task) |
| 363 |
import_task.add(self.lib) |
| 364 |
item = self.lib.items().get() |
| 365 |
assert item.artist == expected.artist |
| 366 |
assert item.album == expected.album |
| 367 |
assert item.title == expected.title |
| 368 |
p.stop() |