-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_write.py
201 lines (153 loc) · 6.28 KB
/
test_write.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
"""Tests the ``write`` plugin."""
import datetime
from unittest.mock import patch
import mediafile
import pytest
import moe
from moe import config
from moe.library import Track
from moe.plugins import write as moe_write
from tests.conftest import album_factory, extra_factory, track_factory
@pytest.fixture
def mock_write():
"""Mock the `write_tags` api call."""
with patch("moe.plugins.write.write_tags", autospec=True) as mock_edit:
yield mock_edit
@pytest.fixture
def _tmp_write_config(tmp_config):
"""Mock the `write_tags` api call."""
tmp_config("default_plugins = ['write']")
class WritePlugin:
"""Implement the write plugin hookspecs for testing."""
@staticmethod
@moe.hookimpl
def write_custom_tags(track):
"""Write the title."""
audio_file = mediafile.MediaFile(track.path)
audio_file.title = "new title"
audio_file.save()
class TestHooks:
"""Test any write plugin hookspecs."""
def test_write_custom_tags(self, tmp_config):
"""Plugins can write tags to a track."""
tmp_config(
"default_plugins = ['write']",
extra_plugins=[config.ExtraPlugin(WritePlugin, "write_test")],
)
track = track_factory(title="old title", exists=True)
config.CONFIG.pm.hook.write_custom_tags(track=track)
new_track = Track.from_file(track.path)
assert new_track.title == "new title"
class TestWriteTags:
"""Tests `write_tags()`."""
def test_write_tags(self, tmp_config):
"""We can write track changes to the file."""
tmp_config()
track = track_factory(exists=True)
album = "Bigger, Better, Faster, More!"
albumartist = "4 Non Blondes"
artist = "4 Non Blondes"
artists = {"4 Non Blondes", "Me"}
barcode = "1234"
catalog_nums = {"1", "2"}
country = "US"
date = datetime.date(1996, 10, 13)
disc = 2
disc_total = 2
genres = {"alternative", "rock"}
label = "Interscope Records"
media = "CD"
original_date = datetime.date(1996, 1, 13)
title = "What's Up"
track_num = 3
track_total = 10
track.album = album
track.albumartist = albumartist
track.artist = artist
track.artists = artists
track.album_obj.barcode = barcode
track.album_obj.catalog_nums = catalog_nums
track.album_obj.country = country
track.album_obj.date = date
track.album_obj.original_date = original_date
track.disc = disc
track.album_obj.disc_total = disc_total
track.genres = genres
track.album_obj.label = label
track.album_obj.media = media
track.title = title
track.track_num = track_num
track.album_obj.track_total = track_total
moe_write.write_tags(track)
new_track = Track.from_file(track.path)
new_album = new_track.album_obj
assert new_track.album == album
assert new_track.albumartist == albumartist
assert new_track.artist == artist
assert new_track.artists == artists
assert new_track.disc == disc
assert new_track.genres == genres
assert new_track.title == title
assert new_track.track_num == track_num
assert new_album.barcode == barcode
assert new_album.catalog_nums == catalog_nums
assert new_album.country == country
assert new_album.date == date
assert new_album.disc_total == disc_total
assert new_album.label == label
assert new_album.media == media
assert new_album.original_date == original_date
assert new_album.track_total == track_total
@pytest.mark.usefixtures("_tmp_write_config")
class TestProcessNewItems:
"""Test the `process_new_items` hook implementation."""
def test_process_track(self, mock_write):
"""Any altered Tracks have their tags written."""
track = track_factory()
config.CONFIG.pm.hook.process_new_items(items=[track])
mock_write.assert_called_once_with(track)
def test_process_extra(self, mock_write):
"""Any altered extras are ignored."""
config.CONFIG.pm.hook.process_new_items(items=[extra_factory()])
mock_write.assert_not_called()
def test_process_album(self, mock_write):
"""Any altered albums are ignored."""
config.CONFIG.pm.hook.process_new_items(items=[album_factory()])
mock_write.assert_not_called()
def test_process_multiple_tracks(self, mock_write):
"""All altered tracks are written."""
tracks = [track_factory(), track_factory()]
config.CONFIG.pm.hook.process_new_items(items=tracks)
for track in tracks:
mock_write.assert_any_call(track)
assert mock_write.call_count == 2
@pytest.mark.usefixtures("_tmp_write_config")
class TestProcessChangedItems:
"""Test the `process_changed_items` hook implementation."""
def test_process_track(self, mock_write):
"""Any altered Tracks have their tags written."""
track = track_factory()
config.CONFIG.pm.hook.process_changed_items(items=[track])
mock_write.assert_called_once_with(track)
def test_process_extra(self, mock_write):
"""Any altered extras are ignored."""
config.CONFIG.pm.hook.process_changed_items(items=[extra_factory()])
mock_write.assert_not_called()
def test_process_album(self, mock_write):
"""Any altered albums should have their tracks written."""
album = album_factory()
config.CONFIG.pm.hook.process_changed_items(items=[album])
for track in album.tracks:
mock_write.assert_any_call(track)
def test_process_multiple_tracks(self, mock_write):
"""All altered tracks are written."""
tracks = [track_factory(), track_factory()]
config.CONFIG.pm.hook.process_changed_items(items=tracks)
for track in tracks:
mock_write.assert_any_call(track)
assert mock_write.call_count == 2
def test_dont_write_tracks_twice(self, mock_write):
"""Don't write a track twice if it's album is also in `items`."""
track = track_factory()
config.CONFIG.pm.hook.process_changed_items(items=[track, track.album_obj])
mock_write.assert_called_once_with(track)