test_library.py 3.27 KB
Newer Older
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
import pytest
import uuid
from mopidy import models

import mopidy_funkwhale.library


def test_convert_artist_to_model():
    payload = {
        'id': 42,
        'mbid': str(uuid.uuid4()),
        'name': "Test artist",
    }

    result = mopidy_funkwhale.library.convert_to_artist(payload)

    assert type(result) == models.Artist
    assert result.musicbrainz_id == payload['mbid']
    assert result.uri == 'funkwhale:artists:%s' % (payload['id'],)
    assert result.name == payload['name']
    assert result.sortname == payload['name']


def test_convert_album_to_model():
    payload = {
        "id": 3,
        "tracks": [1, 2, 3, 4],
        "mbid": str(uuid.uuid4()),
        "title": "Test album",
        "artist": {
            'id': 42,
            'mbid': str(uuid.uuid4()),
            'name': "Test artist",
        },
        "release_date": "2017-01-01",
        "cover": {
            "original": "/media/albums/covers/2018/10/03/b4e94b07e-da27-4df4-ae2a-d924a9448544.jpg"
        },
    }

    result = mopidy_funkwhale.library.convert_to_album(payload)

    assert type(result) == models.Album
    assert result.musicbrainz_id == payload['mbid']
    assert result.uri == 'funkwhale:albums:%s' % (payload['id'],)
    assert result.name == payload['title']
    assert result.date == payload['release_date']
    assert result.num_tracks == len(payload['tracks'])
    assert result.artists == frozenset([mopidy_funkwhale.library.convert_to_artist(payload['artist'])])
    assert result.images == frozenset([payload['cover']['original']])


def test_convert_album_to_model():
    payload = {
        "id": 2,
        "title": 'Test track',
        "mbid": str(uuid.uuid4()),
        "creation_date": "2017-01-01",
        "position": 12,
        "bitrate": 128000,
        "duration": 120,
        "artist": {
            'id': 43,
            'mbid': str(uuid.uuid4()),
            'name': "Test artist 2",
        },
        "album": {
            "id": 3,
            "tracks": [1, 2, 3, 4],
            "mbid": str(uuid.uuid4()),
            "title": "Test album",
            "artist": {
                'id': 42,
                'mbid': str(uuid.uuid4()),
                'name': "Test artist",
            },
            "release_date": "2017-01-01",
            "cover": {
                "original": "/media/albums/covers/2018/10/03/b4e94b07e-da27-4df4-ae2a-d924a9448544.jpg"
            },
        }
    }

    result = mopidy_funkwhale.library.convert_to_track(payload)

    assert type(result) == models.Track
    assert result.musicbrainz_id == payload['mbid']
    assert result.uri == 'funkwhale:tracks:%s' % (payload['id'],)
    assert result.name == payload['title']
    assert result.date == payload['album']['release_date']
    assert result.length == payload['duration'] * 1000
    assert result.bitrate == payload['bitrate'] / 1000

    assert result.album == mopidy_funkwhale.library.convert_to_album(payload['album'])
    assert result.artists == frozenset([mopidy_funkwhale.library.convert_to_artist(payload['artist'])])

97

98
99
100
101
102
103
104
@pytest.mark.parametrize('uri, expected', [
    ('funkwhale:albums:42', ('album', 42)),
    ('funkwhale:tracks:42', ('track', 42)),
    ('funkwhale:artists:42', ('artist', 42)),
])
def test_parse_uri(uri, expected):
    assert mopidy_funkwhale.library.parse_uri(uri) == expected