test_library.py 8.28 KB
Newer Older
1
import pytest
Agate's avatar
Agate committed
2
import time
3
4
5
6
7
import uuid
from mopidy import models

import mopidy_funkwhale.library

Agate's avatar
Agate committed
8
9
from . import factories

10
11

def test_convert_artist_to_model():
Agate's avatar
Agate committed
12
    payload = {"id": 42, "mbid": str(uuid.uuid4()), "name": "Test artist"}
13
14
15
16

    result = mopidy_funkwhale.library.convert_to_artist(payload)

    assert type(result) == models.Artist
Agate's avatar
Agate committed
17
18
19
20
    assert result.musicbrainz_id == payload["mbid"]
    assert result.uri == "funkwhale:artists:%s" % (payload["id"],)
    assert result.name == payload["name"]
    assert result.sortname == payload["name"]
21
22
23
24
25
26
27
28


def test_convert_album_to_model():
    payload = {
        "id": 3,
        "tracks": [1, 2, 3, 4],
        "mbid": str(uuid.uuid4()),
        "title": "Test album",
Agate's avatar
Agate committed
29
        "artist": {"id": 42, "mbid": str(uuid.uuid4()), "name": "Test artist"},
30
31
32
33
34
35
36
37
38
        "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
Agate's avatar
Agate committed
39
40
41
42
43
44
45
46
    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"])]
    )
47
48


49
def test_convert_track_to_model():
50
51
    payload = {
        "id": 2,
Agate's avatar
Agate committed
52
        "title": "Test track",
53
54
55
        "mbid": str(uuid.uuid4()),
        "creation_date": "2017-01-01",
        "position": 12,
Agate's avatar
Agate committed
56
        "artist": {"id": 43, "mbid": str(uuid.uuid4()), "name": "Test artist 2"},
57
58
59
60
61
        "album": {
            "id": 3,
            "tracks": [1, 2, 3, 4],
            "mbid": str(uuid.uuid4()),
            "title": "Test album",
Agate's avatar
Agate committed
62
            "artist": {"id": 42, "mbid": str(uuid.uuid4()), "name": "Test artist"},
63
64
65
66
            "release_date": "2017-01-01",
            "cover": {
                "original": "/media/albums/covers/2018/10/03/b4e94b07e-da27-4df4-ae2a-d924a9448544.jpg"
            },
Agate's avatar
Agate committed
67
        },
68
        "uploads": [{"bitrate": 128000, "duration": 120}],
69
70
71
72
73
    }

    result = mopidy_funkwhale.library.convert_to_track(payload)

    assert type(result) == models.Track
Agate's avatar
Agate committed
74
75
76
77
    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"]
78
79
    assert result.length == payload["uploads"][0]["duration"] * 1000
    assert result.bitrate == payload["uploads"][0]["bitrate"] / 1000
Agate's avatar
Agate committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

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


@pytest.mark.parametrize(
    "uri, expected",
    [
        ("funkwhale:albums:42", ("album", 42)),
        ("funkwhale:tracks:42", ("track", 42)),
        ("funkwhale:artists:42", ("artist", 42)),
    ],
)
95
96
def test_parse_uri(uri, expected):
    assert mopidy_funkwhale.library.parse_uri(uri) == expected
Agate's avatar
Agate committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113


@pytest.mark.parametrize("type", ["track", "album", "artist"])
def test_parse_uri(type):
    obj = getattr(models, type.capitalize())(uri="hello:world", name="Hello")
    expected = getattr(models.Ref, type)(uri=obj.uri, name=obj.name)
    assert mopidy_funkwhale.library.to_ref(obj) == expected


@pytest.mark.parametrize(
    "path, expected_handler,remaining",
    [
        ("funkwhale:directory:favorites", "browse_favorites", []),
        ("funkwhale:directory:favorites:by-date", "browse_favorites", ["by-date"]),
    ],
)
def test_browse_routing(library, path, expected_handler, mocker, remaining):
Agate's avatar
Agate committed
114
115
116
    handler = mocker.patch.object(
        library, expected_handler, return_value=("test", False)
    )
Agate's avatar
Agate committed
117
118
119
120
121
122

    assert library.browse(path) == "test"
    assert handler.called_once_with(remaining)


def test_browse_favorites_root(library):
Agate's avatar
Agate committed
123
124
125
126
127
128
129
130
131
132
133
    expected = (
        [
            models.Ref.directory(
                uri="funkwhale:directory:favorites:recent", name="Recent"
            ),
            # models.Ref.directory(
            #     uri="funkwhale:directory:favorites:by-artist", name="By artist"
            # ),
        ],
        False,
    )
