test_library.py 5.28 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
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
154
155
156
157
158
159
160
161
162
163
164


@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):
    handler = mocker.patch.object(library, expected_handler, return_value="test")

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


def test_browse_favorites_root(library):
    expected = [
        models.Ref.directory(uri="funkwhale:directory:favorites:recent", name="Recent"),
        models.Ref.directory(
            uri="funkwhale:directory:favorites:by-artist", name="By artist"
        ),
    ]
    assert library.browse_favorites([]) == expected


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

    expected = [mopidy_funkwhale.library.convert_to_track(track, ref=True)]
    result = library.browse_favorites(["recent"])

    assert result == expected


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