| 1 |
"""Tests for the 'zero' plugin""" |
| 2 |
|
| 3 |
from mediafile import MediaFile |
| 4 |
|
| 5 |
from beets.library import Item |
| 6 |
from beets.test.helper import IOMixin, PluginTestCase |
| 7 |
from beetsplug.zero import ZeroPlugin |
| 8 |
|
| 9 |
|
| 10 |
class ZeroPluginTest(IOMixin, PluginTestCase): |
| 11 |
plugin = "zero" |
| 12 |
preload_plugin = False |
| 13 |
|
| 14 |
def test_no_patterns(self): |
| 15 |
item = self.add_item_fixture( |
| 16 |
comments="test comment", title="Title", month=1, year=2000 |
| 17 |
) |
| 18 |
item.write() |
| 19 |
|
| 20 |
with self.configure_plugin({"fields": ["comments", "month"]}): |
| 21 |
item.write() |
| 22 |
|
| 23 |
mf = MediaFile(item.filepath) |
| 24 |
assert mf.comments is None |
| 25 |
assert mf.month is None |
| 26 |
assert mf.title == "Title" |
| 27 |
assert mf.year == 2000 |
| 28 |
|
| 29 |
def test_pattern_match(self): |
| 30 |
item = self.add_item_fixture(comments="encoded by encoder") |
| 31 |
item.write() |
| 32 |
|
| 33 |
with self.configure_plugin( |
| 34 |
{"fields": ["comments"], "comments": ["encoded by"]} |
| 35 |
): |
| 36 |
item.write() |
| 37 |
|
| 38 |
mf = MediaFile(item.filepath) |
| 39 |
assert mf.comments is None |
| 40 |
|
| 41 |
def test_pattern_nomatch(self): |
| 42 |
item = self.add_item_fixture(comments="recorded at place") |
| 43 |
item.write() |
| 44 |
|
| 45 |
with self.configure_plugin( |
| 46 |
{"fields": ["comments"], "comments": ["encoded_by"]} |
| 47 |
): |
| 48 |
item.write() |
| 49 |
|
| 50 |
mf = MediaFile(item.filepath) |
| 51 |
assert mf.comments == "recorded at place" |
| 52 |
|
| 53 |
def test_do_not_change_database(self): |
| 54 |
item = self.add_item_fixture(year=2000) |
| 55 |
item.write() |
| 56 |
|
| 57 |
with self.configure_plugin({"fields": ["year"]}): |
| 58 |
item.write() |
| 59 |
|
| 60 |
assert item["year"] == 2000 |
| 61 |
|
| 62 |
def test_change_database(self): |
| 63 |
item = self.add_item_fixture(year=2000) |
| 64 |
item.write() |
| 65 |
|
| 66 |
with self.configure_plugin( |
| 67 |
{"fields": ["year"], "update_database": True} |
| 68 |
): |
| 69 |
item.write() |
| 70 |
|
| 71 |
assert item["year"] == 0 |
| 72 |
|
| 73 |
def test_album_art(self): |
| 74 |
path = self.create_mediafile_fixture(images=["jpg"]) |
| 75 |
item = Item.from_path(path) |
| 76 |
|
| 77 |
with self.configure_plugin({"fields": ["images"]}): |
| 78 |
item.write() |
| 79 |
|
| 80 |
mf = MediaFile(path) |
| 81 |
assert not mf.images |
| 82 |
|
| 83 |
def test_auto_false(self): |
| 84 |
item = self.add_item_fixture(year=2000) |
| 85 |
item.write() |
| 86 |
|
| 87 |
with self.configure_plugin( |
| 88 |
{"fields": ["year"], "update_database": True, "auto": False} |
| 89 |
): |
| 90 |
item.write() |
| 91 |
|
| 92 |
assert item["year"] == 2000 |
| 93 |
|
| 94 |
def test_subcommand_update_database_true(self): |
| 95 |
item = self.add_item_fixture( |
| 96 |
year=2016, day=13, month=3, comments="test comment" |
| 97 |
) |
| 98 |
item.write() |
| 99 |
item_id = item.id |
| 100 |
|
| 101 |
with self.configure_plugin( |
| 102 |
{"fields": ["comments"], "update_database": True, "auto": False} |
| 103 |
): |
| 104 |
self.io.addinput("y") |
| 105 |
self.run_command("zero") |
| 106 |
|
| 107 |
mf = MediaFile(item.filepath) |
| 108 |
item = self.lib.get_item(item_id) |
| 109 |
|
| 110 |
assert item["year"] == 2016 |
| 111 |
assert mf.year == 2016 |
| 112 |
assert mf.comments is None |
| 113 |
assert item["comments"] == "" |
| 114 |
|
| 115 |
def test_subcommand_update_database_false(self): |
| 116 |
item = self.add_item_fixture( |
| 117 |
year=2016, day=13, month=3, comments="test comment" |
| 118 |
) |
| 119 |
item.write() |
| 120 |
item_id = item.id |
| 121 |
|
| 122 |
with self.configure_plugin( |
| 123 |
{"fields": ["comments"], "update_database": False, "auto": False} |
| 124 |
): |
| 125 |
self.io.addinput("y") |
| 126 |
self.run_command("zero") |
| 127 |
|
| 128 |
mf = MediaFile(item.filepath) |
| 129 |
item = self.lib.get_item(item_id) |
| 130 |
|
| 131 |
assert item["year"] == 2016 |
| 132 |
assert mf.year == 2016 |
| 133 |
assert item["comments"] == "test comment" |
| 134 |
assert mf.comments is None |
| 135 |
|
| 136 |
def test_subcommand_query_include(self): |
| 137 |
item = self.add_item_fixture( |
| 138 |
year=2016, day=13, month=3, comments="test comment" |
| 139 |
) |
| 140 |
|
| 141 |
item.write() |
| 142 |
|
| 143 |
with self.configure_plugin( |
| 144 |
{"fields": ["comments"], "update_database": False, "auto": False} |
| 145 |
): |
| 146 |
self.run_command("zero", "year: 2016") |
| 147 |
|
| 148 |
mf = MediaFile(item.filepath) |
| 149 |
|
| 150 |
assert mf.year == 2016 |
| 151 |
assert mf.comments is None |
| 152 |
|
| 153 |
def test_subcommand_query_exclude(self): |
| 154 |
item = self.add_item_fixture( |
| 155 |
year=2016, day=13, month=3, comments="test comment" |
| 156 |
) |
| 157 |
|
| 158 |
item.write() |
| 159 |
|
| 160 |
with self.configure_plugin( |
| 161 |
{"fields": ["comments"], "update_database": False, "auto": False} |
| 162 |
): |
| 163 |
self.run_command("zero", "year: 0000") |
| 164 |
|
| 165 |
mf = MediaFile(item.filepath) |
| 166 |
|
| 167 |
assert mf.year == 2016 |
| 168 |
assert mf.comments == "test comment" |
| 169 |
|
| 170 |
def test_no_fields(self): |
| 171 |
item = self.add_item_fixture(year=2016) |
| 172 |
item.write() |
| 173 |
mediafile = MediaFile(item.filepath) |
| 174 |
assert mediafile.year == 2016 |
| 175 |
|
| 176 |
item_id = item.id |
| 177 |
|
| 178 |
with self.configure_plugin({"fields": []}): |
| 179 |
self.io.addinput("y") |
| 180 |
self.run_command("zero") |
| 181 |
|
| 182 |
item = self.lib.get_item(item_id) |
| 183 |
|
| 184 |
assert item["year"] == 2016 |
| 185 |
assert mediafile.year == 2016 |
| 186 |
|
| 187 |
def test_whitelist_and_blacklist(self): |
| 188 |
item = self.add_item_fixture(year=2016) |
| 189 |
item.write() |
| 190 |
mf = MediaFile(item.filepath) |
| 191 |
assert mf.year == 2016 |
| 192 |
|
| 193 |
item_id = item.id |
| 194 |
|
| 195 |
with self.configure_plugin( |
| 196 |
{"fields": ["year"], "keep_fields": ["comments"]} |
| 197 |
): |
| 198 |
self.io.addinput("y") |
| 199 |
self.run_command("zero") |
| 200 |
|
| 201 |
item = self.lib.get_item(item_id) |
| 202 |
|
| 203 |
assert item["year"] == 2016 |
| 204 |
assert mf.year == 2016 |
| 205 |
|
| 206 |
def test_keep_fields(self): |
| 207 |
item = self.add_item_fixture(year=2016, comments="test comment") |
| 208 |
tags = {"comments": "test comment", "year": 2016} |
| 209 |
|
| 210 |
with self.configure_plugin( |
| 211 |
{"fields": None, "keep_fields": ["year"], "update_database": True} |
| 212 |
): |
| 213 |
z = ZeroPlugin() |
| 214 |
z.write_event(item, item.path, tags) |
| 215 |
|
| 216 |
assert tags["comments"] is None |
| 217 |
assert tags["year"] == 2016 |
| 218 |
|
| 219 |
def test_keep_fields_removes_preserved_tags(self): |
| 220 |
self.config["zero"]["keep_fields"] = ["year"] |
| 221 |
self.config["zero"]["fields"] = None |
| 222 |
self.config["zero"]["update_database"] = True |
| 223 |
|
| 224 |
z = ZeroPlugin() |
| 225 |
|
| 226 |
assert "id" not in z.fields_to_progs |
| 227 |
|
| 228 |
def test_fields_removes_preserved_tags(self): |
| 229 |
self.config["zero"]["fields"] = ["year id"] |
| 230 |
self.config["zero"]["update_database"] = True |
| 231 |
|
| 232 |
z = ZeroPlugin() |
| 233 |
|
| 234 |
assert "id" not in z.fields_to_progs |
| 235 |
|
| 236 |
def test_omit_single_disc_with_tags_single(self): |
| 237 |
item = self.add_item_fixture( |
| 238 |
disctotal=1, disc=1, comments="test comment" |
| 239 |
) |
| 240 |
item.write() |
| 241 |
with self.configure_plugin( |
| 242 |
{"omit_single_disc": True, "fields": ["comments"]} |
| 243 |
): |
| 244 |
item.write() |
| 245 |
|
| 246 |
mf = MediaFile(item.filepath) |
| 247 |
assert mf.comments is None |
| 248 |
assert mf.disc is None |
| 249 |
assert mf.disctotal is None |
| 250 |
|
| 251 |
def test_omit_single_disc_with_tags_multi(self): |
| 252 |
item = self.add_item_fixture( |
| 253 |
disctotal=4, disc=1, comments="test comment" |
| 254 |
) |
| 255 |
item.write() |
| 256 |
with self.configure_plugin( |
| 257 |
{"omit_single_disc": True, "fields": ["comments"]} |
| 258 |
): |
| 259 |
item.write() |
| 260 |
|
| 261 |
mf = MediaFile(item.filepath) |
| 262 |
assert mf.comments is None |
| 263 |
assert mf.disc == 1 |
| 264 |
assert mf.disctotal == 4 |
| 265 |
|
| 266 |
def test_omit_single_disc_only_change_single(self): |
| 267 |
item = self.add_item_fixture(disctotal=1, disc=1) |
| 268 |
item.write() |
| 269 |
|
| 270 |
with self.configure_plugin({"omit_single_disc": True}): |
| 271 |
item.write() |
| 272 |
|
| 273 |
mf = MediaFile(item.filepath) |
| 274 |
assert mf.disc is None |
| 275 |
assert mf.disctotal is None |
| 276 |
|
| 277 |
def test_omit_single_disc_only_change_multi(self): |
| 278 |
item = self.add_item_fixture(disctotal=4, disc=1) |
| 279 |
item.write() |
| 280 |
|
| 281 |
with self.configure_plugin({"omit_single_disc": True}): |
| 282 |
item.write() |
| 283 |
|
| 284 |
mf = MediaFile(item.filepath) |
| 285 |
assert mf.disc == 1 |
| 286 |
assert mf.disctotal == 4 |
| 287 |
|
| 288 |
def test_empty_query_n_response_no_changes(self): |
| 289 |
item = self.add_item_fixture( |
| 290 |
year=2016, day=13, month=3, comments="test comment" |
| 291 |
) |
| 292 |
item.write() |
| 293 |
item_id = item.id |
| 294 |
with self.configure_plugin( |
| 295 |
{"fields": ["comments"], "update_database": True, "auto": False} |
| 296 |
): |
| 297 |
self.io.addinput("n") |
| 298 |
self.run_command("zero") |
| 299 |
|
| 300 |
mf = MediaFile(item.filepath) |
| 301 |
item = self.lib.get_item(item_id) |
| 302 |
|
| 303 |
assert item["year"] == 2016 |
| 304 |
assert mf.year == 2016 |
| 305 |
assert mf.comments == "test comment" |
| 306 |
assert item["comments"] == "test comment" |
| 307 |
|
| 308 |
def test_keep_fields_images_preserves_art(self): |
| 309 |
path = self.create_mediafile_fixture(images=["jpg"]) |
| 310 |
item = Item.from_path(path) |
| 311 |
|
| 312 |
with self.configure_plugin({"fields": None, "keep_fields": ["images"]}): |
| 313 |
item.write() |
| 314 |
|
| 315 |
mf = MediaFile(path) |
| 316 |
assert mf.images, ( |
| 317 |
"images should be preserved when 'images' is in keep_fields" |
| 318 |
) |