forked from zmb3/spotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page_test.go
119 lines (112 loc) · 2.63 KB
/
page_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
package spotify
import (
"context"
"errors"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)
func TestClient_NextPage(t *testing.T) {
testTable := []struct {
Name string
Input *basePage
ExpectedPath string
Err error
}{
{
"success",
&basePage{
Next: "/v1/albums/0sNOF9WDwhWunNAHPD3Baj/tracks",
Total: 600,
},
"/v1/albums/0sNOF9WDwhWunNAHPD3Baj/tracks",
nil,
},
{
"no more pages",
&basePage{
Next: "",
},
"",
ErrNoMorePages,
},
{
"nil pointer error",
nil,
"",
errors.New("spotify: p must be a non-nil pointer to a page"),
},
}
for _, tt := range testTable {
t.Run(tt.Name, func(t *testing.T) {
wasCalled := false
client, server := testClientString(200, `{"total": 100}`, func(request *http.Request) {
wasCalled = true
assert.Equal(t, tt.ExpectedPath, request.URL.RequestURI())
})
if tt.Input != nil && tt.Input.Next != "" {
tt.Input.Next = server.URL + tt.Input.Next // add fake server url so we intercept the message
}
err := client.NextPage(context.Background(), tt.Input)
assert.Equal(t, tt.ExpectedPath != "", wasCalled)
if tt.Err == nil {
assert.NoError(t, err)
assert.Equal(t, 100, int(tt.Input.Total)) // value should be from original 600
} else {
assert.EqualError(t, err, tt.Err.Error())
}
})
}
}
func TestClient_PreviousPage(t *testing.T) {
testTable := []struct {
Name string
Input *basePage
ExpectedPath string
Err error
}{
{
"success",
&basePage{
Previous: "/v1/albums/0sNOF9WDwhWunNAHPD3Baj/tracks",
Total: 600,
},
"/v1/albums/0sNOF9WDwhWunNAHPD3Baj/tracks",
nil,
},
{
"no more pages",
&basePage{
Previous: "",
},
"",
ErrNoMorePages,
},
{
"nil pointer error",
nil,
"",
errors.New("spotify: p must be a non-nil pointer to a page"),
},
}
for _, tt := range testTable {
t.Run(tt.Name, func(t *testing.T) {
wasCalled := false
client, server := testClientString(200, `{"total": 100}`, func(request *http.Request) {
wasCalled = true
assert.Equal(t, tt.ExpectedPath, request.URL.RequestURI())
})
if tt.Input != nil && tt.Input.Previous != "" {
tt.Input.Previous = server.URL + tt.Input.Previous // add fake server url so we intercept the message
}
err := client.PreviousPage(context.Background(), tt.Input)
assert.Equal(t, tt.ExpectedPath != "", wasCalled)
if tt.Err == nil {
assert.NoError(t, err)
assert.Equal(t, 100, int(tt.Input.Total)) // value should be from original 600
} else {
assert.EqualError(t, err, tt.Err.Error())
}
})
}
}