-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_test.go
294 lines (290 loc) · 9.4 KB
/
system_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
package retroachievements_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/joshraphael/go-retroachievements"
"github.com/joshraphael/go-retroachievements/models"
"github.com/stretchr/testify/require"
)
func TestGetConsoleIDs(tt *testing.T) {
active := true
gameSystems := true
tests := []struct {
name string
params models.GetConsoleIDsParameters
modifyURL func(url string) string
responseCode int
responseMessage []models.GetConsoleIDs
responseError models.ErrorResponse
response func(messageBytes []byte, errorBytes []byte) []byte
assert func(t *testing.T, resp []models.GetConsoleIDs, err error)
}{
{
name: "fail to call endpoint",
params: models.GetConsoleIDsParameters{
OnlyActive: &active,
OnlyGameSystems: &gameSystems,
},
modifyURL: func(url string) string {
return ""
},
responseCode: http.StatusOK,
response: func(messageBytes []byte, errorBytes []byte) []byte {
return messageBytes
},
assert: func(t *testing.T, resp []models.GetConsoleIDs, err error) {
require.Nil(t, resp)
require.EqualError(t, err, "calling endpoint: Get \"/API/API_GetConsoleIDs.php?a=1&g=1&y=some_secret\": unsupported protocol scheme \"\"")
},
},
{
name: "error response",
params: models.GetConsoleIDsParameters{
OnlyActive: &active,
OnlyGameSystems: &gameSystems,
},
modifyURL: func(url string) string {
return url
},
responseCode: http.StatusUnauthorized,
responseError: models.ErrorResponse{
Message: "test",
Errors: []models.ErrorDetail{
{
Status: http.StatusUnauthorized,
Code: "unauthorized",
Title: "Not Authorized",
},
},
},
response: func(messageBytes []byte, errorBytes []byte) []byte {
return errorBytes
},
assert: func(t *testing.T, resp []models.GetConsoleIDs, err error) {
require.Nil(t, resp)
require.EqualError(t, err, "parsing response list: error code 401 returned: {\"message\":\"test\",\"errors\":[{\"status\":401,\"code\":\"unauthorized\",\"title\":\"Not Authorized\"}]}")
},
},
{
name: "success",
params: models.GetConsoleIDsParameters{
OnlyActive: &active,
OnlyGameSystems: &gameSystems,
},
modifyURL: func(url string) string {
return url
},
responseCode: http.StatusOK,
responseMessage: []models.GetConsoleIDs{
{
ID: 1,
Name: "Genesis/Mega Drive",
IconURL: "https://static.retroachievements.org/assets/images/system/md.png",
Active: true,
IsGameSystem: true,
},
{
ID: 2,
Name: "Nintendo 64",
IconURL: "https://static.retroachievements.org/assets/images/system/n64.png",
Active: true,
IsGameSystem: true,
},
},
response: func(messageBytes []byte, errorBytes []byte) []byte {
return messageBytes
},
assert: func(t *testing.T, resp []models.GetConsoleIDs, err error) {
require.NotNil(t, resp)
require.Len(t, resp, 2)
require.Equal(t, 1, resp[0].ID)
require.Equal(t, "Genesis/Mega Drive", resp[0].Name)
require.Equal(t, "https://static.retroachievements.org/assets/images/system/md.png", resp[0].IconURL)
require.True(t, resp[0].Active)
require.True(t, resp[0].IsGameSystem)
require.Equal(t, 2, resp[1].ID)
require.Equal(t, "Nintendo 64", resp[1].Name)
require.Equal(t, "https://static.retroachievements.org/assets/images/system/n64.png", resp[1].IconURL)
require.True(t, resp[1].Active)
require.True(t, resp[1].IsGameSystem)
require.NoError(t, err)
},
},
}
for _, test := range tests {
tt.Run(test.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
expectedPath := "/API/API_GetConsoleIDs.php"
if r.URL.Path != expectedPath {
t.Errorf("Expected to request '%s', got: %s", expectedPath, r.URL.Path)
}
w.WriteHeader(test.responseCode)
messageBytes, err := json.Marshal(test.responseMessage)
require.NoError(t, err)
errBytes, err := json.Marshal(test.responseError)
require.NoError(t, err)
resp := test.response(messageBytes, errBytes)
num, err := w.Write(resp)
require.NoError(t, err)
require.Equal(t, num, len(resp))
}))
defer server.Close()
client := retroachievements.New(test.modifyURL(server.URL), "go-retroachievements/v0.0.0", "some_secret")
resp, err := client.GetConsoleIDs(test.params)
test.assert(t, resp, err)
})
}
}
func TestGetGameList(tt *testing.T) {
hasAchievements := true
includeHashes := true
count := 10
offset := 12
dateModified, err := time.Parse(time.DateTime, "2024-11-16 09:39:51")
require.NoError(tt, err)
forumTopic := 27761
tests := []struct {
name string
params models.GetGameListParameters
modifyURL func(url string) string
responseCode int
responseMessage []models.GetGameList
responseError models.ErrorResponse
response func(messageBytes []byte, errorBytes []byte) []byte
assert func(t *testing.T, resp []models.GetGameList, err error)
}{
{
name: "fail to call endpoint",
params: models.GetGameListParameters{
SystemID: 1,
HasAchievements: &hasAchievements,
IncludeHashes: &includeHashes,
Count: &count,
Offset: &offset,
},
modifyURL: func(url string) string {
return ""
},
responseCode: http.StatusOK,
response: func(messageBytes []byte, errorBytes []byte) []byte {
return messageBytes
},
assert: func(t *testing.T, resp []models.GetGameList, err error) {
require.Nil(t, resp)
require.EqualError(t, err, "calling endpoint: Get \"/API/API_GetGameList.php?c=10&f=1&h=1&i=1&o=12&y=some_secret\": unsupported protocol scheme \"\"")
},
},
{
name: "error response",
params: models.GetGameListParameters{
SystemID: 1,
HasAchievements: &hasAchievements,
IncludeHashes: &includeHashes,
Count: &count,
Offset: &offset,
},
modifyURL: func(url string) string {
return url
},
responseCode: http.StatusUnauthorized,
responseError: models.ErrorResponse{
Message: "test",
Errors: []models.ErrorDetail{
{
Status: http.StatusUnauthorized,
Code: "unauthorized",
Title: "Not Authorized",
},
},
},
response: func(messageBytes []byte, errorBytes []byte) []byte {
return errorBytes
},
assert: func(t *testing.T, resp []models.GetGameList, err error) {
require.Nil(t, resp)
require.EqualError(t, err, "parsing response list: error code 401 returned: {\"message\":\"test\",\"errors\":[{\"status\":401,\"code\":\"unauthorized\",\"title\":\"Not Authorized\"}]}")
},
},
{
name: "success",
params: models.GetGameListParameters{
SystemID: 1,
HasAchievements: &hasAchievements,
IncludeHashes: &includeHashes,
Count: &count,
Offset: &offset,
},
modifyURL: func(url string) string {
return url
},
responseCode: http.StatusOK,
responseMessage: []models.GetGameList{
{
Title: "~Hack~ ~Demo~ Gatoslip: Chapter 0",
ID: 30734,
ConsoleID: 1,
ConsoleName: "Genesis/Mega Drive",
ImageIcon: "/Images/101272.png",
NumAchievements: 28,
NumLeaderboards: 1,
Points: 289,
DateModified: &models.DateTime{
Time: dateModified,
},
ForumTopicID: &forumTopic,
Hashes: []string{
"b4c6bcd0b0db9d2cc0f47e2fbb86a97b",
},
},
},
response: func(messageBytes []byte, errorBytes []byte) []byte {
return messageBytes
},
assert: func(t *testing.T, resp []models.GetGameList, err error) {
require.NotNil(t, resp)
require.Len(t, resp, 1)
require.Equal(t, "~Hack~ ~Demo~ Gatoslip: Chapter 0", resp[0].Title)
require.Equal(t, 30734, resp[0].ID)
require.Equal(t, 1, resp[0].ConsoleID)
require.Equal(t, "Genesis/Mega Drive", resp[0].ConsoleName)
require.Equal(t, "/Images/101272.png", resp[0].ImageIcon)
require.Equal(t, 28, resp[0].NumAchievements)
require.Equal(t, 1, resp[0].NumLeaderboards)
require.Equal(t, 289, resp[0].Points)
require.NotNil(t, resp[0].DateModified)
require.Equal(t, dateModified, resp[0].DateModified.Time)
require.NotNil(t, resp[0].ForumTopicID)
require.Equal(t, 27761, *resp[0].ForumTopicID)
require.Len(t, resp[0].Hashes, 1)
require.Equal(t, "b4c6bcd0b0db9d2cc0f47e2fbb86a97b", resp[0].Hashes[0])
require.NoError(t, err)
},
},
}
for _, test := range tests {
tt.Run(test.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
expectedPath := "/API/API_GetGameList.php"
if r.URL.Path != expectedPath {
t.Errorf("Expected to request '%s', got: %s", expectedPath, r.URL.Path)
}
w.WriteHeader(test.responseCode)
messageBytes, err := json.Marshal(test.responseMessage)
require.NoError(t, err)
errBytes, err := json.Marshal(test.responseError)
require.NoError(t, err)
resp := test.response(messageBytes, errBytes)
num, err := w.Write(resp)
require.NoError(t, err)
require.Equal(t, num, len(resp))
}))
defer server.Close()
client := retroachievements.New(test.modifyURL(server.URL), "go-retroachievements/v0.0.0", "some_secret")
resp, err := client.GetGameList(test.params)
test.assert(t, resp, err)
})
}
}