Skip to content

Commit b296c17

Browse files
committed
tests: use testify/assert instead of manual comparisons
Replace all manual compare+explicit fail patterns in unit tests with stretchr/testify assert and require helpers. This reduces boilerplate and produces more readable failure messages on test errors. Signed-off-by: Robin Jarry <rjarry@redhat.com>
1 parent ad1e0e7 commit b296c17

30 files changed

Lines changed: 748 additions & 1923 deletions

‎go.mod‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/go-chi/chi/v5 v5.3.1
1515
github.com/go-sql-driver/mysql v1.10.0
1616
github.com/jackc/pgx/v5 v5.10.0
17+
github.com/stretchr/testify v1.11.1
1718
github.com/uptrace/bun v1.2.18
1819
github.com/uptrace/bun/dialect/mysqldialect v1.2.18
1920
github.com/uptrace/bun/dialect/pgdialect v1.2.18
@@ -29,6 +30,7 @@ require (
2930
github.com/andybalholm/brotli v1.2.1 // indirect
3031
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
3132
github.com/cli/browser v1.3.0 // indirect
33+
github.com/davecgh/go-spew v1.1.1 // indirect
3234
github.com/dustin/go-humanize v1.0.1 // indirect
3335
github.com/fatih/color v1.16.0 // indirect
3436
github.com/fsnotify/fsnotify v1.7.0 // indirect
@@ -42,6 +44,7 @@ require (
4244
github.com/natefinch/atomic v1.0.1 // indirect
4345
github.com/ncruces/go-strftime v1.0.0 // indirect
4446
github.com/pelletier/go-toml v1.9.5 // indirect
47+
github.com/pmezard/go-difflib v1.0.0 // indirect
4548
github.com/puzpuzpuz/xsync/v3 v3.5.1 // indirect
4649
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
4750
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
@@ -53,6 +56,7 @@ require (
5356
golang.org/x/sys v0.47.0 // indirect
5457
golang.org/x/text v0.40.0 // indirect
5558
golang.org/x/tools v0.47.0 // indirect
59+
gopkg.in/yaml.v3 v3.0.1 // indirect
5660
modernc.org/libc v1.74.1 // indirect
5761
modernc.org/mathutil v1.7.1 // indirect
5862
modernc.org/memory v1.11.0 // indirect

‎go.sum‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo
6363
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
6464
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
6565
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
66+
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
67+
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
68+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
69+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
6670
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
6771
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
6872
github.com/mattn/go-isatty v0.0.23 h1:cYwCQTQf3HB6xUC+BtyCLZNr7IzbOmoZbmssVNzSyiQ=
@@ -79,6 +83,8 @@ github.com/puzpuzpuz/xsync/v3 v3.5.1 h1:GJYJZwO6IdxN/IKbneznS6yPkVC+c3zyY/j19c++
7983
github.com/puzpuzpuz/xsync/v3 v3.5.1/go.mod h1:VjzYrABPabuM4KyBh1Ftq6u8nhwY5tBPKP9jpmh0nnA=
8084
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
8185
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
86+
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
87+
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
8288
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
8389
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
8490
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
@@ -150,6 +156,8 @@ golang.org/x/tools v0.47.0 h1:7Kn5x/d1svx/PzryTsqeoZN4TZwqeH5pGWjefhLi/1Q=
150156
golang.org/x/tools v0.47.0/go.mod h1:dFHnyTvFWY212G+h7ZY4Vsp/K3U4/7W9TyVaAul8uCA=
151157
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
152158
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
159+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
160+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
153161
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
154162
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
155163
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

‎pkg/api/bundle_test.go‎

Lines changed: 33 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
"encoding/json"
1111
"fmt"
1212
"testing"
13+
14+
"github.com/stretchr/testify/assert"
15+
"github.com/stretchr/testify/require"
1316
)
1417

1518
func TestBundleList(t *testing.T) {
@@ -20,9 +23,7 @@ func TestBundleList(t *testing.T) {
2023
VALUES (?, ?, 'my bundle', true)`, userID, projID)
2124

2225
items := getList(t, s, "/api/1.4/bundles")
23-
if len(items) != 1 {
24-
t.Fatalf("got %d, want 1", len(items))
25-
}
26+
require.Len(t, items, 1)
2627
b := items[0]
2728
assertField(t, b, "id")
2829
assertField(t, b, "name")
@@ -34,9 +35,7 @@ func TestBundleList(t *testing.T) {
3435
func TestBundleListEmpty(t *testing.T) {
3536
s := newTestServer(t)
3637
items := getList(t, s, "/api/1.4/bundles")
37-
if len(items) != 0 {
38-
t.Errorf("got %d, want 0", len(items))
39-
}
38+
assert.Len(t, items, 0)
4039
}
4140

4241
func TestBundleListPublicOnly(t *testing.T) {
@@ -49,12 +48,8 @@ func TestBundleListPublicOnly(t *testing.T) {
4948
VALUES (?, ?, 'private', false)`, userID, projID)
5049

5150
items := getList(t, s, "/api/1.4/bundles")
52-
if len(items) != 1 {
53-
t.Errorf("got %d, want 1", len(items))
54-
}
55-
if items[0]["name"] != "public" {
56-
t.Errorf("name = %v, want public", items[0]["name"])
57-
}
51+
assert.Len(t, items, 1)
52+
assert.Equal(t, "public", items[0]["name"])
5853
}
5954

6055
func TestBundleListAuthSeesOwnPrivate(t *testing.T) {
@@ -72,16 +67,12 @@ func TestBundleListAuthSeesOwnPrivate(t *testing.T) {
7267
VALUES (?, ?, 'public-one', true)`, otherID, projID)
7368

7469
resp := s.authRequest(t, "GET", "/api/1.4/bundles", token, nil)
75-
if resp.StatusCode != 200 {
76-
t.Fatalf("status = %d", resp.StatusCode)
77-
}
70+
require.Equal(t, 200, resp.StatusCode)
7871
var items []map[string]any
7972
json.NewDecoder(resp.Body).Decode(&items)
8073
resp.Body.Close()
8174

82-
if len(items) != 2 {
83-
t.Fatalf("got %d, want 2 (own private + public)", len(items))
84-
}
75+
require.Len(t, items, 2)
8576
}
8677

8778
func TestBundleDetail(t *testing.T) {
@@ -94,17 +85,13 @@ func TestBundleDetail(t *testing.T) {
9485
userID, projID).Scan(context.Background(), &bundleID)
9586

9687
b := getOne(t, s, fmt.Sprintf("/api/1.4/bundles/%d", bundleID))
97-
if b["name"] != "detail bundle" {
98-
t.Errorf("name = %v", b["name"])
99-
}
88+
assert.Equal(t, "detail bundle", b["name"])
10089
}
10190

10291
func TestBundleDetailInvalid(t *testing.T) {
10392
s := newTestServer(t)
10493
resp := s.get(t, "/api/1.4/bundles/invalid")
105-
if resp.StatusCode != 422 {
106-
t.Errorf("status = %d, want 422", resp.StatusCode)
107-
}
94+
assert.Equal(t, 422, resp.StatusCode)
10895
}
10996

11097
func TestBundleDetailPatches(t *testing.T) {
@@ -128,24 +115,16 @@ func TestBundleDetailPatches(t *testing.T) {
128115
assertNested(t, b, "project", "id")
129116

130117
patches := b["patches"].([]any)
131-
if len(patches) != 1 {
132-
t.Fatalf("patches: got %d, want 1", len(patches))
133-
}
118+
require.Len(t, patches, 1)
134119
patchObj := patches[0].(map[string]any)
135-
if int(patchObj["id"].(float64)) != int(patchID) {
136-
t.Errorf("patches[0].id = %v, want %d", patchObj["id"], patchID)
137-
}
138-
if patchObj["name"] != "bundle patch" {
139-
t.Errorf("patches[0].name = %v, want 'bundle patch'", patchObj["name"])
140-
}
120+
assert.Equal(t, float64(patchID), patchObj["id"])
121+
assert.Equal(t, "bundle patch", patchObj["name"])
141122
}
142123

143124
func TestBundleNotFound(t *testing.T) {
144125
s := newTestServer(t)
145126
resp := s.get(t, "/api/1.4/bundles/99999")
146-
if resp.StatusCode != 404 {
147-
t.Errorf("status = %d, want 404", resp.StatusCode)
148-
}
127+
assert.Equal(t, 404, resp.StatusCode)
149128
}
150129

151130
func TestBundleFilterOwner(t *testing.T) {
@@ -156,13 +135,9 @@ func TestBundleFilterOwner(t *testing.T) {
156135
VALUES (?, ?, 'b', true)`, userID, projID)
157136

158137
items := getList(t, s, fmt.Sprintf("/api/1.4/bundles/?owner=%d", userID))
159-
if len(items) != 1 {
160-
t.Errorf("got %d, want 1", len(items))
161-
}
138+
assert.Len(t, items, 1)
162139
items = getList(t, s, "/api/1.4/bundles/?owner=99999")
163-
if len(items) != 0 {
164-
t.Errorf("got %d, want 0", len(items))
165-
}
140+
assert.Len(t, items, 0)
166141
}
167142

168143
func TestBundleFilterProject(t *testing.T) {
@@ -173,13 +148,9 @@ func TestBundleFilterProject(t *testing.T) {
173148
VALUES (?, ?, 'b', true)`, userID, projID)
174149

175150
items := getList(t, s, fmt.Sprintf("/api/1.4/bundles/?project=%d", projID))
176-
if len(items) != 1 {
177-
t.Errorf("got %d, want 1", len(items))
178-
}
151+
assert.Len(t, items, 1)
179152
items = getList(t, s, "/api/1.4/bundles/?project=99999")
180-
if len(items) != 0 {
181-
t.Errorf("got %d, want 0", len(items))
182-
}
153+
assert.Len(t, items, 0)
183154
}
184155

185156
// --- Create ---
@@ -191,9 +162,7 @@ func TestBundleCreateAnonymous(t *testing.T) {
191162

192163
resp := s.authRequest(t, "POST", "/api/1.4/bundles", "",
193164
map[string]any{"name": "b", "patches": []int{patchID}})
194-
if resp.StatusCode != 401 {
195-
t.Errorf("status = %d, want 401", resp.StatusCode)
196-
}
165+
assert.Equal(t, 401, resp.StatusCode)
197166
}
198167

199168
func TestBundleCreateValid(t *testing.T) {
@@ -210,9 +179,7 @@ func TestBundleCreateValid(t *testing.T) {
210179
"public": true,
211180
"patches": []int{p1, p2},
212181
})
213-
if resp.StatusCode != 201 {
214-
t.Fatalf("status = %d, want 201", resp.StatusCode)
215-
}
182+
require.Equal(t, 201, resp.StatusCode)
216183

217184
var b map[string]any
218185
json.NewDecoder(resp.Body).Decode(&b)
@@ -224,9 +191,7 @@ func TestBundleCreateValid(t *testing.T) {
224191
assertNested(t, b, "project", "id")
225192

226193
patches := b["patches"].([]any)
227-
if len(patches) != 2 {
228-
t.Fatalf("patches: got %d, want 2", len(patches))
229-
}
194+
require.Len(t, patches, 2)
230195
}
231196

232197
func TestBundleCreateEmptyPatches(t *testing.T) {
@@ -240,9 +205,7 @@ func TestBundleCreateEmptyPatches(t *testing.T) {
240205
"name": "empty-bundle",
241206
"patches": []int{},
242207
})
243-
if resp.StatusCode != 400 {
244-
t.Errorf("status = %d, want 400", resp.StatusCode)
245-
}
208+
assert.Equal(t, 400, resp.StatusCode)
246209
}
247210

248211
func TestBundleCreateCrossProject(t *testing.T) {
@@ -268,9 +231,7 @@ func TestBundleCreateCrossProject(t *testing.T) {
268231
"name": "cross-bundle",
269232
"patches": []int{p1, p2},
270233
})
271-
if resp.StatusCode != 400 {
272-
t.Errorf("status = %d, want 400", resp.StatusCode)
273-
}
234+
assert.Equal(t, 400, resp.StatusCode)
274235
}
275236

276237
// --- Update ---
@@ -289,9 +250,7 @@ func TestBundleUpdateOwner(t *testing.T) {
289250
"public": false,
290251
"patches": []int{patchID},
291252
})
292-
if resp.StatusCode != 201 {
293-
t.Fatalf("create status = %d", resp.StatusCode)
294-
}
253+
require.Equal(t, 201, resp.StatusCode)
295254
var created map[string]any
296255
json.NewDecoder(resp.Body).Decode(&created)
297256
resp.Body.Close()
@@ -303,9 +262,7 @@ func TestBundleUpdateOwner(t *testing.T) {
303262
"name": "new-name",
304263
"public": true,
305264
})
306-
if resp.StatusCode != 200 {
307-
t.Fatalf("update status = %d, want 200", resp.StatusCode)
308-
}
265+
require.Equal(t, 200, resp.StatusCode)
309266
var updated map[string]any
310267
json.NewDecoder(resp.Body).Decode(&updated)
311268
resp.Body.Close()
@@ -338,17 +295,13 @@ func TestBundleUpdatePatches(t *testing.T) {
338295
map[string]any{
339296
"patches": []int{p2, p3},
340297
})
341-
if resp.StatusCode != 200 {
342-
t.Fatalf("status = %d, want 200", resp.StatusCode)
343-
}
298+
require.Equal(t, 200, resp.StatusCode)
344299
var updated map[string]any
345300
json.NewDecoder(resp.Body).Decode(&updated)
346301
resp.Body.Close()
347302

348303
patches := updated["patches"].([]any)
349-
if len(patches) != 2 {
350-
t.Fatalf("patches: got %d, want 2", len(patches))
351-
}
304+
require.Len(t, patches, 2)
352305
}
353306

354307
func TestBundleUpdateNonOwner(t *testing.T) {
@@ -368,9 +321,7 @@ func TestBundleUpdateNonOwner(t *testing.T) {
368321

369322
resp := s.authRequest(t, "PATCH", fmt.Sprintf("/api/1.4/bundles/%d", bundleID), otherToken,
370323
map[string]any{"name": "hijacked"})
371-
if resp.StatusCode != 403 {
372-
t.Errorf("status = %d, want 403", resp.StatusCode)
373-
}
324+
assert.Equal(t, 403, resp.StatusCode)
374325
}
375326

376327
// --- Delete ---
@@ -393,15 +344,11 @@ func TestBundleDeleteOwner(t *testing.T) {
393344
bundleID := int(created["id"].(float64))
394345

395346
resp = s.authRequest(t, "DELETE", fmt.Sprintf("/api/1.4/bundles/%d", bundleID), token, nil)
396-
if resp.StatusCode != 204 {
397-
t.Errorf("status = %d, want 204", resp.StatusCode)
398-
}
347+
assert.Equal(t, 204, resp.StatusCode)
399348

400349
// verify it's gone
401350
resp = s.get(t, fmt.Sprintf("/api/1.4/bundles/%d", bundleID))
402-
if resp.StatusCode != 404 {
403-
t.Errorf("after delete: status = %d, want 404", resp.StatusCode)
404-
}
351+
assert.Equal(t, 404, resp.StatusCode)
405352
}
406353

407354
func TestBundleDeleteNonOwner(t *testing.T) {
@@ -417,15 +364,11 @@ func TestBundleDeleteNonOwner(t *testing.T) {
417364
ownerID, projID).Scan(context.Background(), &bundleID)
418365

419366
resp := s.authRequest(t, "DELETE", fmt.Sprintf("/api/1.4/bundles/%d", bundleID), otherToken, nil)
420-
if resp.StatusCode != 403 {
421-
t.Errorf("status = %d, want 403", resp.StatusCode)
422-
}
367+
assert.Equal(t, 403, resp.StatusCode)
423368
}
424369

425370
func TestBundleDeleteAnonymous(t *testing.T) {
426371
s := newTestServer(t)
427372
resp := s.authRequest(t, "DELETE", "/api/1.4/bundles/1", "", nil)
428-
if resp.StatusCode != 401 {
429-
t.Errorf("status = %d, want 401", resp.StatusCode)
430-
}
373+
assert.Equal(t, 401, resp.StatusCode)
431374
}

0 commit comments

Comments
 (0)