| 1 |
import os |
| 2 |
import unittest |
| 3 |
from unittest.mock import Mock, patch |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from beets import config, library |
| 8 |
from beets.autotag import AlbumInfo, AlbumMatch, Source, TrackInfo, distance |
| 9 |
from beets.exceptions import UserError |
| 10 |
from beets.test import _common |
| 11 |
from beets.test.helper import BeetsTestCase, IOMixin |
| 12 |
from beets.ui.commands.import_ import paths_from_logfile |
| 13 |
from beets.ui.commands.import_.display import show_change |
| 14 |
from beets.ui.commands.import_.session import summarize_items |
| 15 |
|
| 16 |
|
| 17 |
class ImportTest(BeetsTestCase): |
| 18 |
def test_quiet_timid_disallowed(self): |
| 19 |
config["import"]["quiet"] = True |
| 20 |
config["import"]["timid"] = True |
| 21 |
with pytest.raises(UserError): |
| 22 |
self.run_command("import") |
| 23 |
|
| 24 |
def test_parse_paths_from_logfile(self): |
| 25 |
if os.path.__name__ == "ntpath": |
| 26 |
logfile_content = ( |
| 27 |
"import started Wed Jun 15 23:08:26 2022\n" |
| 28 |
"asis C:\\music\\Beatles, The\\The Beatles; C:\\music\\Beatles, The\\The Beatles\\CD 01; C:\\music\\Beatles, The\\The Beatles\\CD 02\n" # noqa: E501 |
| 29 |
"duplicate-replace C:\\music\\Bill Evans\\Trio '65\n" |
| 30 |
"skip C:\\music\\Michael Jackson\\Bad\n" |
| 31 |
"skip C:\\music\\Soulwax\\Any Minute Now\n" |
| 32 |
) |
| 33 |
expected_paths = [ |
| 34 |
"C:\\music\\Beatles, The\\The Beatles", |
| 35 |
"C:\\music\\Michael Jackson\\Bad", |
| 36 |
"C:\\music\\Soulwax\\Any Minute Now", |
| 37 |
] |
| 38 |
else: |
| 39 |
logfile_content = ( |
| 40 |
"import started Wed Jun 15 23:08:26 2022\n" |
| 41 |
"asis /music/Beatles, The/The Beatles; /music/Beatles, The/The Beatles/CD 01; /music/Beatles, The/The Beatles/CD 02\n" # noqa: E501 |
| 42 |
"duplicate-replace /music/Bill Evans/Trio '65\n" |
| 43 |
"skip /music/Michael Jackson/Bad\n" |
| 44 |
"skip /music/Soulwax/Any Minute Now\n" |
| 45 |
) |
| 46 |
expected_paths = [ |
| 47 |
"/music/Beatles, The/The Beatles", |
| 48 |
"/music/Michael Jackson/Bad", |
| 49 |
"/music/Soulwax/Any Minute Now", |
| 50 |
] |
| 51 |
|
| 52 |
logfile = self.temp_path / "logfile.log" |
| 53 |
logfile.write_text(logfile_content) |
| 54 |
actual_paths = list(paths_from_logfile(logfile)) |
| 55 |
assert actual_paths == expected_paths |
| 56 |
|
| 57 |
|
| 58 |
@patch("beets.ui.term_width", Mock(return_value=54)) |
| 59 |
class ShowChangeTestCase(IOMixin, BeetsTestCase): |
| 60 |
def _show_change(self): |
| 61 |
"""Return an unicode string representing the changes""" |
| 62 |
long_name = f"a{' very' * 10} long name" |
| 63 |
album = "another album" |
| 64 |
albumartist = f"another artist with {long_name}" |
| 65 |
|
| 66 |
def make_item(**kwargs): |
| 67 |
return _common.item(album=album, albumartist=albumartist, **kwargs) |
| 68 |
|
| 69 |
items = [ |
| 70 |
make_item(track=1, title="first title"), |
| 71 |
make_item(track=2, title="", path=b"/path/to/file.mp3"), |
| 72 |
make_item(track=3, title="caf\xe9"), |
| 73 |
make_item(track=4, title=f"title with {long_name}"), |
| 74 |
] |
| 75 |
info = AlbumInfo( |
| 76 |
album="caf\xe9", |
| 77 |
album_id="album id", |
| 78 |
artist="the artist", |
| 79 |
artist_id="artist id", |
| 80 |
tracks=[ |
| 81 |
TrackInfo(title="first title", index=1), |
| 82 |
TrackInfo(title="second title", index=2), |
| 83 |
TrackInfo(title="third title", index=3), |
| 84 |
TrackInfo(title="fourth title", index=4), |
| 85 |
], |
| 86 |
) |
| 87 |
item_info_pairs = list(zip(items, info.tracks)) |
| 88 |
self.config["ui"]["color"] = False |
| 89 |
self.config["import"]["detail"] = True |
| 90 |
source = Source.from_items(items) |
| 91 |
change_dist = distance( |
| 92 |
source.data, info, item_info_pairs, len(items) - len(info.tracks) |
| 93 |
) |
| 94 |
change_dist._penalties = {"album": [0.1], "artist": [0.1]} |
| 95 |
show_change( |
| 96 |
source, AlbumMatch(change_dist, info, dict(item_info_pairs)) |
| 97 |
) |
| 98 |
return self.io.getoutput() |
| 99 |
|
| 100 |
def test_newline_layout(self): |
| 101 |
self.config["ui"]["import"]["layout"] = "newline" |
| 102 |
msg = self._show_change() |
| 103 |
assert ( |
| 104 |
msg |
| 105 |
== """ |
| 106 |
Match (90.0%): |
| 107 |
the artist - café |
| 108 |
≠ album, artist |
| 109 |
None, None, None, None, None, None, None |
| 110 |
≠ Artist: another artist with a very very very very |
| 111 |
very very very very very very long name |
| 112 |
-> the artist |
| 113 |
≠ Album: another album -> café |
| 114 |
* (#1) first title (1:00) |
| 115 |
≠ (#2) file.mp3 (1:00) |
| 116 |
-> (#2) second title (0:00) |
| 117 |
≠ (#3) café (1:00) -> (#3) third title (0:00) |
| 118 |
≠ (#4) title with a very very very very very very |
| 119 |
very very very very long name (1:00) |
| 120 |
-> (#4) fourth title (0:00) |
| 121 |
""" |
| 122 |
) |
| 123 |
|
| 124 |
def test_column_layout(self): |
| 125 |
self.config["ui"]["import"]["layout"] = "column" |
| 126 |
msg = self._show_change() |
| 127 |
assert ( |
| 128 |
msg |
| 129 |
== """ |
| 130 |
Match (90.0%): |
| 131 |
the artist - café |
| 132 |
≠ album, artist |
| 133 |
None, None, None, None, None, None, None |
| 134 |
≠ Artist: another artist -> the artist |
| 135 |
with a very |
| 136 |
very very very |
| 137 |
very very very |
| 138 |
very very very |
| 139 |
long name |
| 140 |
≠ Album: another album -> café |
| 141 |
* (#1) first title (1:00) |
| 142 |
≠ (#2) file.mp (1:00) -> (#2) second (0:00) |
| 143 |
3 title |
| 144 |
≠ (#3) café (1:00) -> (#3) third title (0:00) |
| 145 |
≠ (#4) title (1:00) -> (#4) fourth (0:00) |
| 146 |
with a very title |
| 147 |
very very very |
| 148 |
very very very |
| 149 |
very very very |
| 150 |
long name |
| 151 |
""" # noqa: W291 |
| 152 |
) |
| 153 |
|
| 154 |
|
| 155 |
@patch("beets.library.Item.try_filesize", Mock(return_value=987)) |
| 156 |
class SummarizeItemsTest(unittest.TestCase): |
| 157 |
def setUp(self): |
| 158 |
super().setUp() |
| 159 |
item = library.Item() |
| 160 |
item.bitrate = 4321 |
| 161 |
item.length = 10 * 60 + 54 |
| 162 |
item.format = "F" |
| 163 |
self.item = item |
| 164 |
|
| 165 |
def test_summarize_item(self): |
| 166 |
summary = summarize_items([], True) |
| 167 |
assert summary == "" |
| 168 |
|
| 169 |
summary = summarize_items([self.item], True) |
| 170 |
assert summary == "F, 4kbps, 10:54, 987.0 B" |
| 171 |
|
| 172 |
def test_summarize_items(self): |
| 173 |
summary = summarize_items([], False) |
| 174 |
assert summary == "0 items" |
| 175 |
|
| 176 |
summary = summarize_items([self.item], False) |
| 177 |
assert summary == "1 items, F, 4kbps, 10:54, 987.0 B" |
| 178 |
|
| 179 |
# make a copy of self.item |
| 180 |
i2 = self.item.copy() |
| 181 |
|
| 182 |
summary = summarize_items([self.item, i2], False) |
| 183 |
assert summary == "2 items, F, 4kbps, 21:48, 1.9 KiB" |
| 184 |
|
| 185 |
i2.format = "G" |
| 186 |
summary = summarize_items([self.item, i2], False) |
| 187 |
assert summary == "2 items, F 1, G 1, 4kbps, 21:48, 1.9 KiB" |
| 188 |
|
| 189 |
summary = summarize_items([self.item, i2, i2], False) |
| 190 |
assert summary == "3 items, G 2, F 1, 4kbps, 32:42, 2.9 KiB" |