-
Notifications
You must be signed in to change notification settings - Fork 299
/
search_test.go
131 lines (121 loc) · 4.99 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
121
122
123
124
125
126
127
128
129
130
131
package spotify
import (
"context"
"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(context.Background(), "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(context.Background(), "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(context.Background(), "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, nil, nil}
if client.NextAlbumResults(context.Background(), nilResults) != ErrNoMorePages ||
client.NextArtistResults(context.Background(), nilResults) != ErrNoMorePages ||
client.NextPlaylistResults(context.Background(), nilResults) != ErrNoMorePages ||
client.NextTrackResults(context.Background(), nilResults) != ErrNoMorePages ||
client.NextShowResults(context.Background(), nilResults) != ErrNoMorePages ||
client.NextEpisodeResults(context.Background(), nilResults) != ErrNoMorePages {
t.Error("Next search result page should have failed for nil results")
}
if client.PreviousAlbumResults(context.Background(), nilResults) != ErrNoMorePages ||
client.PreviousArtistResults(context.Background(), nilResults) != ErrNoMorePages ||
client.PreviousPlaylistResults(context.Background(), nilResults) != ErrNoMorePages ||
client.PreviousTrackResults(context.Background(), nilResults) != ErrNoMorePages ||
client.PreviousShowResults(context.Background(), nilResults) != ErrNoMorePages ||
client.PreviousEpisodeResults(context.Background(), 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),
Shows: new(SimpleShowPage),
Episodes: new(SimpleEpisodePage),
}
if client.NextAlbumResults(context.Background(), emptyURL) != ErrNoMorePages ||
client.NextArtistResults(context.Background(), emptyURL) != ErrNoMorePages ||
client.NextPlaylistResults(context.Background(), emptyURL) != ErrNoMorePages ||
client.NextTrackResults(context.Background(), emptyURL) != ErrNoMorePages ||
client.NextShowResults(context.Background(), emptyURL) != ErrNoMorePages ||
client.NextEpisodeResults(context.Background(), emptyURL) != ErrNoMorePages {
t.Error("Next search result page should have failed with empty URL")
}
if client.PreviousAlbumResults(context.Background(), emptyURL) != ErrNoMorePages ||
client.PreviousArtistResults(context.Background(), emptyURL) != ErrNoMorePages ||
client.PreviousPlaylistResults(context.Background(), emptyURL) != ErrNoMorePages ||
client.PreviousTrackResults(context.Background(), emptyURL) != ErrNoMorePages ||
client.PreviousShowResults(context.Background(), emptyURL) != ErrNoMorePages ||
client.PreviousEpisodeResults(context.Background(), emptyURL) != ErrNoMorePages {
t.Error("Previous search result page should have failed with empty URL")
}
}