-
Notifications
You must be signed in to change notification settings - Fork 17
/
gameengine_test.go
213 lines (188 loc) · 5.69 KB
/
gameengine_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
package igdb
import (
"encoding/json"
"github.com/pkg/errors"
"io/ioutil"
"net/http"
"reflect"
"testing"
)
const (
testGameEngineGet string = "test_data/gameengine_get.json"
testGameEngineList string = "test_data/gameengine_list.json"
)
func TestGameEngineService_Get(t *testing.T) {
f, err := ioutil.ReadFile(testGameEngineGet)
if err != nil {
t.Fatal(err)
}
init := make([]*GameEngine, 1)
err = json.Unmarshal(f, &init)
if err != nil {
t.Fatal(err)
}
var tests = []struct {
name string
file string
id int
opts []Option
wantGameEngine *GameEngine
wantErr error
}{
{"Valid response", testGameEngineGet, 103, []Option{SetFields("name")}, init[0], nil},
{"Invalid ID", testFileEmpty, -1, nil, nil, ErrNegativeID},
{"Empty response", testFileEmpty, 103, nil, nil, errInvalidJSON},
{"Invalid option", testFileEmpty, 103, []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()
eng, err := c.GameEngines.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(eng, test.wantGameEngine) {
t.Errorf("got: <%v>, \nwant: <%v>", eng, test.wantGameEngine)
}
})
}
}
func TestGameEngineService_List(t *testing.T) {
f, err := ioutil.ReadFile(testGameEngineList)
if err != nil {
t.Fatal(err)
}
init := make([]*GameEngine, 0)
err = json.Unmarshal(f, &init)
if err != nil {
t.Fatal(err)
}
var tests = []struct {
name string
file string
ids []int
opts []Option
wantGameEngines []*GameEngine
wantErr error
}{
{"Valid response", testGameEngineList, []int{224, 203, 611, 84, 229}, []Option{SetLimit(5)}, init, nil},
{"Zero IDs", testFileEmpty, nil, nil, nil, ErrEmptyIDs},
{"Invalid ID", testFileEmpty, []int{-500}, nil, nil, ErrNegativeID},
{"Empty response", testFileEmpty, []int{224, 203, 611, 84, 229}, nil, nil, errInvalidJSON},
{"Invalid option", testFileEmpty, []int{224, 203, 611, 84, 229}, []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()
eng, err := c.GameEngines.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(eng, test.wantGameEngines) {
t.Errorf("got: <%v>, \nwant: <%v>", eng, test.wantGameEngines)
}
})
}
}
func TestGameEngineService_Index(t *testing.T) {
f, err := ioutil.ReadFile(testGameEngineList)
if err != nil {
t.Fatal(err)
}
init := make([]*GameEngine, 0)
err = json.Unmarshal(f, &init)
if err != nil {
t.Fatal(err)
}
tests := []struct {
name string
file string
opts []Option
wantGameEngines []*GameEngine
wantErr error
}{
{"Valid response", testGameEngineList, []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()
eng, err := c.GameEngines.Index(test.opts...)
if errors.Cause(err) != test.wantErr {
t.Errorf("got: <%v>, want: <%v>", errors.Cause(err), test.wantErr)
}
if !reflect.DeepEqual(eng, test.wantGameEngines) {
t.Errorf("got: <%v>, \nwant: <%v>", eng, test.wantGameEngines)
}
})
}
}
func TestGameEngineService_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.GameEngines.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 TestGameEngineService_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},
{"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.GameEngines.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)
}
})
}
}