-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathversion_test.go
295 lines (251 loc) Β· 8.17 KB
/
version_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
package readme_test
import (
"fmt"
"testing"
"github.com/h2non/gock"
"github.com/liveoaklabs/readme-api-go-client/readme"
"github.com/liveoaklabs/readme-api-go-client/tests/testdata"
"github.com/stretchr/testify/assert"
)
func Test_Version_GetAll(t *testing.T) {
t.Run("when called with no parameters", func(t *testing.T) {
// Arrange
expect := testdata.VersionSummary
gock.New(TestClient.APIURL).
Get(readme.VersionEndpoint).
Reply(200).
JSON(expect)
defer gock.Off()
// Act
got, _, err := TestClient.Version.GetAll()
// Assert
assert.NoError(t, err, "it does not returns an error")
assert.Equal(t, expect, got, "it returns slice of Version structs")
assert.True(t, gock.IsDone(), "it makes the expected API call")
})
// Test API error responses.
testCases := []struct {
status int
errMsg string
}{
{400, "APIKEY_EMPTY"},
{403, "APIKEY_MISMATCH"},
}
for _, tc := range testCases {
testName := fmt.Sprintf("when API responds with %v", tc.status)
t.Run(testName, func(t *testing.T) {
// Arrange
expect := readme.APIErrorResponse{Error: tc.errMsg}
expectErr := fmt.Sprintf("ReadMe API Error: %v on GET", tc.status)
gock.New(TestClient.APIURL).
Get(readme.VersionEndpoint).
Reply(tc.status).
JSON(expect)
defer gock.Off()
// Act
_, apiResponse, err := TestClient.Version.GetAll()
// Assert
assert.Error(t, err, "it returns an error")
assert.ErrorContains(t, err, expectErr,
"it returns expected API error")
assert.Equal(t, apiResponse.APIErrorResponse.Error, tc.errMsg,
"it returns an APIErrorResponse")
assert.Equal(t, apiResponse.HTTPResponse.StatusCode, tc.status,
"it returns an HTTPResponse with expected status code",
)
assert.True(t, gock.IsDone(), "it makes the expected API call")
})
}
}
func Test_Version_Get(t *testing.T) {
t.Run("when called with existing version number", func(t *testing.T) {
// Arrange
expect := testdata.Versions[0]
gock.New(TestClient.APIURL).
Get(readme.VersionEndpoint + "/" + expect.Version).
Reply(200).
JSON(expect)
defer gock.Off()
// Act
got, _, err := TestClient.Version.Get(expect.Version)
// Assert
assert.NoError(t, err, "it does not return an error")
assert.Equal(t, expect, got, "it returns a single Version struct")
assert.True(t, gock.IsDone(), "it makes the expected API call")
})
t.Run("when requested id is invalid", func(t *testing.T) {
// Arrange
gock.New(TestClient.APIURL).
Get(readme.VersionEndpoint).
Reply(200).
JSON(testdata.Versions)
defer gock.Off()
// Act
_, _, err := TestClient.Version.Get("id:invalidinvalidinvalid")
// Assert
assert.ErrorContains(t, err, "no match for version ID invalidinvalidinvalid",
"it returns the expected error")
assert.True(t, gock.IsDone(), "it makes the expected API call")
})
// Test API error responses.
testCases := []struct {
status int
errMsg string
}{
{400, "APIKEY_EMPTY"},
{403, "APIKEY_MISMATCH"},
{404, "VERSION_NOTFOUND"},
}
for _, tc := range testCases {
testName := fmt.Sprintf("when API responds with %v", tc.status)
t.Run(testName, func(t *testing.T) {
// Arrange
expect := readme.APIErrorResponse{Error: tc.errMsg}
gock.New(TestClient.APIURL).
Get(readme.VersionEndpoint + "/99.99.99").
Reply(tc.status).
JSON(expect)
defer gock.Off()
expectErr := fmt.Sprintf("ReadMe API Error: %v on GET", tc.status)
// Act
_, apiResponse, err := TestClient.Version.Get("99.99.99")
// Assert
assert.ErrorContains(t, err, expectErr,
"it returns expected API error")
assert.Equal(t, apiResponse.APIErrorResponse.Error, tc.errMsg,
"it returns an APIErrorResponse")
assert.Equal(t, apiResponse.HTTPResponse.StatusCode, tc.status,
"it returns an HTTPResponse with expected status code",
)
assert.True(t, gock.IsDone(), "it makes the expected API call")
})
}
}
// NOTE: `readme.GetVersion()` refers to a private `getVersion()` function that
// has been exported for tests.
func Test_Version_GetAll_Versions(t *testing.T) {
t.Run("when looking up version number by ID", func(t *testing.T) {
// Arrange
expect := testdata.VersionSummary
gock.New(TestClient.APIURL).
Get(readme.VersionEndpoint).
Reply(200).
JSON(expect)
defer gock.Off()
// Act
got, err := TestClient.Version.GetVersion("id:" + expect[0].ID)
// Assert
assert.NoError(t, err, "it does not return an error")
assert.Equal(t, expect[0].Version, got, "version matches",
"it returns the expected version")
assert.True(t, gock.IsDone(), "it makes the expected API call")
})
t.Run("when looking up by ID encounters unmarshal error", func(t *testing.T) {
// Arrange
gock.New(TestClient.APIURL).
Get(readme.VersionEndpoint).
Reply(200).
JSON(`[x]`)
defer gock.Off()
expect := "unable to parse API response: invalid character"
// Act
_, err := TestClient.Version.GetVersion("id:638cf4cfdea3ff0096d1a95a")
// Assert
assert.ErrorContains(t, err, expect, "it returns the expected error")
assert.True(t, gock.IsDone(), "it makes the expected API call")
})
}
func Test_Version_Create_and_Update(t *testing.T) {
t.Run("when create is successful", func(t *testing.T) {
// Arrange
expect := testdata.Versions[1]
gock.New(TestClient.APIURL).
Post(readme.VersionEndpoint).
Reply(200).
JSON(expect)
defer gock.Off()
createParams := readme.VersionParams{
From: "1.0.0",
Version: expect.Version,
}
// Act
got, _, err := TestClient.Version.Create(createParams)
// Assert
assert.NoError(t, err, "it does not return an error")
assert.Equal(t, expect, got, "it returns expected VersionSaved struct")
assert.True(t, gock.IsDone(), "it makes the expected API call")
})
t.Run("when update is called with a valid version and API response is 200", func(t *testing.T) {
// Arrange
expect := testdata.Versions[1]
gock.New(TestClient.APIURL).
Put(readme.VersionEndpoint + "/" + expect.Version).
Reply(200).
JSON(expect)
defer gock.Off()
hidden := true
updateParams := readme.VersionParams{
From: "1.0.0",
Version: expect.Version,
IsHidden: &hidden,
}
// Act
got, _, err := TestClient.Version.Update(expect.VersionClean, updateParams)
// Assert
assert.NoError(t, err, "it does not return an error")
assert.Equal(t, expect, got, "it returns expected VersionSaved struct")
assert.True(t, gock.IsDone(), "it makes the expected API call")
})
t.Run("when update is called with a non-existing version and the API responds with 400", func(t *testing.T) {
// Arrange
gock.New(TestClient.APIURL).
Put(readme.VersionEndpoint + "/1.1.x").
Reply(404).
JSON(readme.APIErrorResponse{Error: "VERSION_NOTFOUND"})
defer gock.Off()
updateParams := readme.VersionParams{
Version: "1.1.x",
}
expect := "ReadMe API Error: 404 on PUT"
// Act
_, _, err := TestClient.Version.Update("1.1.x", updateParams)
// Assert
assert.ErrorContains(t, err, expect, "it returns the expected error")
assert.True(t, gock.IsDone(), "it makes the expected API call")
})
}
func Test_Version_Delete(t *testing.T) {
t.Run("when called with valid existing version and API response is 200", func(t *testing.T) {
// Arrange
gock.New(TestClient.APIURL).
Delete(readme.VersionEndpoint + "/1.0.0").
Reply(200).
JSON("{}")
defer gock.Off()
// Act
got, _, err := TestClient.Version.Delete("1.0.0")
// Assert
assert.NoError(t, err, "it does not return an error")
assert.True(t, got, "it returns true")
assert.True(t, gock.IsDone(), "it makes the expected API call")
})
t.Run("when API responds with 400", func(t *testing.T) {
// Arrange
expect := readme.APIErrorResponse{Error: "VERSION_CANT_REMOVE_STABLE"}
gock.New(TestClient.APIURL).
Delete(readme.VersionEndpoint + "/1.0.0").
Reply(400).
JSON(expect)
defer gock.Off()
// Act
got, apiErrorResponse, err := TestClient.Version.Delete("1.0.0")
// Assert
assert.ErrorContains(t, err, "ReadMe API Error: 400 on DELETE",
"it returns the expected error")
assert.Equal(t, apiErrorResponse.APIErrorResponse.Error,
"VERSION_CANT_REMOVE_STABLE",
"it returns an APIErrorResponse")
assert.False(t, got, "it returns false")
assert.True(t, gock.IsDone(), "it makes the expected API call")
})
}