-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgame_test.go
351 lines (303 loc) · 8.75 KB
/
game_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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package igdb
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"io/ioutil"
"net/http"
"reflect"
"testing"
)
const (
testGameGet string = "test_data/game_get.json"
testGameList string = "test_data/game_list.json"
testGameSearch string = "test_data/game_search.json"
)
func TestGameService_Get(t *testing.T) {
f, err := ioutil.ReadFile(testGameGet)
if err != nil {
t.Fatal(err)
}
init := make([]*Game, 1)
err = json.Unmarshal(f, &init)
if err != nil {
t.Fatal(err)
}
var tests = []struct {
name string
file string
id int
opts []Option
wantGame *Game
wantErr error
}{
{"Valid response", testGameGet, 7346, []Option{SetFields("name")}, init[0], nil},
{"Invalid ID", testFileEmpty, -1, nil, nil, ErrNegativeID},
{"Empty response", testFileEmpty, 7346, nil, nil, errInvalidJSON},
{"Invalid option", testFileEmpty, 7346, []Option{SetOffset(-99999)}, nil, ErrOutOfRange},
{"No results", testFileEmptyArray, 0, nil, nil, ErrNoResults},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ts, c, err := testServerFile(http.StatusOK, test.file)
if err != nil {
t.Fatal(err)
}
defer ts.Close()
g, err := c.Games.Get(test.id, test.opts...)
if errors.Cause(err) != test.wantErr {
t.Errorf("got: <%v>, want: <%v>", errors.Cause(err), test.wantErr)
}
if !reflect.DeepEqual(g, test.wantGame) {
t.Errorf("got: <%v>, \nwant: <%v>", g, test.wantGame)
}
})
}
}
func TestGameService_List(t *testing.T) {
f, err := ioutil.ReadFile(testGameList)
if err != nil {
t.Fatal(err)
}
init := make([]*Game, 0)
err = json.Unmarshal(f, &init)
if err != nil {
t.Fatal(err)
}
var tests = []struct {
name string
file string
ids []int
opts []Option
wantGames []*Game
wantErr error
}{
{"Valid response", testGameList, []int{105842, 32478, 98774, 104945, 69530}, []Option{SetLimit(5)}, init, nil},
{"Zero IDs", testFileEmpty, nil, nil, nil, ErrEmptyIDs},
{"Invalid ID", testFileEmpty, []int{-500}, nil, nil, ErrNegativeID},
{"Empty response", testFileEmpty, []int{105842, 32478, 98774, 104945, 69530}, nil, nil, errInvalidJSON},
{"Invalid option", testFileEmpty, []int{105842, 32478, 98774, 104945, 69530}, []Option{SetOffset(-99999)}, nil, ErrOutOfRange},
{"No results", testFileEmptyArray, []int{0, 9999999}, nil, nil, ErrNoResults},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ts, c, err := testServerFile(http.StatusOK, test.file)
if err != nil {
t.Fatal(err)
}
defer ts.Close()
g, err := c.Games.List(test.ids, test.opts...)
if errors.Cause(err) != test.wantErr {
t.Errorf("got: <%v>, want: <%v>", errors.Cause(err), test.wantErr)
}
if !reflect.DeepEqual(g, test.wantGames) {
t.Errorf("got: <%v>, \nwant: <%v>", g, test.wantGames)
}
})
}
}
func TestGameService_Index(t *testing.T) {
f, err := ioutil.ReadFile(testGameList)
if err != nil {
t.Fatal(err)
}
init := make([]*Game, 0)
err = json.Unmarshal(f, &init)
if err != nil {
t.Fatal(err)
}
tests := []struct {
name string
file string
opts []Option
wantGames []*Game
wantErr error
}{
{"Valid response", testGameList, []Option{SetLimit(5)}, init, nil},
{"Empty response", testFileEmpty, nil, nil, errInvalidJSON},
{"Invalid option", testFileEmpty, []Option{SetOffset(-99999)}, nil, ErrOutOfRange},
{"No results", testFileEmptyArray, nil, nil, ErrNoResults},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ts, c, err := testServerFile(http.StatusOK, test.file)
if err != nil {
t.Fatal(err)
}
defer ts.Close()
g, err := c.Games.Index(test.opts...)
if errors.Cause(err) != test.wantErr {
t.Errorf("got: <%v>, want: <%v>", errors.Cause(err), test.wantErr)
}
if !reflect.DeepEqual(g, test.wantGames) {
t.Errorf("got: <%v>, \nwant: <%v>", g, test.wantGames)
}
})
}
}
func TestGameService_Search(t *testing.T) {
f, err := ioutil.ReadFile(testGameSearch)
if err != nil {
t.Fatal(err)
}
init := make([]*Game, 0)
err = json.Unmarshal(f, &init)
if err != nil {
t.Fatal(err)
}
var tests = []struct {
name string
file string
qry string
opts []Option
wantGames []*Game
wantErr error
}{
{"Valid response", testGameSearch, "mario", []Option{SetLimit(5)}, init, nil},
{"Empty query", testFileEmpty, "", []Option{SetLimit(5)}, nil, ErrEmptyQry},
{"Empty response", testFileEmpty, "mario", nil, nil, errInvalidJSON},
{"Invalid option", testFileEmpty, "mario", []Option{SetOffset(-99999)}, nil, ErrOutOfRange},
{"No results", testFileEmptyArray, "non-existent entry", nil, nil, ErrNoResults},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ts, c, err := testServerFile(http.StatusOK, test.file)
if err != nil {
t.Fatal(err)
}
defer ts.Close()
g, err := c.Games.Search(test.qry, test.opts...)
if errors.Cause(err) != test.wantErr {
t.Errorf("got: <%v>, want: <%v>", errors.Cause(err), test.wantErr)
}
if !reflect.DeepEqual(g, test.wantGames) {
t.Errorf("got: <%v>, \nwant: <%v>", g, test.wantGames)
}
})
}
}
func TestGameService_Count(t *testing.T) {
var tests = []struct {
name string
resp string
opts []Option
wantCount int
wantErr error
}{
{"Happy path", `{"count": 100}`, []Option{SetFilter("hypes", OpGreaterThan, "75")}, 100, nil},
{"Empty response", "", nil, 0, errInvalidJSON},
{"Invalid option", "", []Option{SetLimit(-100)}, 0, ErrOutOfRange},
{"No results", "[]", nil, 0, ErrNoResults},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ts, c := testServerString(http.StatusOK, test.resp)
defer ts.Close()
count, err := c.Games.Count(test.opts...)
if errors.Cause(err) != test.wantErr {
t.Errorf("got: <%v>, want: <%v>", errors.Cause(err), test.wantErr)
}
if count != test.wantCount {
t.Fatalf("got: <%v>, want: <%v>", count, test.wantCount)
}
})
}
}
func TestGameService_Fields(t *testing.T) {
var tests = []struct {
name string
resp string
wantFields []string
wantErr error
}{
{"Happy path", `["name", "slug", "url"]`, []string{"url", "slug", "name"}, nil},
{"Dot operator", `["logo.url", "background.id"]`, []string{"background.id", "logo.url"}, nil},
{"Asterisk", `["*"]`, []string{"*"}, nil},
{"Empty response", "", nil, errInvalidJSON},
{"No results", "[]", nil, nil},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ts, c := testServerString(http.StatusOK, test.resp)
defer ts.Close()
fields, err := c.Games.Fields()
if errors.Cause(err) != test.wantErr {
t.Errorf("got: <%v>, want: <%v>", errors.Cause(err), test.wantErr)
}
if !equalSlice(fields, test.wantFields) {
t.Fatalf("Expected fields '%v', got '%v'", test.wantFields, fields)
}
})
}
}
func ExampleGameService_Get() {
c := NewClient("YOUR_CLIENT_ID", "YOUR_APP_ACCESS_TOKEN", nil)
g, err := c.Games.Get(7346, SetFields("name", "url", "summary", "storyline", "rating", "hypes", "cover"))
if err != nil {
fmt.Println(err)
return
}
fmt.Println("IGDB entry for The Legend of Zelda: Breath of the Wild\n", *g)
}
func ExampleGameService_List() {
c := NewClient("YOUR_CLIENT_ID", "YOUR_APP_ACCESS_TOKEN", nil)
g, err := c.Games.List([]int{1721, 2777, 1074})
if err != nil {
fmt.Println(err)
return
}
fmt.Println("IGDB entries for Megaman 8, Kirby Air Ride, and Super Mario 64")
for _, v := range g {
fmt.Println(*v)
}
}
func ExampleGameService_Index() {
c := NewClient("YOUR_CLIENT_ID", "YOUR_APP_ACCESS_TOKEN", nil)
g, err := c.Games.Index(
SetLimit(5),
SetFilter("hypes", OpGreaterThan, "80"),
)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("IGDB entries for 5 Games with hypes above 80")
for _, v := range g {
fmt.Println(*v)
}
}
func ExampleGameService_Search() {
c := NewClient("YOUR_CLIENT_ID", "YOUR_APP_ACCESS_TOKEN", nil)
g, err := c.Games.Search(
"mario",
SetFields("*"),
SetFilter("rating", OpGreaterThanEqual, "80"),
SetOrder("rating", OrderDescending),
SetLimit(3))
if err != nil {
fmt.Println(err)
return
}
fmt.Println("IGDB entries for Super Mario 64, Mario Kart 8, and Mario Party")
for _, v := range g {
fmt.Println(*v)
}
}
func ExampleGameService_Count() {
c := NewClient("YOUR_CLIENT_ID", "YOUR_APP_ACCESS_TOKEN", nil)
ct, err := c.Games.Count(SetFilter("created_at", OpGreaterThan, "1993-12-15"))
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Number of games created after December 15, 1993: ", ct)
}
func ExampleGameService_Fields() {
c := NewClient("YOUR_CLIENT_ID", "YOUR_APP_ACCESS_TOKEN", nil)
fl, err := c.Games.Fields()
if err != nil {
fmt.Println(err)
return
}
fmt.Println("List of available fields for the IGDB Game object: ", fl)
}