Agate's avatar
Agate committed
134
135
136
137
138
139
140
    assert library.browse_favorites([]) == expected


def test_browse_favorites_recent(library, client, requests_mock):
    track = factories.TrackJSONFactory()
    url = (
        client.session.url_base
Agate's avatar
Agate committed
141
        + "tracks/?favorites=true&page_size=50&&ordering=-creation_date"
Agate's avatar
Agate committed
142
143
144
    )
    requests_mock.get(url, json={"results": [track]})

Agate's avatar
Agate committed
145
    expected = [mopidy_funkwhale.library.convert_to_track(track, ref=True)], True
Agate's avatar
Agate committed
146
147
148
149
150
    result = library.browse_favorites(["recent"])

    assert result == expected


Agate's avatar
Agate committed
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
def test_browse_artists_root(library):
    expected = (
        [
            models.Ref.directory(
                uri="funkwhale:directory:artists:recent", name="Recent"
            ),
            models.Ref.directory(
                uri="funkwhale:directory:artists:by-name", name="By name"
            ),
        ],
        False,
    )
    assert library.browse_artists([]) == expected


def test_browse_artists_recent(client, library, requests_mock):
    artist1 = factories.ArtistJSONFactory()
    artist2 = factories.ArtistJSONFactory()
    url = (
        client.session.url_base
        + "artists/?page_size=50&ordering=-creation_date&playable=true"
    )
    requests_mock.get(url, json={"results": [artist1, artist2]})
    uri_prefix = "funkwhale:directory:artists:recent"

    expected = (
        [
            mopidy_funkwhale.library.convert_to_artist(
                artist1, uri_prefix=uri_prefix, ref=True
            ),
            mopidy_funkwhale.library.convert_to_artist(
                artist2, uri_prefix=uri_prefix, ref=True
            ),
        ],
        True,
    )
    assert library.browse_artists(["recent"]) == expected


def test_browse_artists_albums(client, library, requests_mock):
    album1 = factories.AlbumJSONFactory()
    album2 = factories.AlbumJSONFactory(artist=album1["artist"])
    url = (
        client.session.url_base
Georg Krause's avatar
Georg Krause committed
195
        + "albums/?ordering=title&page_size=50&playable=true&artist=%s"
Agate's avatar
Agate committed
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
        % album1["artist"]["id"]
    )
    requests_mock.get(url, json={"results": [album1, album2]})
    uri_prefix = "funkwhale:directory:artists:by-name:%s" % album1["artist"]["id"]
    expected = (
        [
            mopidy_funkwhale.library.convert_to_album(
                album1, uri_prefix=uri_prefix, ref=True
            ),
            mopidy_funkwhale.library.convert_to_album(
                album2, uri_prefix=uri_prefix, ref=True
            ),
        ],
        True,
    )
    assert library.browse_artists(["by-name", album1["artist"]["id"]]) == expected


def test_browse_artists_album_single(client, library, requests_mock):
    track = factories.TrackJSONFactory()
    url = (
        client.session.url_base
        + "tracks/?page_size=50&ordering=position&playable=true&album="
        + str(track["album"]["id"])
    )
    requests_mock.get(url, json={"results": [track]})
    expected = ([mopidy_funkwhale.library.convert_to_track(track, ref=True)], True)
    assert (
        library.browse_artists(
            ["by-name", track["album"]["artist"]["id"], track["album"]["id"]]
        )
        == expected
    )


Agate's avatar
Agate committed
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
def test_cache_set():
    cache = mopidy_funkwhale.library.Cache()
    cache.set("hello:world", "value")
    assert cache["hello:world"][0] < time.time()
    assert cache["hello:world"][1] == "value"


def test_cache_get():
    cache = mopidy_funkwhale.library.Cache()
    cache["hello:world"] = (time.time(), "value")
    assert cache.get("hello:world") == "value"


def test_cache_key_too_old():
    cache = mopidy_funkwhale.library.Cache(max_age=60)
    t = time.time() - 60
    cache["hello:world"] = (t, "value")
    assert cache.get("hello:world") is None
    assert "hello:world" not in cache
Agate's avatar
Agate committed
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265


def test_lookup_from_cache(library):
    track = object()
    library.cache.set("funkwhale:artists:42", track)

    result = library.lookup("funkwhale:artists:42")
    assert result == [track]


def test_lookup_from_cache_iterable(library):
    track = [object()]
    library.cache.set("funkwhale:artists:42", track)

    result = library.lookup("funkwhale:artists:42")
    assert result == track