File: test_zero.py

package info (click to toggle)
beets 2.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 7,988 kB
  • sloc: python: 46,429; javascript: 8,018; xml: 334; sh: 261; makefile: 125
file content (320 lines) | stat: -rw-r--r-- 9,040 bytes parent folder | download | duplicates (2)
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
"""Tests for the 'zero' plugin"""

from mediafile import MediaFile

from beets.library import Item
from beets.test.helper import PluginTestCase, control_stdin
from beets.util import syspath
from beetsplug.zero import ZeroPlugin


class ZeroPluginTest(PluginTestCase):
    plugin = "zero"
    preload_plugin = False

    def test_no_patterns(self):
        item = self.add_item_fixture(
            comments="test comment",
            title="Title",
            month=1,
            year=2000,
        )
        item.write()

        with self.configure_plugin({"fields": ["comments", "month"]}):
            item.write()

        mf = MediaFile(syspath(item.path))
        assert mf.comments is None
        assert mf.month is None
        assert mf.title == "Title"
        assert mf.year == 2000

    def test_pattern_match(self):
        item = self.add_item_fixture(comments="encoded by encoder")
        item.write()

        with self.configure_plugin(
            {"fields": ["comments"], "comments": ["encoded by"]}
        ):
            item.write()

        mf = MediaFile(syspath(item.path))
        assert mf.comments is None

    def test_pattern_nomatch(self):
        item = self.add_item_fixture(comments="recorded at place")
        item.write()

        with self.configure_plugin(
            {"fields": ["comments"], "comments": ["encoded_by"]}
        ):
            item.write()

        mf = MediaFile(syspath(item.path))
        assert mf.comments == "recorded at place"

    def test_do_not_change_database(self):
        item = self.add_item_fixture(year=2000)
        item.write()

        with self.configure_plugin({"fields": ["year"]}):
            item.write()

        assert item["year"] == 2000

    def test_change_database(self):
        item = self.add_item_fixture(year=2000)
        item.write()

        with self.configure_plugin(
            {"fields": ["year"], "update_database": True}
        ):
            item.write()

        assert item["year"] == 0

    def test_album_art(self):
        path = self.create_mediafile_fixture(images=["jpg"])
        item = Item.from_path(path)

        with self.configure_plugin({"fields": ["images"]}):
            item.write()

        mf = MediaFile(syspath(path))
        assert not mf.images

    def test_auto_false(self):
        item = self.add_item_fixture(year=2000)
        item.write()

        with self.configure_plugin(
            {"fields": ["year"], "update_database": True, "auto": False}
        ):
            item.write()

        assert item["year"] == 2000

    def test_subcommand_update_database_true(self):
        item = self.add_item_fixture(
            year=2016, day=13, month=3, comments="test comment"
        )
        item.write()
        item_id = item.id

        with (
            self.configure_plugin(
                {"fields": ["comments"], "update_database": True, "auto": False}
            ),
            control_stdin("y"),
        ):
            self.run_command("zero")

        mf = MediaFile(syspath(item.path))
        item = self.lib.get_item(item_id)

        assert item["year"] == 2016
        assert mf.year == 2016
        assert mf.comments is None
        assert item["comments"] == ""

    def test_subcommand_update_database_false(self):
        item = self.add_item_fixture(
            year=2016, day=13, month=3, comments="test comment"
        )
        item.write()
        item_id = item.id

        with (
            self.configure_plugin(
                {
                    "fields": ["comments"],
                    "update_database": False,
                    "auto": False,
                }
            ),
            control_stdin("y"),
        ):
            self.run_command("zero")

        mf = MediaFile(syspath(item.path))
        item = self.lib.get_item(item_id)

        assert item["year"] == 2016
        assert mf.year == 2016
        assert item["comments"] == "test comment"
        assert mf.comments is None

    def test_subcommand_query_include(self):
        item = self.add_item_fixture(
            year=2016, day=13, month=3, comments="test comment"
        )

        item.write()

        with self.configure_plugin(
            {"fields": ["comments"], "update_database": False, "auto": False}
        ):
            self.run_command("zero", "year: 2016")

        mf = MediaFile(syspath(item.path))

        assert mf.year == 2016
        assert mf.comments is None

    def test_subcommand_query_exclude(self):
        item = self.add_item_fixture(
            year=2016, day=13, month=3, comments="test comment"
        )

        item.write()

        with self.configure_plugin(
            {"fields": ["comments"], "update_database": False, "auto": False}
        ):
            self.run_command("zero", "year: 0000")

        mf = MediaFile(syspath(item.path))

        assert mf.year == 2016
        assert mf.comments == "test comment"

    def test_no_fields(self):
        item = self.add_item_fixture(year=2016)
        item.write()
        mediafile = MediaFile(syspath(item.path))
        assert mediafile.year == 2016

        item_id = item.id

        with self.configure_plugin({"fields": []}), control_stdin("y"):
            self.run_command("zero")

        item = self.lib.get_item(item_id)

        assert item["year"] == 2016
        assert mediafile.year == 2016

    def test_whitelist_and_blacklist(self):
        item = self.add_item_fixture(year=2016)
        item.write()
        mf = MediaFile(syspath(item.path))
        assert mf.year == 2016

        item_id = item.id

        with (
            self.configure_plugin(
                {"fields": ["year"], "keep_fields": ["comments"]}
            ),
            control_stdin("y"),
        ):
            self.run_command("zero")

        item = self.lib.get_item(item_id)

        assert item["year"] == 2016
        assert mf.year == 2016

    def test_keep_fields(self):
        item = self.add_item_fixture(year=2016, comments="test comment")
        tags = {
            "comments": "test comment",
            "year": 2016,
        }

        with self.configure_plugin(
            {"fields": None, "keep_fields": ["year"], "update_database": True}
        ):
            z = ZeroPlugin()
            z.write_event(item, item.path, tags)

        assert tags["comments"] is None
        assert tags["year"] == 2016

    def test_keep_fields_removes_preserved_tags(self):
        self.config["zero"]["keep_fields"] = ["year"]
        self.config["zero"]["fields"] = None
        self.config["zero"]["update_database"] = True

        z = ZeroPlugin()

        assert "id" not in z.fields_to_progs

    def test_fields_removes_preserved_tags(self):
        self.config["zero"]["fields"] = ["year id"]
        self.config["zero"]["update_database"] = True

        z = ZeroPlugin()

        assert "id" not in z.fields_to_progs

    def test_omit_single_disc_with_tags_single(self):
        item = self.add_item_fixture(
            disctotal=1, disc=1, comments="test comment"
        )
        item.write()
        with self.configure_plugin(
            {"omit_single_disc": True, "fields": ["comments"]}
        ):
            item.write()

        mf = MediaFile(syspath(item.path))
        assert mf.comments is None
        assert mf.disc == 0

    def test_omit_single_disc_with_tags_multi(self):
        item = self.add_item_fixture(
            disctotal=4, disc=1, comments="test comment"
        )
        item.write()
        with self.configure_plugin(
            {"omit_single_disc": True, "fields": ["comments"]}
        ):
            item.write()

        mf = MediaFile(syspath(item.path))
        assert mf.comments is None
        assert mf.disc == 1

    def test_omit_single_disc_only_change_single(self):
        item = self.add_item_fixture(disctotal=1, disc=1)
        item.write()

        with self.configure_plugin({"omit_single_disc": True}):
            item.write()

        mf = MediaFile(syspath(item.path))
        assert mf.disc == 0

    def test_omit_single_disc_only_change_multi(self):
        item = self.add_item_fixture(disctotal=4, disc=1)
        item.write()

        with self.configure_plugin({"omit_single_disc": True}):
            item.write()

        mf = MediaFile(syspath(item.path))
        assert mf.disc == 1

    def test_empty_query_n_response_no_changes(self):
        item = self.add_item_fixture(
            year=2016, day=13, month=3, comments="test comment"
        )
        item.write()
        item_id = item.id
        with (
            self.configure_plugin(
                {"fields": ["comments"], "update_database": True, "auto": False}
            ),
            control_stdin("n"),
        ):
            self.run_command("zero")

        mf = MediaFile(syspath(item.path))
        item = self.lib.get_item(item_id)

        assert item["year"] == 2016
        assert mf.year == 2016
        assert mf.comments == "test comment"
        assert item["comments"] == "test comment"