test_library.py 8.31 KB
Newer Older
1
import pytest
2
import time
3
4
5
6
7
import uuid
from mopidy import models

import mopidy_funkwhale.library

8
9
from . import factories

10
11

def test_convert_artist_to_model():
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
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",
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
39
40
41
42
43
44
45
46
47
    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"]])
48
49
50
51
52


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

    result = mopidy_funkwhale.library.convert_to_track(payload)

    assert type(result) == models.Track
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    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"])]
    )


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


@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):
116
117
118
    handler = mocker.patch.object(
        library, expected_handler, return_value=("test", False)
    )
119
120
121
122
123
124

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


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


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

147
    expected = [mopidy_funkwhale.library.convert_to_track(track, ref=True)], True
148
149
150
151
152
    result = library.browse_favorites(["recent"])

    assert result == expected


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
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
231
232
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
        + "albums/?page_size=50&ordering=title&playable=true&artist%s"
        % 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
    )


233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267


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