forked from mopidy/mopidy-spotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_images.py
219 lines (163 loc) · 5.85 KB
/
test_images.py
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
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
import pytest
from mopidy import models
from mopidy_spotify import images
@pytest.fixture
def img_provider(provider):
images._cache = {}
return provider
def test_get_artist_images(web_client_mock, img_provider):
uris = [
"spotify:artist:4FCGgZrVQtcbDFEap3OAb2",
"http://open.spotify.com/artist/0Nsz79ZcE8E4i3XZhCzZ1l",
]
web_client_mock.get.return_value = {
"artists": [
{
"id": "4FCGgZrVQtcbDFEap3OAb2",
"images": [
{"height": 640, "url": "img://1/a", "width": 640},
{"height": 300, "url": "img://1/b", "width": 300},
],
},
{
"id": "0Nsz79ZcE8E4i3XZhCzZ1l",
"images": [{"height": 64, "url": "img://2/a", "width": 64}],
},
]
}
result = img_provider.get_images(uris)
web_client_mock.get.assert_called_once_with(
"artists",
params={"ids": "4FCGgZrVQtcbDFEap3OAb2,0Nsz79ZcE8E4i3XZhCzZ1l"},
)
assert len(result) == 2
assert sorted(result.keys()) == sorted(uris)
assert len(result[uris[0]]) == 2
assert len(result[uris[1]]) == 1
image1a = result[uris[0]][0]
assert isinstance(image1a, models.Image)
assert image1a.uri == "img://1/a"
assert image1a.height == 640
assert image1a.width == 640
image1b = result[uris[0]][1]
assert isinstance(image1b, models.Image)
assert image1b.uri == "img://1/b"
assert image1b.height == 300
assert image1b.width == 300
image2a = result[uris[1]][0]
assert isinstance(image2a, models.Image)
assert image2a.uri == "img://2/a"
assert image2a.height == 64
assert image2a.width == 64
def test_get_album_images(web_client_mock, img_provider):
uris = [
"http://play.spotify.com/album/1utFPuvgBHXzLJdqhCDOkg",
]
web_client_mock.get.return_value = {
"albums": [
{
"id": "1utFPuvgBHXzLJdqhCDOkg",
"images": [{"height": 640, "url": "img://1/a", "width": 640}],
}
]
}
result = img_provider.get_images(uris)
web_client_mock.get.assert_called_once_with(
"albums", params={"ids": "1utFPuvgBHXzLJdqhCDOkg"}
)
assert len(result) == 1
assert sorted(result.keys()) == sorted(uris)
assert len(result[uris[0]]) == 1
image = result[uris[0]][0]
assert isinstance(image, models.Image)
assert image.uri == "img://1/a"
assert image.height == 640
assert image.width == 640
def test_get_track_images(web_client_mock, img_provider):
uris = [
"spotify:track:41shEpOKyyadtG6lDclooa",
]
web_client_mock.get.return_value = {
"tracks": [
{
"id": "41shEpOKyyadtG6lDclooa",
"album": {
"uri": "spotify:album:1utFPuvgBHXzLJdqhCDOkg",
"images": [
{"height": 640, "url": "img://1/a", "width": 640}
],
},
}
]
}
result = img_provider.get_images(uris)
web_client_mock.get.assert_called_once_with(
"tracks", params={"ids": "41shEpOKyyadtG6lDclooa"}
)
assert len(result) == 1
assert sorted(result.keys()) == sorted(uris)
assert len(result[uris[0]]) == 1
image = result[uris[0]][0]
assert isinstance(image, models.Image)
assert image.uri == "img://1/a"
assert image.height == 640
assert image.width == 640
def test_get_playlist_image(web_client_mock, img_provider):
uris = ["spotify:playlist:41shEpOKyyadtG6lDclooa"]
web_client_mock.get.return_value = {
"id": "41shEpOKyyadtG6lDclooa",
"images": [{"height": 640, "url": "img://1/a", "width": 640}],
}
result = img_provider.get_images(uris)
web_client_mock.get.assert_called_once_with(
"playlists/41shEpOKyyadtG6lDclooa"
)
assert len(result) == 1
assert sorted(result.keys()) == sorted(uris)
assert len(result[uris[0]]) == 1
image = result[uris[0]][0]
assert isinstance(image, models.Image)
assert image.uri == "img://1/a"
assert image.height == 640
assert image.width == 640
def test_results_are_cached(web_client_mock, img_provider):
uris = [
"spotify:track:41shEpOKyyadtG6lDclooa",
]
web_client_mock.get.return_value = {
"tracks": [
{
"id": "41shEpOKyyadtG6lDclooa",
"album": {
"uri": "spotify:album:1utFPuvgBHXzLJdqhCDOkg",
"images": [
{"height": 640, "url": "img://1/a", "width": 640}
],
},
}
]
}
result1 = img_provider.get_images(uris)
result2 = img_provider.get_images(uris)
assert web_client_mock.get.call_count == 1
assert result1 == result2
def test_max_50_ids_per_request(web_client_mock, img_provider):
uris = [f"spotify:track:{i}" for i in range(51)]
web_client_mock.get.return_value = {}
img_provider.get_images(uris)
assert web_client_mock.get.call_count == 2
request_ids_1 = web_client_mock.get.call_args_list[0][1]["params"]["ids"]
assert request_ids_1 == ",".join(str(i) for i in range(50))
request_ids_2 = web_client_mock.get.call_args_list[1][1]["params"]["ids"]
assert request_ids_2 == "50"
def test_invalid_uri_fails(img_provider):
with pytest.raises(ValueError) as exc:
img_provider.get_images(["foo:bar"])
assert str(exc.value) == "Could not parse 'foo:bar' as a Spotify URI"
def test_no_uris_gives_no_results(img_provider):
result = img_provider.get_images([])
assert result == {}
def test_service_returns_empty_result(web_client_mock, img_provider):
web_client_mock.get.return_value = {"tracks": [{}]}
result = img_provider.get_images(["spotify:track:41shEpOKyyadtG6lDclooa"])
assert result == {}