forked from beetbox/beets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_importer.py
2028 lines (1642 loc) · 68.8 KB
/
test_importer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is part of beets.
# Copyright 2016, Adrian Sampson.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
"""Tests for the general importer functionality.
"""
import os
import re
import shutil
import stat
import sys
import unicodedata
import unittest
from io import StringIO
from tarfile import TarFile
from tempfile import mkstemp
from unittest.mock import Mock, patch
from zipfile import ZipFile
from mediafile import MediaFile
from beets import config, importer, logging, util
from beets.autotag import AlbumInfo, AlbumMatch, TrackInfo
from beets.importer import albums_in_dir
from beets.test import _common
from beets.test.helper import (
AutotagStub,
ImportHelper,
TestHelper,
capture_log,
has_program,
)
from beets.util import bytestring_path, displayable_path, py3_path, syspath
class ScrubbedImportTest(_common.TestCase, ImportHelper):
def setUp(self):
self.setup_beets(disk=True)
self.load_plugins("scrub")
self._create_import_dir(2)
self._setup_import_session(autotag=False)
def tearDown(self):
self.unload_plugins()
self.teardown_beets()
def test_tags_not_scrubbed(self):
config["plugins"] = ["scrub"]
config["scrub"]["auto"] = False
config["import"]["write"] = True
for mediafile in self.import_media:
self.assertEqual(mediafile.artist, "Tag Artist")
self.assertEqual(mediafile.album, "Tag Album")
self.importer.run()
for item in self.lib.items():
imported_file = os.path.join(item.path)
imported_file = MediaFile(imported_file)
self.assertEqual(imported_file.artist, "Tag Artist")
self.assertEqual(imported_file.album, "Tag Album")
def test_tags_restored(self):
config["plugins"] = ["scrub"]
config["scrub"]["auto"] = True
config["import"]["write"] = True
for mediafile in self.import_media:
self.assertEqual(mediafile.artist, "Tag Artist")
self.assertEqual(mediafile.album, "Tag Album")
self.importer.run()
for item in self.lib.items():
imported_file = os.path.join(item.path)
imported_file = MediaFile(imported_file)
self.assertEqual(imported_file.artist, "Tag Artist")
self.assertEqual(imported_file.album, "Tag Album")
def test_tags_not_restored(self):
config["plugins"] = ["scrub"]
config["scrub"]["auto"] = True
config["import"]["write"] = False
for mediafile in self.import_media:
self.assertEqual(mediafile.artist, "Tag Artist")
self.assertEqual(mediafile.album, "Tag Album")
self.importer.run()
for item in self.lib.items():
imported_file = os.path.join(item.path)
imported_file = MediaFile(imported_file)
self.assertIsNone(imported_file.artist)
self.assertIsNone(imported_file.album)
@_common.slow_test()
class NonAutotaggedImportTest(_common.TestCase, ImportHelper):
def setUp(self):
self.setup_beets(disk=True)
self._create_import_dir(2)
self._setup_import_session(autotag=False)
def tearDown(self):
self.teardown_beets()
def test_album_created_with_track_artist(self):
self.importer.run()
albums = self.lib.albums()
self.assertEqual(len(albums), 1)
self.assertEqual(albums[0].albumartist, "Tag Artist")
def test_import_copy_arrives(self):
self.importer.run()
for mediafile in self.import_media:
self.assert_file_in_lib(
b"Tag Artist",
b"Tag Album",
util.bytestring_path(f"{mediafile.title}.mp3"),
)
def test_threaded_import_copy_arrives(self):
config["threaded"] = True
self.importer.run()
for mediafile in self.import_media:
self.assert_file_in_lib(
b"Tag Artist",
b"Tag Album",
util.bytestring_path(f"{mediafile.title}.mp3"),
)
def test_import_with_move_deletes_import_files(self):
config["import"]["move"] = True
for mediafile in self.import_media:
self.assertExists(mediafile.path)
self.importer.run()
for mediafile in self.import_media:
self.assertNotExists(mediafile.path)
def test_import_with_move_prunes_directory_empty(self):
config["import"]["move"] = True
self.assertExists(os.path.join(self.import_dir, b"the_album"))
self.importer.run()
self.assertNotExists(os.path.join(self.import_dir, b"the_album"))
def test_import_with_move_prunes_with_extra_clutter(self):
self.touch(os.path.join(self.import_dir, b"the_album", b"alog.log"))
config["clutter"] = ["*.log"]
config["import"]["move"] = True
self.assertExists(os.path.join(self.import_dir, b"the_album"))
self.importer.run()
self.assertNotExists(os.path.join(self.import_dir, b"the_album"))
def test_threaded_import_move_arrives(self):
config["import"]["move"] = True
config["import"]["threaded"] = True
self.importer.run()
for mediafile in self.import_media:
self.assert_file_in_lib(
b"Tag Artist",
b"Tag Album",
util.bytestring_path(f"{mediafile.title}.mp3"),
)
def test_threaded_import_move_deletes_import(self):
config["import"]["move"] = True
config["threaded"] = True
self.importer.run()
for mediafile in self.import_media:
self.assertNotExists(mediafile.path)
def test_import_without_delete_retains_files(self):
config["import"]["delete"] = False
self.importer.run()
for mediafile in self.import_media:
self.assertExists(mediafile.path)
def test_import_with_delete_removes_files(self):
config["import"]["delete"] = True
self.importer.run()
for mediafile in self.import_media:
self.assertNotExists(mediafile.path)
def test_import_with_delete_prunes_directory_empty(self):
config["import"]["delete"] = True
self.assertExists(os.path.join(self.import_dir, b"the_album"))
self.importer.run()
self.assertNotExists(os.path.join(self.import_dir, b"the_album"))
@unittest.skipUnless(_common.HAVE_SYMLINK, "need symlinks")
def test_import_link_arrives(self):
config["import"]["link"] = True
self.importer.run()
for mediafile in self.import_media:
filename = os.path.join(
self.libdir,
b"Tag Artist",
b"Tag Album",
util.bytestring_path(f"{mediafile.title}.mp3"),
)
self.assertExists(filename)
self.assertTrue(os.path.islink(syspath(filename)))
self.assert_equal_path(
util.bytestring_path(os.readlink(syspath(filename))),
mediafile.path,
)
@unittest.skipUnless(_common.HAVE_HARDLINK, "need hardlinks")
def test_import_hardlink_arrives(self):
config["import"]["hardlink"] = True
self.importer.run()
for mediafile in self.import_media:
filename = os.path.join(
self.libdir,
b"Tag Artist",
b"Tag Album",
util.bytestring_path(f"{mediafile.title}.mp3"),
)
self.assertExists(filename)
s1 = os.stat(syspath(mediafile.path))
s2 = os.stat(syspath(filename))
self.assertTrue(
(s1[stat.ST_INO], s1[stat.ST_DEV])
== (s2[stat.ST_INO], s2[stat.ST_DEV])
)
def create_archive(session):
(handle, path) = mkstemp(dir=py3_path(session.temp_dir))
path = bytestring_path(path)
os.close(handle)
archive = ZipFile(py3_path(path), mode="w")
archive.write(syspath(os.path.join(_common.RSRC, b"full.mp3")), "full.mp3")
archive.close()
path = bytestring_path(path)
return path
class RmTempTest(unittest.TestCase, ImportHelper, _common.Assertions):
"""Tests that temporarily extracted archives are properly removed
after usage.
"""
def setUp(self):
self.setup_beets()
self.want_resume = False
self.config["incremental"] = False
self._old_home = None
def tearDown(self):
self.teardown_beets()
def test_rm(self):
zip_path = create_archive(self)
archive_task = importer.ArchiveImportTask(zip_path)
archive_task.extract()
tmp_path = archive_task.toppath
self._setup_import_session(autotag=False, import_dir=tmp_path)
self.assertExists(tmp_path)
archive_task.finalize(self)
self.assertNotExists(tmp_path)
class ImportZipTest(unittest.TestCase, ImportHelper):
def setUp(self):
self.setup_beets()
def tearDown(self):
self.teardown_beets()
def test_import_zip(self):
zip_path = create_archive(self)
self.assertEqual(len(self.lib.items()), 0)
self.assertEqual(len(self.lib.albums()), 0)
self._setup_import_session(autotag=False, import_dir=zip_path)
self.importer.run()
self.assertEqual(len(self.lib.items()), 1)
self.assertEqual(len(self.lib.albums()), 1)
class ImportTarTest(ImportZipTest):
def create_archive(self):
(handle, path) = mkstemp(dir=syspath(self.temp_dir))
path = bytestring_path(path)
os.close(handle)
archive = TarFile(py3_path(path), mode="w")
archive.add(
syspath(os.path.join(_common.RSRC, b"full.mp3")), "full.mp3"
)
archive.close()
return path
@unittest.skipIf(not has_program("unrar"), "unrar program not found")
class ImportRarTest(ImportZipTest):
def create_archive(self):
return os.path.join(_common.RSRC, b"archive.rar")
class Import7zTest(ImportZipTest):
def create_archive(self):
return os.path.join(_common.RSRC, b"archive.7z")
@unittest.skip("Implement me!")
class ImportPasswordRarTest(ImportZipTest):
def create_archive(self):
return os.path.join(_common.RSRC, b"password.rar")
class ImportSingletonTest(_common.TestCase, ImportHelper):
"""Test ``APPLY`` and ``ASIS`` choices for an import session with
singletons config set to True.
"""
def setUp(self):
self.setup_beets()
self._create_import_dir(1)
self._setup_import_session()
config["import"]["singletons"] = True
self.matcher = AutotagStub().install()
def tearDown(self):
self.teardown_beets()
self.matcher.restore()
def test_apply_asis_adds_track(self):
self.assertIsNone(self.lib.items().get())
self.importer.add_choice(importer.action.ASIS)
self.importer.run()
self.assertEqual(self.lib.items().get().title, "Tag Title 1")
def test_apply_asis_does_not_add_album(self):
self.assertIsNone(self.lib.albums().get())
self.importer.add_choice(importer.action.ASIS)
self.importer.run()
self.assertIsNone(self.lib.albums().get())
def test_apply_asis_adds_singleton_path(self):
self.assert_lib_dir_empty()
self.importer.add_choice(importer.action.ASIS)
self.importer.run()
self.assert_file_in_lib(b"singletons", b"Tag Title 1.mp3")
def test_apply_candidate_adds_track(self):
self.assertIsNone(self.lib.items().get())
self.importer.add_choice(importer.action.APPLY)
self.importer.run()
self.assertEqual(self.lib.items().get().title, "Applied Title 1")
def test_apply_candidate_does_not_add_album(self):
self.importer.add_choice(importer.action.APPLY)
self.importer.run()
self.assertIsNone(self.lib.albums().get())
def test_apply_candidate_adds_singleton_path(self):
self.assert_lib_dir_empty()
self.importer.add_choice(importer.action.APPLY)
self.importer.run()
self.assert_file_in_lib(b"singletons", b"Applied Title 1.mp3")
def test_skip_does_not_add_first_track(self):
self.importer.add_choice(importer.action.SKIP)
self.importer.run()
self.assertIsNone(self.lib.items().get())
def test_skip_adds_other_tracks(self):
self._create_import_dir(2)
self.importer.add_choice(importer.action.SKIP)
self.importer.add_choice(importer.action.ASIS)
self.importer.run()
self.assertEqual(len(self.lib.items()), 1)
def test_import_single_files(self):
resource_path = os.path.join(_common.RSRC, b"empty.mp3")
single_path = os.path.join(self.import_dir, b"track_2.mp3")
util.copy(resource_path, single_path)
import_files = [
os.path.join(self.import_dir, b"the_album"),
single_path,
]
self._setup_import_session(singletons=False)
self.importer.paths = import_files
self.importer.add_choice(importer.action.ASIS)
self.importer.add_choice(importer.action.ASIS)
self.importer.run()
self.assertEqual(len(self.lib.items()), 2)
self.assertEqual(len(self.lib.albums()), 2)
def test_set_fields(self):
genre = "\U0001F3B7 Jazz"
collection = "To Listen"
config["import"]["set_fields"] = {
"collection": collection,
"genre": genre,
"title": "$title - formatted",
}
# As-is item import.
self.assertIsNone(self.lib.albums().get())
self.importer.add_choice(importer.action.ASIS)
self.importer.run()
for item in self.lib.items():
item.load() # TODO: Not sure this is necessary.
self.assertEqual(item.genre, genre)
self.assertEqual(item.collection, collection)
self.assertEqual(item.title, "Tag Title 1 - formatted")
# Remove item from library to test again with APPLY choice.
item.remove()
# Autotagged.
self.assertIsNone(self.lib.albums().get())
self.importer.clear_choices()
self.importer.add_choice(importer.action.APPLY)
self.importer.run()
for item in self.lib.items():
item.load()
self.assertEqual(item.genre, genre)
self.assertEqual(item.collection, collection)
self.assertEqual(item.title, "Applied Title 1 - formatted")
class ImportTest(_common.TestCase, ImportHelper):
"""Test APPLY, ASIS and SKIP choices."""
def setUp(self):
self.setup_beets()
self._create_import_dir(1)
self._setup_import_session()
self.matcher = AutotagStub().install()
self.matcher.macthin = AutotagStub.GOOD
def tearDown(self):
self.teardown_beets()
self.matcher.restore()
def test_apply_asis_adds_album(self):
self.assertIsNone(self.lib.albums().get())
self.importer.add_choice(importer.action.ASIS)
self.importer.run()
self.assertEqual(self.lib.albums().get().album, "Tag Album")
def test_apply_asis_adds_tracks(self):
self.assertIsNone(self.lib.items().get())
self.importer.add_choice(importer.action.ASIS)
self.importer.run()
self.assertEqual(self.lib.items().get().title, "Tag Title 1")
def test_apply_asis_adds_album_path(self):
self.assert_lib_dir_empty()
self.importer.add_choice(importer.action.ASIS)
self.importer.run()
self.assert_file_in_lib(b"Tag Artist", b"Tag Album", b"Tag Title 1.mp3")
def test_apply_candidate_adds_album(self):
self.assertIsNone(self.lib.albums().get())
self.importer.add_choice(importer.action.APPLY)
self.importer.run()
self.assertEqual(self.lib.albums().get().album, "Applied Album")
def test_apply_candidate_adds_tracks(self):
self.assertIsNone(self.lib.items().get())
self.importer.add_choice(importer.action.APPLY)
self.importer.run()
self.assertEqual(self.lib.items().get().title, "Applied Title 1")
def test_apply_candidate_adds_album_path(self):
self.assert_lib_dir_empty()
self.importer.add_choice(importer.action.APPLY)
self.importer.run()
self.assert_file_in_lib(
b"Applied Artist", b"Applied Album", b"Applied Title 1.mp3"
)
def test_apply_from_scratch_removes_other_metadata(self):
config["import"]["from_scratch"] = True
for mediafile in self.import_media:
mediafile.genre = "Tag Genre"
mediafile.save()
self.importer.add_choice(importer.action.APPLY)
self.importer.run()
self.assertEqual(self.lib.items().get().genre, "")
def test_apply_from_scratch_keeps_format(self):
config["import"]["from_scratch"] = True
self.importer.add_choice(importer.action.APPLY)
self.importer.run()
self.assertEqual(self.lib.items().get().format, "MP3")
def test_apply_from_scratch_keeps_bitrate(self):
config["import"]["from_scratch"] = True
bitrate = 80000
self.importer.add_choice(importer.action.APPLY)
self.importer.run()
self.assertEqual(self.lib.items().get().bitrate, bitrate)
def test_apply_with_move_deletes_import(self):
config["import"]["move"] = True
import_file = os.path.join(
self.import_dir, b"the_album", b"track_1.mp3"
)
self.assertExists(import_file)
self.importer.add_choice(importer.action.APPLY)
self.importer.run()
self.assertNotExists(import_file)
def test_apply_with_delete_deletes_import(self):
config["import"]["delete"] = True
import_file = os.path.join(
self.import_dir, b"the_album", b"track_1.mp3"
)
self.assertExists(import_file)
self.importer.add_choice(importer.action.APPLY)
self.importer.run()
self.assertNotExists(import_file)
def test_skip_does_not_add_track(self):
self.importer.add_choice(importer.action.SKIP)
self.importer.run()
self.assertIsNone(self.lib.items().get())
def test_skip_non_album_dirs(self):
self.assertIsDir(os.path.join(self.import_dir, b"the_album"))
self.touch(b"cruft", dir=self.import_dir)
self.importer.add_choice(importer.action.APPLY)
self.importer.run()
self.assertEqual(len(self.lib.albums()), 1)
def test_unmatched_tracks_not_added(self):
self._create_import_dir(2)
self.matcher.matching = self.matcher.MISSING
self.importer.add_choice(importer.action.APPLY)
self.importer.run()
self.assertEqual(len(self.lib.items()), 1)
def test_empty_directory_warning(self):
import_dir = os.path.join(self.temp_dir, b"empty")
self.touch(b"non-audio", dir=import_dir)
self._setup_import_session(import_dir=import_dir)
with capture_log() as logs:
self.importer.run()
import_dir = displayable_path(import_dir)
self.assertIn(f"No files imported from {import_dir}", logs)
def test_empty_directory_singleton_warning(self):
import_dir = os.path.join(self.temp_dir, b"empty")
self.touch(b"non-audio", dir=import_dir)
self._setup_import_session(import_dir=import_dir, singletons=True)
with capture_log() as logs:
self.importer.run()
import_dir = displayable_path(import_dir)
self.assertIn(f"No files imported from {import_dir}", logs)
def test_asis_no_data_source(self):
self.assertIsNone(self.lib.items().get())
self.importer.add_choice(importer.action.ASIS)
self.importer.run()
with self.assertRaises(AttributeError):
self.lib.items().get().data_source
def test_set_fields(self):
genre = "\U0001F3B7 Jazz"
collection = "To Listen"
comments = "managed by beets"
config["import"]["set_fields"] = {
"genre": genre,
"collection": collection,
"comments": comments,
"album": "$album - formatted",
}
# As-is album import.
self.assertIsNone(self.lib.albums().get())
self.importer.add_choice(importer.action.ASIS)
self.importer.run()
for album in self.lib.albums():
album.load() # TODO: Not sure this is necessary.
self.assertEqual(album.genre, genre)
self.assertEqual(album.comments, comments)
for item in album.items():
self.assertEqual(item.get("genre", with_album=False), genre)
self.assertEqual(
item.get("collection", with_album=False), collection
)
self.assertEqual(
item.get("comments", with_album=False), comments
)
self.assertEqual(
item.get("album", with_album=False), "Tag Album - formatted"
)
# Remove album from library to test again with APPLY choice.
album.remove()
# Autotagged.
self.assertIsNone(self.lib.albums().get())
self.importer.clear_choices()
self.importer.add_choice(importer.action.APPLY)
self.importer.run()
for album in self.lib.albums():
album.load()
self.assertEqual(album.genre, genre)
self.assertEqual(album.comments, comments)
for item in album.items():
self.assertEqual(item.get("genre", with_album=False), genre)
self.assertEqual(
item.get("collection", with_album=False), collection
)
self.assertEqual(
item.get("comments", with_album=False), comments
)
self.assertEqual(
item.get("album", with_album=False),
"Applied Album - formatted",
)
class ImportTracksTest(_common.TestCase, ImportHelper):
"""Test TRACKS and APPLY choice."""
def setUp(self):
self.setup_beets()
self._create_import_dir(1)
self._setup_import_session()
self.matcher = AutotagStub().install()
def tearDown(self):
self.teardown_beets()
self.matcher.restore()
def test_apply_tracks_adds_singleton_track(self):
self.assertIsNone(self.lib.items().get())
self.assertIsNone(self.lib.albums().get())
self.importer.add_choice(importer.action.TRACKS)
self.importer.add_choice(importer.action.APPLY)
self.importer.add_choice(importer.action.APPLY)
self.importer.run()
self.assertEqual(self.lib.items().get().title, "Applied Title 1")
self.assertIsNone(self.lib.albums().get())
def test_apply_tracks_adds_singleton_path(self):
self.assert_lib_dir_empty()
self.importer.add_choice(importer.action.TRACKS)
self.importer.add_choice(importer.action.APPLY)
self.importer.add_choice(importer.action.APPLY)
self.importer.run()
self.assert_file_in_lib(b"singletons", b"Applied Title 1.mp3")
class ImportCompilationTest(_common.TestCase, ImportHelper):
"""Test ASIS import of a folder containing tracks with different artists."""
def setUp(self):
self.setup_beets()
self._create_import_dir(3)
self._setup_import_session()
self.matcher = AutotagStub().install()
def tearDown(self):
self.teardown_beets()
self.matcher.restore()
def test_asis_homogenous_sets_albumartist(self):
self.importer.add_choice(importer.action.ASIS)
self.importer.run()
self.assertEqual(self.lib.albums().get().albumartist, "Tag Artist")
for item in self.lib.items():
self.assertEqual(item.albumartist, "Tag Artist")
def test_asis_heterogenous_sets_various_albumartist(self):
self.import_media[0].artist = "Other Artist"
self.import_media[0].save()
self.import_media[1].artist = "Another Artist"
self.import_media[1].save()
self.importer.add_choice(importer.action.ASIS)
self.importer.run()
self.assertEqual(self.lib.albums().get().albumartist, "Various Artists")
for item in self.lib.items():
self.assertEqual(item.albumartist, "Various Artists")
def test_asis_heterogenous_sets_compilation(self):
self.import_media[0].artist = "Other Artist"
self.import_media[0].save()
self.import_media[1].artist = "Another Artist"
self.import_media[1].save()
self.importer.add_choice(importer.action.ASIS)
self.importer.run()
for item in self.lib.items():
self.assertTrue(item.comp)
def test_asis_sets_majority_albumartist(self):
self.import_media[0].artist = "Other Artist"
self.import_media[0].save()
self.import_media[1].artist = "Other Artist"
self.import_media[1].save()
self.importer.add_choice(importer.action.ASIS)
self.importer.run()
self.assertEqual(self.lib.albums().get().albumartist, "Other Artist")
for item in self.lib.items():
self.assertEqual(item.albumartist, "Other Artist")
def test_asis_albumartist_tag_sets_albumartist(self):
self.import_media[0].artist = "Other Artist"
self.import_media[1].artist = "Another Artist"
for mediafile in self.import_media:
mediafile.albumartist = "Album Artist"
mediafile.mb_albumartistid = "Album Artist ID"
mediafile.save()
self.importer.add_choice(importer.action.ASIS)
self.importer.run()
self.assertEqual(self.lib.albums().get().albumartist, "Album Artist")
self.assertEqual(
self.lib.albums().get().mb_albumartistid, "Album Artist ID"
)
for item in self.lib.items():
self.assertEqual(item.albumartist, "Album Artist")
self.assertEqual(item.mb_albumartistid, "Album Artist ID")
def test_asis_albumartists_tag_sets_multi_albumartists(self):
self.import_media[0].artist = "Other Artist"
self.import_media[0].artists = ["Other Artist", "Other Artist 2"]
self.import_media[1].artist = "Another Artist"
self.import_media[1].artists = ["Another Artist", "Another Artist 2"]
for mediafile in self.import_media:
mediafile.albumartist = "Album Artist"
mediafile.albumartists = ["Album Artist 1", "Album Artist 2"]
mediafile.mb_albumartistid = "Album Artist ID"
mediafile.save()
self.importer.add_choice(importer.action.ASIS)
self.importer.run()
self.assertEqual(self.lib.albums().get().albumartist, "Album Artist")
self.assertEqual(
self.lib.albums().get().albumartists,
["Album Artist 1", "Album Artist 2"],
)
self.assertEqual(
self.lib.albums().get().mb_albumartistid, "Album Artist ID"
)
# Make sure both custom media items get tested
asserted_multi_artists_0 = False
asserted_multi_artists_1 = False
for item in self.lib.items():
self.assertEqual(item.albumartist, "Album Artist")
self.assertEqual(
item.albumartists, ["Album Artist 1", "Album Artist 2"]
)
self.assertEqual(item.mb_albumartistid, "Album Artist ID")
if item.artist == "Other Artist":
asserted_multi_artists_0 = True
self.assertEqual(
item.artists, ["Other Artist", "Other Artist 2"]
)
if item.artist == "Another Artist":
asserted_multi_artists_1 = True
self.assertEqual(
item.artists, ["Another Artist", "Another Artist 2"]
)
self.assertTrue(asserted_multi_artists_0 and asserted_multi_artists_1)
class ImportExistingTest(_common.TestCase, ImportHelper):
"""Test importing files that are already in the library directory."""
def setUp(self):
self.setup_beets()
self._create_import_dir(1)
self.matcher = AutotagStub().install()
self._setup_import_session()
self.setup_importer = self.importer
self.setup_importer.default_choice = importer.action.APPLY
self._setup_import_session(import_dir=self.libdir)
def tearDown(self):
self.teardown_beets()
self.matcher.restore()
def test_does_not_duplicate_item(self):
self.setup_importer.run()
self.assertEqual(len(self.lib.items()), 1)
self.importer.add_choice(importer.action.APPLY)
self.importer.run()
self.assertEqual(len(self.lib.items()), 1)
def test_does_not_duplicate_album(self):
self.setup_importer.run()
self.assertEqual(len(self.lib.albums()), 1)
self.importer.add_choice(importer.action.APPLY)
self.importer.run()
self.assertEqual(len(self.lib.albums()), 1)
def test_does_not_duplicate_singleton_track(self):
self.setup_importer.add_choice(importer.action.TRACKS)
self.setup_importer.add_choice(importer.action.APPLY)
self.setup_importer.run()
self.assertEqual(len(self.lib.items()), 1)
self.importer.add_choice(importer.action.TRACKS)
self.importer.add_choice(importer.action.APPLY)
self.importer.run()
self.assertEqual(len(self.lib.items()), 1)
def test_asis_updates_metadata(self):
self.setup_importer.run()
medium = MediaFile(self.lib.items().get().path)
medium.title = "New Title"
medium.save()
self.importer.add_choice(importer.action.ASIS)
self.importer.run()
self.assertEqual(self.lib.items().get().title, "New Title")
def test_asis_updated_moves_file(self):
self.setup_importer.run()
medium = MediaFile(self.lib.items().get().path)
medium.title = "New Title"
medium.save()
old_path = os.path.join(
b"Applied Artist", b"Applied Album", b"Applied Title 1.mp3"
)
self.assert_file_in_lib(old_path)
self.importer.add_choice(importer.action.ASIS)
self.importer.run()
self.assert_file_in_lib(
b"Applied Artist", b"Applied Album", b"New Title.mp3"
)
self.assert_file_not_in_lib(old_path)
def test_asis_updated_without_copy_does_not_move_file(self):
self.setup_importer.run()
medium = MediaFile(self.lib.items().get().path)
medium.title = "New Title"
medium.save()
old_path = os.path.join(
b"Applied Artist", b"Applied Album", b"Applied Title 1.mp3"
)
self.assert_file_in_lib(old_path)
config["import"]["copy"] = False
self.importer.add_choice(importer.action.ASIS)
self.importer.run()
self.assert_file_not_in_lib(
b"Applied Artist", b"Applied Album", b"New Title.mp3"
)
self.assert_file_in_lib(old_path)
def test_outside_file_is_copied(self):
config["import"]["copy"] = False
self.setup_importer.run()
self.assert_equal_path(
self.lib.items().get().path, self.import_media[0].path
)
config["import"]["copy"] = True
self._setup_import_session()
self.importer.add_choice(importer.action.APPLY)
self.importer.run()
new_path = os.path.join(
b"Applied Artist", b"Applied Album", b"Applied Title 1.mp3"
)
self.assert_file_in_lib(new_path)
self.assert_equal_path(
self.lib.items().get().path, os.path.join(self.libdir, new_path)
)
def test_outside_file_is_moved(self):
config["import"]["copy"] = False
self.setup_importer.run()
self.assert_equal_path(
self.lib.items().get().path, self.import_media[0].path
)
self._setup_import_session(move=True)
self.importer.add_choice(importer.action.APPLY)
self.importer.run()
self.assertNotExists(self.import_media[0].path)
class GroupAlbumsImportTest(_common.TestCase, ImportHelper):
def setUp(self):
self.setup_beets()
self._create_import_dir(3)
self.matcher = AutotagStub().install()
self.matcher.matching = AutotagStub.NONE
self._setup_import_session()
# Split tracks into two albums and use both as-is
self.importer.add_choice(importer.action.ALBUMS)
self.importer.add_choice(importer.action.ASIS)
self.importer.add_choice(importer.action.ASIS)
def tearDown(self):
self.teardown_beets()
self.matcher.restore()
def test_add_album_for_different_artist_and_different_album(self):
self.import_media[0].artist = "Artist B"
self.import_media[0].album = "Album B"
self.import_media[0].save()
self.importer.run()
albums = {album.album for album in self.lib.albums()}
self.assertEqual(albums, {"Album B", "Tag Album"})
def test_add_album_for_different_artist_and_same_albumartist(self):
self.import_media[0].artist = "Artist B"
self.import_media[0].albumartist = "Album Artist"
self.import_media[0].save()
self.import_media[1].artist = "Artist C"
self.import_media[1].albumartist = "Album Artist"
self.import_media[1].save()
self.importer.run()
artists = {album.albumartist for album in self.lib.albums()}
self.assertEqual(artists, {"Album Artist", "Tag Artist"})
def test_add_album_for_same_artist_and_different_album(self):
self.import_media[0].album = "Album B"
self.import_media[0].save()
self.importer.run()
albums = {album.album for album in self.lib.albums()}
self.assertEqual(albums, {"Album B", "Tag Album"})
def test_add_album_for_same_album_and_different_artist(self):
self.import_media[0].artist = "Artist B"
self.import_media[0].save()
self.importer.run()
artists = {album.albumartist for album in self.lib.albums()}
self.assertEqual(artists, {"Artist B", "Tag Artist"})
def test_incremental(self):
config["import"]["incremental"] = True
self.import_media[0].album = "Album B"
self.import_media[0].save()
self.importer.run()
albums = {album.album for album in self.lib.albums()}
self.assertEqual(albums, {"Album B", "Tag Album"})