| 1 |
import codecs |
| 2 |
from typing import ClassVar |
| 3 |
from unittest.mock import patch |
| 4 |
|
| 5 |
from beets.dbcore.query import TrueQuery |
| 6 |
from beets.importer import Action |
| 7 |
from beets.library import Item |
| 8 |
from beets.test.helper import ( |
| 9 |
AutotagImportTestCase, |
| 10 |
AutotagStub, |
| 11 |
BeetsTestCase, |
| 12 |
IOMixin, |
| 13 |
PluginMixin, |
| 14 |
TerminalImportMixin, |
| 15 |
) |
| 16 |
from beetsplug.edit import EditPlugin, dump, load |
| 17 |
|
| 18 |
|
| 19 |
class ModifyFileMocker: |
| 20 |
"""Helper for modifying a file, replacing or editing its contents. Used for |
| 21 |
mocking the calls to the external editor during testing. |
| 22 |
""" |
| 23 |
|
| 24 |
def __init__(self, contents=None, replacements=None): |
| 25 |
"""`self.contents` and `self.replacements` are initialized here, in |
| 26 |
order to keep the rest of the functions of this class with the same |
| 27 |
signature as `EditPlugin.get_editor()`, making mocking easier. |
| 28 |
- `contents`: string with the contents of the file to be used for |
| 29 |
`overwrite_contents()` |
| 30 |
- `replacement`: dict with the in-place replacements to be used for |
| 31 |
`replace_contents()`, in the form {'previous string': 'new string'} |
| 32 |
|
| 33 |
TODO: check if it can be solved more elegantly with a decorator |
| 34 |
""" |
| 35 |
self.contents = contents |
| 36 |
self.replacements = replacements |
| 37 |
self.action = self.overwrite_contents |
| 38 |
if replacements: |
| 39 |
self.action = self.replace_contents |
| 40 |
|
| 41 |
# The two methods below mock the `edit` utility function in the plugin. |
| 42 |
|
| 43 |
def overwrite_contents(self, filename, log): |
| 44 |
"""Modify `filename`, replacing its contents with `self.contents`. If |
| 45 |
`self.contents` is empty, the file remains unchanged. |
| 46 |
""" |
| 47 |
if self.contents: |
| 48 |
with codecs.open(filename, "w", encoding="utf-8") as f: |
| 49 |
f.write(self.contents) |
| 50 |
|
| 51 |
def replace_contents(self, filename, log): |
| 52 |
"""Modify `filename`, reading its contents and replacing the strings |
| 53 |
specified in `self.replacements`. |
| 54 |
""" |
| 55 |
with codecs.open(filename, "r", encoding="utf-8") as f: |
| 56 |
contents = f.read() |
| 57 |
for old, new_ in self.replacements.items(): |
| 58 |
contents = contents.replace(old, new_) |
| 59 |
with codecs.open(filename, "w", encoding="utf-8") as f: |
| 60 |
f.write(contents) |
| 61 |
|
| 62 |
|
| 63 |
class EditMixin(PluginMixin): |
| 64 |
"""Helper containing some common functionality used for the Edit tests.""" |
| 65 |
|
| 66 |
plugin = "edit" |
| 67 |
|
| 68 |
def assertItemFieldsModified( |
| 69 |
self, library_items, items, fields=[], allowed=["path"] |
| 70 |
): |
| 71 |
"""Assert that items in the library (`lib_items`) have different values |
| 72 |
on the specified `fields` (and *only* on those fields), compared to |
| 73 |
`items`. |
| 74 |
|
| 75 |
An empty `fields` list results in asserting that no modifications have |
| 76 |
been performed. `allowed` is a list of field changes that are ignored |
| 77 |
(they may or may not have changed; the assertion doesn't care). |
| 78 |
""" |
| 79 |
for lib_item, item in zip(library_items, items): |
| 80 |
diff_fields = [ |
| 81 |
field |
| 82 |
for field in lib_item._fields |
| 83 |
if lib_item[field] != item[field] |
| 84 |
] |
| 85 |
assert set(diff_fields).difference(allowed) == set(fields) |
| 86 |
|
| 87 |
def run_mocked_interpreter(self, modify_file_args={}, stdin=[]): |
| 88 |
"""Run the edit command during an import session, with mocked stdin and |
| 89 |
yaml writing. |
| 90 |
""" |
| 91 |
m = ModifyFileMocker(**modify_file_args) |
| 92 |
with patch("beetsplug.edit.edit", side_effect=m.action): |
| 93 |
for char in stdin: |
| 94 |
self.importer.add_choice(char) |
| 95 |
self.importer.run() |
| 96 |
|
| 97 |
def run_mocked_command(self, modify_file_args={}, stdin=[], args=[]): |
| 98 |
"""Run the edit command, with mocked stdin and yaml writing, and |
| 99 |
passing `args` to `run_command`.""" |
| 100 |
m = ModifyFileMocker(**modify_file_args) |
| 101 |
with patch("beetsplug.edit.edit", side_effect=m.action): |
| 102 |
for char in stdin: |
| 103 |
self.io.addinput(char) |
| 104 |
self.run_command("edit", *args) |
| 105 |
|
| 106 |
|
| 107 |
@patch("beets.library.Item.write") |
| 108 |
class EditCommandTest(IOMixin, EditMixin, BeetsTestCase): |
| 109 |
"""Black box tests for `beetsplug.edit`. Command line interaction is |
| 110 |
simulated using mocked stdin, and yaml editing via an external editor is |
| 111 |
simulated using `ModifyFileMocker`. |
| 112 |
""" |
| 113 |
|
| 114 |
TRACK_COUNT = 10 |
| 115 |
|
| 116 |
def setUp(self): |
| 117 |
super().setUp() |
| 118 |
# Add an album, storing the original fields for comparison. |
| 119 |
self.album = self.add_album_fixture(track_count=self.TRACK_COUNT) |
| 120 |
self.album_orig = {f: self.album[f] for f in self.album._fields} |
| 121 |
self.items_orig = [ |
| 122 |
{f: item[f] for f in item._fields} for item in self.album.items() |
| 123 |
] |
| 124 |
|
| 125 |
def test_title_edit_discard(self, mock_write): |
| 126 |
"""Edit title for all items in the library, then discard changes.""" |
| 127 |
# Edit track titles. |
| 128 |
self.run_mocked_command( |
| 129 |
{"replacements": {"t\u00eftle": "modified t\u00eftle"}}, |
| 130 |
# Cancel. |
| 131 |
["c"], |
| 132 |
) |
| 133 |
|
| 134 |
assert mock_write.call_count == 0 |
| 135 |
self.assertItemFieldsModified(self.album.items(), self.items_orig, []) |
| 136 |
|
| 137 |
def test_title_edit_apply(self, mock_write): |
| 138 |
"""Edit title for all items in the library, then apply changes.""" |
| 139 |
# Edit track titles. |
| 140 |
self.run_mocked_command( |
| 141 |
{"replacements": {"t\u00eftle": "modified t\u00eftle"}}, |
| 142 |
# Apply changes. |
| 143 |
["a"], |
| 144 |
) |
| 145 |
|
| 146 |
assert mock_write.call_count == self.TRACK_COUNT |
| 147 |
self.assertItemFieldsModified( |
| 148 |
self.album.items(), self.items_orig, ["title", "mtime"] |
| 149 |
) |
| 150 |
|
| 151 |
def test_single_title_edit_apply(self, mock_write): |
| 152 |
"""Edit title for one item in the library, then apply changes.""" |
| 153 |
# Edit one track title. |
| 154 |
self.run_mocked_command( |
| 155 |
{"replacements": {"t\u00eftle 9": "modified t\u00eftle 9"}}, |
| 156 |
# Apply changes. |
| 157 |
["a"], |
| 158 |
) |
| 159 |
|
| 160 |
assert mock_write.call_count == 1 |
| 161 |
# No changes except on last item. |
| 162 |
self.assertItemFieldsModified( |
| 163 |
list(self.album.items())[:-1], self.items_orig[:-1], [] |
| 164 |
) |
| 165 |
assert list(self.album.items())[-1].title == "modified t\u00eftle 9" |
| 166 |
|
| 167 |
def test_title_edit_keep_editing_then_apply(self, mock_write): |
| 168 |
"""Edit titles, keep editing once, then apply changes.""" |
| 169 |
self.run_mocked_command( |
| 170 |
{"replacements": {"t\u00eftle": "modified t\u00eftle"}}, |
| 171 |
# keep Editing, then Apply |
| 172 |
["e", "a"], |
| 173 |
) |
| 174 |
|
| 175 |
assert mock_write.call_count == self.TRACK_COUNT |
| 176 |
self.assertItemFieldsModified( |
| 177 |
self.album.items(), self.items_orig, ["title", "mtime"] |
| 178 |
) |
| 179 |
|
| 180 |
def test_title_edit_keep_editing_then_cancel(self, mock_write): |
| 181 |
"""Edit titles, keep editing once, then cancel.""" |
| 182 |
self.run_mocked_command( |
| 183 |
{"replacements": {"t\u00eftle": "modified t\u00eftle"}}, |
| 184 |
# keep Editing, then Cancel |
| 185 |
["e", "c"], |
| 186 |
) |
| 187 |
|
| 188 |
assert mock_write.call_count == 0 |
| 189 |
self.assertItemFieldsModified(self.album.items(), self.items_orig, []) |
| 190 |
|
| 191 |
def test_noedit(self, mock_write): |
| 192 |
"""Do not edit anything.""" |
| 193 |
# Do not edit anything. |
| 194 |
self.run_mocked_command( |
| 195 |
{"contents": None}, |
| 196 |
# No stdin. |
| 197 |
[], |
| 198 |
) |
| 199 |
|
| 200 |
assert mock_write.call_count == 0 |
| 201 |
self.assertItemFieldsModified(self.album.items(), self.items_orig, []) |
| 202 |
|
| 203 |
def test_album_edit_apply(self, mock_write): |
| 204 |
"""Edit the album field for all items in the library, apply changes. |
| 205 |
By design, the album should not be updated."" |
| 206 |
""" |
| 207 |
# Edit album. |
| 208 |
self.run_mocked_command( |
| 209 |
{"replacements": {"\u00e4lbum": "modified \u00e4lbum"}}, |
| 210 |
# Apply changes. |
| 211 |
["a"], |
| 212 |
) |
| 213 |
|
| 214 |
assert mock_write.call_count == self.TRACK_COUNT |
| 215 |
self.assertItemFieldsModified( |
| 216 |
self.album.items(), self.items_orig, ["album", "mtime"] |
| 217 |
) |
| 218 |
# Ensure album is *not* modified. |
| 219 |
self.album.load() |
| 220 |
assert self.album.album == "\u00e4lbum" |
| 221 |
|
| 222 |
def test_single_edit_add_field(self, mock_write): |
| 223 |
"""Edit the yaml file appending an extra field to the first item, then |
| 224 |
apply changes.""" |
| 225 |
# Append "foo: bar" to item with id == 2. ("id: 1" would match both |
| 226 |
# "id: 1" and "id: 10") |
| 227 |
self.run_mocked_command( |
| 228 |
{"replacements": {"id: 2": "id: 2\nfoo: bar"}}, |
| 229 |
# Apply changes. |
| 230 |
["a"], |
| 231 |
) |
| 232 |
|
| 233 |
assert self.lib.items("id:2")[0].foo == "bar" |
| 234 |
# Even though a flexible attribute was written (which is not directly |
| 235 |
# written to the tags), write should still be called since templates |
| 236 |
# might use it. |
| 237 |
assert mock_write.call_count == 1 |
| 238 |
|
| 239 |
def test_a_album_edit_apply(self, mock_write): |
| 240 |
"""Album query (-a), edit album field, apply changes.""" |
| 241 |
self.run_mocked_command( |
| 242 |
{"replacements": {"\u00e4lbum": "modified \u00e4lbum"}}, |
| 243 |
# Apply changes. |
| 244 |
["a"], |
| 245 |
args=["-a"], |
| 246 |
) |
| 247 |
|
| 248 |
self.album.load() |
| 249 |
assert mock_write.call_count == self.TRACK_COUNT |
| 250 |
assert self.album.album == "modified \u00e4lbum" |
| 251 |
self.assertItemFieldsModified( |
| 252 |
self.album.items(), self.items_orig, ["album", "mtime"] |
| 253 |
) |
| 254 |
|
| 255 |
def test_a_albumartist_edit_apply(self, mock_write): |
| 256 |
"""Album query (-a), edit albumartist field, apply changes.""" |
| 257 |
self.run_mocked_command( |
| 258 |
{"replacements": {"album artist": "modified album artist"}}, |
| 259 |
# Apply changes. |
| 260 |
["a"], |
| 261 |
args=["-a"], |
| 262 |
) |
| 263 |
|
| 264 |
self.album.load() |
| 265 |
assert mock_write.call_count == self.TRACK_COUNT |
| 266 |
assert self.album.albumartist == "the modified album artist" |
| 267 |
self.assertItemFieldsModified( |
| 268 |
self.album.items(), self.items_orig, ["albumartist", "mtime"] |
| 269 |
) |
| 270 |
|
| 271 |
def test_a_album_edit_preserves_missing_artpath(self, mock_write): |
| 272 |
"""Album query (-a), edit album field, preserve missing artpath.""" |
| 273 |
self.config["edit"]["albumfields"] = "album artpath" |
| 274 |
|
| 275 |
self.run_mocked_command( |
| 276 |
{"replacements": {"\u00e4lbum": "modified \u00e4lbum"}}, |
| 277 |
# Apply changes. |
| 278 |
["a"], |
| 279 |
args=["-a"], |
| 280 |
) |
| 281 |
|
| 282 |
self.album.load() |
| 283 |
assert mock_write.call_count == self.TRACK_COUNT |
| 284 |
assert self.album.album == "modified \u00e4lbum" |
| 285 |
assert self.album.artpath is None |
| 286 |
self.assertItemFieldsModified( |
| 287 |
self.album.items(), self.items_orig, ["album", "mtime"] |
| 288 |
) |
| 289 |
|
| 290 |
def test_malformed_yaml(self, mock_write): |
| 291 |
"""Edit the yaml file incorrectly (resulting in a malformed yaml |
| 292 |
document).""" |
| 293 |
# Edit the yaml file to an invalid file. |
| 294 |
self.run_mocked_command( |
| 295 |
{"contents": "!MALFORMED"}, |
| 296 |
# Edit again to fix? No. |
| 297 |
["n"], |
| 298 |
) |
| 299 |
|
| 300 |
assert mock_write.call_count == 0 |
| 301 |
|
| 302 |
def test_invalid_yaml(self, mock_write): |
| 303 |
"""Edit the yaml file incorrectly (resulting in a well-formed but |
| 304 |
invalid yaml document).""" |
| 305 |
# Edit the yaml file to an invalid but parseable file. |
| 306 |
self.run_mocked_command( |
| 307 |
{"contents": "wellformed: yes, but invalid"}, |
| 308 |
# No stdin. |
| 309 |
[], |
| 310 |
) |
| 311 |
|
| 312 |
assert mock_write.call_count == 0 |
| 313 |
|
| 314 |
|
| 315 |
class ApplyDataMatchingTest(PluginMixin, BeetsTestCase): |
| 316 |
"""`apply_data` must match documents to objects by their ``id`` field, |
| 317 |
not by their position in the list. A reordered or otherwise misaligned |
| 318 |
document list must not cause one object's data to be applied to a |
| 319 |
different object. |
| 320 |
""" |
| 321 |
|
| 322 |
plugin = "edit" |
| 323 |
|
| 324 |
def make_items(self): |
| 325 |
items = [] |
| 326 |
for i in (1, 2, 3): |
| 327 |
item = Item(id=i, title=f"Title {i}", track=i, artist="Artist") |
| 328 |
items.append(item) |
| 329 |
return items |
| 330 |
|
| 331 |
def test_matches_by_id_when_documents_are_reordered(self): |
| 332 |
plugin = EditPlugin() |
| 333 |
items = self.make_items() |
| 334 |
old_data = [ |
| 335 |
{"id": i.id, "title": i.title, "track": i.track} for i in items |
| 336 |
] |
| 337 |
|
| 338 |
# The saved documents come back in a different order than `items`, |
| 339 |
# as could happen after a "keep editing" round-trip or a reordering |
| 340 |
# editor action. Only track 2's title was actually edited. |
| 341 |
new_data = [ |
| 342 |
{"id": 3, "title": "Title 3", "track": 3}, |
| 343 |
{"id": 1, "title": "Title 1", "track": 1}, |
| 344 |
{"id": 2, "title": "Modified Title 2", "track": 2}, |
| 345 |
] |
| 346 |
|
| 347 |
plugin.apply_data(items, old_data, new_data) |
| 348 |
|
| 349 |
assert [i.title for i in items] == [ |
| 350 |
"Title 1", |
| 351 |
"Modified Title 2", |
| 352 |
"Title 3", |
| 353 |
] |
| 354 |
assert [i.track for i in items] == [1, 2, 3] |
| 355 |
|
| 356 |
def test_ignores_document_with_missing_id(self): |
| 357 |
plugin = EditPlugin() |
| 358 |
items = self.make_items() |
| 359 |
old_data = [ |
| 360 |
{"id": i.id, "title": i.title, "track": i.track} for i in items |
| 361 |
] |
| 362 |
|
| 363 |
# A document without an `id` must never be silently applied to an |
| 364 |
# unrelated object. |
| 365 |
new_data = [ |
| 366 |
{"title": "Header-like doc", "track": 99}, |
| 367 |
{"id": 2, "title": "Modified Title 2", "track": 2}, |
| 368 |
{"id": 3, "title": "Title 3", "track": 3}, |
| 369 |
] |
| 370 |
|
| 371 |
plugin.apply_data(items, old_data, new_data) |
| 372 |
|
| 373 |
assert items[0].title == "Title 1" |
| 374 |
assert items[0].track == 1 |
| 375 |
|
| 376 |
def test_ignores_documents_with_duplicate_id(self): |
| 377 |
plugin = EditPlugin() |
| 378 |
items = self.make_items() |
| 379 |
old_data = [ |
| 380 |
{"id": i.id, "title": i.title, "track": i.track} for i in items |
| 381 |
] |
| 382 |
|
| 383 |
# The user changed document 2's `id` to 1, an id that already |
| 384 |
# belongs to another document. Neither document should be applied: |
| 385 |
# we can't tell which one is legitimately item 1's data. |
| 386 |
new_data = [ |
| 387 |
{"id": 1, "title": "Title 1", "track": 1}, |
| 388 |
{"id": 1, "title": "Modified Title 2", "track": 2}, |
| 389 |
{"id": 3, "title": "Title 3", "track": 3}, |
| 390 |
] |
| 391 |
|
| 392 |
plugin.apply_data(items, old_data, new_data) |
| 393 |
|
| 394 |
assert items[0].title == "Title 1" |
| 395 |
assert items[0].track == 1 |
| 396 |
assert items[1].title == "Title 2" |
| 397 |
assert items[1].track == 2 |
| 398 |
|
| 399 |
|
| 400 |
class AlbumHeaderFieldsTest(PluginMixin, BeetsTestCase): |
| 401 |
"""`_importer_edit_album_header` must strip item-only fixed fields |
| 402 |
(title, track, path, ...), since the header is built from a single |
| 403 |
item and then applied to every item in the album. Flexible fields |
| 404 |
are not part of either model's fixed schema, so they must be left |
| 405 |
alone rather than discarded by an overly broad filter. |
| 406 |
""" |
| 407 |
|
| 408 |
plugin = "edit" |
| 409 |
|
| 410 |
class _StubTask: |
| 411 |
is_album = True |
| 412 |
|
| 413 |
def __init__(self, items): |
| 414 |
self.items = items |
| 415 |
|
| 416 |
def test_keeps_flexible_fields_but_drops_item_only_fields(self): |
| 417 |
item = Item(id=1, title="Title 1", track=1, album="Album", mood="Happy") |
| 418 |
task = self._StubTask([item]) |
| 419 |
|
| 420 |
self.config["edit"]["albumfields"] = "album mood track title" |
| 421 |
|
| 422 |
header = EditPlugin()._importer_edit_album_header(task) |
| 423 |
|
| 424 |
assert header == {"album": "Album", "mood": "Happy"} |
| 425 |
|
| 426 |
|
| 427 |
class EditDuringImporterTestCase( |
| 428 |
EditMixin, TerminalImportMixin, AutotagImportTestCase |
| 429 |
): |
| 430 |
"""TODO""" |
| 431 |
|
| 432 |
matching = AutotagStub.GOOD |
| 433 |
|
| 434 |
IGNORED: ClassVar[list[str]] = ["added", "album_id", "id", "mtime", "path"] |
| 435 |
|
| 436 |
def setUp(self): |
| 437 |
super().setUp() |
| 438 |
# Create some mediafiles, and store them for comparison. |
| 439 |
self.prepare_album_for_import(1) |
| 440 |
self.items_orig = [Item.from_path(f.path) for f in self.import_media] |
| 441 |
|
| 442 |
|
| 443 |
class EditDuringImporterNonSingletonTest(EditDuringImporterTestCase): |
| 444 |
def setUp(self): |
| 445 |
super().setUp() |
| 446 |
self.importer = self.setup_importer() |
| 447 |
|
| 448 |
def test_importer_edit_album_header_and_items(self): |
| 449 |
"""Edit both the album header and per-track fields simultaneously.""" |
| 450 |
self.config["edit"]["itemfields"] = "title" |
| 451 |
self.config["edit"]["albumfields"] = "album" |
| 452 |
|
| 453 |
self.run_mocked_interpreter( |
| 454 |
{ |
| 455 |
"replacements": { |
| 456 |
"Tag Album": "Modified Album", |
| 457 |
"Tag Track": "Modified Track", |
| 458 |
} |
| 459 |
}, |
| 460 |
# eDit, Apply changes. |
| 461 |
["d", "a"], |
| 462 |
) |
| 463 |
|
| 464 |
# All items should have the new album and new title. |
| 465 |
assert all(i.album == "Modified Album" for i in self.lib.items()) |
| 466 |
assert all("Modified Track" in i.title for i in self.lib.items()) |
| 467 |
assert self.lib.albums()[0].album == "Modified Album" |
| 468 |
|
| 469 |
def test_importer_edit_album_header_skip_no_albumfields(self): |
| 470 |
"""When albumfields is empty, no header section is produced; editing |
| 471 |
works as before. |
| 472 |
""" |
| 473 |
self.config["edit"]["itemfields"] = "title" |
| 474 |
self.config["edit"]["albumfields"] = "" |
| 475 |
|
| 476 |
self.run_mocked_interpreter( |
| 477 |
{"replacements": {"Tag Track": "Edited Track"}}, |
| 478 |
# eDit, Apply changes. |
| 479 |
["d", "a"], |
| 480 |
) |
| 481 |
|
| 482 |
assert all("Edited Track" in i.title for i in self.lib.items()) |
| 483 |
|
| 484 |
def test_importer_edit_album_header_ignores_item_only_fields(self): |
| 485 |
"""`albumfields` may be misconfigured with item-only fields (e.g. |
| 486 |
`track`, `title`, `path`) that vary per track. Those must not end |
| 487 |
up in the header, since the header gets applied to every item and |
| 488 |
would otherwise stamp one track's values onto the whole album. |
| 489 |
""" |
| 490 |
self.prepare_album_for_import(3) |
| 491 |
self.items_orig = [Item.from_path(f.path) for f in self.import_media] |
| 492 |
|
| 493 |
self.config["edit"]["itemfields"] = "track title artist album" |
| 494 |
self.config["edit"]["albumfields"] = "album albumartist track title" |
| 495 |
|
| 496 |
self.run_mocked_interpreter( |
| 497 |
{"replacements": {"Tag Album": "Modified Album"}}, |
| 498 |
# eDit, Apply changes. |
| 499 |
["d", "a"], |
| 500 |
) |
| 501 |
|
| 502 |
titles = [i.title for i in self.lib.items()] |
| 503 |
tracks = [i.track for i in self.lib.items()] |
| 504 |
assert len(set(titles)) == len(titles) |
| 505 |
assert len(set(tracks)) == len(tracks) |
| 506 |
assert all(i.album == "Modified Album" for i in self.lib.items()) |
| 507 |
|
| 508 |
def test_importer_edit_candidate_keeps_rejected_albumfields_in_tracks(self): |
| 509 |
"""Item-only album fields remain editable in candidate tracks.""" |
| 510 |
self.config["edit"]["albumfields"] = "album title track" |
| 511 |
self.config["edit"]["itemfields"] = "album title track" |
| 512 |
|
| 513 |
seen_docs = {} |
| 514 |
|
| 515 |
def inspect_and_edit(filename, log): |
| 516 |
with codecs.open(filename, encoding="utf-8") as f: |
| 517 |
docs = load(f.read()) |
| 518 |
|
| 519 |
headers = [doc for doc in docs if "id" not in doc] |
| 520 |
tracks = [doc for doc in docs if "id" in doc] |
| 521 |
seen_docs["headers"] = headers |
| 522 |
seen_docs["tracks"] = tracks |
| 523 |
tracks[0]["title"] = "Issue 6953 edited title" |
| 524 |
|
| 525 |
with codecs.open(filename, "w", encoding="utf-8") as f: |
| 526 |
f.write(dump(docs)) |
| 527 |
|
| 528 |
with patch("beetsplug.edit.edit", side_effect=inspect_and_edit): |
| 529 |
self.importer.add_choice("c") |
| 530 |
self.importer.add_choice("1") |
| 531 |
self.importer.add_choice("a") |
| 532 |
self.importer.run() |
| 533 |
|
| 534 |
assert len(seen_docs["headers"]) == 1 |
| 535 |
assert set(seen_docs["headers"][0]) == {"album"} |
| 536 |
assert len(seen_docs["tracks"]) == 1 |
| 537 |
assert set(seen_docs["tracks"][0]) == {"id", "title", "track"} |
| 538 |
|
| 539 |
items = list(self.lib.items()) |
| 540 |
assert len(items) == 1 |
| 541 |
assert items[0].title == "Issue 6953 edited title" |
| 542 |
assert items[0].mb_trackid == "match 1" |
| 543 |
|
| 544 |
albums = list(self.lib.albums()) |
| 545 |
assert len(albums) == 1 |
| 546 |
assert albums[0].mb_albumid == "albumid" |
| 547 |
|
| 548 |
def test_importer_edit_album_header_albumartist(self): |
| 549 |
"""Edit albumartist in the header (default albumfields).""" |
| 550 |
self.run_mocked_interpreter( |
| 551 |
{"replacements": {"Tag Artist": "Modified Artist"}}, |
| 552 |
# eDit, Apply changes. |
| 553 |
["d", "a"], |
| 554 |
) |
| 555 |
|
| 556 |
assert all( |
| 557 |
i.albumartist is not None and "Modified Artist" in i.albumartist |
| 558 |
for i in self.lib.items() |
| 559 |
) or all( |
| 560 |
i.albumartist is None and "Tag Artist" in i.artist |
| 561 |
for i in self.lib.items() |
| 562 |
) |
| 563 |
|
| 564 |
def test_importer_edit_album_header_reordered(self): |
| 565 |
"""If the user moves the album header document below the track |
| 566 |
documents, it must still be recognized as the header (identified by |
| 567 |
the absence of an `id` field) rather than misapplying a track's |
| 568 |
fields to every item. |
| 569 |
""" |
| 570 |
self.config["edit"]["itemfields"] = "title" |
| 571 |
self.config["edit"]["albumfields"] = "album" |
| 572 |
|
| 573 |
def reorder_and_edit(filename, log): |
| 574 |
with codecs.open(filename, encoding="utf-8") as f: |
| 575 |
docs = load(f.read()) |
| 576 |
header = next(d for d in docs if "id" not in d) |
| 577 |
tracks = [d for d in docs if "id" in d] |
| 578 |
header["album"] = "Modified Album" |
| 579 |
with codecs.open(filename, "w", encoding="utf-8") as f: |
| 580 |
f.write(dump([*tracks, header])) |
| 581 |
|
| 582 |
with patch("beetsplug.edit.edit", side_effect=reorder_and_edit): |
| 583 |
self.importer.add_choice("d") |
| 584 |
self.importer.add_choice("a") |
| 585 |
self.importer.run() |
| 586 |
|
| 587 |
assert all(i.album == "Modified Album" for i in self.lib.items()) |
| 588 |
assert all("Tag Track" in i.title for i in self.lib.items()) |
| 589 |
|
| 590 |
def test_edit_apply_asis(self): |
| 591 |
"""Edit the album field for all items in the library, apply changes, |
| 592 |
using the original item tags. |
| 593 |
""" |
| 594 |
# Edit track titles. |
| 595 |
self.run_mocked_interpreter( |
| 596 |
{"replacements": {"Tag Track": "Edited Track"}}, |
| 597 |
# eDit, Apply changes. |
| 598 |
["d", "a"], |
| 599 |
) |
| 600 |
|
| 601 |
# Check that only the 'title' field is modified. |
| 602 |
self.assertItemFieldsModified( |
| 603 |
self.lib.items(), |
| 604 |
self.items_orig, |
| 605 |
["title", "albumartist", "albumartists"], |
| 606 |
[*self.IGNORED, "mb_albumartistid", "mb_albumartistids"], |
| 607 |
) |
| 608 |
assert all("Edited Track" in i.title for i in self.lib.items()) |
| 609 |
|
| 610 |
# Ensure album is *not* fetched from a candidate. |
| 611 |
assert self.lib.albums()[0].mb_albumid == "" |
| 612 |
|
| 613 |
def test_edit_discard_asis(self): |
| 614 |
"""Edit the album field for all items in the library, discard changes, |
| 615 |
using the original item tags. |
| 616 |
""" |
| 617 |
# Edit track titles. |
| 618 |
self.run_mocked_interpreter( |
| 619 |
{"replacements": {"Tag Track": "Edited Track"}}, |
| 620 |
# eDit, Cancel, Use as-is. |
| 621 |
["d", "c", "u"], |
| 622 |
) |
| 623 |
|
| 624 |
# Check that nothing is modified, the album is imported ASIS. |
| 625 |
self.assertItemFieldsModified( |
| 626 |
self.lib.items(), |
| 627 |
self.items_orig, |
| 628 |
[], |
| 629 |
[*self.IGNORED, "albumartist", "mb_albumartistid"], |
| 630 |
) |
| 631 |
assert all("Tag Track" in i.title for i in self.lib.items()) |
| 632 |
|
| 633 |
# Ensure album is *not* fetched from a candidate. |
| 634 |
assert self.lib.albums()[0].mb_albumid == "" |
| 635 |
|
| 636 |
def test_edit_apply_candidate(self): |
| 637 |
"""Edit the album field for all items in the library, apply changes, |
| 638 |
using a candidate. |
| 639 |
""" |
| 640 |
# Edit track titles. |
| 641 |
self.run_mocked_interpreter( |
| 642 |
{"replacements": {"Applied Track": "Edited Track"}}, |
| 643 |
# edit Candidates, 1, Apply changes. |
| 644 |
["c", "1", "a"], |
| 645 |
) |
| 646 |
|
| 647 |
# Check that 'title' field is modified, and other fields come from |
| 648 |
# the candidate. |
| 649 |
assert all("Edited Track " in i.title for i in self.lib.items()) |
| 650 |
assert all("match " in i.mb_trackid for i in self.lib.items()) |
| 651 |
|
| 652 |
# Ensure album is fetched from a candidate. |
| 653 |
assert "albumid" in self.lib.albums()[0].mb_albumid |
| 654 |
|
| 655 |
def test_edit_retag_apply(self): |
| 656 |
"""Import the album using a candidate, then retag and edit and apply |
| 657 |
changes. |
| 658 |
""" |
| 659 |
self.run_mocked_interpreter( |
| 660 |
{}, |
| 661 |
# 1, Apply changes. |
| 662 |
["1", Action.APPLY], |
| 663 |
) |
| 664 |
|
| 665 |
# Retag and edit track titles. On retag, the importer will reset items |
| 666 |
# ids but not the db connections. |
| 667 |
self.importer.paths = [] |
| 668 |
self.importer.query = TrueQuery() |
| 669 |
self.run_mocked_interpreter( |
| 670 |
{"replacements": {"Applied Track": "Edited Track"}}, |
| 671 |
# eDit, Apply changes. |
| 672 |
["d", "a"], |
| 673 |
) |
| 674 |
|
| 675 |
# Check that 'title' field is modified, and other fields come from |
| 676 |
# the candidate. |
| 677 |
assert all("Edited Track " in i.title for i in self.lib.items()) |
| 678 |
assert all("match " in i.mb_trackid for i in self.lib.items()) |
| 679 |
|
| 680 |
# Ensure album is fetched from a candidate. |
| 681 |
assert "albumid" in self.lib.albums()[0].mb_albumid |
| 682 |
|
| 683 |
def test_edit_discard_candidate(self): |
| 684 |
"""Edit the album field for all items in the library, discard changes, |
| 685 |
using a candidate. |
| 686 |
""" |
| 687 |
# Edit track titles. |
| 688 |
self.run_mocked_interpreter( |
| 689 |
{"replacements": {"Applied Track": "Edited Track"}}, |
| 690 |
# edit Candidates, 1, Apply changes. |
| 691 |
["c", "1", "a"], |
| 692 |
) |
| 693 |
|
| 694 |
# Check that 'title' field is modified, and other fields come from |
| 695 |
# the candidate. |
| 696 |
assert all("Edited Track " in i.title for i in self.lib.items()) |
| 697 |
assert all("match " in i.mb_trackid for i in self.lib.items()) |
| 698 |
|
| 699 |
# Ensure album is fetched from a candidate. |
| 700 |
assert "albumid" in self.lib.albums()[0].mb_albumid |
| 701 |
|
| 702 |
def test_edit_apply_candidate_singleton(self): |
| 703 |
"""Edit the album field for all items in the library, apply changes, |
| 704 |
using a candidate and singleton mode. |
| 705 |
""" |
| 706 |
# Edit track titles. |
| 707 |
self.run_mocked_interpreter( |
| 708 |
{"replacements": {"Applied Track": "Edited Track"}}, |
| 709 |
# edit Candidates, 1, Apply changes, aBort. |
| 710 |
["c", "1", "a", "b"], |
| 711 |
) |
| 712 |
|
| 713 |
# Check that 'title' field is modified, and other fields come from |
| 714 |
# the candidate. |
| 715 |
assert all("Edited Track " in i.title for i in self.lib.items()) |
| 716 |
assert all("match " in i.mb_trackid for i in self.lib.items()) |
| 717 |
|
| 718 |
|
| 719 |
class EditDuringImporterSingletonTest(EditDuringImporterTestCase): |
| 720 |
def setUp(self): |
| 721 |
super().setUp() |
| 722 |
self.importer = self.setup_singleton_importer() |
| 723 |
|
| 724 |
def test_edit_apply_asis_singleton(self): |
| 725 |
"""Edit the album field for all items in the library, apply changes, |
| 726 |
using the original item tags and singleton mode. |
| 727 |
""" |
| 728 |
# Edit track titles. |
| 729 |
self.run_mocked_interpreter( |
| 730 |
{"replacements": {"Tag Track": "Edited Track"}}, |
| 731 |
# eDit, Apply changes, aBort. |
| 732 |
["d", "a", "b"], |
| 733 |
) |
| 734 |
|
| 735 |
# Check that only the 'title' field is modified. |
| 736 |
self.assertItemFieldsModified( |
| 737 |
self.lib.items(), |
| 738 |
self.items_orig, |
| 739 |
["title"], |
| 740 |
[*self.IGNORED, "albumartist", "mb_albumartistid"], |
| 741 |
) |
| 742 |
assert all("Edited Track" in i.title for i in self.lib.items()) |