Newer
Older
import os
Eliot Berriot
committed
import pytest
Eliot Berriot
committed
from funkwhale_api.music.management.commands import check_inplace_files
from funkwhale_api.music.management.commands import fix_uploads
Eliot Berriot
committed
from funkwhale_api.music.management.commands import prune_library
Eliot Berriot
committed
DATA_DIR = os.path.dirname(os.path.abspath(__file__))
Eliot Berriot
committed
def test_fix_uploads_bitrate_length(factories, mocker):
upload1 = factories["music.Upload"](bitrate=1, duration=2)
upload2 = factories["music.Upload"](bitrate=None, duration=None)
c = fix_uploads.Command()
Eliot Berriot
committed
mocker.patch(
"funkwhale_api.music.utils.get_audio_file_data",
return_value={"bitrate": 42, "length": 43},
)
Eliot Berriot
committed
c.fix_file_data(dry_run=False)
Eliot Berriot
committed
# not updated
assert upload1.bitrate == 1
assert upload1.duration == 2
Eliot Berriot
committed
# updated
assert upload2.bitrate == 42
assert upload2.duration == 43
Eliot Berriot
committed
def test_fix_uploads_size(factories, mocker):
upload1 = factories["music.Upload"]()
upload2 = factories["music.Upload"]()
upload1.__class__.objects.filter(pk=upload1.pk).update(size=1)
upload2.__class__.objects.filter(pk=upload2.pk).update(size=None)
c = fix_uploads.Command()
Eliot Berriot
committed
mocker.patch("funkwhale_api.music.models.Upload.get_file_size", return_value=2)
Eliot Berriot
committed
c.fix_file_size(dry_run=False)
Eliot Berriot
committed
# not updated
Eliot Berriot
committed
# updated
mp3_path = os.path.join(DATA_DIR, "test.mp3")
ogg_path = os.path.join(DATA_DIR, "test.ogg")
audio_file__from_path=mp3_path,
source="file://{}".format(mp3_path),
mimetype="application/x-empty",
)
# this one already has a mimetype set, to it should not be updated
audio_file__from_path=ogg_path,
source="file://{}".format(ogg_path),
mimetype="audio/something",
)
c.fix_mimetypes(dry_run=False)
assert upload1.mimetype == "audio/mpeg"
assert upload2.mimetype == "audio/something"
Eliot Berriot
committed
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
def test_prune_library_dry_run(factories):
prunable = factories["music.Track"]()
not_prunable = factories["music.Track"]()
c = prune_library.Command()
options = {
"prune_artists": True,
"prune_albums": True,
"prune_tracks": True,
"exclude_favorites": False,
"exclude_listenings": False,
"exclude_playlists": False,
"dry_run": True,
}
c.handle(**options)
for t in [prunable, not_prunable]:
# nothing pruned, because dry run
t.refresh_from_db()
def test_prune_library(factories, mocker):
prunable_track = factories["music.Track"]()
not_prunable_track = factories["music.Track"]()
prunable_tracks = prunable_track.__class__.objects.filter(pk=prunable_track.pk)
get_prunable_tracks = mocker.patch(
"funkwhale_api.music.tasks.get_prunable_tracks", return_value=prunable_tracks
)
prunable_album = factories["music.Album"]()
not_prunable_album = factories["music.Album"]()
prunable_albums = prunable_album.__class__.objects.filter(pk=prunable_album.pk)
get_prunable_albums = mocker.patch(
"funkwhale_api.music.tasks.get_prunable_albums", return_value=prunable_albums
)
prunable_artist = factories["music.Artist"]()
not_prunable_artist = factories["music.Artist"]()
prunable_artists = prunable_artist.__class__.objects.filter(pk=prunable_artist.pk)
get_prunable_artists = mocker.patch(
"funkwhale_api.music.tasks.get_prunable_artists", return_value=prunable_artists
)
c = prune_library.Command()
options = {
"exclude_favorites": mocker.Mock(),
"exclude_listenings": mocker.Mock(),
"exclude_playlists": mocker.Mock(),
"prune_artists": True,
"prune_albums": True,
"prune_tracks": True,
"dry_run": False,
}
c.handle(**options)
get_prunable_tracks.assert_called_once_with(
exclude_favorites=options["exclude_favorites"],
exclude_listenings=options["exclude_listenings"],
exclude_playlists=options["exclude_playlists"],
)
get_prunable_albums.assert_called_once()
get_prunable_artists.assert_called_once()
with pytest.raises(prunable_track.DoesNotExist):
prunable_track.refresh_from_db()
with pytest.raises(prunable_album.DoesNotExist):
prunable_album.refresh_from_db()
with pytest.raises(prunable_artist.DoesNotExist):
prunable_artist.refresh_from_db()
for o in [not_prunable_track, not_prunable_album, not_prunable_artist]:
o.refresh_from_db()
Eliot Berriot
committed
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
def test_check_inplace_files_dry_run(factories, tmpfile):
prunable = factories["music.Upload"](source="file:///notfound", audio_file=None)
not_prunable = factories["music.Upload"](
source="file://{}".format(tmpfile.name), audio_file=None
)
c = check_inplace_files.Command()
c.handle(dry_run=True)
for u in [prunable, not_prunable]:
# nothing pruned, because dry run
u.refresh_from_db()
def test_check_inplace_files_no_dry_run(factories, tmpfile):
prunable = factories["music.Upload"](source="file:///notfound", audio_file=None)
not_prunable = [
factories["music.Upload"](
source="file://{}".format(tmpfile.name), audio_file=None
),
factories["music.Upload"](source="upload://"),
factories["music.Upload"](source="https://"),
]
c = check_inplace_files.Command()
c.handle(dry_run=False)
with pytest.raises(prunable.DoesNotExist):
prunable.refresh_from_db()
for u in not_prunable:
u.refresh_from_db()