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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
|
# 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
import pytest
from mediafile import MediaFile, UnreadableFileError
import beets.dbcore.query
import beets.library
from beets import config, plugins, util
from beets.library import Album
from beets.test import _common
from beets.test._common import item
from beets.test.helper import BeetsTestCase, ItemInDBTestCase
from beets.util import bytestring_path, syspath
# Shortcut to path normalization.
np = util.normpath
class LoadTest(ItemInDBTestCase):
def test_load_restores_data_from_db(self):
original_title = self.i.title
self.i.title = "something"
self.i.load()
assert original_title == self.i.title
def test_load_clears_dirty_flags(self):
self.i.artist = "something"
assert "artist" in self.i._dirty
self.i.load()
assert "artist" not in self.i._dirty
class StoreTest(ItemInDBTestCase):
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 = ?", (self.i.title,))
.fetchone()["year"]
)
assert 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 = ?", (self.i.title,))
.fetchone()["genre"]
)
assert new_genre == original_genre
def test_store_clears_dirty_flags(self):
self.i.composer = "tvp"
self.i.store()
assert "composer" not in self.i._dirty
def test_store_album_cascades_flex_deletes(self):
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()
assert "flex1" not in album
assert "flex1" not in album.items()[0]
class AddTest(BeetsTestCase):
def setUp(self):
super().setUp()
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 = ?",
(self.i.composer,),
)
.fetchone()["grouping"]
)
assert 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 = ?",
(self.i.composer,),
)
.fetchone()["grouping"]
)
assert new_grouping == self.i.grouping
class RemoveTest(ItemInDBTestCase):
def test_remove_deletes_from_db(self):
self.i.remove()
c = self.lib._connection().execute("select * from items")
assert c.fetchone() is None
class GetSetTest(BeetsTestCase):
def setUp(self):
super().setUp()
self.i = item()
def test_set_changes_value(self):
self.i.bpm = 4915
assert self.i.bpm == 4915
def test_set_sets_dirty_flag(self):
self.i.comp = not self.i.comp
assert "comp" in self.i._dirty
def test_set_does_not_dirty_if_value_unchanged(self):
self.i.title = self.i.title
assert "title" not in self.i._dirty
def test_invalid_field_raises_attributeerror(self):
with pytest.raises(AttributeError):
self.i.xyzzy
def test_album_fallback(self):
# integration test of item-album fallback
i = item(self.lib)
album = self.lib.add_album([i])
album["flex"] = "foo"
album.store()
assert "flex" in i
assert "flex" not in i.keys(with_album=False)
assert i["flex"] == "foo"
assert i.get("flex") == "foo"
assert i.get("flex", with_album=False) is None
assert i.get("flexx") is None
class DestinationTest(BeetsTestCase):
"""Confirm tests handle temporary directory path containing '.'"""
def create_temp_dir(self, **kwargs):
kwargs["prefix"] = "."
super().create_temp_dir(**kwargs)
def setUp(self):
super().setUp()
self.i = item(self.lib)
def test_directory_works_with_trailing_slash(self):
self.lib.directory = b"one/"
self.lib.path_formats = [("default", "two")]
assert 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")]
assert 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"
assert 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"
assert 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"
assert 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
assert 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
assert self.i.destination() == np("base/0001-02-03")
def test_destination_escapes_slashes(self):
self.i.album = "one/two"
dest = self.i.destination()
assert b"one" in dest
assert b"two" in dest
assert b"one/two" not in dest
def test_destination_escapes_leading_dot(self):
self.i.album = ".something"
dest = self.i.destination()
assert b"something" in dest
assert b"/.something" not in dest
def test_destination_preserves_legitimate_slashes(self):
self.i.artist = "one"
self.i.album = "two"
dest = self.i.destination()
assert os.path.join(b"one", b"two") in 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):
assert 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()
assert 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()
assert b"one \\ two" not in p
assert b"one / two" not in p
assert b"two \\ three" not in p
assert b"two / three" not in p
def test_path_with_format(self):
self.lib.path_formats = [("default", "$artist/$album ($format)")]
p = self.i.destination()
assert b"(FLAC)" in 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()
assert 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")]
assert 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"),
]
assert 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"),
]
assert 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")]
assert 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"),
]
assert 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"),
]
assert 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")
assert name == newname
def test_get_formatted_pads_with_zero(self):
with _common.platform_posix():
self.i.track = 1
name = self.i.formatted().get("track")
assert 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")
assert val == "12kbps"
def test_get_formatted_uses_khz_samplerate(self):
with _common.platform_posix():
self.i.samplerate = 12345
val = self.i.formatted().get("samplerate")
assert val == "12kHz"
def test_get_formatted_datetime(self):
with _common.platform_posix():
self.i.added = 1368302461.210265
val = self.i.formatted().get("added")
assert 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")
assert 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()
assert 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()
assert 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()
assert 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()
assert 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)
assert 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)
assert 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()
assert b"?" not in p
# We use UTF-8 to encode Windows paths now.
assert "h\u0259d".encode() in 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)
assert 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"
assert 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"
assert 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"
assert 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")]
assert 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"
assert 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"
assert 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()
assert 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()
assert 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])
assert self.i.destination() == np("one/two")
album["flex"] = "foo"
album.store()
assert 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()
assert self.i.destination() == np("one/foo/two")
class ItemFormattedMappingTest(ItemInDBTestCase):
def test_formatted_item_value(self):
formatted = self.i.formatted()
assert formatted["artist"] == "the artist"
def test_get_unset_field(self):
formatted = self.i.formatted()
with pytest.raises(KeyError):
formatted["other_field"]
def test_get_method_with_default(self):
formatted = self.i.formatted()
assert formatted.get("other_field") == ""
def test_get_method_with_specified_default(self):
formatted = self.i.formatted()
assert formatted.get("other_field", "default") == "default"
def test_item_precedence(self):
album = self.lib.add_album([self.i])
album["artist"] = "foo"
album.store()
assert "foo" != self.i.formatted().get("artist")
def test_album_flex_field(self):
album = self.lib.add_album([self.i])
album["flex"] = "foo"
album.store()
assert "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)
assert formatted["album"] == "foo"
def test_artist_falls_back_to_albumartist(self):
self.i.artist = ""
formatted = self.i.formatted()
assert formatted["artist"] == "the album artist"
def test_albumartist_falls_back_to_artist(self):
self.i.albumartist = ""
formatted = self.i.formatted()
assert formatted["albumartist"] == "the artist"
def test_both_artist_and_albumartist_empty(self):
self.i.artist = ""
self.i.albumartist = ""
formatted = self.i.formatted()
assert 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()
assert actual == dest
class DestinationFunctionTest(BeetsTestCase, PathFormattingMixin):
def setUp(self):
super().setUp()
self.lib.directory = b"/base"
self.lib.path_formats = [("default", "path")]
self.i = item(self.lib)
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_capitalize_variable(self):
self._setf("%capitalize{$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(BeetsTestCase, PathFormattingMixin):
def setUp(self):
super().setUp()
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 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(BeetsTestCase, PathFormattingMixin):
def setUp(self):
super().setUp()
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 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(BeetsTestCase):
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.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()
assert 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(BeetsTestCase):
def setUp(self):
super().setUp()
self.i = item()
self.lib.add_album((self.i,))
def test_albuminfo_reflects_metadata(self):
ai = self.lib.get_album(self.i)
assert ai.mb_albumartistid == self.i.mb_albumartistid
assert ai.albumartist == self.i.albumartist
assert ai.album == self.i.album
assert 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)
assert 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.
assert c.fetchone() is not None
assert c.fetchone() is None
def test_individual_tracks_have_no_albuminfo(self):
i2 = item()
i2.album = "aTotallyDifferentAlbum"
self.lib.add(i2)
ai = self.lib.get_album(i2)
assert ai is None
def test_get_album_by_id(self):
ai = self.lib.get_album(self.i)
ai = self.lib.get_album(self.i.id)
assert ai is not None
def test_album_items_consistent(self):
ai = self.lib.get_album(self.i)
for i in ai.items():
if i.id == self.i.id:
break
else:
self.fail("item not found")
def test_albuminfo_changes_affect_items(self):
ai = self.lib.get_album(self.i)
ai.album = "myNewAlbum"
ai.store()
i = self.lib.items()[0]
assert i.album == "myNewAlbum"
def test_albuminfo_change_albumartist_changes_items(self):
ai = self.lib.get_album(self.i)
ai.albumartist = "myNewArtist"
ai.store()
i = self.lib.items()[0]
assert i.albumartist == "myNewArtist"
assert i.artist != "myNewArtist"
def test_albuminfo_change_artist_does_change_items(self):
ai = self.lib.get_album(self.i)
ai.artist = "myNewArtist"
ai.store(inherit=True)
i = self.lib.items()[0]
assert i.artist == "myNewArtist"
def test_albuminfo_change_artist_does_not_change_items(self):
ai = self.lib.get_album(self.i)
ai.artist = "myNewArtist"
ai.store(inherit=False)
i = self.lib.items()[0]
assert i.artist != "myNewArtist"
def test_albuminfo_remove_removes_items(self):
item_id = self.i.id
self.lib.get_album(self.i).remove()
c = self.lib._connection().execute(
"SELECT id FROM items WHERE id=?", (item_id,)
)
assert c.fetchone() is None
def test_removing_last_item_removes_album(self):
assert len(self.lib.albums()) == 1
self.i.remove()
assert len(self.lib.albums()) == 0
def test_noop_albuminfo_changes_affect_items(self):
i = self.lib.items()[0]
i.album = "foobar"
i.store()
ai = self.lib.get_album(self.i)
ai.album = ai.album
ai.store()
i = self.lib.items()[0]
assert i.album == ai.album
class ArtDestinationTest(BeetsTestCase):
def setUp(self):
super().setUp()
config["art_filename"] = "artimage"
config["replace"] = {"X": "Y"}
self.lib.replacements = [(re.compile("X"), "Y")]
self.i = item(self.lib)
self.i.path = self.i.destination()
self.ai = self.lib.add_album((self.i,))
def test_art_filename_respects_setting(self):
art = self.ai.art_destination("something.jpg")
new_art = bytestring_path("%sartimage.jpg" % os.path.sep)
assert new_art in art
def test_art_path_in_item_dir(self):
art = self.ai.art_destination("something.jpg")
track = self.i.destination()
assert os.path.dirname(art) == os.path.dirname(track)
def test_art_path_sanitized(self):
config["art_filename"] = "artXimage"
art = self.ai.art_destination("something.jpg")
assert b"artYimage" in art
class PathStringTest(BeetsTestCase):
def setUp(self):
super().setUp()
self.i = item(self.lib)
def test_item_path_is_bytestring(self):
assert isinstance(self.i.path, bytes)
def test_fetched_item_path_is_bytestring(self):
i = list(self.lib.items())[0]
assert isinstance(i.path, bytes)
def test_unicode_path_becomes_bytestring(self):
self.i.path = "unicodepath"
assert isinstance(self.i.path, bytes)
def test_unicode_in_database_becomes_bytestring(self):
self.lib._connection().execute(
"""
update items set path=? where id=?
""",
(self.i.id, "somepath"),
)
i = list(self.lib.items())[0]
assert isinstance(i.path, bytes)
def test_special_chars_preserved_in_database(self):
path = "b\xe1r".encode()
self.i.path = path
self.i.store()
i = list(self.lib.items())[0]
assert i.path == path
def test_special_char_path_added_to_database(self):
self.i.remove()
path = "b\xe1r".encode()
i = item()
i.path = path
self.lib.add(i)
i = list(self.lib.items())[0]
assert i.path == path
def test_destination_returns_bytestring(self):
self.i.artist = "b\xe1r"
dest = self.i.destination()
assert isinstance(dest, bytes)
def test_art_destination_returns_bytestring(self):
self.i.artist = "b\xe1r"
alb = self.lib.add_album([self.i])
dest = alb.art_destination("image.jpg")
assert isinstance(dest, bytes)
def test_artpath_stores_special_chars(self):
path = b"b\xe1r"
alb = self.lib.add_album([self.i])
alb.artpath = path
alb.store()
alb = self.lib.get_album(self.i)
assert path == alb.artpath
def test_sanitize_path_with_special_chars(self):
path = "b\xe1r?"
new_path = util.sanitize_path(path)
assert new_path.startswith("b\xe1r")
def test_sanitize_path_returns_unicode(self):
path = "b\xe1r?"
new_path = util.sanitize_path(path)
assert isinstance(new_path, str)
def test_unicode_artpath_becomes_bytestring(self):
alb = self.lib.add_album([self.i])
alb.artpath = "somep\xe1th"
assert isinstance(alb.artpath, bytes)
def test_unicode_artpath_in_database_decoded(self):
alb = self.lib.add_album([self.i])
self.lib._connection().execute(
"update albums set artpath=? where id=?", ("somep\xe1th", alb.id)
)
alb = self.lib.get_album(alb.id)
assert isinstance(alb.artpath, bytes)
class MtimeTest(BeetsTestCase):
def setUp(self):
super().setUp()
self.ipath = os.path.join(self.temp_dir, b"testfile.mp3")
shutil.copy(
syspath(os.path.join(_common.RSRC, b"full.mp3")),
syspath(self.ipath),
)
self.i = beets.library.Item.from_path(self.ipath)
self.lib.add(self.i)
def tearDown(self):
super().tearDown()
if os.path.exists(self.ipath):
os.remove(self.ipath)
def _mtime(self):
return int(os.path.getmtime(self.ipath))
def test_mtime_initially_up_to_date(self):
assert self.i.mtime >= self._mtime()
def test_mtime_reset_on_db_modify(self):
self.i.title = "something else"
assert self.i.mtime < self._mtime()
def test_mtime_up_to_date_after_write(self):
self.i.title = "something else"
self.i.write()
assert self.i.mtime >= self._mtime()
def test_mtime_up_to_date_after_read(self):
self.i.title = "something else"
self.i.read()
assert self.i.mtime >= self._mtime()
class ImportTimeTest(BeetsTestCase):
def added(self):
self.track = item()
self.album = self.lib.add_album((self.track,))
assert self.album.added > 0
assert self.track.added > 0
def test_atime_for_singleton(self):
self.singleton = item(self.lib)
assert self.singleton.added > 0
class TemplateTest(ItemInDBTestCase):
def test_year_formatted_in_template(self):
self.i.year = 123
self.i.store()
assert self.i.evaluate_template("$year") == "0123"
def test_album_flexattr_appears_in_item_template(self):
self.album = self.lib.add_album([self.i])
self.album.foo = "baz"
self.album.store()
assert self.i.evaluate_template("$foo") == "baz"
def test_album_and_item_format(self):
config["format_album"] = "foö $foo"
album = beets.library.Album()
album.foo = "bar"
album.tagada = "togodo"
assert f"{album}" == "foö bar"
assert f"{album:$tagada}" == "togodo"
assert str(album) == "foö bar"
assert bytes(album) == b"fo\xc3\xb6 bar"
config["format_item"] = "bar $foo"
item = beets.library.Item()
item.foo = "bar"
item.tagada = "togodo"
assert f"{item}" == "bar bar"
assert f"{item:$tagada}" == "togodo"
class UnicodePathTest(ItemInDBTestCase):
def test_unicode_path(self):
self.i.path = os.path.join(_common.RSRC, "unicode\u2019d.mp3".encode())
# If there are any problems with unicode paths, we will raise
# here and fail.
self.i.read()
self.i.write()
class WriteTest(BeetsTestCase):
def test_write_nonexistant(self):
item = self.create_item()
item.path = b"/path/does/not/exist"
with pytest.raises(beets.library.ReadError):
item.write()
@unittest.skip('fails under some autopkgtests')
def test_no_write_permission(self):
item = self.add_item_fixture()
path = syspath(item.path)
os.chmod(path, stat.S_IRUSR)
try:
with pytest.raises(beets.library.WriteError):
item.write()
finally:
# Restore write permissions so the file can be cleaned up.
os.chmod(path, stat.S_IRUSR | stat.S_IWUSR)
def test_write_with_custom_path(self):
item = self.add_item_fixture()
custom_path = os.path.join(self.temp_dir, b"custom.mp3")
shutil.copy(syspath(item.path), syspath(custom_path))
item["artist"] = "new artist"
assert MediaFile(syspath(custom_path)).artist != "new artist"
assert MediaFile(syspath(item.path)).artist != "new artist"
item.write(custom_path)
assert MediaFile(syspath(custom_path)).artist == "new artist"
assert MediaFile(syspath(item.path)).artist != "new artist"
def test_write_custom_tags(self):
item = self.add_item_fixture(artist="old artist")
item.write(tags={"artist": "new artist"})
assert item.artist != "new artist"
assert MediaFile(syspath(item.path)).artist == "new artist"
def test_write_multi_tags(self):
item = self.add_item_fixture(artist="old artist")
item.write(tags={"artists": ["old artist", "another artist"]})
assert MediaFile(syspath(item.path)).artists == [
"old artist",
"another artist",
]
def test_write_multi_tags_id3v23(self):
item = self.add_item_fixture(artist="old artist")
item.write(
tags={"artists": ["old artist", "another artist"]}, id3v23=True
)
assert MediaFile(syspath(item.path)).artists == [
"old artist/another artist"
]
def test_write_date_field(self):
# Since `date` is not a MediaField, this should do nothing.
item = self.add_item_fixture()
clean_year = item.year
item.date = "foo"
item.write()
assert MediaFile(syspath(item.path)).year == clean_year
class ItemReadTest(unittest.TestCase):
def test_unreadable_raise_read_error(self):
unreadable = os.path.join(_common.RSRC, b"image-2x3.png")
item = beets.library.Item()
with pytest.raises(beets.library.ReadError) as exc_info:
item.read(unreadable)
assert isinstance(exc_info.value.reason, UnreadableFileError)
def test_nonexistent_raise_read_error(self):
item = beets.library.Item()
with pytest.raises(beets.library.ReadError):
item.read("/thisfiledoesnotexist")
class FilesizeTest(BeetsTestCase):
def test_filesize(self):
item = self.add_item_fixture()
assert item.filesize != 0
def test_nonexistent_file(self):
item = beets.library.Item()
assert item.filesize == 0
class ParseQueryTest(unittest.TestCase):
def test_parse_invalid_query_string(self):
with pytest.raises(beets.dbcore.query.ParsingError):
beets.library.parse_query_string('foo"', None)
def test_parse_bytes(self):
with pytest.raises(AssertionError):
beets.library.parse_query_string(b"query", None)
class LibraryFieldTypesTest(unittest.TestCase):
"""Test format() and parse() for library-specific field types"""
def test_datetype(self):
t = beets.library.DateType()
# format
time_format = beets.config["time_format"].as_str()
time_local = time.strftime(time_format, time.localtime(123456789))
assert time_local == t.format(123456789)
# parse
assert 123456789.0 == t.parse(time_local)
assert 123456789.0 == t.parse("123456789.0")
assert t.null == t.parse("not123456789.0")
assert t.null == t.parse("1973-11-29")
def test_pathtype(self):
t = beets.library.PathType()
# format
assert "/tmp" == t.format("/tmp")
assert "/tmp/\xe4lbum" == t.format("/tmp/\u00e4lbum")
# parse
assert np(b"/tmp") == t.parse("/tmp")
assert np(b"/tmp/\xc3\xa4lbum") == t.parse("/tmp/\u00e4lbum/")
def test_musicalkey(self):
t = beets.library.MusicalKey()
# parse
assert "C#m" == t.parse("c#m")
assert "Gm" == t.parse("g minor")
assert "Not c#m" == t.parse("not C#m")
def test_durationtype(self):
t = beets.library.DurationType()
# format
assert "1:01" == t.format(61.23)
assert "60:01" == t.format(3601.23)
assert "0:00" == t.format(None)
# parse
assert 61.0 == t.parse("1:01")
assert 61.23 == t.parse("61.23")
assert 3601.0 == t.parse("60:01")
assert t.null == t.parse("1:00:01")
assert t.null == t.parse("not61.23")
# config format_raw_length
beets.config["format_raw_length"] = True
assert 61.23 == t.format(61.23)
assert 3601.23 == t.format(3601.23)
|