-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_track.py
431 lines (313 loc) · 13.1 KB
/
test_track.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
"""Tests a Track object."""
import pytest
import moe
import moe.write as moe_write
from moe.config import ExtraPlugin
from moe.library import MetaTrack, Track, TrackError
from moe.library.album import MetaAlbum
from tests.conftest import album_factory, extra_factory, track_factory
class MyTrackPlugin:
"""Plugin that implements the extra hooks for testing."""
@staticmethod
@moe.hookimpl
def is_unique_track(track, other):
"""Tracks with the same title aren't unique."""
if track.title == other.title:
return False
@staticmethod
@moe.hookimpl
def read_custom_tags(track_path, album_fields, track_fields):
"""Override a new fields for the track and album."""
album_fields["title"] = "custom album title"
track_fields["title"] = "custom track title"
class TestHooks:
"""Test track hooks."""
def test_is_unique_track(self, tmp_config):
"""Plugins can add additional unique constraints."""
tmp_config(extra_plugins=[ExtraPlugin(MyTrackPlugin, "track_plugin")])
track = track_factory()
dup_track = track_factory(title=track.title)
assert not track.is_unique(dup_track)
class TestInit:
"""Test Track initialization."""
def test_album_init(self):
"""Creating a Track should also create the corresponding Album."""
assert track_factory().album
def test_guess_disc_multi_disc(self):
"""Guess the disc if not given."""
album = album_factory(num_discs=3, exists=True)
track1 = album.get_track(1, disc=1)
track2 = album.get_track(1, disc=2)
track3 = album.get_track(1, disc=3)
assert track1 and track2 and track3 # noqa: PT018
new_track1 = Track(album, track1.path, track1.title, track1.track_num)
new_track2 = Track(album, track2.path, track2.title, track2.track_num)
new_track3 = Track(album, track3.path, track3.title, track3.track_num)
assert new_track1.disc == 1
assert new_track2.disc == 2
assert new_track3.disc == 3
def test_guess_disc_single_disc(self):
"""Guess the disc if there are no disc sub directories."""
track = track_factory()
assert track.path.parent == track.album.path
new_track = Track(
track.album,
track.path,
track.title,
track.track_num,
)
assert new_track.disc == 1
def test_default_artist(self):
"""Use the album artist if an artist is not given."""
album = album_factory(artist="use me")
track = track_factory(album=album, artist=None)
assert track.artist == "use me"
def test_use_given_artist(self):
"""Don't use the album artist if an artist is given."""
album = album_factory(artist="dont use me")
track = track_factory(album=album, artist="use me")
assert track.artist == "use me"
def test_custom_fields(self):
"""Custom fields can be assigned using kwargs."""
track = track_factory()
new_track = Track(
track.album,
track.path,
track.title,
track.track_num,
custom="custom",
)
assert new_track.custom["custom"] == "custom"
class TestMetaInit:
"""Test MetaTrack initialization."""
def test_default_artist(self):
"""Use the album artist if an artist is not given."""
album = MetaAlbum(artist="use me")
track = MetaTrack(album, 1, artist=None)
assert track.artist == "use me"
def test_use_given_artist(self):
"""Don't use the album artist if an artist is given."""
album = MetaAlbum(artist="dont use me")
track = MetaTrack(album, 1, artist="use me")
assert track.artist == "use me"
class TestFromFile:
"""Test initialization from given file path."""
def test_non_track_file(self):
"""Raise `TrackError` if the given path does not correspond to a track file."""
with pytest.raises(TrackError):
Track.from_file(extra_factory().path)
def test_albumartist_backup(self, tmp_config):
"""Use artist as a backup for albumartist if missing."""
tmp_config()
track = track_factory(exists=True)
track.album.artist = ""
track.artist = "Backup"
moe_write.write_tags(track)
track = Track.from_file(track.path)
assert track.album.artist
def test_read_custom_tags(self, tmp_config):
"""Plugins can add additional track and album fields via `read_custom_tags`."""
tmp_config(extra_plugins=[ExtraPlugin(MyTrackPlugin, "track_plugin")])
track = track_factory(exists=True)
new_track = Track.from_file(track.path)
assert new_track.album.title == "custom album title"
assert new_track.title == "custom track title"
def test_read_track_fields(self, tmp_config):
"""Plugins can add additional album fields via the `read_album_fields` hook."""
tmp_config(extra_plugins=[ExtraPlugin(MyTrackPlugin, "track_plugin")])
track = track_factory(exists=True)
new_track = Track.from_file(track.path)
assert new_track.title == "custom track title"
def test_missing_artist(self, tmp_config):
"""Raise ValueError if track is missing both an artist and albumartist."""
tmp_config()
track = track_factory(exists=True)
track.album.artist = None
track.artist = None
moe_write.write_tags(track)
with pytest.raises(ValueError, match="missing"):
Track.from_file(track.path)
def test_missing_album_title(self, tmp_config):
"""Raise ValueError if track is missing the album title."""
tmp_config()
track = track_factory(exists=True)
track.album.title = None
moe_write.write_tags(track)
with pytest.raises(ValueError, match="missing"):
Track.from_file(track.path)
def test_missing_date(self, tmp_config):
"""Raise ValueError if track is missing the date."""
tmp_config()
track = track_factory(exists=True)
track.album.date = None
moe_write.write_tags(track)
with pytest.raises(ValueError, match="missing"):
Track.from_file(track.path)
def test_missing_title(self, tmp_config):
"""Raise ValueError if track is missing the title."""
tmp_config()
track = track_factory(exists=True)
track.title = None
moe_write.write_tags(track)
with pytest.raises(ValueError, match="missing"):
Track.from_file(track.path)
def test_missing_track_num(self, tmp_config):
"""Raise ValueError if track is missing the track number."""
tmp_config()
track = track_factory(exists=True)
track.track_num = None
moe_write.write_tags(track)
with pytest.raises(ValueError, match="missing"):
Track.from_file(track.path)
def test_missing_disc_total(self, tmp_config):
"""Use the default disc total if missing from tags."""
tmp_config()
track = track_factory(exists=True)
track.album.disc_total = None
moe_write.write_tags(track)
Track.from_file(track.path).album.disc_total = 1
class TestEquality:
"""Test equality of tracks."""
def test_meta_equals(self):
"""Meta Tracks with the same metadata are equal."""
album = MetaAlbum(artist="meta_artist")
track1 = MetaTrack(album, 1)
track2 = MetaTrack(album, 1)
assert track1 == track2
def test_meta_not_equals(self):
"""Meta Tracks with different metadata are not equal."""
album = MetaAlbum(artist="meta_artist")
track1 = MetaTrack(album, 1)
track2 = MetaTrack(album, 2)
assert track1 != track2
def test_equals(self):
"""Tracks with the same metadata are equal."""
track1 = track_factory()
track2 = track_factory(dup_track=track1)
assert track1 == track2
def test_not_equals(self):
"""Tracks with different fields are not equal."""
track1 = track_factory(title="track1")
track2 = track_factory(title="track2")
assert track1 != track2
def test_not_equals_not_track(self):
"""Not equal if not comparing two tracks."""
assert track_factory() != "test"
class TestIsUnique:
"""Test `is_unique()`."""
def test_same_path(self):
"""Tracks with the same path are not unique."""
track = track_factory()
dup_track = track_factory(path=track.path)
assert not track.is_unique(dup_track)
def test_same_track_disc_num(self):
"""Tracks with the same album, track #, and disc # are not unique."""
track = track_factory()
dup_track = track_factory(
album=track.album, track_num=track.track_num, disc=track.disc
)
assert not track.is_unique(dup_track)
def test_default(self):
"""Tracks with no matching parameters are unique."""
track1 = track_factory()
track2 = track_factory()
assert track1.is_unique(track2)
class TestMerge:
"""Test merging two tracks."""
def test_conflict_persists(self):
"""Don't overwrite any conflicts."""
track = track_factory(title="keep")
other_track = track_factory(title="discard")
track.merge(other_track)
assert track.title == "keep"
def test_merge_non_conflict(self):
"""Apply any non-conflicting fields."""
track = track_factory()
other_track = track_factory(title="keep", genres={"keep"})
track.title = ""
track.genres = set()
track.merge(other_track)
assert track.title == "keep"
assert track.genres == {"keep"}
def test_none_merge(self):
"""Don't merge in any null values."""
track = track_factory(title="keep")
other_track = track_factory()
other_track.title = ""
track.merge(other_track)
assert track.title == "keep"
def test_db_delete(self, tmp_session):
"""Remove the other track from the db if it exists."""
track = track_factory()
other_track = track_factory()
tmp_session.add(other_track)
tmp_session.flush()
track.merge(other_track)
assert tmp_session.query(Track).one()
def test_overwrite(self):
"""Fields are overwritten if the option is given."""
track = track_factory(title="1")
other_track = track_factory(title="2")
track.merge(other_track, overwrite=True)
assert track.title == "2"
def test_custom_fields(self):
"""Merge custom fields too."""
track = track_factory(custom="")
other_track = track_factory(custom="new")
track.merge(other_track)
assert track.custom["custom"] == "new"
class TestProperties:
"""Test various track properties."""
def test_genre(self):
"""Genre should concat genres."""
track = track_factory(genres={"1", "2"})
assert track.genre == "1;2" or track.genre == "2;1"
def test_set_genre(self):
"""Setting genre should split into strings."""
track = track_factory(genre=None)
assert track.genre is None
assert track.genres is None
track.genre = "1; 2"
assert track.genres == {"1", "2"}
def test_audio_format(self):
"""We can get the audio format of a track."""
track = track_factory(exists=True)
assert track.audio_format == "mp3"
def test_bit_depth(self):
"""We can get the bit depth of a track."""
track = track_factory(exists=True)
# Bit depth is unavailable for MP3.
assert track.bit_depth == 0
def test_sample_rate(self):
"""We can get the sample rate of a track."""
track = track_factory(exists=True)
assert track.sample_rate == 44100
class TestListDuplicates:
"""List fields should not cause duplicate errors (just merge silently)."""
def test_genre(self, tmp_session):
"""Duplicate genres don't error."""
track1 = track_factory(genres={"pop"})
track2 = track_factory(genres={"pop"})
tmp_session.add(track1)
tmp_session.add(track2)
tmp_session.flush()
tracks = tmp_session.query(Track).all()
for track in tracks:
assert track.genre == "pop"
class TestLessThan:
"""Test ``__lt__``."""
def test_album_sort(self):
"""Sorting by album first."""
track1 = MetaTrack(album=MetaAlbum(title="a"), track_num=2)
track2 = MetaTrack(album=MetaAlbum(title="b"), track_num=1)
assert track1 < track2
def test_track_num_sort(self):
"""Sorting by track_number if the albums are the same."""
track1 = MetaTrack(album=MetaAlbum(title="a"), track_num=1)
track2 = MetaTrack(album=MetaAlbum(title="a"), track_num=2)
assert track1 < track2
def test_disc_sort(self):
"""Sorting by disc if the track numbers and albums are the same."""
track1 = MetaTrack(album=MetaAlbum(title="a"), track_num=1, disc=1)
track2 = MetaTrack(album=MetaAlbum(title="a"), track_num=1, disc=2)
assert track1 < track2