Skip to content
Snippets Groups Projects
Verified Commit 5d2dbbc8 authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Upgraded celery to 4.1, added endpoint logic for fingerprinting audio files

parent 4834b9e4
Branches
Tags
No related merge requests found
from funkwhale_api.providers.acoustid import get_acoustid_client
from funkwhale_api.music import tasks
def test_set_acoustid_on_track_file(factories, mocker):
track_file = factories['music.TrackFile'](acoustid_track_id=None)
id = 'e475bf79-c1ce-4441-bed7-1e33f226c0a2'
payload = {
'results': [
{'id': id,
'recordings': [
{'artists': [
{'id': '9c6bddde-6228-4d9f-ad0d-03f6fcb19e13',
'name': 'Binärpilot'}],
'duration': 268,
'id': 'f269d497-1cc0-4ae4-a0c4-157ec7d73fcb',
'title': 'Bend'}],
'score': 0.860825}],
'status': 'ok'
}
m = mocker.patch('acoustid.match', return_value=payload)
r = tasks.set_acoustid_on_track_file(track_file_id=track_file.pk)
track_file.refresh_from_db()
assert str(track_file.acoustid_track_id) == id
assert r == id
m.assert_called_once_with('', track_file.audio_file.path, parse=False)
def test_set_acoustid_on_track_file_required_high_score(factories, mocker):
track_file = factories['music.TrackFile'](acoustid_track_id=None)
id = 'e475bf79-c1ce-4441-bed7-1e33f226c0a2'
payload = {
'results': [{'score': 0.79}],
'status': 'ok'
}
m = mocker.patch('acoustid.match', return_value=payload)
r = tasks.set_acoustid_on_track_file(track_file_id=track_file.pk)
track_file.refresh_from_db()
assert track_file.acoustid_track_id is None
from funkwhale_api.providers.acoustid import get_acoustid_client
def test_client_is_configured_with_correct_api_key(preferences):
api_key = 'hello world'
preferences['providers_acoustid__api_key'] = api_key
client = get_acoustid_client()
assert client.api_key == api_key
def test_client_returns_raw_results(db, mocker, preferences):
api_key = 'test'
preferences['providers_acoustid__api_key'] = api_key
payload = {
'results': [
{'id': 'e475bf79-c1ce-4441-bed7-1e33f226c0a2',
'recordings': [
{'artists': [
{'id': '9c6bddde-6228-4d9f-ad0d-03f6fcb19e13',
'name': 'Binärpilot'}],
'duration': 268,
'id': 'f269d497-1cc0-4ae4-a0c4-157ec7d73fcb',
'title': 'Bend'}],
'score': 0.860825}],
'status': 'ok'
}
m = mocker.patch('acoustid.match', return_value=payload)
client = get_acoustid_client()
response = client.match('/tmp/noopfile.mp3')
assert response == payload
m.assert_called_once_with('test', '/tmp/noopfile.mp3', parse=False)
import os
import acoustid
import datetime
import os
from .music import data as api_data
from funkwhale_api.providers.audiofile import tasks
DATA_DIR = os.path.join(
......@@ -9,7 +11,36 @@ DATA_DIR = os.path.join(
)
def test_can_import_single_audio_file(db, mocker):
def test_import_file_with_acoustid(db, mocker, preferences):
mbid = api_data.tracks['get']['8bitadventures']['recording']['id']
payload = {
'results': [{
'id': 'e475bf79-c1ce-4441-bed7-1e33f226c0a2',
'recordings': [{'id': mbid}],
'score': 0.86
}]
}
path = os.path.join(DATA_DIR, 'dummy_file.ogg')
m = mocker.patch('acoustid.match', return_value=payload)
mocker.patch(
'funkwhale_api.musicbrainz.api.artists.get',
return_value=api_data.artists['get']['adhesive_wombat'])
mocker.patch(
'funkwhale_api.musicbrainz.api.releases.get',
return_value=api_data.albums['get']['marsupial'])
mocker.patch(
'funkwhale_api.musicbrainz.api.recordings.get',
return_value=api_data.tracks['get']['8bitadventures'])
track_file = tasks.from_path(path)
result = payload['results'][0]
assert track_file.acoustid_track_id == result['id']
assert track_file.track.mbid == result['recordings'][0]['id']
m.assert_called_once_with('', path, parse=False)
def test_can_import_single_audio_file_without_acoustid(db, mocker):
mocker.patch('acoustid.match', side_effect=acoustid.WebServiceError('test'))
metadata = {
'artist': ['Test artist'],
'album': ['Test album'],
......@@ -20,7 +51,6 @@ def test_can_import_single_audio_file(db, mocker):
'musicbrainz_trackid': ['bd21ac48-46d8-4e78-925f-d9cc2a294656'],
'musicbrainz_artistid': ['013c8e5b-d72a-4cd3-8dee-6c64d6125823'],
}
m1 = mocker.patch('mutagen.File', return_value=metadata)
m2 = mocker.patch(
'funkwhale_api.music.metadata.Metadata.get_file_type',
......
import pytest
from funkwhale_api.taskapp import celery
class Dummy:
@staticmethod
def noop(instance):
pass
def test_require_instance_decorator(factories, mocker):
user = factories['users.User']()
@celery.require_instance(user.__class__, 'user')
def t(user):
Dummy.noop(user)
m = mocker.patch.object(Dummy, 'noop')
t(user_id=user.pk)
m.assert_called_once_with(user)
def test_require_instance_decorator_accepts_qs(factories, mocker):
user = factories['users.User'](is_active=False)
qs = user.__class__.objects.filter(is_active=True)
@celery.require_instance(qs, 'user')
def t(user):
pass
with pytest.raises(user.__class__.DoesNotExist):
t(user_id=user.pk)
......@@ -30,7 +30,7 @@ services:
links:
- postgres
- redis
command: python manage.py celery worker
command: celery -A funkwhale_api.taskapp worker
environment:
- "DJANGO_ALLOWED_HOSTS=localhost"
- "DJANGO_SETTINGS_MODULE=config.settings.local"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment