forked from ramsayleung/rspotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_with_credential.rs
328 lines (297 loc) · 10.6 KB
/
test_with_credential.rs
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
mod util;
use rspotify::{
model::{AlbumId, AlbumType, ArtistId, Country, Market, PlaylistId, TrackId, UserId},
prelude::*,
ClientCredsSpotify,
};
use maybe_async::maybe_async;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;
/// Generating a new basic client for the requests.
#[maybe_async]
pub async fn creds_client() -> ClientCredsSpotify {
let creds = util::get_credentials();
let spotify = ClientCredsSpotify::new(creds);
spotify.request_token().await.unwrap();
spotify
}
#[maybe_async::test(
feature = "__sync",
async(all(feature = "__async", not(target_arch = "wasm32")), tokio::test),
async(all(feature = "__async", target_arch = "wasm32"), wasm_bindgen_test)
)]
async fn test_album() {
let birdy_uri = AlbumId::from_uri("spotify:album:0sNOF9WDwhWunNAHPD3Baj").unwrap();
creds_client().await.album(birdy_uri, None).await.unwrap();
}
#[maybe_async::test(
feature = "__sync",
async(all(feature = "__async", not(target_arch = "wasm32")), tokio::test),
async(all(feature = "__async", target_arch = "wasm32"), wasm_bindgen_test)
)]
async fn test_albums() {
let track_uris = [
AlbumId::from_uri("spotify:album:41MnTivkwTO3UUJ8DrqEJJ").unwrap(),
AlbumId::from_uri("spotify:album:6JWc4iAiJ9FjyK0B59ABb4").unwrap(),
AlbumId::from_uri("spotify:album:6UXCm6bOO4gFlDQZV5yL37").unwrap(),
];
creds_client().await.albums(track_uris, None).await.unwrap();
}
#[maybe_async::test(
feature = "__sync",
async(all(feature = "__async", not(target_arch = "wasm32")), tokio::test),
async(all(feature = "__async", target_arch = "wasm32"), wasm_bindgen_test)
)]
async fn test_album_tracks() {
let birdy_uri = AlbumId::from_uri("spotify:album:6akEvsycLGftJxYudPjmqK").unwrap();
creds_client()
.await
.album_track_manual(birdy_uri, None, Some(2), None)
.await
.unwrap();
}
#[maybe_async::test(
feature = "__sync",
async(all(feature = "__async", not(target_arch = "wasm32")), tokio::test),
async(all(feature = "__async", target_arch = "wasm32"), wasm_bindgen_test)
)]
async fn test_artist_related_artists() {
let birdy_uri = ArtistId::from_uri("spotify:artist:43ZHCT0cAZBISjO8DG9PnE").unwrap();
creds_client()
.await
.artist_related_artists(birdy_uri)
.await
.unwrap();
}
#[maybe_async::test(
feature = "__sync",
async(all(feature = "__async", not(target_arch = "wasm32")), tokio::test),
async(all(feature = "__async", target_arch = "wasm32"), wasm_bindgen_test)
)]
async fn test_artist() {
let birdy_uri = ArtistId::from_uri("spotify:artist:2WX2uTcsvV5OnS0inACecP").unwrap();
creds_client().await.artist(birdy_uri).await.unwrap();
}
#[maybe_async::test(
feature = "__sync",
async(all(feature = "__async", not(target_arch = "wasm32")), tokio::test),
async(all(feature = "__async", target_arch = "wasm32"), wasm_bindgen_test)
)]
async fn test_artists_albums() {
let birdy_uri = ArtistId::from_uri("spotify:artist:2WX2uTcsvV5OnS0inACecP").unwrap();
creds_client()
.await
.artist_albums_manual(
birdy_uri,
Some(AlbumType::Album),
Some(Market::Country(Country::UnitedStates)),
Some(10),
None,
)
.await
.unwrap();
}
#[maybe_async::test(
feature = "__sync",
async(all(feature = "__async", not(target_arch = "wasm32")), tokio::test),
async(all(feature = "__async", target_arch = "wasm32"), wasm_bindgen_test)
)]
async fn test_artists_albums_with_multiple_album_types() {
let birdy_uri = ArtistId::from_uri("spotify:artist:2WX2uTcsvV5OnS0inACecP").unwrap();
let include_groups = [
AlbumType::Album,
AlbumType::Single,
AlbumType::Compilation,
AlbumType::AppearsOn,
];
creds_client()
.await
.artist_albums_manual(
birdy_uri,
include_groups,
Some(Market::Country(Country::UnitedStates)),
Some(10),
None,
)
.await
.unwrap();
}
#[maybe_async::test(
feature = "__sync",
async(all(feature = "__async", not(target_arch = "wasm32")), tokio::test),
async(all(feature = "__async", target_arch = "wasm32"), wasm_bindgen_test)
)]
async fn test_artists_albums_with_zero_album_type() {
let birdy_uri = ArtistId::from_uri("spotify:artist:2WX2uTcsvV5OnS0inACecP").unwrap();
creds_client()
.await
.artist_albums_manual(
birdy_uri,
[],
Some(Market::Country(Country::UnitedStates)),
Some(10),
None,
)
.await
.unwrap();
}
#[maybe_async::test(
feature = "__sync",
async(all(feature = "__async", not(target_arch = "wasm32")), tokio::test),
async(all(feature = "__async", target_arch = "wasm32"), wasm_bindgen_test)
)]
async fn test_artists() {
let artist_uris = [
ArtistId::from_uri("spotify:artist:0oSGxfWSnnOXhD2fKuz2Gy").unwrap(),
ArtistId::from_uri("spotify:artist:3dBVyJ7JuOMt4GE9607Qin").unwrap(),
];
creds_client().await.artists(artist_uris).await.unwrap();
}
#[maybe_async::test(
feature = "__sync",
async(all(feature = "__async", not(target_arch = "wasm32")), tokio::test),
async(all(feature = "__async", target_arch = "wasm32"), wasm_bindgen_test)
)]
async fn test_artist_top_tracks() {
let birdy_uri = ArtistId::from_uri("spotify:artist:2WX2uTcsvV5OnS0inACecP").unwrap();
creds_client()
.await
.artist_top_tracks(birdy_uri, Some(Market::Country(Country::UnitedStates)))
.await
.unwrap();
}
#[maybe_async::test(
feature = "__sync",
async(all(feature = "__async", not(target_arch = "wasm32")), tokio::test),
async(all(feature = "__async", target_arch = "wasm32"), wasm_bindgen_test)
)]
async fn test_audio_analysis() {
let track = TrackId::from_id("06AKEBrKUckW0KREUWRnvT").unwrap();
creds_client().await.track_analysis(track).await.unwrap();
}
#[maybe_async::test(
feature = "__sync",
async(all(feature = "__async", not(target_arch = "wasm32")), tokio::test),
async(all(feature = "__async", target_arch = "wasm32"), wasm_bindgen_test)
)]
async fn test_audio_features() {
let track = TrackId::from_uri("spotify:track:06AKEBrKUckW0KREUWRnvT").unwrap();
creds_client().await.track_features(track).await.unwrap();
}
#[maybe_async::test(
feature = "__sync",
async(all(feature = "__async", not(target_arch = "wasm32")), tokio::test),
async(all(feature = "__async", target_arch = "wasm32"), wasm_bindgen_test)
)]
async fn test_audios_features() {
let mut tracks_ids = vec![];
let track_id1 = TrackId::from_uri("spotify:track:4JpKVNYnVcJ8tuMKjAj50A").unwrap();
tracks_ids.push(track_id1);
let track_id2 = TrackId::from_uri("spotify:track:24JygzOLM0EmRQeGtFcIcG").unwrap();
tracks_ids.push(track_id2);
let track_id_no_features = TrackId::from_uri("spotify:track:6z99LwRS28wga9xc2u09Po").unwrap();
tracks_ids.push(track_id_no_features);
creds_client()
.await
.tracks_features(tracks_ids)
.await
.unwrap();
}
#[maybe_async::test(
feature = "__sync",
async(all(feature = "__async", not(target_arch = "wasm32")), tokio::test),
async(all(feature = "__async", target_arch = "wasm32"), wasm_bindgen_test)
)]
async fn test_user() {
let birdy_uri = UserId::from_id("tuggareutangranser").unwrap();
creds_client().await.user(birdy_uri).await.unwrap();
}
#[maybe_async::test(
feature = "__sync",
async(all(feature = "__async", not(target_arch = "wasm32")), tokio::test),
async(all(feature = "__async", target_arch = "wasm32"), wasm_bindgen_test)
)]
async fn test_track() {
let birdy_uri = TrackId::from_uri("spotify:track:6rqhFgbbKwnb9MLmUQDhG6").unwrap();
creds_client().await.track(birdy_uri, None).await.unwrap();
}
#[maybe_async::test(
feature = "__sync",
async(all(feature = "__async", not(target_arch = "wasm32")), tokio::test),
async(all(feature = "__async", target_arch = "wasm32"), wasm_bindgen_test)
)]
async fn test_tracks() {
let track_uris = [
TrackId::from_uri("spotify:track:3n3Ppam7vgaVa1iaRUc9Lp").unwrap(),
TrackId::from_uri("spotify:track:3twNvmDtFQtAd5gMKedhLD").unwrap(),
];
creds_client().await.tracks(track_uris, None).await.unwrap();
}
#[maybe_async::test(
feature = "__sync",
async(all(feature = "__async", not(target_arch = "wasm32")), tokio::test),
async(all(feature = "__async", target_arch = "wasm32"), wasm_bindgen_test)
)]
async fn test_existing_playlist() {
let playlist_id = PlaylistId::from_id("37i9dQZF1DZ06evO45P0Eo").unwrap();
creds_client()
.await
.playlist(playlist_id, None, None)
.await
.unwrap();
}
#[maybe_async::test(
feature = "__sync",
async(all(feature = "__async", not(target_arch = "wasm32")), tokio::test),
async(all(feature = "__async", target_arch = "wasm32"), wasm_bindgen_test)
)]
async fn test_fake_playlist() {
let playlist_id = PlaylistId::from_id("fakeid").unwrap();
let playlist = creds_client().await.playlist(playlist_id, None, None).await;
assert!(playlist.is_err());
}
pub mod test_pagination {
use super::*;
static ALBUM: &str = "spotify:album:2T7DdrOvsqOqU9bGTkjBYu";
static SONG_NAMES: &[&str; 10] = &[
"Human After All",
"The Prime Time of Your Life",
"Robot Rock",
"Steam Machine",
"Make Love",
"The Brainwasher",
"On / Off",
"Television Rules the Nation",
"Technologic",
"Emotion",
];
/// This test iterates a request of 10 items, with 5 requests of 2 items.
#[cfg(feature = "__sync")]
#[test]
fn test_pagination_sync() {
let mut client = creds_client();
client.config.pagination_chunks = 2;
let album = AlbumId::from_uri(ALBUM).unwrap();
let names = client
.album_track(album, None)
.map(|track| track.unwrap().name)
.collect::<Vec<_>>();
assert_eq!(names, SONG_NAMES);
}
/// This test iterates a request of 10 items, with 5 requests of 2 items.
#[cfg(feature = "__async")]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
async fn test_pagination_async() {
use futures_util::StreamExt;
let mut client = creds_client().await;
client.config.pagination_chunks = 2;
let album = AlbumId::from_uri(ALBUM).unwrap();
let names = client
.album_track(album, None)
.map(|track| track.unwrap().name)
.collect::<Vec<_>>()
.await;
assert_eq!(names, SONG_NAMES);
}
}