-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleagues_test.go
179 lines (161 loc) · 4.31 KB
/
leagues_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
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
package poeapi
import "testing"
func TestLeaguesOptionsToQueryParams(t *testing.T) {
opts := GetLeaguesOptions{
Type: "season",
Realm: "pc",
Season: "testseason",
Compact: true,
Limit: 5,
Offset: 5,
}
params := opts.toQueryParams()
expected := "compact=1&limit=5&offset=5&realm=pc&season=testseason&type=season"
if params != expected {
t.Fatalf("failed to encode leagues query params: expected %s, got %s",
expected, params)
}
}
func TestValidateGetLeaguesOptions(t *testing.T) {
opts := GetLeaguesOptions{
Type: "main",
Realm: "pc",
Compact: false,
Limit: 0,
Offset: 0,
}
if err := validateGetLeaguesOptions(opts); err != nil {
t.Fatalf("failed to validate leagues options: %v", err)
}
}
func TestValidateGetLeaguesOptionsWithInvalidLeagueType(t *testing.T) {
opts := GetLeaguesOptions{
Type: "invalid",
Realm: "pc",
Compact: false,
Limit: 0,
Offset: 0,
}
if err := validateGetLeaguesOptions(opts); err != ErrInvalidLeagueType {
t.Fatal("failed to detect invalid league type in leagues options")
}
}
func TestValidateGetLeaguesOptionsWithInvalidRealm(t *testing.T) {
opts := GetLeaguesOptions{
Type: "main",
Realm: "invalid",
Compact: false,
Limit: 0,
Offset: 0,
}
if err := validateGetLeaguesOptions(opts); err != ErrInvalidRealm {
t.Fatal("failed to detect invalid realm in leagues options")
}
}
func TestValidateGetLeaguesOptionsWithInvalidSeason(t *testing.T) {
opts := GetLeaguesOptions{
Type: "season",
Realm: "pc",
Season: "",
Compact: false,
Limit: 0,
Offset: 0,
}
if err := validateGetLeaguesOptions(opts); err != ErrInvalidSeason {
t.Fatal("failed to detect invalid season in leagues options")
}
}
func TestValidateGetLeaguesOptionsWithInvalidLimit(t *testing.T) {
opts := GetLeaguesOptions{
Type: "main",
Realm: "pc",
Compact: false,
Limit: -1,
Offset: 0,
}
if err := validateGetLeaguesOptions(opts); err != ErrInvalidLimit {
t.Fatal("failed to detect invalid limit in leagues options")
}
}
func TestValidateGetLeaguesOptionsWithInvalidCompactLimit(t *testing.T) {
opts := GetLeaguesOptions{
Type: "main",
Realm: "pc",
Compact: true,
Limit: 231,
Offset: 0,
}
if err := validateGetLeaguesOptions(opts); err != ErrInvalidLimit {
t.Fatal("failed to detect invalid limit in leagues options with compact=1")
}
}
func TestValidateGetLeaguesOptionsWithInvalidNonCompactLimit(t *testing.T) {
opts := GetLeaguesOptions{
Type: "main",
Realm: "pc",
Compact: false,
Limit: 51,
Offset: 0,
}
if err := validateGetLeaguesOptions(opts); err != ErrInvalidLimit {
t.Fatal("failed to detect invalid limit in leagues options with compact=0")
}
}
func TestValidateGetLeaguesOptionsWithInvalidOffset(t *testing.T) {
opts := GetLeaguesOptions{
Type: "main",
Realm: "pc",
Compact: false,
Limit: 0,
Offset: -1,
}
if err := validateGetLeaguesOptions(opts); err != ErrInvalidOffset {
t.Fatal("failed to detect invalid offset in leagues options")
}
}
func TestGetLeagues(t *testing.T) {
c := client{
host: testHost,
useSSL: false,
useCache: false,
limiter: newRateLimiter(UnlimitedRate, UnlimitedRate),
httpClient: testClient,
}
_, err := c.GetLeagues(GetLeaguesOptions{})
if err != nil {
t.Fatalf("failed to get all leagues: %v", err)
}
}
func TestGetLeaguesWithInvalidOptions(t *testing.T) {
c := client{
host: testHost,
useSSL: false,
useCache: false,
limiter: newRateLimiter(UnlimitedRate, UnlimitedRate),
httpClient: testClient,
}
_, err := c.GetLeagues(GetLeaguesOptions{Realm: "toaster"})
if err != ErrInvalidRealm {
t.Fatal("failed to detect invalid options in leagues request")
}
}
func TestParseLeaguesResponse(t *testing.T) {
resp, err := loadFixture("fixtures/leagues.json")
if err != nil {
t.Fatalf("failed to read fixture for leagues test: %v", err)
}
_, err = parseLeaguesResponse(resp)
if err != nil {
t.Fatalf("failed to parse leagues response: %v", err)
}
}
func TestParseLeaguesResponseWithInvalidJSON(t *testing.T) {
resp, err := loadFixture("fixtures/invalid.json")
if err != nil {
t.Fatalf("failed to load fixture for leagues response parsing: %v", err)
}
_, err = parseLeaguesResponse(resp)
if err == nil {
t.Fatal("failed to detect error in leagues response parsing")
}
}