forked from zmb3/spotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_test.go
120 lines (110 loc) · 3.84 KB
/
search_test.go
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
package spotify
import (
"net/http"
"testing"
)
func TestSearchArtist(t *testing.T) {
client, server := testClientFile(http.StatusOK, "test_data/search_artist.txt")
defer server.Close()
result, err := client.Search("tania bowra", SearchTypeArtist)
if err != nil {
t.Error(err)
}
if result.Albums != nil {
t.Error("Searched for artists but received album results")
}
if result.Playlists != nil {
t.Error("Searched for artists but received playlist results")
}
if result.Tracks != nil {
t.Error("Searched for artists but received track results")
}
if result.Artists == nil || len(result.Artists.Artists) == 0 {
t.Error("Didn't receive artist results")
}
if result.Artists.Artists[0].Name != "Tania Bowra" {
t.Error("Got wrong artist name")
}
}
func TestSearchTracks(t *testing.T) {
client, server := testClientFile(http.StatusOK, "test_data/search_tracks.txt")
defer server.Close()
result, err := client.Search("uptown", SearchTypeTrack)
if err != nil {
t.Error(err)
}
if result.Albums != nil {
t.Error("Searched for tracks but got album results")
}
if result.Playlists != nil {
t.Error("Searched for tracks but got playlist results")
}
if result.Artists != nil {
t.Error("Searched for tracks but got artist results")
}
if result.Tracks == nil || len(result.Tracks.Tracks) == 0 {
t.Fatal("Didn't receive track results")
}
if name := result.Tracks.Tracks[0].Name; name != "Uptown Funk" {
t.Errorf("Got %s, wanted Uptown Funk\n", name)
}
}
func TestSearchPlaylistTrack(t *testing.T) {
client, server := testClientFile(http.StatusOK, "test_data/search_trackplaylist.txt")
defer server.Close()
result, err := client.Search("holiday", SearchTypePlaylist|SearchTypeTrack)
if err != nil {
t.Error(err)
}
if result.Albums != nil {
t.Error("Searched for playlists and tracks but received album results")
}
if result.Artists != nil {
t.Error("Searched for playlists and tracks but received artist results")
}
if result.Tracks == nil {
t.Error("Didn't receive track results")
}
if result.Playlists == nil {
t.Error("Didn't receive playlist results")
}
}
func TestPrevNextSearchPageErrors(t *testing.T) {
client, server := testClientString(0, "")
defer server.Close()
// we expect to get ErrNoMorePages when trying to get the prev/next page
// under either of these conditions:
// 1) there are no results (nil)
nilResults := &SearchResult{nil, nil, nil, nil}
if client.NextAlbumResults(nilResults) != ErrNoMorePages ||
client.NextArtistResults(nilResults) != ErrNoMorePages ||
client.NextPlaylistResults(nilResults) != ErrNoMorePages ||
client.NextTrackResults(nilResults) != ErrNoMorePages {
t.Error("Next search result page should have failed for nil results")
}
if client.PreviousAlbumResults(nilResults) != ErrNoMorePages ||
client.PreviousArtistResults(nilResults) != ErrNoMorePages ||
client.PreviousPlaylistResults(nilResults) != ErrNoMorePages ||
client.PreviousTrackResults(nilResults) != ErrNoMorePages {
t.Error("Previous search result page should have failed for nil results")
}
// 2) the prev/next URL is empty
emptyURL := &SearchResult{
Artists: new(FullArtistPage),
Albums: new(SimpleAlbumPage),
Playlists: new(SimplePlaylistPage),
Tracks: new(FullTrackPage),
}
if client.NextAlbumResults(emptyURL) != ErrNoMorePages ||
client.NextArtistResults(emptyURL) != ErrNoMorePages ||
client.NextPlaylistResults(emptyURL) != ErrNoMorePages ||
client.NextTrackResults(emptyURL) != ErrNoMorePages {
t.Error("Next search result page should have failed with empty URL")
}
if client.PreviousAlbumResults(emptyURL) != ErrNoMorePages ||
client.PreviousArtistResults(emptyURL) != ErrNoMorePages ||
client.PreviousPlaylistResults(emptyURL) != ErrNoMorePages ||
client.PreviousTrackResults(emptyURL) != ErrNoMorePages {
t.Error("Previous search result page should have failed with empty URL")
}
}