-
Notifications
You must be signed in to change notification settings - Fork 17
/
platformversioncompany_test.go
214 lines (188 loc) · 6.29 KB
/
platformversioncompany_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 (
testPlatformVersionCompanyGet string = "test_data/platformversioncompany_get.json"
testPlatformVersionCompanyList string = "test_data/platformversioncompany_list.json"
)
func TestPlatformVersionCompanyService_Get(t *testing.T) {
f, err := ioutil.ReadFile(testPlatformVersionCompanyGet)
if err != nil {
t.Fatal(err)
}
init := make([]*PlatformVersionCompany, 1)
err = json.Unmarshal(f, &init)
if err != nil {
t.Fatal(err)
}
var tests = []struct {
name string
file string
id int
opts []Option
wantPlatformVersionCompany *PlatformVersionCompany
wantErr error
}{
{"Valid response", testPlatformVersionCompanyGet, 151, []Option{SetFields("name")}, init[0], nil},
{"Invalid ID", testFileEmpty, -1, nil, nil, ErrNegativeID},
{"Empty response", testFileEmpty, 151, nil, nil, errInvalidJSON},
{"Invalid option", testFileEmpty, 151, []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()
com, err := c.PlatformVersionCompanies.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(com, test.wantPlatformVersionCompany) {
t.Errorf("got: <%v>, \nwant: <%v>", com, test.wantPlatformVersionCompany)
}
})
}
}
func TestPlatformVersionCompanyService_List(t *testing.T) {
f, err := ioutil.ReadFile(testPlatformVersionCompanyList)
if err != nil {
t.Fatal(err)
}
init := make([]*PlatformVersionCompany, 0)
err = json.Unmarshal(f, &init)
if err != nil {
t.Fatal(err)
}
var tests = []struct {
name string
file string
ids []int
opts []Option
wantPlatformVersionCompanies []*PlatformVersionCompany
wantErr error
}{
{"Valid response", testPlatformVersionCompanyList, []int{152, 159, 117, 162, 87}, []Option{SetLimit(5)}, init, nil},
{"Zero IDs", testFileEmpty, nil, nil, nil, ErrEmptyIDs},
{"Invalid ID", testFileEmpty, []int{-500}, nil, nil, ErrNegativeID},
{"Empty response", testFileEmpty, []int{152, 159, 117, 162, 87}, nil, nil, errInvalidJSON},
{"Invalid option", testFileEmpty, []int{152, 159, 117, 162, 87}, []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()
com, err := c.PlatformVersionCompanies.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(com, test.wantPlatformVersionCompanies) {
t.Errorf("got: <%v>, \nwant: <%v>", com, test.wantPlatformVersionCompanies)
}
})
}
}
func TestPlatformVersionCompanyService_Index(t *testing.T) {
f, err := ioutil.ReadFile(testPlatformVersionCompanyList)
if err != nil {
t.Fatal(err)
}
init := make([]*PlatformVersionCompany, 0)
err = json.Unmarshal(f, &init)
if err != nil {
t.Fatal(err)
}
tests := []struct {
name string
file string
opts []Option
wantPlatformVersionCompanies []*PlatformVersionCompany
wantErr error
}{
{"Valid response", testPlatformVersionCompanyList, []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()
com, err := c.PlatformVersionCompanies.Index(test.opts...)
if errors.Cause(err) != test.wantErr {
t.Errorf("got: <%v>, want: <%v>", errors.Cause(err), test.wantErr)
}
if !reflect.DeepEqual(com, test.wantPlatformVersionCompanies) {
t.Errorf("got: <%v>, \nwant: <%v>", com, test.wantPlatformVersionCompanies)
}
})
}
}
func TestPlatformVersionCompanyService_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.PlatformVersionCompanies.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 TestPlatformVersionCompanyService_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.PlatformVersionCompanies.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)
}
})
}
}