| 1 |
from typing import Any |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from beets.library import Item |
| 6 |
from beetsplug.deezer import DeezerPlugin |
| 7 |
|
| 8 |
JSONDict = dict[str, Any] |
| 9 |
|
| 10 |
|
| 11 |
@pytest.fixture |
| 12 |
def plugin(): |
| 13 |
return DeezerPlugin() |
| 14 |
|
| 15 |
|
| 16 |
class TestSearchQuery: |
| 17 |
def test_track_query_is_free_text(self, plugin): |
| 18 |
query, filters = plugin.get_search_query_with_filters( |
| 19 |
"track", [Item()], "Artist", "Title", False |
| 20 |
) |
| 21 |
|
| 22 |
assert query == "Title Artist" |
| 23 |
assert filters == {} |
| 24 |
|
| 25 |
def test_track_query_tolerates_missing_artist(self, plugin): |
| 26 |
query, _ = plugin.get_search_query_with_filters( |
| 27 |
"track", [Item()], "", "Title", False |
| 28 |
) |
| 29 |
|
| 30 |
assert query == "Title" |
| 31 |
|
| 32 |
def test_album_query_filters_on_album_and_artist(self, plugin): |
| 33 |
query, filters = plugin.get_search_query_with_filters( |
| 34 |
"album", [Item()], "Artist", "Album", False |
| 35 |
) |
| 36 |
|
| 37 |
assert query == 'album:"Album" artist:"Artist"' |
| 38 |
assert filters == {} |
| 39 |
|
| 40 |
def test_album_query_omits_artist_for_various_artists(self, plugin): |
| 41 |
query, _ = plugin.get_search_query_with_filters( |
| 42 |
"album", [Item()], "Various Artists", "Album", True |
| 43 |
) |
| 44 |
|
| 45 |
assert query == 'album:"Album"' |
| 46 |
|
| 47 |
|
| 48 |
def make_track(artist_id: int, position: int) -> JSONDict: |
| 49 |
"""Build a minimal Deezer track payload credited to ``artist_id``.""" |
| 50 |
return { |
| 51 |
"title": f"Track {position}", |
| 52 |
"id": 1000 + position, |
| 53 |
"link": f"https://www.deezer.com/track/{1000 + position}", |
| 54 |
"duration": 200, |
| 55 |
"track_position": position, |
| 56 |
"disk_number": 1, |
| 57 |
"artist": {"id": artist_id, "name": f"Artist {artist_id}"}, |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
class TestGetTrack: |
| 62 |
def track_data(self, **fields): |
| 63 |
return { |
| 64 |
"id": 1, |
| 65 |
"title": "Title", |
| 66 |
"duration": 100, |
| 67 |
"link": "https://www.deezer.com/track/1", |
| 68 |
**fields, |
| 69 |
} |
| 70 |
|
| 71 |
def test_uses_contributors_when_artist_is_missing(self, plugin): |
| 72 |
track = plugin._get_track( |
| 73 |
self.track_data(contributors=[{"id": 2, "name": "Artist"}]) |
| 74 |
) |
| 75 |
|
| 76 |
assert track.artist == "Artist" |
| 77 |
assert track.artist_id == "2" |
| 78 |
|
| 79 |
def test_falls_back_to_artist_without_contributors(self, plugin): |
| 80 |
track = plugin._get_track( |
| 81 |
self.track_data(artist={"id": 2, "name": "Artist"}) |
| 82 |
) |
| 83 |
|
| 84 |
assert track.artist == "Artist" |
| 85 |
assert track.artist_id == "2" |
| 86 |
|
| 87 |
def test_tolerates_missing_artist_and_contributors(self, plugin): |
| 88 |
track = plugin._get_track(self.track_data()) |
| 89 |
|
| 90 |
assert track.artist is None |
| 91 |
assert track.artist_id is None |
| 92 |
|
| 93 |
|
| 94 |
class TestVariousArtistsDetection: |
| 95 |
def make_album( |
| 96 |
self, album_artist_id: int, contributor_ids: list[int] |
| 97 |
) -> JSONDict: |
| 98 |
"""Build a minimal Deezer album payload.""" |
| 99 |
return { |
| 100 |
"title": "Some Album", |
| 101 |
"link": "https://www.deezer.com/album/1", |
| 102 |
"record_type": "album", |
| 103 |
"label": "Some Label", |
| 104 |
"release_date": "2017-01-01", |
| 105 |
"artist": { |
| 106 |
"id": album_artist_id, |
| 107 |
"name": f"Artist {album_artist_id}", |
| 108 |
}, |
| 109 |
"contributors": [ |
| 110 |
{"id": aid, "name": f"Artist {aid}"} for aid in contributor_ids |
| 111 |
], |
| 112 |
"cover_xl": None, |
| 113 |
} |
| 114 |
|
| 115 |
def mock_album( |
| 116 |
self, |
| 117 |
requests_mock, |
| 118 |
album_artist_id, |
| 119 |
track_artist_ids, |
| 120 |
contributor_ids=None, |
| 121 |
): |
| 122 |
"""Mock the album and album-tracks endpoints for album id 1. |
| 123 |
|
| 124 |
``contributor_ids`` overrides the album-level contributors, which |
| 125 |
otherwise cover every artist involved. |
| 126 |
""" |
| 127 |
if contributor_ids is None: |
| 128 |
contributor_ids = list( |
| 129 |
dict.fromkeys([album_artist_id, *track_artist_ids]) |
| 130 |
) |
| 131 |
requests_mock.get( |
| 132 |
f"{DeezerPlugin.album_url}1", |
| 133 |
json=self.make_album(album_artist_id, contributor_ids), |
| 134 |
) |
| 135 |
requests_mock.get( |
| 136 |
f"{DeezerPlugin.album_url}1/tracks", |
| 137 |
json={ |
| 138 |
"data": [ |
| 139 |
make_track(aid, i) |
| 140 |
for i, aid in enumerate(track_artist_ids, start=1) |
| 141 |
] |
| 142 |
}, |
| 143 |
) |
| 144 |
|
| 145 |
def test_compilation_with_non_va_album_artist(self, plugin, requests_mock): |
| 146 |
# Album credited to a single "main" artist that performs on only one |
| 147 |
# of many tracks: this is a compilation and should be flagged as VA. |
| 148 |
self.mock_album( |
| 149 |
requests_mock, |
| 150 |
album_artist_id=100, |
| 151 |
track_artist_ids=[100, 200, 300, 400, 500, 600], |
| 152 |
) |
| 153 |
|
| 154 |
album_info = plugin.album_for_id("1") |
| 155 |
|
| 156 |
assert album_info is not None |
| 157 |
assert album_info.va is True |
| 158 |
assert album_info.artist == "Various Artists" |
| 159 |
|
| 160 |
def test_plurality_artist_album_not_va(self, plugin, requests_mock): |
| 161 |
# Album whose main artist performs on 2 of 5 tracks (40%) is above |
| 162 |
# the importer's single-artist threshold and stays single-artist. |
| 163 |
self.mock_album( |
| 164 |
requests_mock, |
| 165 |
album_artist_id=100, |
| 166 |
track_artist_ids=[100, 100, 200, 300, 400], |
| 167 |
contributor_ids=[100], |
| 168 |
) |
| 169 |
|
| 170 |
album_info = plugin.album_for_id("1") |
| 171 |
|
| 172 |
assert album_info is not None |
| 173 |
assert album_info.va is False |
| 174 |
assert album_info.artist == "Artist 100" |
| 175 |
|
| 176 |
def test_single_artist_album_not_va(self, plugin, requests_mock): |
| 177 |
# Album whose main artist performs on every track is not a compilation. |
| 178 |
self.mock_album( |
| 179 |
requests_mock, |
| 180 |
album_artist_id=100, |
| 181 |
track_artist_ids=[100, 100, 100, 100], |
| 182 |
) |
| 183 |
|
| 184 |
album_info = plugin.album_for_id("1") |
| 185 |
|
| 186 |
assert album_info is not None |
| 187 |
assert album_info.va is False |
| 188 |
assert album_info.artist == "Artist 100" |