| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from http import HTTPStatus |
| 4 |
from typing import TYPE_CHECKING, Any |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from beets.dbcore.types import MULTI_VALUE_DELIMITER |
| 9 |
|
| 10 |
if TYPE_CHECKING: |
| 11 |
from flask.testing import Client |
| 12 |
|
| 13 |
|
| 14 |
@pytest.fixture(scope="session") |
| 15 |
def helper(session_helper): |
| 16 |
"""Keep the helper temp dir alive past the module-scoped Flask app. |
| 17 |
|
| 18 |
``create_app`` opens a configured SQLite library before tests replace it |
| 19 |
with ``helper.lib``. On Windows, module teardown can otherwise try to |
| 20 |
delete ``library.db`` while the app still holds a file handle. The API |
| 21 |
assertions filter known data, so sharing state for the session is harmless. |
| 22 |
""" |
| 23 |
return session_helper |
| 24 |
|
| 25 |
|
| 26 |
@pytest.fixture(scope="module") |
| 27 |
def app(helper): |
| 28 |
from beetsplug.aura import create_app |
| 29 |
|
| 30 |
app = create_app() |
| 31 |
app.config["lib"] = helper.lib |
| 32 |
return app |
| 33 |
|
| 34 |
|
| 35 |
@pytest.fixture(scope="module") |
| 36 |
def item(helper): |
| 37 |
return helper.add_item_fixture( |
| 38 |
album="Album", |
| 39 |
title="Title", |
| 40 |
artist="Artist", |
| 41 |
albumartist="Album Artist", |
| 42 |
genres=["rock", "pop"], |
| 43 |
track=2, |
| 44 |
) |
| 45 |
|
| 46 |
|
| 47 |
@pytest.fixture(scope="module") |
| 48 |
def album(helper, item): |
| 49 |
return helper.lib.add_album([item]) |
| 50 |
|
| 51 |
|
| 52 |
@pytest.fixture(scope="module", autouse=True) |
| 53 |
def _other_album_and_item(helper): |
| 54 |
"""Add another item and album to prove that filtering works.""" |
| 55 |
item = helper.add_item_fixture( |
| 56 |
album="Other Album", |
| 57 |
title="Other Title", |
| 58 |
artist="Other Artist", |
| 59 |
albumartist="Other Album Artist", |
| 60 |
) |
| 61 |
helper.lib.add_album([item]) |
| 62 |
|
| 63 |
|
| 64 |
class TestAuraResponse: |
| 65 |
@pytest.fixture |
| 66 |
def get_response_data(self, client: Client, item): |
| 67 |
"""Return a callback accepting `endpoint` and `params` parameters.""" |
| 68 |
|
| 69 |
def get(endpoint: str, params: dict[str, str]) -> dict[str, Any] | None: |
| 70 |
"""Add additional `params` and GET the given endpoint. |
| 71 |
|
| 72 |
`include` parameter is added to every call to check that the |
| 73 |
functionality that fetches related entities works. |
| 74 |
|
| 75 |
Before returning the response data, ensure that the request is |
| 76 |
successful. |
| 77 |
""" |
| 78 |
response = client.get( |
| 79 |
endpoint, |
| 80 |
query_string={"include": "tracks,artists,albums", **params}, |
| 81 |
) |
| 82 |
|
| 83 |
assert response.status_code == HTTPStatus.OK |
| 84 |
|
| 85 |
return response.json |
| 86 |
|
| 87 |
return get |
| 88 |
|
| 89 |
@pytest.fixture(scope="class") |
| 90 |
def track_document(self, item, album): |
| 91 |
return { |
| 92 |
"type": "track", |
| 93 |
"id": str(item.id), |
| 94 |
"attributes": { |
| 95 |
"album": item.album, |
| 96 |
"albumartist": item.albumartist, |
| 97 |
"artist": item.artist, |
| 98 |
"genre": item.genres, |
| 99 |
"genres": item.genres, |
| 100 |
"size": item.filepath.stat().st_size, |
| 101 |
"title": item.title, |
| 102 |
"track": item.track, |
| 103 |
}, |
| 104 |
"relationships": { |
| 105 |
"albums": {"data": [{"id": str(album.id), "type": "album"}]}, |
| 106 |
"artists": {"data": [{"id": item.artist, "type": "artist"}]}, |
| 107 |
}, |
| 108 |
} |
| 109 |
|
| 110 |
@pytest.fixture(scope="class") |
| 111 |
def artist_document(self, item): |
| 112 |
return { |
| 113 |
"type": "artist", |
| 114 |
"id": item.artist, |
| 115 |
"attributes": {"name": item.artist}, |
| 116 |
"relationships": { |
| 117 |
"tracks": {"data": [{"id": str(item.id), "type": "track"}]} |
| 118 |
}, |
| 119 |
} |
| 120 |
|
| 121 |
@pytest.fixture(scope="class") |
| 122 |
def album_document(self, album): |
| 123 |
return { |
| 124 |
"type": "album", |
| 125 |
"id": str(album.id), |
| 126 |
"attributes": { |
| 127 |
"artist": album.albumartist, |
| 128 |
"title": album.album, |
| 129 |
"genre": album.genres, |
| 130 |
"genres": album.genres, |
| 131 |
}, |
| 132 |
"relationships": { |
| 133 |
"tracks": {"data": [{"id": str(album.id), "type": "track"}]} |
| 134 |
}, |
| 135 |
} |
| 136 |
|
| 137 |
def test_tracks( |
| 138 |
self, |
| 139 |
get_response_data, |
| 140 |
item, |
| 141 |
album_document, |
| 142 |
artist_document, |
| 143 |
track_document, |
| 144 |
): |
| 145 |
"""Test tracks response. |
| 146 |
|
| 147 |
1. Filter by a field which has an int type. |
| 148 |
2. Sort by a field that has no value: make sure the item is included in |
| 149 |
the response. |
| 150 |
""" |
| 151 |
data = get_response_data( |
| 152 |
"/aura/tracks", {"filter[track]": item.track, "sort": "mb_trackid"} |
| 153 |
) |
| 154 |
|
| 155 |
assert data == { |
| 156 |
"data": [track_document], |
| 157 |
"included": [artist_document, album_document], |
| 158 |
} |
| 159 |
|
| 160 |
def test_artists( |
| 161 |
self, get_response_data, item, artist_document, track_document |
| 162 |
): |
| 163 |
data = get_response_data( |
| 164 |
"/aura/artists", {"filter[artist]": item.artist} |
| 165 |
) |
| 166 |
|
| 167 |
assert data == {"data": [artist_document], "included": [track_document]} |
| 168 |
|
| 169 |
def test_albums( |
| 170 |
self, get_response_data, album, album_document, track_document |
| 171 |
): |
| 172 |
"""Test albums response. |
| 173 |
|
| 174 |
1. Filter by a field which has a list type. |
| 175 |
""" |
| 176 |
data = get_response_data( |
| 177 |
"/aura/albums", |
| 178 |
{"filter[genres]": MULTI_VALUE_DELIMITER.join(album.genres)}, |
| 179 |
) |
| 180 |
|
| 181 |
assert data == {"data": [album_document], "included": [track_document]} |