forked from beetbox/beets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_library.py
1447 lines (1182 loc) · 48.9 KB
/
test_library.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 non-query database functions of Item.
"""
import os
import os.path
import re
import shutil
import stat
import sys
import time
import unicodedata
import unittest
from mediafile import MediaFile, UnreadableFileError
import beets.dbcore.query
import beets.library
from beets import config, plugins, util
from beets.test import _common
from beets.test._common import item
from beets.test.helper import TestHelper
from beets.util import bytestring_path, syspath
# Shortcut to path normalization.
np = util.normpath
class LoadTest(_common.LibTestCase):
def test_load_restores_data_from_db(self):
original_title = self.i.title
self.i.title = "something"
self.i.load()
self.assertEqual(original_title, self.i.title)
def test_load_clears_dirty_flags(self):
self.i.artist = "something"
self.assertIn("artist", self.i._dirty)
self.i.load()
self.assertNotIn("artist", self.i._dirty)
class StoreTest(_common.LibTestCase):
def test_store_changes_database_value(self):
self.i.year = 1987
self.i.store()
new_year = (
self.lib._connection()
.execute("select year from items where " 'title="the title"')
.fetchone()["year"]
)
self.assertEqual(new_year, 1987)
def test_store_only_writes_dirty_fields(self):
original_genre = self.i.genre
self.i._values_fixed["genre"] = "beatboxing" # change w/o dirtying
self.i.store()
new_genre = (
self.lib._connection()
.execute("select genre from items where " 'title="the title"')
.fetchone()["genre"]
)
self.assertEqual(new_genre, original_genre)
def test_store_clears_dirty_flags(self):
self.i.composer = "tvp"
self.i.store()
self.assertNotIn("composer", self.i._dirty)
def test_store_album_cascades_flex_deletes(self):
album = _common.album()
album.flex1 = "Flex-1"
self.lib.add(album)
item = _common.item()
item.album_id = album.id
item.flex1 = "Flex-1"
self.lib.add(item)
del album.flex1
album.store()
self.assertNotIn("flex1", album)
self.assertNotIn("flex1", album.items()[0])
class AddTest(_common.TestCase):
def setUp(self):
super().setUp()
self.lib = beets.library.Library(":memory:")
self.i = item()
def test_item_add_inserts_row(self):
self.lib.add(self.i)
new_grouping = (
self.lib._connection()
.execute(
"select grouping from items " 'where composer="the composer"'
)
.fetchone()["grouping"]
)
self.assertEqual(new_grouping, self.i.grouping)
def test_library_add_path_inserts_row(self):
i = beets.library.Item.from_path(
os.path.join(_common.RSRC, b"full.mp3")
)
self.lib.add(i)
new_grouping = (
self.lib._connection()
.execute(
"select grouping from items " 'where composer="the composer"'
)
.fetchone()["grouping"]
)
self.assertEqual(new_grouping, self.i.grouping)
class RemoveTest(_common.LibTestCase):
def test_remove_deletes_from_db(self):
self.i.remove()
c = self.lib._connection().execute("select * from items")
self.assertIsNone(c.fetchone())
class GetSetTest(_common.TestCase):
def setUp(self):
super().setUp()
self.i = item()
def test_set_changes_value(self):
self.i.bpm = 4915
self.assertEqual(self.i.bpm, 4915)
def test_set_sets_dirty_flag(self):
self.i.comp = not self.i.comp
self.assertIn("comp", self.i._dirty)
def test_set_does_not_dirty_if_value_unchanged(self):
self.i.title = self.i.title
self.assertNotIn("title", self.i._dirty)
def test_invalid_field_raises_attributeerror(self):
self.assertRaises(AttributeError, getattr, self.i, "xyzzy")
def test_album_fallback(self):
# integration test of item-album fallback
lib = beets.library.Library(":memory:")
i = item(lib)
album = lib.add_album([i])
album["flex"] = "foo"
album.store()
self.assertIn("flex", i)
self.assertNotIn("flex", i.keys(with_album=False))
self.assertEqual(i["flex"], "foo")
self.assertEqual(i.get("flex"), "foo")
self.assertIsNone(i.get("flex", with_album=False))
self.assertIsNone(i.get("flexx"))
class DestinationTest(_common.TestCase):
def setUp(self):
super().setUp()
# default directory is ~/Music and the only reason why it was switched
# to ~/.Music is to confirm that tests works well when path to
# temporary directory contains .
self.lib = beets.library.Library(":memory:", "~/.Music")
self.i = item(self.lib)
def tearDown(self):
super().tearDown()
self.lib._connection().close()
# Reset config if it was changed in test cases
config.clear()
config.read(user=False, defaults=True)
def test_directory_works_with_trailing_slash(self):
self.lib.directory = b"one/"
self.lib.path_formats = [("default", "two")]
self.assertEqual(self.i.destination(), np("one/two"))
def test_directory_works_without_trailing_slash(self):
self.lib.directory = b"one"
self.lib.path_formats = [("default", "two")]
self.assertEqual(self.i.destination(), np("one/two"))
def test_destination_substitutes_metadata_values(self):
self.lib.directory = b"base"
self.lib.path_formats = [("default", "$album/$artist $title")]
self.i.title = "three"
self.i.artist = "two"
self.i.album = "one"
self.assertEqual(self.i.destination(), np("base/one/two three"))
def test_destination_preserves_extension(self):
self.lib.directory = b"base"
self.lib.path_formats = [("default", "$title")]
self.i.path = "hey.audioformat"
self.assertEqual(self.i.destination(), np("base/the title.audioformat"))
def test_lower_case_extension(self):
self.lib.directory = b"base"
self.lib.path_formats = [("default", "$title")]
self.i.path = "hey.MP3"
self.assertEqual(self.i.destination(), np("base/the title.mp3"))
def test_destination_pads_some_indices(self):
self.lib.directory = b"base"
self.lib.path_formats = [
("default", "$track $tracktotal $disc $disctotal $bpm")
]
self.i.track = 1
self.i.tracktotal = 2
self.i.disc = 3
self.i.disctotal = 4
self.i.bpm = 5
self.assertEqual(self.i.destination(), np("base/01 02 03 04 5"))
def test_destination_pads_date_values(self):
self.lib.directory = b"base"
self.lib.path_formats = [("default", "$year-$month-$day")]
self.i.year = 1
self.i.month = 2
self.i.day = 3
self.assertEqual(self.i.destination(), np("base/0001-02-03"))
def test_destination_escapes_slashes(self):
self.i.album = "one/two"
dest = self.i.destination()
self.assertIn(b"one", dest)
self.assertIn(b"two", dest)
self.assertNotIn(b"one/two", dest)
def test_destination_escapes_leading_dot(self):
self.i.album = ".something"
dest = self.i.destination()
self.assertIn(b"something", dest)
self.assertNotIn(b"/.something", dest)
def test_destination_preserves_legitimate_slashes(self):
self.i.artist = "one"
self.i.album = "two"
dest = self.i.destination()
self.assertIn(os.path.join(b"one", b"two"), dest)
def test_destination_long_names_truncated(self):
self.i.title = "X" * 300
self.i.artist = "Y" * 300
for c in self.i.destination().split(util.PATH_SEP):
self.assertLessEqual(len(c), 255)
def test_destination_long_names_keep_extension(self):
self.i.title = "X" * 300
self.i.path = b"something.extn"
dest = self.i.destination()
self.assertEqual(dest[-5:], b".extn")
def test_distination_windows_removes_both_separators(self):
self.i.title = "one \\ two / three.mp3"
with _common.platform_windows():
p = self.i.destination()
self.assertNotIn(b"one \\ two", p)
self.assertNotIn(b"one / two", p)
self.assertNotIn(b"two \\ three", p)
self.assertNotIn(b"two / three", p)
def test_path_with_format(self):
self.lib.path_formats = [("default", "$artist/$album ($format)")]
p = self.i.destination()
self.assertIn(b"(FLAC)", p)
def test_heterogeneous_album_gets_single_directory(self):
i1, i2 = item(), item()
self.lib.add_album([i1, i2])
i1.year, i2.year = 2009, 2010
self.lib.path_formats = [("default", "$album ($year)/$track $title")]
dest1, dest2 = i1.destination(), i2.destination()
self.assertEqual(os.path.dirname(dest1), os.path.dirname(dest2))
def test_default_path_for_non_compilations(self):
self.i.comp = False
self.lib.add_album([self.i])
self.lib.directory = b"one"
self.lib.path_formats = [("default", "two"), ("comp:true", "three")]
self.assertEqual(self.i.destination(), np("one/two"))
def test_singleton_path(self):
i = item(self.lib)
self.lib.directory = b"one"
self.lib.path_formats = [
("default", "two"),
("singleton:true", "four"),
("comp:true", "three"),
]
self.assertEqual(i.destination(), np("one/four"))
def test_comp_before_singleton_path(self):
i = item(self.lib)
i.comp = True
self.lib.directory = b"one"
self.lib.path_formats = [
("default", "two"),
("comp:true", "three"),
("singleton:true", "four"),
]
self.assertEqual(i.destination(), np("one/three"))
def test_comp_path(self):
self.i.comp = True
self.lib.add_album([self.i])
self.lib.directory = b"one"
self.lib.path_formats = [
("default", "two"),
("comp:true", "three"),
]
self.assertEqual(self.i.destination(), np("one/three"))
def test_albumtype_query_path(self):
self.i.comp = True
self.lib.add_album([self.i])
self.i.albumtype = "sometype"
self.lib.directory = b"one"
self.lib.path_formats = [
("default", "two"),
("albumtype:sometype", "four"),
("comp:true", "three"),
]
self.assertEqual(self.i.destination(), np("one/four"))
def test_albumtype_path_fallback_to_comp(self):
self.i.comp = True
self.lib.add_album([self.i])
self.i.albumtype = "sometype"
self.lib.directory = b"one"
self.lib.path_formats = [
("default", "two"),
("albumtype:anothertype", "four"),
("comp:true", "three"),
]
self.assertEqual(self.i.destination(), np("one/three"))
def test_get_formatted_does_not_replace_separators(self):
with _common.platform_posix():
name = os.path.join("a", "b")
self.i.title = name
newname = self.i.formatted().get("title")
self.assertEqual(name, newname)
def test_get_formatted_pads_with_zero(self):
with _common.platform_posix():
self.i.track = 1
name = self.i.formatted().get("track")
self.assertTrue(name.startswith("0"))
def test_get_formatted_uses_kbps_bitrate(self):
with _common.platform_posix():
self.i.bitrate = 12345
val = self.i.formatted().get("bitrate")
self.assertEqual(val, "12kbps")
def test_get_formatted_uses_khz_samplerate(self):
with _common.platform_posix():
self.i.samplerate = 12345
val = self.i.formatted().get("samplerate")
self.assertEqual(val, "12kHz")
def test_get_formatted_datetime(self):
with _common.platform_posix():
self.i.added = 1368302461.210265
val = self.i.formatted().get("added")
self.assertTrue(val.startswith("2013"))
def test_get_formatted_none(self):
with _common.platform_posix():
self.i.some_other_field = None
val = self.i.formatted().get("some_other_field")
self.assertEqual(val, "")
def test_artist_falls_back_to_albumartist(self):
self.i.artist = ""
self.i.albumartist = "something"
self.lib.path_formats = [("default", "$artist")]
p = self.i.destination()
self.assertEqual(p.rsplit(util.PATH_SEP, 1)[1], b"something")
def test_albumartist_falls_back_to_artist(self):
self.i.artist = "trackartist"
self.i.albumartist = ""
self.lib.path_formats = [("default", "$albumartist")]
p = self.i.destination()
self.assertEqual(p.rsplit(util.PATH_SEP, 1)[1], b"trackartist")
def test_artist_overrides_albumartist(self):
self.i.artist = "theartist"
self.i.albumartist = "something"
self.lib.path_formats = [("default", "$artist")]
p = self.i.destination()
self.assertEqual(p.rsplit(util.PATH_SEP, 1)[1], b"theartist")
def test_albumartist_overrides_artist(self):
self.i.artist = "theartist"
self.i.albumartist = "something"
self.lib.path_formats = [("default", "$albumartist")]
p = self.i.destination()
self.assertEqual(p.rsplit(util.PATH_SEP, 1)[1], b"something")
def test_unicode_normalized_nfd_on_mac(self):
instr = unicodedata.normalize("NFC", "caf\xe9")
self.lib.path_formats = [("default", instr)]
dest = self.i.destination(platform="darwin", fragment=True)
self.assertEqual(dest, unicodedata.normalize("NFD", instr))
def test_unicode_normalized_nfc_on_linux(self):
instr = unicodedata.normalize("NFD", "caf\xe9")
self.lib.path_formats = [("default", instr)]
dest = self.i.destination(platform="linux", fragment=True)
self.assertEqual(dest, unicodedata.normalize("NFC", instr))
def test_non_mbcs_characters_on_windows(self):
oldfunc = sys.getfilesystemencoding
sys.getfilesystemencoding = lambda: "mbcs"
try:
self.i.title = "h\u0259d"
self.lib.path_formats = [("default", "$title")]
p = self.i.destination()
self.assertNotIn(b"?", p)
# We use UTF-8 to encode Windows paths now.
self.assertIn("h\u0259d".encode(), p)
finally:
sys.getfilesystemencoding = oldfunc
def test_unicode_extension_in_fragment(self):
self.lib.path_formats = [("default", "foo")]
self.i.path = util.bytestring_path("bar.caf\xe9")
dest = self.i.destination(platform="linux", fragment=True)
self.assertEqual(dest, "foo.caf\xe9")
def test_asciify_and_replace(self):
config["asciify_paths"] = True
self.lib.replacements = [(re.compile('"'), "q")]
self.lib.directory = b"lib"
self.lib.path_formats = [("default", "$title")]
self.i.title = "\u201c\u00f6\u2014\u00cf\u201d"
self.assertEqual(self.i.destination(), np("lib/qo--Iq"))
def test_asciify_character_expanding_to_slash(self):
config["asciify_paths"] = True
self.lib.directory = b"lib"
self.lib.path_formats = [("default", "$title")]
self.i.title = "ab\xa2\xbdd"
self.assertEqual(self.i.destination(), np("lib/abC_ 1_2d"))
def test_destination_with_replacements(self):
self.lib.directory = b"base"
self.lib.replacements = [(re.compile(r"a"), "e")]
self.lib.path_formats = [("default", "$album/$title")]
self.i.title = "foo"
self.i.album = "bar"
self.assertEqual(self.i.destination(), np("base/ber/foo"))
def test_destination_with_replacements_argument(self):
self.lib.directory = b"base"
self.lib.replacements = [(re.compile(r"a"), "f")]
self.lib.path_formats = [("default", "$album/$title")]
self.i.title = "foo"
self.i.album = "bar"
replacements = [(re.compile(r"a"), "e")]
self.assertEqual(
self.i.destination(replacements=replacements), np("base/ber/foo")
)
@unittest.skip("unimplemented: #359")
def test_destination_with_empty_component(self):
self.lib.directory = b"base"
self.lib.replacements = [(re.compile(r"^$"), "_")]
self.lib.path_formats = [("default", "$album/$artist/$title")]
self.i.title = "three"
self.i.artist = ""
self.i.albumartist = ""
self.i.album = "one"
self.assertEqual(self.i.destination(), np("base/one/_/three"))
@unittest.skip("unimplemented: #359")
def test_destination_with_empty_final_component(self):
self.lib.directory = b"base"
self.lib.replacements = [(re.compile(r"^$"), "_")]
self.lib.path_formats = [("default", "$album/$title")]
self.i.title = ""
self.i.album = "one"
self.i.path = "foo.mp3"
self.assertEqual(self.i.destination(), np("base/one/_.mp3"))
def test_legalize_path_one_for_one_replacement(self):
# Use a replacement that should always replace the last X in any
# path component with a Z.
self.lib.replacements = [
(re.compile(r"X$"), "Z"),
]
# Construct an item whose untruncated path ends with a Y but whose
# truncated version ends with an X.
self.i.title = "X" * 300 + "Y"
# The final path should reflect the replacement.
dest = self.i.destination()
self.assertEqual(dest[-2:], b"XZ")
def test_legalize_path_one_for_many_replacement(self):
# Use a replacement that should always replace the last X in any
# path component with four Zs.
self.lib.replacements = [
(re.compile(r"X$"), "ZZZZ"),
]
# Construct an item whose untruncated path ends with a Y but whose
# truncated version ends with an X.
self.i.title = "X" * 300 + "Y"
# The final path should ignore the user replacement and create a path
# of the correct length, containing Xs.
dest = self.i.destination()
self.assertEqual(dest[-2:], b"XX")
def test_album_field_query(self):
self.lib.directory = b"one"
self.lib.path_formats = [("default", "two"), ("flex:foo", "three")]
album = self.lib.add_album([self.i])
self.assertEqual(self.i.destination(), np("one/two"))
album["flex"] = "foo"
album.store()
self.assertEqual(self.i.destination(), np("one/three"))
def test_album_field_in_template(self):
self.lib.directory = b"one"
self.lib.path_formats = [("default", "$flex/two")]
album = self.lib.add_album([self.i])
album["flex"] = "foo"
album.store()
self.assertEqual(self.i.destination(), np("one/foo/two"))
class ItemFormattedMappingTest(_common.LibTestCase):
def test_formatted_item_value(self):
formatted = self.i.formatted()
self.assertEqual(formatted["artist"], "the artist")
def test_get_unset_field(self):
formatted = self.i.formatted()
with self.assertRaises(KeyError):
formatted["other_field"]
def test_get_method_with_default(self):
formatted = self.i.formatted()
self.assertEqual(formatted.get("other_field"), "")
def test_get_method_with_specified_default(self):
formatted = self.i.formatted()
self.assertEqual(formatted.get("other_field", "default"), "default")
def test_item_precedence(self):
album = self.lib.add_album([self.i])
album["artist"] = "foo"
album.store()
self.assertNotEqual("foo", self.i.formatted().get("artist"))
def test_album_flex_field(self):
album = self.lib.add_album([self.i])
album["flex"] = "foo"
album.store()
self.assertEqual("foo", self.i.formatted().get("flex"))
def test_album_field_overrides_item_field_for_path(self):
# Make the album inconsistent with the item.
album = self.lib.add_album([self.i])
album.album = "foo"
album.store()
self.i.album = "bar"
self.i.store()
# Ensure the album takes precedence.
formatted = self.i.formatted(for_path=True)
self.assertEqual(formatted["album"], "foo")
def test_artist_falls_back_to_albumartist(self):
self.i.artist = ""
formatted = self.i.formatted()
self.assertEqual(formatted["artist"], "the album artist")
def test_albumartist_falls_back_to_artist(self):
self.i.albumartist = ""
formatted = self.i.formatted()
self.assertEqual(formatted["albumartist"], "the artist")
def test_both_artist_and_albumartist_empty(self):
self.i.artist = ""
self.i.albumartist = ""
formatted = self.i.formatted()
self.assertEqual(formatted["albumartist"], "")
class PathFormattingMixin:
"""Utilities for testing path formatting."""
def _setf(self, fmt):
self.lib.path_formats.insert(0, ("default", fmt))
def _assert_dest(self, dest, i=None):
if i is None:
i = self.i
with _common.platform_posix():
actual = i.destination()
self.assertEqual(actual, dest)
class DestinationFunctionTest(_common.TestCase, PathFormattingMixin):
def setUp(self):
super().setUp()
self.lib = beets.library.Library(":memory:")
self.lib.directory = b"/base"
self.lib.path_formats = [("default", "path")]
self.i = item(self.lib)
def tearDown(self):
super().tearDown()
self.lib._connection().close()
def test_upper_case_literal(self):
self._setf("%upper{foo}")
self._assert_dest(b"/base/FOO")
def test_upper_case_variable(self):
self._setf("%upper{$title}")
self._assert_dest(b"/base/THE TITLE")
def test_title_case_variable(self):
self._setf("%title{$title}")
self._assert_dest(b"/base/The Title")
def test_title_case_variable_aphostrophe(self):
self._setf("%title{I can't}")
self._assert_dest(b"/base/I Can't")
def test_asciify_variable(self):
self._setf("%asciify{ab\xa2\xbdd}")
self._assert_dest(b"/base/abC_ 1_2d")
def test_left_variable(self):
self._setf("%left{$title, 3}")
self._assert_dest(b"/base/the")
def test_right_variable(self):
self._setf("%right{$title,3}")
self._assert_dest(b"/base/tle")
def test_if_false(self):
self._setf("x%if{,foo}")
self._assert_dest(b"/base/x")
def test_if_false_value(self):
self._setf("x%if{false,foo}")
self._assert_dest(b"/base/x")
def test_if_true(self):
self._setf("%if{bar,foo}")
self._assert_dest(b"/base/foo")
def test_if_else_false(self):
self._setf("%if{,foo,baz}")
self._assert_dest(b"/base/baz")
def test_if_else_false_value(self):
self._setf("%if{false,foo,baz}")
self._assert_dest(b"/base/baz")
def test_if_int_value(self):
self._setf("%if{0,foo,baz}")
self._assert_dest(b"/base/baz")
def test_nonexistent_function(self):
self._setf("%foo{bar}")
self._assert_dest(b"/base/%foo{bar}")
def test_if_def_field_return_self(self):
self.i.bar = 3
self._setf("%ifdef{bar}")
self._assert_dest(b"/base/3")
def test_if_def_field_not_defined(self):
self._setf(" %ifdef{bar}/$artist")
self._assert_dest(b"/base/the artist")
def test_if_def_field_not_defined_2(self):
self._setf("$artist/%ifdef{bar}")
self._assert_dest(b"/base/the artist")
def test_if_def_true(self):
self._setf("%ifdef{artist,cool}")
self._assert_dest(b"/base/cool")
def test_if_def_true_complete(self):
self.i.series = "Now"
self._setf("%ifdef{series,$series Series,Albums}/$album")
self._assert_dest(b"/base/Now Series/the album")
def test_if_def_false_complete(self):
self._setf("%ifdef{plays,$plays,not_played}")
self._assert_dest(b"/base/not_played")
def test_first(self):
self.i.genres = "Pop; Rock; Classical Crossover"
self._setf("%first{$genres}")
self._assert_dest(b"/base/Pop")
def test_first_skip(self):
self.i.genres = "Pop; Rock; Classical Crossover"
self._setf("%first{$genres,1,2}")
self._assert_dest(b"/base/Classical Crossover")
def test_first_different_sep(self):
self._setf("%first{Alice / Bob / Eve,2,0, / , & }")
self._assert_dest(b"/base/Alice & Bob")
class DisambiguationTest(_common.TestCase, PathFormattingMixin):
def setUp(self):
super().setUp()
self.lib = beets.library.Library(":memory:")
self.lib.directory = b"/base"
self.lib.path_formats = [("default", "path")]
self.i1 = item()
self.i1.year = 2001
self.lib.add_album([self.i1])
self.i2 = item()
self.i2.year = 2002
self.lib.add_album([self.i2])
self.lib._connection().commit()
self._setf("foo%aunique{albumartist album,year}/$title")
def tearDown(self):
super().tearDown()
self.lib._connection().close()
def test_unique_expands_to_disambiguating_year(self):
self._assert_dest(b"/base/foo [2001]/the title", self.i1)
def test_unique_with_default_arguments_uses_albumtype(self):
album2 = self.lib.get_album(self.i1)
album2.albumtype = "bar"
album2.store()
self._setf("foo%aunique{}/$title")
self._assert_dest(b"/base/foo [bar]/the title", self.i1)
def test_unique_expands_to_nothing_for_distinct_albums(self):
album2 = self.lib.get_album(self.i2)
album2.album = "different album"
album2.store()
self._assert_dest(b"/base/foo/the title", self.i1)
def test_use_fallback_numbers_when_identical(self):
album2 = self.lib.get_album(self.i2)
album2.year = 2001
album2.store()
self._assert_dest(b"/base/foo [1]/the title", self.i1)
self._assert_dest(b"/base/foo [2]/the title", self.i2)
def test_unique_falls_back_to_second_distinguishing_field(self):
self._setf("foo%aunique{albumartist album,month year}/$title")
self._assert_dest(b"/base/foo [2001]/the title", self.i1)
def test_unique_sanitized(self):
album2 = self.lib.get_album(self.i2)
album2.year = 2001
album1 = self.lib.get_album(self.i1)
album1.albumtype = "foo/bar"
album2.store()
album1.store()
self._setf("foo%aunique{albumartist album,albumtype}/$title")
self._assert_dest(b"/base/foo [foo_bar]/the title", self.i1)
def test_drop_empty_disambig_string(self):
album1 = self.lib.get_album(self.i1)
album1.albumdisambig = None
album2 = self.lib.get_album(self.i2)
album2.albumdisambig = "foo"
album1.store()
album2.store()
self._setf("foo%aunique{albumartist album,albumdisambig}/$title")
self._assert_dest(b"/base/foo/the title", self.i1)
def test_change_brackets(self):
self._setf("foo%aunique{albumartist album,year,()}/$title")
self._assert_dest(b"/base/foo (2001)/the title", self.i1)
def test_remove_brackets(self):
self._setf("foo%aunique{albumartist album,year,}/$title")
self._assert_dest(b"/base/foo 2001/the title", self.i1)
def test_key_flexible_attribute(self):
album1 = self.lib.get_album(self.i1)
album1.flex = "flex1"
album2 = self.lib.get_album(self.i2)
album2.flex = "flex2"
album1.store()
album2.store()
self._setf("foo%aunique{albumartist album flex,year}/$title")
self._assert_dest(b"/base/foo/the title", self.i1)
class SingletonDisambiguationTest(_common.TestCase, PathFormattingMixin):
def setUp(self):
super().setUp()
self.lib = beets.library.Library(":memory:")
self.lib.directory = b"/base"
self.lib.path_formats = [("default", "path")]
self.i1 = item()
self.i1.year = 2001
self.lib.add(self.i1)
self.i2 = item()
self.i2.year = 2002
self.lib.add(self.i2)
self.lib._connection().commit()
self._setf("foo/$title%sunique{artist title,year}")
def tearDown(self):
super().tearDown()
self.lib._connection().close()
def test_sunique_expands_to_disambiguating_year(self):
self._assert_dest(b"/base/foo/the title [2001]", self.i1)
def test_sunique_with_default_arguments_uses_trackdisambig(self):
self.i1.trackdisambig = "live version"
self.i1.year = self.i2.year
self.i1.store()
self._setf("foo/$title%sunique{}")
self._assert_dest(b"/base/foo/the title [live version]", self.i1)
def test_sunique_expands_to_nothing_for_distinct_singletons(self):
self.i2.title = "different track"
self.i2.store()
self._assert_dest(b"/base/foo/the title", self.i1)
def test_sunique_does_not_match_album(self):
self.lib.add_album([self.i2])
self._assert_dest(b"/base/foo/the title", self.i1)
def test_sunique_use_fallback_numbers_when_identical(self):
self.i2.year = self.i1.year
self.i2.store()
self._assert_dest(b"/base/foo/the title [1]", self.i1)
self._assert_dest(b"/base/foo/the title [2]", self.i2)
def test_sunique_falls_back_to_second_distinguishing_field(self):
self._setf("foo/$title%sunique{albumartist album,month year}")
self._assert_dest(b"/base/foo/the title [2001]", self.i1)
def test_sunique_sanitized(self):
self.i2.year = self.i1.year
self.i1.trackdisambig = "foo/bar"
self.i2.store()
self.i1.store()
self._setf("foo/$title%sunique{artist title,trackdisambig}")
self._assert_dest(b"/base/foo/the title [foo_bar]", self.i1)
def test_drop_empty_disambig_string(self):
self.i1.trackdisambig = None
self.i2.trackdisambig = "foo"
self.i1.store()
self.i2.store()
self._setf("foo/$title%sunique{albumartist album,trackdisambig}")
self._assert_dest(b"/base/foo/the title", self.i1)
def test_change_brackets(self):
self._setf("foo/$title%sunique{artist title,year,()}")
self._assert_dest(b"/base/foo/the title (2001)", self.i1)
def test_remove_brackets(self):
self._setf("foo/$title%sunique{artist title,year,}")
self._assert_dest(b"/base/foo/the title 2001", self.i1)
def test_key_flexible_attribute(self):
self.i1.flex = "flex1"
self.i2.flex = "flex2"
self.i1.store()
self.i2.store()
self._setf("foo/$title%sunique{artist title flex,year}")
self._assert_dest(b"/base/foo/the title", self.i1)
class PluginDestinationTest(_common.TestCase):
def setUp(self):
super().setUp()
# Mock beets.plugins.item_field_getters.
self._tv_map = {}
def field_getters():
getters = {}
for key, value in self._tv_map.items():
getters[key] = lambda _: value
return getters
self.old_field_getters = plugins.item_field_getters
plugins.item_field_getters = field_getters
self.lib = beets.library.Library(":memory:")
self.lib.directory = b"/base"
self.lib.path_formats = [("default", "$artist $foo")]
self.i = item(self.lib)
def tearDown(self):
super().tearDown()
plugins.item_field_getters = self.old_field_getters
def _assert_dest(self, dest):
with _common.platform_posix():
the_dest = self.i.destination()
self.assertEqual(the_dest, b"/base/" + dest)
def test_undefined_value_not_substituted(self):
self._assert_dest(b"the artist $foo")
def test_plugin_value_not_substituted(self):
self._tv_map = {
"foo": "bar",
}
self._assert_dest(b"the artist bar")
def test_plugin_value_overrides_attribute(self):
self._tv_map = {
"artist": "bar",
}
self._assert_dest(b"bar $foo")
def test_plugin_value_sanitized(self):
self._tv_map = {
"foo": "bar/baz",
}
self._assert_dest(b"the artist bar_baz")
class AlbumInfoTest(_common.TestCase):
def setUp(self):
super().setUp()
self.lib = beets.library.Library(":memory:")
self.i = item()
self.lib.add_album((self.i,))
def test_albuminfo_reflects_metadata(self):
ai = self.lib.get_album(self.i)
self.assertEqual(ai.mb_albumartistid, self.i.mb_albumartistid)
self.assertEqual(ai.albumartist, self.i.albumartist)
self.assertEqual(ai.album, self.i.album)
self.assertEqual(ai.year, self.i.year)
def test_albuminfo_stores_art(self):
ai = self.lib.get_album(self.i)
ai.artpath = "/my/great/art"
ai.store()
new_ai = self.lib.get_album(self.i)
self.assertEqual(new_ai.artpath, b"/my/great/art")
def test_albuminfo_for_two_items_doesnt_duplicate_row(self):
i2 = item(self.lib)
self.lib.get_album(self.i)
self.lib.get_album(i2)
c = self.lib._connection().cursor()
c.execute("select * from albums where album=?", (self.i.album,))
# Cursor should only return one row.
self.assertIsNotNone(c.fetchone())
self.assertIsNone(c.fetchone())
def test_individual_tracks_have_no_albuminfo(self):
i2 = item()
i2.album = "aTotallyDifferentAlbum"
self.lib.add(i2)
ai = self.lib.get_album(i2)
self.assertIsNone(ai)