File: test_aura.py

package info (click to toggle)
beets 2.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 7,988 kB
  • sloc: python: 46,429; javascript: 8,018; xml: 334; sh: 261; makefile: 125
file content (153 lines) | stat: -rw-r--r-- 4,330 bytes parent folder | download | duplicates (2)
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
97
98
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
import os
from http import HTTPStatus
from pathlib import Path
from typing import Any, Optional

import pytest
from flask.testing import Client

from beets.test.helper import TestHelper


@pytest.fixture(scope="session", autouse=True)
def helper():
    helper = TestHelper()
    helper.setup_beets()
    yield helper
    helper.teardown_beets()


@pytest.fixture(scope="session")
def app(helper):
    from beetsplug.aura import create_app

    app = create_app()
    app.config["lib"] = helper.lib
    return app


@pytest.fixture(scope="session")
def item(helper):
    return helper.add_item_fixture(
        album="Album",
        title="Title",
        artist="Artist",
        albumartist="Album Artist",
    )


@pytest.fixture(scope="session")
def album(helper, item):
    return helper.lib.add_album([item])


@pytest.fixture(scope="session", autouse=True)
def _other_album_and_item(helper):
    """Add another item and album to prove that filtering works."""
    item = helper.add_item_fixture(
        album="Other Album",
        title="Other Title",
        artist="Other Artist",
        albumartist="Other Album Artist",
    )
    helper.lib.add_album([item])


class TestAuraResponse:
    @pytest.fixture
    def get_response_data(self, client: Client, item):
        """Return a callback accepting `endpoint` and `params` parameters."""

        def get(
            endpoint: str, params: dict[str, str]
        ) -> Optional[dict[str, Any]]:
            """Add additional `params` and GET the given endpoint.

            `include` parameter is added to every call to check that the
            functionality that fetches related entities works.

            Before returning the response data, ensure that the request is
            successful.
            """
            response = client.get(
                endpoint,
                query_string={"include": "tracks,artists,albums", **params},
            )

            assert response.status_code == HTTPStatus.OK

            return response.json

        return get

    @pytest.fixture(scope="class")
    def track_document(self, item, album):
        return {
            "type": "track",
            "id": str(item.id),
            "attributes": {
                "album": item.album,
                "albumartist": item.albumartist,
                "artist": item.artist,
                "size": Path(os.fsdecode(item.path)).stat().st_size,
                "title": item.title,
                "track": 1,
            },
            "relationships": {
                "albums": {"data": [{"id": str(album.id), "type": "album"}]},
                "artists": {"data": [{"id": item.artist, "type": "artist"}]},
            },
        }

    @pytest.fixture(scope="class")
    def artist_document(self, item):
        return {
            "type": "artist",
            "id": item.artist,
            "attributes": {"name": item.artist},
            "relationships": {
                "tracks": {"data": [{"id": str(item.id), "type": "track"}]}
            },
        }

    @pytest.fixture(scope="class")
    def album_document(self, album):
        return {
            "type": "album",
            "id": str(album.id),
            "attributes": {"artist": album.albumartist, "title": album.album},
            "relationships": {
                "tracks": {"data": [{"id": str(album.id), "type": "track"}]}
            },
        }

    def test_tracks(
        self,
        get_response_data,
        item,
        album_document,
        artist_document,
        track_document,
    ):
        data = get_response_data("/aura/tracks", {"filter[title]": item.title})

        assert data == {
            "data": [track_document],
            "included": [artist_document, album_document],
        }

    def test_artists(
        self, get_response_data, item, artist_document, track_document
    ):
        data = get_response_data(
            "/aura/artists", {"filter[artist]": item.artist}
        )

        assert data == {"data": [artist_document], "included": [track_document]}

    def test_albums(
        self, get_response_data, album, album_document, track_document
    ):
        data = get_response_data("/aura/albums", {"filter[album]": album.album})

        assert data == {"data": [album_document], "included": [track_document]}