-
Notifications
You must be signed in to change notification settings - Fork 17
/
screenshot_test.go
214 lines (188 loc) · 5.73 KB
/
screenshot_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
package igdb
import (
"encoding/json"
"github.com/pkg/errors"
"io/ioutil"
"net/http"
"reflect"
"testing"
)
const (
testScreenshotGet string = "test_data/screenshot_get.json"
testScreenshotList string = "test_data/screenshot_list.json"
)
func TestScreenshotService_Get(t *testing.T) {
f, err := ioutil.ReadFile(testScreenshotGet)
if err != nil {
t.Fatal(err)
}
init := make([]*Screenshot, 1)
err = json.Unmarshal(f, &init)
if err != nil {
t.Fatal(err)
}
var tests = []struct {
name string
file string
id int
opts []Option
wantScreenshot *Screenshot
wantErr error
}{
{"Valid response", testScreenshotGet, 65852, []Option{SetFields("name")}, init[0], nil},
{"Invalid ID", testFileEmpty, -1, nil, nil, ErrNegativeID},
{"Empty response", testFileEmpty, 65852, nil, nil, errInvalidJSON},
{"Invalid option", testFileEmpty, 65852, []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()
shot, err := c.Screenshots.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(shot, test.wantScreenshot) {
t.Errorf("got: <%v>, \nwant: <%v>", shot, test.wantScreenshot)
}
})
}
}
func TestScreenshotService_List(t *testing.T) {
f, err := ioutil.ReadFile(testScreenshotList)
if err != nil {
t.Fatal(err)
}
init := make([]*Screenshot, 0)
err = json.Unmarshal(f, &init)
if err != nil {
t.Fatal(err)
}
var tests = []struct {
name string
file string
ids []int
opts []Option
wantScreenshots []*Screenshot
wantErr error
}{
{"Valid response", testScreenshotList, []int{740, 210478, 210664, 210757}, []Option{SetLimit(5)}, init, nil},
{"Zero IDs", testFileEmpty, nil, nil, nil, ErrEmptyIDs},
{"Invalid ID", testFileEmpty, []int{-500}, nil, nil, ErrNegativeID},
{"Empty response", testFileEmpty, []int{740, 210478, 210664, 210757}, nil, nil, errInvalidJSON},
{"Invalid option", testFileEmpty, []int{740, 210478, 210664, 210757}, []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()
shot, err := c.Screenshots.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(shot, test.wantScreenshots) {
t.Errorf("got: <%v>, \nwant: <%v>", shot, test.wantScreenshots)
}
})
}
}
func TestScreenshotService_Index(t *testing.T) {
f, err := ioutil.ReadFile(testScreenshotList)
if err != nil {
t.Fatal(err)
}
init := make([]*Screenshot, 0)
err = json.Unmarshal(f, &init)
if err != nil {
t.Fatal(err)
}
tests := []struct {
name string
file string
opts []Option
wantScreenshots []*Screenshot
wantErr error
}{
{"Valid response", testScreenshotList, []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()
shot, err := c.Screenshots.Index(test.opts...)
if errors.Cause(err) != test.wantErr {
t.Errorf("got: <%v>, want: <%v>", errors.Cause(err), test.wantErr)
}
if !reflect.DeepEqual(shot, test.wantScreenshots) {
t.Errorf("got: <%v>, \nwant: <%v>", shot, test.wantScreenshots)
}
})
}
}
func TestScreenshotService_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(-99999)}, 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.Screenshots.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 TestScreenshotService_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.Screenshots.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)
}
})
}
}